2012-01-27 21:06:04 +01:00
|
|
|
<?php
|
|
|
|
|
2012-07-16 03:17:13 +02:00
|
|
|
class Application_Model_Scheduler
|
|
|
|
{
|
2012-01-31 18:59:27 +01:00
|
|
|
private $con;
|
2012-02-02 18:13:20 +01:00
|
|
|
private $fileInfo = array(
|
|
|
|
"id" => "",
|
|
|
|
"cliplength" => "",
|
|
|
|
"cuein" => "00:00:00",
|
|
|
|
"cueout" => "00:00:00",
|
|
|
|
"fadein" => "00:00:00",
|
|
|
|
"fadeout" => "00:00:00",
|
|
|
|
"sched_id" => null,
|
2012-07-25 04:24:08 +02:00
|
|
|
"type" => 0 //default type of '0' to represent files. type '1' represents a webstream
|
2012-02-02 18:13:20 +01:00
|
|
|
);
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-03-29 19:20:02 +02:00
|
|
|
private $epochNow;
|
2012-03-28 10:50:02 +02:00
|
|
|
private $nowDT;
|
2012-03-28 14:23:25 +02:00
|
|
|
private $user;
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-04-12 18:00:38 +02:00
|
|
|
private $checkUserPermissions = true;
|
2012-01-27 21:06:04 +01:00
|
|
|
|
2012-07-16 03:17:13 +02:00
|
|
|
public function __construct()
|
|
|
|
{
|
2012-02-02 23:15:39 +01:00
|
|
|
$this->con = Propel::getConnection(CcSchedulePeer::DATABASE_NAME);
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-08-31 18:18:37 +02:00
|
|
|
//subtracting one because sometimes when we cancel a track, we set its end time
|
|
|
|
//to epochNow and then send the new schedule to pypo. Sometimes the currently cancelled
|
|
|
|
//track can still be included in the new schedule because it may have a few ms left to play.
|
|
|
|
//subtracting 1 second from epochNow resolves this issue.
|
|
|
|
$this->epochNow = microtime(true)-1;
|
2012-03-29 19:20:02 +02:00
|
|
|
$this->nowDT = DateTime::createFromFormat("U.u", $this->epochNow, new DateTimeZone("UTC"));
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-07-16 03:17:13 +02:00
|
|
|
if ($this->nowDT === false) {
|
2012-05-06 21:34:41 +02:00
|
|
|
// DateTime::createFromFormat does not support millisecond string formatting in PHP 5.3.2 (Ubuntu 10.04).
|
|
|
|
// In PHP 5.3.3 (Ubuntu 10.10), this has been fixed.
|
|
|
|
$this->nowDT = DateTime::createFromFormat("U", time(), new DateTimeZone("UTC"));
|
2012-07-11 00:51:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->user = Application_Model_User::getCurrentUser();
|
2012-03-28 14:23:25 +02:00
|
|
|
}
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-07-16 03:17:13 +02:00
|
|
|
public function setCheckUserPermissions($value)
|
|
|
|
{
|
2012-04-12 18:00:38 +02:00
|
|
|
$this->checkUserPermissions = $value;
|
|
|
|
}
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2013-04-12 22:34:36 +02:00
|
|
|
private function validateItemMove($itemsToMove, $afterItem)
|
|
|
|
{
|
|
|
|
$afterInstanceId = $afterItem["instance"];
|
|
|
|
|
|
|
|
foreach ($itemsToMove as $itemToMove) {
|
|
|
|
$ccShowInstance = CcShowInstancesQuery::create()
|
|
|
|
->findPk($itemToMove["instance"]);
|
|
|
|
$linked = $ccShowInstance->getCcShow()->isLinked();
|
|
|
|
|
|
|
|
if ($linked && $itemToMove["instance"] != $afterInstanceId) {
|
|
|
|
throw new Exception(_("Linked items can only be moved within its own show"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-16 03:17:13 +02:00
|
|
|
/*
|
|
|
|
* make sure any incoming requests for scheduling are ligit.
|
|
|
|
*
|
|
|
|
* @param array $items, an array containing pks of cc_schedule items.
|
|
|
|
*/
|
|
|
|
private function validateRequest($items)
|
|
|
|
{
|
2012-03-29 19:20:02 +02:00
|
|
|
$nowEpoch = floatval($this->nowDT->format("U.u"));
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-03-28 14:23:25 +02:00
|
|
|
for ($i = 0; $i < count($items); $i++) {
|
|
|
|
$id = $items[$i]["id"];
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-03-28 14:23:25 +02:00
|
|
|
//could be added to the beginning of a show, which sends id = 0;
|
|
|
|
if ($id > 0) {
|
2013-04-04 22:30:33 +02:00
|
|
|
//schedule_id of where we are inserting after?
|
2012-03-28 14:23:25 +02:00
|
|
|
$schedInfo[$id] = $items[$i]["instance"];
|
|
|
|
}
|
|
|
|
|
2013-04-04 22:30:33 +02:00
|
|
|
//what is timestamp for?
|
|
|
|
//format is instance_id => timestamp
|
2012-07-16 03:17:13 +02:00
|
|
|
$instanceInfo[$items[$i]["instance"]] = $items[$i]["timestamp"];
|
2012-03-28 14:23:25 +02:00
|
|
|
}
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-03-28 14:23:25 +02:00
|
|
|
if (count($instanceInfo) === 0) {
|
|
|
|
throw new Exception("Invalid Request.");
|
|
|
|
}
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-03-28 18:43:44 +02:00
|
|
|
$schedIds = array();
|
|
|
|
if (isset($schedInfo)) {
|
|
|
|
$schedIds = array_keys($schedInfo);
|
|
|
|
}
|
2012-03-28 14:23:25 +02:00
|
|
|
$schedItems = CcScheduleQuery::create()->findPKs($schedIds, $this->con);
|
|
|
|
$instanceIds = array_keys($instanceInfo);
|
|
|
|
$showInstances = CcShowInstancesQuery::create()->findPKs($instanceIds, $this->con);
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-03-28 14:23:25 +02:00
|
|
|
//an item has been deleted
|
2012-07-16 03:17:13 +02:00
|
|
|
if (count($schedIds) !== count($schedItems)) {
|
2012-11-15 21:52:51 +01:00
|
|
|
throw new OutDatedScheduleException(_("The schedule you're viewing is out of date! (sched mismatch)"));
|
2012-03-28 14:23:25 +02:00
|
|
|
}
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-07-16 03:17:13 +02:00
|
|
|
//a show has been deleted
|
|
|
|
if (count($instanceIds) !== count($showInstances)) {
|
2012-11-15 21:52:51 +01:00
|
|
|
throw new OutDatedScheduleException(_("The schedule you're viewing is out of date! (instance mismatch)"));
|
2012-03-28 14:23:25 +02:00
|
|
|
}
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-03-28 14:23:25 +02:00
|
|
|
foreach ($schedItems as $schedItem) {
|
|
|
|
$id = $schedItem->getDbId();
|
|
|
|
$instance = $schedItem->getCcShowInstances($this->con);
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-07-16 03:17:13 +02:00
|
|
|
if (intval($schedInfo[$id]) !== $instance->getDbId()) {
|
2012-11-15 21:52:51 +01:00
|
|
|
throw new OutDatedScheduleException(_("The schedule you're viewing is out of date!"));
|
2012-03-28 14:23:25 +02:00
|
|
|
}
|
|
|
|
}
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-03-28 14:23:25 +02:00
|
|
|
foreach ($showInstances as $instance) {
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-03-28 14:23:25 +02:00
|
|
|
$id = $instance->getDbId();
|
|
|
|
$show = $instance->getCcShow($this->con);
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-07-16 03:17:13 +02:00
|
|
|
if ($this->checkUserPermissions && $this->user->canSchedule($show->getDbId()) === false) {
|
2012-11-15 21:52:51 +01:00
|
|
|
throw new Exception(sprintf(_("You are not allowed to schedule show %s."), $show->getDbName()));
|
2012-03-28 14:23:25 +02:00
|
|
|
}
|
2012-11-02 18:24:47 +01:00
|
|
|
|
|
|
|
if ($instance->getDbRecord()) {
|
2012-11-15 21:52:51 +01:00
|
|
|
throw new Exception(_("You cannot add files to recording shows."));
|
2012-11-02 18:24:47 +01:00
|
|
|
}
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-07-16 03:17:13 +02:00
|
|
|
$showEndEpoch = floatval($instance->getDbEnds("U.u"));
|
|
|
|
|
|
|
|
if ($showEndEpoch < $nowEpoch) {
|
2012-11-15 21:52:51 +01:00
|
|
|
throw new OutDatedScheduleException(sprintf(_("The show %s is over and cannot be scheduled."), $show->getDbName()));
|
2012-03-28 14:23:25 +02:00
|
|
|
}
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-04-02 15:20:13 +02:00
|
|
|
$ts = intval($instanceInfo[$id]);
|
2012-07-16 03:17:13 +02:00
|
|
|
$lastSchedTs = intval($instance->getDbLastScheduled("U")) ? : 0;
|
2012-04-02 15:20:13 +02:00
|
|
|
if ($ts < $lastSchedTs) {
|
2012-08-22 00:41:56 +02:00
|
|
|
Logging::info("ts {$ts} last sched {$lastSchedTs}");
|
2012-11-15 21:52:51 +01:00
|
|
|
throw new OutDatedScheduleException(sprintf(_("The show %s has been previously updated!"), $show->getDbName()));
|
2012-03-28 14:23:25 +02:00
|
|
|
}
|
2012-07-16 03:17:13 +02:00
|
|
|
}
|
2012-01-27 21:06:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-01-31 18:59:27 +01:00
|
|
|
* @param $id
|
|
|
|
* @param $type
|
|
|
|
*
|
|
|
|
* @return $files
|
|
|
|
*/
|
2012-07-16 03:17:13 +02:00
|
|
|
private function retrieveMediaFiles($id, $type)
|
|
|
|
{
|
2012-01-31 18:59:27 +01:00
|
|
|
$files = array();
|
|
|
|
|
2012-02-01 18:47:08 +01:00
|
|
|
if ($type === "audioclip") {
|
2012-02-02 23:15:39 +01:00
|
|
|
$file = CcFilesQuery::create()->findPK($id, $this->con);
|
2013-01-28 20:21:11 +01:00
|
|
|
$storedFile = new Application_Model_StoredFile($file->getDbId());
|
2012-01-31 18:59:27 +01:00
|
|
|
|
2012-11-05 16:57:18 +01:00
|
|
|
if (is_null($file) || !$file->visible()) {
|
2012-11-15 21:52:51 +01:00
|
|
|
throw new Exception(_("A selected File does not exist!"));
|
2012-07-16 03:17:13 +02:00
|
|
|
} else {
|
2012-02-16 19:46:14 +01:00
|
|
|
$data = $this->fileInfo;
|
|
|
|
$data["id"] = $id;
|
2013-01-28 20:21:11 +01:00
|
|
|
$data["cliplength"] = $storedFile->getRealClipLength(
|
|
|
|
$file->getDbCuein(),
|
|
|
|
$file->getDbCueout());
|
|
|
|
|
2013-01-03 20:02:06 +01:00
|
|
|
$data["cuein"] = $file->getDbCuein();
|
|
|
|
$data["cueout"] = $file->getDbCueout();
|
2012-01-31 18:59:27 +01:00
|
|
|
|
2012-02-27 21:29:02 +01:00
|
|
|
$defaultFade = Application_Model_Preference::GetDefaultFade();
|
2012-04-13 11:09:52 +02:00
|
|
|
if (isset($defaultFade)) {
|
2012-02-27 21:29:02 +01:00
|
|
|
//fade is in format SS.uuuuuu
|
|
|
|
$data["fadein"] = $defaultFade;
|
|
|
|
$data["fadeout"] = $defaultFade;
|
|
|
|
}
|
|
|
|
|
2012-02-16 19:46:14 +01:00
|
|
|
$files[] = $data;
|
|
|
|
}
|
2012-08-29 16:58:03 +02:00
|
|
|
} elseif ($type === "playlist") {
|
2012-07-27 17:51:50 +02:00
|
|
|
$pl = new Application_Model_Playlist($id);
|
|
|
|
$contents = $pl->getContents();
|
2012-08-29 16:58:03 +02:00
|
|
|
|
|
|
|
foreach ($contents as $plItem) {
|
|
|
|
if ($plItem['type'] == 0) {
|
2012-07-27 17:51:50 +02:00
|
|
|
$data["id"] = $plItem['item_id'];
|
|
|
|
$data["cliplength"] = $plItem['length'];
|
|
|
|
$data["cuein"] = $plItem['cuein'];
|
|
|
|
$data["cueout"] = $plItem['cueout'];
|
|
|
|
$data["fadein"] = $plItem['fadein'];
|
|
|
|
$data["fadeout"] = $plItem['fadeout'];
|
|
|
|
$data["type"] = 0;
|
2012-08-29 16:58:03 +02:00
|
|
|
$files[] = $data;
|
2012-08-30 20:36:23 +02:00
|
|
|
} elseif ($plItem['type'] == 1) {
|
|
|
|
$data["id"] = $plItem['item_id'];
|
|
|
|
$data["cliplength"] = $plItem['length'];
|
|
|
|
$data["cuein"] = $plItem['cuein'];
|
|
|
|
$data["cueout"] = $plItem['cueout'];
|
|
|
|
$data["fadein"] = "00.500000";//$plItem['fadein'];
|
|
|
|
$data["fadeout"] = "00.500000";//$plItem['fadeout'];
|
|
|
|
$data["type"] = 1;
|
|
|
|
$files[] = $data;
|
2012-08-29 16:58:03 +02:00
|
|
|
} elseif ($plItem['type'] == 2) {
|
2012-07-27 17:51:50 +02:00
|
|
|
// if it's a block
|
2012-08-29 16:58:03 +02:00
|
|
|
$bl = new Application_Model_Block($plItem['item_id']);
|
|
|
|
if ($bl->isStatic()) {
|
2012-07-27 17:51:50 +02:00
|
|
|
foreach ($bl->getContents() as $track) {
|
2012-08-29 16:58:03 +02:00
|
|
|
$data["id"] = $track['item_id'];
|
|
|
|
$data["cliplength"] = $track['length'];
|
|
|
|
$data["cuein"] = $track['cuein'];
|
|
|
|
$data["cueout"] = $track['cueout'];
|
|
|
|
$data["fadein"] = $track['fadein'];
|
2012-07-27 17:51:50 +02:00
|
|
|
$data["fadeout"] = $track['fadeout'];
|
2012-08-29 16:58:03 +02:00
|
|
|
$data["type"] = 0;
|
|
|
|
$files[] = $data;
|
|
|
|
}
|
2012-07-27 17:51:50 +02:00
|
|
|
} else {
|
|
|
|
$dynamicFiles = $bl->getListOfFilesUnderLimit();
|
2012-11-06 22:32:00 +01:00
|
|
|
foreach ($dynamicFiles as $f) {
|
|
|
|
$fileId = $f['id'];
|
2012-07-27 17:51:50 +02:00
|
|
|
$file = CcFilesQuery::create()->findPk($fileId);
|
2012-11-05 16:57:18 +01:00
|
|
|
if (isset($file) && $file->visible()) {
|
2012-08-29 16:58:03 +02:00
|
|
|
$data["id"] = $file->getDbId();
|
2013-02-11 20:17:18 +01:00
|
|
|
$data["cuein"] = $file->getDbCuein();
|
|
|
|
$data["cueout"] = $file->getDbCueout();
|
|
|
|
|
|
|
|
$cuein = Application_Common_DateHelper::calculateLengthInSeconds($data["cuein"]);
|
|
|
|
$cueout = Application_Common_DateHelper::calculateLengthInSeconds($data["cueout"]);
|
|
|
|
$data["cliplength"] = Application_Common_DateHelper::secondsToPlaylistTime($cueout - $cuein);
|
2012-07-30 23:18:33 +02:00
|
|
|
$defaultFade = Application_Model_Preference::GetDefaultFade();
|
|
|
|
if (isset($defaultFade)) {
|
|
|
|
//fade is in format SS.uuuuuu
|
|
|
|
$data["fadein"] = $defaultFade;
|
|
|
|
$data["fadeout"] = $defaultFade;
|
|
|
|
}
|
2012-07-27 17:51:50 +02:00
|
|
|
$data["type"] = 0;
|
2012-08-29 16:58:03 +02:00
|
|
|
$files[] = $data;
|
2012-07-27 17:51:50 +02:00
|
|
|
}
|
|
|
|
}
|
2012-08-29 16:58:03 +02:00
|
|
|
}
|
|
|
|
}
|
2012-07-27 17:51:50 +02:00
|
|
|
}
|
2012-08-29 16:58:03 +02:00
|
|
|
} elseif ($type == "stream") {
|
2012-07-25 04:24:08 +02:00
|
|
|
//need to return
|
|
|
|
$stream = CcWebstreamQuery::create()->findPK($id, $this->con);
|
|
|
|
|
2012-11-05 16:57:18 +01:00
|
|
|
if (is_null($stream) /* || !$file->visible() */) {
|
2012-11-15 21:52:51 +01:00
|
|
|
throw new Exception(_("A selected File does not exist!"));
|
2012-07-25 04:24:08 +02:00
|
|
|
} else {
|
|
|
|
$data = $this->fileInfo;
|
|
|
|
$data["id"] = $id;
|
|
|
|
$data["cliplength"] = $stream->getDbLength();
|
|
|
|
$data["cueout"] = $stream->getDbLength();
|
|
|
|
$data["type"] = 1;
|
|
|
|
|
|
|
|
$defaultFade = Application_Model_Preference::GetDefaultFade();
|
|
|
|
if (isset($defaultFade)) {
|
|
|
|
//fade is in format SS.uuuuuu
|
|
|
|
$data["fadein"] = $defaultFade;
|
|
|
|
$data["fadeout"] = $defaultFade;
|
|
|
|
}
|
|
|
|
|
2012-07-17 17:07:16 +02:00
|
|
|
$files[] = $data;
|
2012-02-02 23:15:39 +01:00
|
|
|
}
|
2012-08-29 16:58:03 +02:00
|
|
|
} elseif ($type == "block") {
|
|
|
|
$bl = new Application_Model_Block($id);
|
|
|
|
if ($bl->isStatic()) {
|
|
|
|
foreach ($bl->getContents() as $track) {
|
|
|
|
$data["id"] = $track['item_id'];
|
|
|
|
$data["cliplength"] = $track['length'];
|
|
|
|
$data["cuein"] = $track['cuein'];
|
|
|
|
$data["cueout"] = $track['cueout'];
|
|
|
|
$data["fadein"] = $track['fadein'];
|
|
|
|
$data["fadeout"] = $track['fadeout'];
|
|
|
|
$data["type"] = 0;
|
|
|
|
$files[] = $data;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$dynamicFiles = $bl->getListOfFilesUnderLimit();
|
2012-11-06 22:32:00 +01:00
|
|
|
foreach ($dynamicFiles as $f) {
|
|
|
|
$fileId = $f['id'];
|
2012-08-29 16:58:03 +02:00
|
|
|
$file = CcFilesQuery::create()->findPk($fileId);
|
2012-11-05 16:57:18 +01:00
|
|
|
if (isset($file) && $file->visible()) {
|
2012-08-29 16:58:03 +02:00
|
|
|
$data["id"] = $file->getDbId();
|
2013-02-11 19:45:51 +01:00
|
|
|
$data["cuein"] = $file->getDbCuein();
|
|
|
|
$data["cueout"] = $file->getDbCueout();
|
|
|
|
|
|
|
|
$cuein = Application_Common_DateHelper::calculateLengthInSeconds($data["cuein"]);
|
|
|
|
$cueout = Application_Common_DateHelper::calculateLengthInSeconds($data["cueout"]);
|
|
|
|
$data["cliplength"] = Application_Common_DateHelper::secondsToPlaylistTime($cueout - $cuein);
|
2012-07-30 23:18:33 +02:00
|
|
|
$defaultFade = Application_Model_Preference::GetDefaultFade();
|
|
|
|
if (isset($defaultFade)) {
|
|
|
|
//fade is in format SS.uuuuuu
|
|
|
|
$data["fadein"] = $defaultFade;
|
|
|
|
$data["fadeout"] = $defaultFade;
|
2012-08-29 16:58:03 +02:00
|
|
|
}
|
|
|
|
$data["type"] = 0;
|
|
|
|
$files[] = $data;
|
|
|
|
}
|
|
|
|
}
|
2012-07-30 23:18:33 +02:00
|
|
|
}
|
2012-01-31 18:59:27 +01:00
|
|
|
}
|
2012-08-29 16:58:03 +02:00
|
|
|
|
2012-01-31 18:59:27 +01:00
|
|
|
return $files;
|
|
|
|
}
|
|
|
|
|
2012-02-01 18:47:08 +01:00
|
|
|
/*
|
2012-02-07 17:29:52 +01:00
|
|
|
* @param DateTime startDT in UTC
|
2012-02-01 18:47:08 +01:00
|
|
|
* @param string duration
|
|
|
|
* in format H:i:s.u (could be more that 24 hours)
|
2012-02-07 17:29:52 +01:00
|
|
|
*
|
|
|
|
* @return DateTime endDT in UTC
|
2012-02-01 18:47:08 +01:00
|
|
|
*/
|
2012-07-16 03:17:13 +02:00
|
|
|
private function findEndTime($p_startDT, $p_duration)
|
|
|
|
{
|
2012-02-07 17:29:52 +01:00
|
|
|
$startEpoch = $p_startDT->format("U.u");
|
2012-07-26 20:41:09 +02:00
|
|
|
$durationSeconds = Application_Common_DateHelper::playlistTimeToSeconds($p_duration);
|
2012-02-07 17:29:52 +01:00
|
|
|
|
|
|
|
//add two float numbers to 6 subsecond precision
|
|
|
|
//DateTime::createFromFormat("U.u") will have a problem if there is no decimal in the resulting number.
|
|
|
|
$endEpoch = bcadd($startEpoch , (string) $durationSeconds, 6);
|
|
|
|
|
|
|
|
$dt = DateTime::createFromFormat("U.u", $endEpoch, new DateTimeZone("UTC"));
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-05-06 21:34:41 +02:00
|
|
|
if ($dt === false) {
|
|
|
|
//PHP 5.3.2 problem
|
|
|
|
$dt = DateTime::createFromFormat("U", intval($endEpoch), new DateTimeZone("UTC"));
|
|
|
|
}
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-02-07 17:29:52 +01:00
|
|
|
return $dt;
|
2012-02-01 18:47:08 +01:00
|
|
|
}
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-07-16 03:17:13 +02:00
|
|
|
private function findNextStartTime($DT, $instance)
|
|
|
|
{
|
|
|
|
$sEpoch = $DT->format("U.u");
|
2012-03-30 12:24:55 +02:00
|
|
|
$nEpoch = $this->epochNow;
|
2012-07-16 03:17:13 +02:00
|
|
|
|
|
|
|
//check for if the show has started.
|
|
|
|
if (bccomp( $nEpoch , $sEpoch , 6) === 1) {
|
|
|
|
//need some kind of placeholder for cc_schedule.
|
|
|
|
//playout_status will be -1.
|
2012-03-30 12:24:55 +02:00
|
|
|
$nextDT = $this->nowDT;
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-07-16 03:17:13 +02:00
|
|
|
$length = bcsub($nEpoch , $sEpoch , 6);
|
2012-07-26 00:42:46 +02:00
|
|
|
$cliplength = Application_Common_DateHelper::secondsToPlaylistTime($length);
|
2012-07-16 03:17:13 +02:00
|
|
|
|
|
|
|
//fillers are for only storing a chunk of time space that has already passed.
|
|
|
|
$filler = new CcSchedule();
|
|
|
|
$filler->setDbStarts($DT)
|
|
|
|
->setDbEnds($this->nowDT)
|
|
|
|
->setDbClipLength($cliplength)
|
2013-02-05 23:57:21 +01:00
|
|
|
->setDbCueIn('00:00:00')
|
|
|
|
->setDbCueOut('00:00:00')
|
2012-07-16 03:17:13 +02:00
|
|
|
->setDbPlayoutStatus(-1)
|
|
|
|
->setDbInstanceId($instance->getDbId())
|
|
|
|
->save($this->con);
|
|
|
|
} else {
|
|
|
|
$nextDT = $DT;
|
2012-03-26 19:08:52 +02:00
|
|
|
}
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-03-26 19:08:52 +02:00
|
|
|
return $nextDT;
|
|
|
|
}
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-07-16 03:17:13 +02:00
|
|
|
/*
|
|
|
|
* @param int $showInstance
|
|
|
|
* @param array $exclude
|
|
|
|
* ids of sched items to remove from the calulation.
|
2012-08-31 18:18:37 +02:00
|
|
|
* This function squeezes all items of a show together so that
|
|
|
|
* there are no gaps between them.
|
2012-07-16 03:17:13 +02:00
|
|
|
*/
|
2012-09-18 21:47:37 +02:00
|
|
|
public function removeGaps($showInstance, $exclude=null)
|
2012-07-16 03:17:13 +02:00
|
|
|
{
|
2012-08-22 00:41:56 +02:00
|
|
|
Logging::info("removing gaps from show instance #".$showInstance);
|
2012-07-16 03:17:13 +02:00
|
|
|
|
|
|
|
$instance = CcShowInstancesQuery::create()->findPK($showInstance, $this->con);
|
|
|
|
if (is_null($instance)) {
|
2012-11-15 21:52:51 +01:00
|
|
|
throw new OutDatedScheduleException(_("The schedule you're viewing is out of date!"));
|
2012-04-16 16:33:32 +02:00
|
|
|
}
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-07-16 03:17:13 +02:00
|
|
|
$itemStartDT = $instance->getDbStarts(null);
|
|
|
|
|
|
|
|
$schedule = CcScheduleQuery::create()
|
|
|
|
->filterByDbInstanceId($showInstance)
|
|
|
|
->filterByDbId($exclude, Criteria::NOT_IN)
|
|
|
|
->orderByDbStarts()
|
|
|
|
->find($this->con);
|
|
|
|
|
|
|
|
foreach ($schedule as $item) {
|
|
|
|
|
|
|
|
$itemEndDT = $this->findEndTime($itemStartDT, $item->getDbClipLength());
|
|
|
|
|
|
|
|
$item->setDbStarts($itemStartDT)
|
|
|
|
->setDbEnds($itemEndDT);
|
|
|
|
|
|
|
|
$itemStartDT = $itemEndDT;
|
|
|
|
}
|
|
|
|
|
|
|
|
$schedule->save($this->con);
|
2012-04-02 15:20:13 +02:00
|
|
|
}
|
2012-02-01 18:47:08 +01:00
|
|
|
|
2013-04-04 22:30:33 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Enter description here ...
|
|
|
|
* @param $scheduleItems
|
|
|
|
* cc_schedule items, where the items get inserted after
|
|
|
|
* @param $filesToInsert
|
|
|
|
* array of schedule item info, what gets inserted into cc_schedule
|
|
|
|
* @param $adjustSched
|
2012-02-02 18:13:20 +01:00
|
|
|
*/
|
2013-04-04 22:30:33 +02:00
|
|
|
private function insertAfter($scheduleItems, $filesToInsert, $adjustSched = true)
|
2012-07-16 03:17:13 +02:00
|
|
|
{
|
2013-04-12 22:34:36 +02:00
|
|
|
Logging::info($scheduleItems);
|
|
|
|
//Logging::info($filesToInsert);
|
|
|
|
|
2012-02-02 18:13:20 +01:00
|
|
|
try {
|
2012-02-16 19:46:14 +01:00
|
|
|
$affectedShowInstances = array();
|
2013-04-04 22:30:33 +02:00
|
|
|
|
|
|
|
//dont want to recalculate times for moved items
|
|
|
|
//only moved items have a sched_id
|
2012-02-06 01:51:22 +01:00
|
|
|
$excludeIds = array();
|
2013-04-04 22:30:33 +02:00
|
|
|
foreach ($filesToInsert as $file) {
|
2012-02-06 01:51:22 +01:00
|
|
|
if (isset($file["sched_id"])) {
|
|
|
|
$excludeIds[] = intval($file["sched_id"]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-16 16:33:32 +02:00
|
|
|
$startProfile = microtime(true);
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2013-04-12 22:34:36 +02:00
|
|
|
/*
|
|
|
|
* We need to prevent items getting added to linked shows more
|
|
|
|
* than once. This can happen if a cursor is selected in the same
|
|
|
|
* position on 2 or more linked shows
|
|
|
|
*/
|
|
|
|
$temp = array();
|
|
|
|
$instance = null;
|
|
|
|
$pos = 0;
|
|
|
|
|
2012-02-01 18:47:08 +01:00
|
|
|
foreach ($scheduleItems as $schedule) {
|
|
|
|
$id = intval($schedule["id"]);
|
2013-04-04 22:30:33 +02:00
|
|
|
|
2013-04-12 22:34:36 +02:00
|
|
|
if ($id != 0) {
|
|
|
|
$ccSchedule = CcScheduleQuery::create()->findPk($schedule["id"]);
|
|
|
|
$ccShowInstance = CcShowInstancesQuery::create()->findPk($ccSchedule->getDbInstanceId());
|
|
|
|
$ccShow = $ccShowInstance->getCcShow();
|
|
|
|
if ($ccShow->isLinked()) {
|
|
|
|
$unique = $ccShow->getDbId() . $ccSchedule->getDbPosition();
|
|
|
|
if (!in_array($unique, $temp)) {
|
|
|
|
$temp[] = $unique;
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$ccShowInstance = CcShowInstancesQuery::create()->findPk($schedule["instance"]);
|
|
|
|
$ccShow = $ccShowInstance->getccShow();
|
|
|
|
if ($ccShow->isLinked()) {
|
|
|
|
$unique = $ccShow->getDbId() . "a";
|
|
|
|
if (!in_array($unique, $temp)) {
|
|
|
|
$temp[] = $unique;
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$instances = $this->getInstances($schedule["instance"]);
|
|
|
|
foreach($instances as $instance) {
|
|
|
|
|
|
|
|
Logging::info($instance->getDbId());
|
2012-02-01 18:47:08 +01:00
|
|
|
if ($id !== 0) {
|
2012-02-02 23:15:39 +01:00
|
|
|
$schedItem = CcScheduleQuery::create()->findPK($id, $this->con);
|
2013-04-12 22:34:36 +02:00
|
|
|
$pos = $schedItem->getDbPosition();
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2013-04-12 22:34:36 +02:00
|
|
|
$ccSchedule = CcScheduleQuery::create()
|
|
|
|
->filterByDbInstanceId($instance->getDbId())
|
|
|
|
->filterByDbPosition($pos)
|
|
|
|
->findOne();
|
|
|
|
|
|
|
|
//$schedItemEndDT = $schedItem->getDbEnds(null);
|
|
|
|
$schedItemEndDT = $ccSchedule->getDbEnds(null);
|
2012-03-26 19:08:52 +02:00
|
|
|
$nextStartDT = $this->findNextStartTime($schedItemEndDT, $instance);
|
2013-04-12 22:34:36 +02:00
|
|
|
|
|
|
|
$pos++;
|
2012-02-01 18:47:08 +01:00
|
|
|
}
|
|
|
|
//selected empty row to add after
|
|
|
|
else {
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2013-04-12 22:34:36 +02:00
|
|
|
//$instance = CcShowInstancesQuery::create()->findPK($schedule["instance"], $this->con);
|
2012-03-28 14:23:25 +02:00
|
|
|
|
2012-03-26 19:08:52 +02:00
|
|
|
$showStartDT = $instance->getDbStarts(null);
|
|
|
|
$nextStartDT = $this->findNextStartTime($showStartDT, $instance);
|
2013-04-12 22:34:36 +02:00
|
|
|
//show is empty so start position counter at 0
|
|
|
|
$pos = 0;
|
2012-02-16 19:46:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!in_array($instance->getDbId(), $affectedShowInstances)) {
|
|
|
|
$affectedShowInstances[] = $instance->getDbId();
|
2012-02-01 18:47:08 +01:00
|
|
|
}
|
|
|
|
|
2013-04-04 22:30:33 +02:00
|
|
|
/*
|
|
|
|
* $adjustSched is true if there are schedule items
|
|
|
|
* following the item just inserted, per show instance
|
|
|
|
*/
|
2012-02-06 01:51:22 +01:00
|
|
|
if ($adjustSched === true) {
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-04-16 16:33:32 +02:00
|
|
|
$pstart = microtime(true);
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-01-31 18:59:27 +01:00
|
|
|
$followingSchedItems = CcScheduleQuery::create()
|
2012-02-06 01:51:22 +01:00
|
|
|
->filterByDBStarts($nextStartDT->format("Y-m-d H:i:s.u"), Criteria::GREATER_EQUAL)
|
2012-02-16 19:46:14 +01:00
|
|
|
->filterByDbInstanceId($instance->getDbId())
|
2012-02-06 01:51:22 +01:00
|
|
|
->filterByDbId($excludeIds, Criteria::NOT_IN)
|
2012-01-31 18:59:27 +01:00
|
|
|
->orderByDbStarts()
|
2012-02-02 23:15:39 +01:00
|
|
|
->find($this->con);
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-07-16 03:17:13 +02:00
|
|
|
$pend = microtime(true);
|
|
|
|
Logging::debug("finding all following items.");
|
2012-04-16 16:33:32 +02:00
|
|
|
Logging::debug(floatval($pend) - floatval($pstart));
|
2012-01-31 18:59:27 +01:00
|
|
|
}
|
|
|
|
|
2013-04-04 22:30:33 +02:00
|
|
|
foreach ($filesToInsert as $file) {
|
2013-04-12 22:34:36 +02:00
|
|
|
Logging::info("INSERTING FILE ----- ".$instance->getDbId());
|
2012-03-29 19:20:02 +02:00
|
|
|
$endTimeDT = $this->findEndTime($nextStartDT, $file['cliplength']);
|
2012-01-31 18:59:27 +01:00
|
|
|
|
2012-02-02 18:13:20 +01:00
|
|
|
//item existed previously and is being moved.
|
|
|
|
//need to keep same id for resources if we want REST.
|
|
|
|
if (isset($file['sched_id'])) {
|
2012-02-02 23:15:39 +01:00
|
|
|
$sched = CcScheduleQuery::create()->findPK($file['sched_id'], $this->con);
|
2012-07-16 03:17:13 +02:00
|
|
|
} else {
|
2012-02-02 18:38:56 +01:00
|
|
|
$sched = new CcSchedule();
|
2012-02-02 18:13:20 +01:00
|
|
|
}
|
2013-01-18 20:28:45 +01:00
|
|
|
|
|
|
|
// default fades are in seconds
|
|
|
|
// we need to convert to '00:00:00' format
|
2013-01-18 20:35:17 +01:00
|
|
|
$file['fadein'] = Application_Common_DateHelper::secondsToPlaylistTime($file['fadein']);
|
|
|
|
$file['fadeout'] = Application_Common_DateHelper::secondsToPlaylistTime($file['fadeout']);
|
2013-04-12 22:34:36 +02:00
|
|
|
|
2012-04-02 15:20:13 +02:00
|
|
|
$sched->setDbStarts($nextStartDT)
|
|
|
|
->setDbEnds($endTimeDT)
|
|
|
|
->setDbCueIn($file['cuein'])
|
|
|
|
->setDbCueOut($file['cueout'])
|
|
|
|
->setDbFadeIn($file['fadein'])
|
|
|
|
->setDbFadeOut($file['fadeout'])
|
|
|
|
->setDbClipLength($file['cliplength'])
|
2013-04-12 22:34:36 +02:00
|
|
|
->setDbPosition($pos)
|
2012-07-25 04:24:08 +02:00
|
|
|
->setDbInstanceId($instance->getDbId());
|
2012-08-29 16:58:03 +02:00
|
|
|
|
|
|
|
switch ($file["type"]) {
|
2012-07-25 04:24:08 +02:00
|
|
|
case 0:
|
|
|
|
$sched->setDbFileId($file['id']);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
$sched->setDbStreamId($file['id']);
|
|
|
|
break;
|
|
|
|
default: break;
|
|
|
|
}
|
2012-08-29 16:58:03 +02:00
|
|
|
|
2012-07-25 04:24:08 +02:00
|
|
|
$sched->save($this->con);
|
2012-01-31 18:59:27 +01:00
|
|
|
|
2012-02-01 18:47:08 +01:00
|
|
|
$nextStartDT = $endTimeDT;
|
2013-04-12 22:34:36 +02:00
|
|
|
$pos++;
|
2013-04-04 22:30:33 +02:00
|
|
|
}//all files have been inserted/moved
|
2012-01-31 18:59:27 +01:00
|
|
|
|
2012-02-06 01:51:22 +01:00
|
|
|
if ($adjustSched === true) {
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-04-16 16:33:32 +02:00
|
|
|
$pstart = microtime(true);
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-01-31 18:59:27 +01:00
|
|
|
//recalculate the start/end times after the inserted items.
|
2012-03-28 14:23:25 +02:00
|
|
|
foreach ($followingSchedItems as $item) {
|
2012-03-29 19:20:02 +02:00
|
|
|
$endTimeDT = $this->findEndTime($nextStartDT, $item->getDbClipLength());
|
2012-01-31 18:59:27 +01:00
|
|
|
|
2012-02-01 18:47:08 +01:00
|
|
|
$item->setDbStarts($nextStartDT);
|
|
|
|
$item->setDbEnds($endTimeDT);
|
2013-04-12 22:34:36 +02:00
|
|
|
$item->setDbPosition($pos);
|
2012-02-01 18:47:08 +01:00
|
|
|
$item->save($this->con);
|
2012-07-11 00:51:32 +02:00
|
|
|
$nextStartDT = $endTimeDT;
|
2013-04-12 22:34:36 +02:00
|
|
|
$pos++;
|
2012-01-31 18:59:27 +01:00
|
|
|
}
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-04-16 16:33:32 +02:00
|
|
|
$pend = microtime(true);
|
2012-07-16 03:17:13 +02:00
|
|
|
Logging::debug("adjusting all following items.");
|
2012-04-16 16:33:32 +02:00
|
|
|
Logging::debug(floatval($pend) - floatval($pstart));
|
2012-01-31 18:59:27 +01:00
|
|
|
}
|
2013-04-12 22:34:36 +02:00
|
|
|
}//for each instance
|
|
|
|
|
|
|
|
}//for each schedule location
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-07-16 03:17:13 +02:00
|
|
|
$endProfile = microtime(true);
|
|
|
|
Logging::debug("finished adding scheduled items.");
|
2012-04-16 16:33:32 +02:00
|
|
|
Logging::debug(floatval($endProfile) - floatval($startProfile));
|
2012-02-01 18:47:08 +01:00
|
|
|
|
2012-03-01 14:59:06 +01:00
|
|
|
//update the status flag in cc_schedule.
|
|
|
|
$instances = CcShowInstancesQuery::create()
|
|
|
|
->filterByPrimaryKeys($affectedShowInstances)
|
|
|
|
->find($this->con);
|
|
|
|
|
2012-04-16 16:33:32 +02:00
|
|
|
$startProfile = microtime(true);
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-03-01 14:59:06 +01:00
|
|
|
foreach ($instances as $instance) {
|
|
|
|
$instance->updateScheduleStatus($this->con);
|
|
|
|
}
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2013-02-01 23:47:07 +01:00
|
|
|
// update is_scheduled flag for each cc_file
|
2013-04-04 22:30:33 +02:00
|
|
|
foreach ($filesToInsert as $file) {
|
2013-02-01 23:47:07 +01:00
|
|
|
$db_file = CcFilesQuery::create()->findPk($file['id'], $this->con);
|
|
|
|
$db_file->setDbIsScheduled(true);
|
|
|
|
$db_file->save($this->con);
|
|
|
|
}
|
|
|
|
|
2012-07-16 03:17:13 +02:00
|
|
|
$endProfile = microtime(true);
|
|
|
|
Logging::debug("updating show instances status.");
|
2012-04-16 16:33:32 +02:00
|
|
|
Logging::debug(floatval($endProfile) - floatval($startProfile));
|
2012-03-01 14:59:06 +01:00
|
|
|
|
2012-04-16 16:33:32 +02:00
|
|
|
$startProfile = microtime(true);
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-02-16 19:46:14 +01:00
|
|
|
//update the last scheduled timestamp.
|
|
|
|
CcShowInstancesQuery::create()
|
2012-02-17 11:14:44 +01:00
|
|
|
->filterByPrimaryKeys($affectedShowInstances)
|
2012-02-16 19:46:14 +01:00
|
|
|
->update(array('DbLastScheduled' => new DateTime("now", new DateTimeZone("UTC"))), $this->con);
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-07-16 03:17:13 +02:00
|
|
|
$endProfile = microtime(true);
|
|
|
|
Logging::debug("updating last scheduled timestamp.");
|
2012-04-16 16:33:32 +02:00
|
|
|
Logging::debug(floatval($endProfile) - floatval($startProfile));
|
2012-07-16 03:17:13 +02:00
|
|
|
} catch (Exception $e) {
|
2012-03-26 19:08:52 +02:00
|
|
|
Logging::debug($e->getMessage());
|
2012-02-02 18:13:20 +01:00
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-12 22:34:36 +02:00
|
|
|
private function getInstances($instanceId)
|
|
|
|
{
|
|
|
|
$ccShowInstance = CcShowInstancesQuery::create()->findPk($instanceId);
|
|
|
|
$ccShow = $ccShowInstance->getCcShow();
|
|
|
|
if ($ccShow->isLinked()) {
|
|
|
|
return $ccShow->getCcShowInstancess();
|
|
|
|
} else {
|
|
|
|
return array($ccShowInstance);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-02 18:38:56 +01:00
|
|
|
/*
|
2013-04-04 22:30:33 +02:00
|
|
|
* @param array $scheduleItems (schedule_id and instance_id it belongs to)
|
|
|
|
* @param array $mediaItems (file|block|playlist|webstream)
|
2012-02-02 18:38:56 +01:00
|
|
|
*/
|
2012-07-16 03:17:13 +02:00
|
|
|
public function scheduleAfter($scheduleItems, $mediaItems, $adjustSched = true)
|
|
|
|
{
|
2012-02-02 18:38:56 +01:00
|
|
|
$this->con->beginTransaction();
|
|
|
|
|
2013-04-04 22:30:33 +02:00
|
|
|
$filesToInsert = array();
|
2012-02-02 18:38:56 +01:00
|
|
|
|
|
|
|
try {
|
2012-03-28 14:23:25 +02:00
|
|
|
$this->validateRequest($scheduleItems);
|
2012-02-02 18:38:56 +01:00
|
|
|
|
2013-04-04 22:30:33 +02:00
|
|
|
/*
|
|
|
|
* create array of arrays
|
|
|
|
* array of schedule item info
|
|
|
|
* (sched_id is the cc_schedule id and is set if an item is being
|
|
|
|
* moved because it is already in cc_schedule)
|
|
|
|
* [0] = Array(
|
|
|
|
* id => 1,
|
|
|
|
* cliplength => 00:04:32,
|
|
|
|
* cuein => 00:00:00,
|
|
|
|
* cueout => 00:04:32,
|
|
|
|
* fadein => 00.5,
|
|
|
|
* fadeout => 00.5,
|
|
|
|
* sched_id => ,
|
|
|
|
* type => 0)
|
|
|
|
* [1] = Array(
|
|
|
|
* id => 2,
|
|
|
|
* cliplength => 00:05:07,
|
|
|
|
* cuein => 00:00:00,
|
|
|
|
* cueout => 00:05:07,
|
|
|
|
* fadein => 00.5,
|
|
|
|
* fadeout => 00.5,
|
|
|
|
* sched_id => ,
|
|
|
|
* type => 0)
|
|
|
|
*/
|
2012-02-28 17:16:43 +01:00
|
|
|
foreach ($mediaItems as $media) {
|
2013-04-04 22:30:33 +02:00
|
|
|
$filesToInsert = array_merge($filesToInsert, $this->retrieveMediaFiles($media["id"], $media["type"]));
|
2012-02-02 18:38:56 +01:00
|
|
|
}
|
2013-04-04 22:30:33 +02:00
|
|
|
$this->insertAfter($scheduleItems, $filesToInsert, $adjustSched);
|
2012-02-02 18:38:56 +01:00
|
|
|
|
|
|
|
$this->con->commit();
|
2012-02-09 21:19:21 +01:00
|
|
|
|
|
|
|
Application_Model_RabbitMq::PushSchedule();
|
2012-07-16 03:17:13 +02:00
|
|
|
} catch (Exception $e) {
|
2012-02-02 18:38:56 +01:00
|
|
|
$this->con->rollback();
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-02 18:13:20 +01:00
|
|
|
/*
|
|
|
|
* @param array $selectedItem
|
|
|
|
* @param array $afterItem
|
|
|
|
*/
|
2012-07-16 03:17:13 +02:00
|
|
|
public function moveItem($selectedItems, $afterItems, $adjustSched = true)
|
|
|
|
{
|
2013-04-12 22:34:36 +02:00
|
|
|
//Logging::info($selectedItems);
|
|
|
|
//Logging::info($afterItems);
|
|
|
|
|
2012-04-16 16:33:32 +02:00
|
|
|
$startProfile = microtime(true);
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-02-02 18:13:20 +01:00
|
|
|
$this->con->beginTransaction();
|
2012-04-16 16:33:32 +02:00
|
|
|
$this->con->useDebug(true);
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-02-02 18:13:20 +01:00
|
|
|
try {
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2013-04-12 22:34:36 +02:00
|
|
|
$this->validateItemMove($selectedItems, $afterItems[0]);
|
2012-03-28 14:23:25 +02:00
|
|
|
$this->validateRequest($selectedItems);
|
|
|
|
$this->validateRequest($afterItems);
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-07-16 03:17:13 +02:00
|
|
|
$endProfile = microtime(true);
|
|
|
|
Logging::debug("validating move request took:");
|
2012-04-16 16:33:32 +02:00
|
|
|
Logging::debug(floatval($endProfile) - floatval($startProfile));
|
2012-07-16 03:17:13 +02:00
|
|
|
|
|
|
|
$afterInstance = CcShowInstancesQuery::create()->findPK($afterItems[0]["instance"], $this->con);
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-03-20 17:55:35 +01:00
|
|
|
//map show instances to cc_schedule primary keys.
|
|
|
|
$modifiedMap = array();
|
|
|
|
$movedData = array();
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-03-20 17:55:35 +01:00
|
|
|
//prepare each of the selected items.
|
|
|
|
for ($i = 0; $i < count($selectedItems); $i++) {
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-07-16 03:17:13 +02:00
|
|
|
$selected = CcScheduleQuery::create()->findPk($selectedItems[$i]["id"], $this->con);
|
|
|
|
$selectedInstance = $selected->getCcShowInstances($this->con);
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-07-16 03:17:13 +02:00
|
|
|
$data = $this->fileInfo;
|
|
|
|
$data["id"] = $selected->getDbFileId();
|
|
|
|
$data["cliplength"] = $selected->getDbClipLength();
|
|
|
|
$data["cuein"] = $selected->getDbCueIn();
|
|
|
|
$data["cueout"] = $selected->getDbCueOut();
|
|
|
|
$data["fadein"] = $selected->getDbFadeIn();
|
|
|
|
$data["fadeout"] = $selected->getDbFadeOut();
|
2012-03-20 17:55:35 +01:00
|
|
|
$data["sched_id"] = $selected->getDbId();
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-03-20 17:55:35 +01:00
|
|
|
$movedData[] = $data;
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-03-20 17:55:35 +01:00
|
|
|
//figure out which items must be removed from calculated show times.
|
|
|
|
$showInstanceId = $selectedInstance->getDbId();
|
|
|
|
$schedId = $selected->getDbId();
|
|
|
|
if (isset($modifiedMap[$showInstanceId])) {
|
2012-07-11 00:51:32 +02:00
|
|
|
array_push($modifiedMap[$showInstanceId], $schedId);
|
2012-07-16 03:17:13 +02:00
|
|
|
} else {
|
2012-03-20 17:55:35 +01:00
|
|
|
$modifiedMap[$showInstanceId] = array($schedId);
|
|
|
|
}
|
2012-07-11 00:51:32 +02:00
|
|
|
}
|
2013-04-12 22:34:36 +02:00
|
|
|
Logging::info($movedData);
|
2012-03-20 17:55:35 +01:00
|
|
|
//calculate times excluding the to be moved items.
|
|
|
|
foreach ($modifiedMap as $instance => $schedIds) {
|
2012-04-16 16:33:32 +02:00
|
|
|
$startProfile = microtime(true);
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-03-20 17:55:35 +01:00
|
|
|
$this->removeGaps($instance, $schedIds);
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-07-16 03:17:13 +02:00
|
|
|
$endProfile = microtime(true);
|
|
|
|
Logging::debug("removing gaps from instance $instance:");
|
2012-04-16 16:33:32 +02:00
|
|
|
Logging::debug(floatval($endProfile) - floatval($startProfile));
|
2012-02-02 18:13:20 +01:00
|
|
|
}
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-04-16 16:33:32 +02:00
|
|
|
$startProfile = microtime(true);
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-03-20 17:55:35 +01:00
|
|
|
$this->insertAfter($afterItems, $movedData, $adjustSched);
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-07-16 03:17:13 +02:00
|
|
|
$endProfile = microtime(true);
|
|
|
|
Logging::debug("inserting after removing gaps.");
|
2012-04-16 16:33:32 +02:00
|
|
|
Logging::debug(floatval($endProfile) - floatval($startProfile));
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-04-16 16:33:32 +02:00
|
|
|
$modified = array_keys($modifiedMap);
|
|
|
|
//need to adjust shows we have moved items from.
|
2012-07-16 03:17:13 +02:00
|
|
|
foreach ($modified as $instanceId) {
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-04-16 16:33:32 +02:00
|
|
|
$instance = CcShowInstancesQuery::create()->findPK($instanceId, $this->con);
|
|
|
|
$instance->updateScheduleStatus($this->con);
|
|
|
|
}
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-04-16 16:33:32 +02:00
|
|
|
$this->con->useDebug(false);
|
2012-02-01 18:47:08 +01:00
|
|
|
$this->con->commit();
|
2012-02-09 21:19:21 +01:00
|
|
|
|
|
|
|
Application_Model_RabbitMq::PushSchedule();
|
2012-07-16 03:17:13 +02:00
|
|
|
} catch (Exception $e) {
|
2012-02-01 18:47:08 +01:00
|
|
|
$this->con->rollback();
|
2012-01-31 18:59:27 +01:00
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-16 03:17:13 +02:00
|
|
|
public function removeItems($scheduledItems, $adjustSched = true)
|
|
|
|
{
|
2012-01-31 18:59:27 +01:00
|
|
|
$showInstances = array();
|
|
|
|
$this->con->beginTransaction();
|
|
|
|
|
|
|
|
try {
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-03-28 14:23:25 +02:00
|
|
|
$this->validateRequest($scheduledItems);
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-02-15 18:27:59 +01:00
|
|
|
$scheduledIds = array();
|
|
|
|
foreach ($scheduledItems as $item) {
|
2012-03-28 14:23:25 +02:00
|
|
|
$scheduledIds[] = $item["id"];
|
2012-02-16 16:29:11 +01:00
|
|
|
}
|
|
|
|
|
2012-03-28 14:23:25 +02:00
|
|
|
$removedItems = CcScheduleQuery::create()->findPks($scheduledIds);
|
2012-02-16 16:29:11 +01:00
|
|
|
|
|
|
|
//check to make sure all items selected are up to date
|
|
|
|
foreach ($removedItems as $removedItem) {
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-02-16 16:29:11 +01:00
|
|
|
$instance = $removedItem->getCcShowInstances($this->con);
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2013-04-12 22:34:36 +02:00
|
|
|
//check if instance is linked and if so get the schedule items
|
|
|
|
//for all linked instances so we can delete them too
|
|
|
|
if ($instance->getCcShow()->isLinked()) {
|
|
|
|
//returns all linked instances if linked
|
|
|
|
$ccShowInstances = $this->getInstances($instance->getDbId());
|
|
|
|
$instanceIds = array();
|
|
|
|
foreach ($ccShowInstances as $ccShowInstance) {
|
|
|
|
$instanceIds[] = $ccShowInstance->getDbId();
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Find all the schedule items that are in the same position
|
|
|
|
* as the selected item by the user.
|
|
|
|
* The position of each track is the same across each linked instance
|
|
|
|
*/
|
|
|
|
$itemsToDelete = CcScheduleQuery::create()
|
|
|
|
->filterByDbPosition($removedItem->getDbPosition())
|
|
|
|
->filterByDbInstanceId($instanceIds, Criteria::IN)
|
|
|
|
->find();
|
|
|
|
foreach ($itemsToDelete as $item) {
|
|
|
|
if (!$removedItems->contains($item)) {
|
|
|
|
$removedItems->append($item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-27 12:44:48 +02:00
|
|
|
//check to truncate the currently playing item instead of deleting it.
|
2012-03-29 19:20:02 +02:00
|
|
|
if ($removedItem->isCurrentItem($this->epochNow)) {
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-03-29 19:20:02 +02:00
|
|
|
$nEpoch = $this->epochNow;
|
2012-03-30 12:24:55 +02:00
|
|
|
$sEpoch = $removedItem->getDbStarts('U.u');
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-07-16 03:17:13 +02:00
|
|
|
$length = bcsub($nEpoch , $sEpoch , 6);
|
2012-07-26 00:42:46 +02:00
|
|
|
$cliplength = Application_Common_DateHelper::secondsToPlaylistTime($length);
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-07-26 20:41:09 +02:00
|
|
|
$cueinSec = Application_Common_DateHelper::playlistTimeToSeconds($removedItem->getDbCueIn());
|
2012-03-30 12:24:55 +02:00
|
|
|
$cueOutSec = bcadd($cueinSec , $length, 6);
|
2012-07-26 00:42:46 +02:00
|
|
|
$cueout = Application_Common_DateHelper::secondsToPlaylistTime($cueOutSec);
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-08-30 23:52:39 +02:00
|
|
|
//Set DbEnds - 1 second because otherwise there can be a timing issue
|
|
|
|
//when sending the new schedule to Pypo where Pypo thinks the track is still
|
|
|
|
//playing.
|
2012-03-29 19:20:02 +02:00
|
|
|
$removedItem->setDbCueOut($cueout)
|
|
|
|
->setDbClipLength($cliplength)
|
2012-08-31 18:18:37 +02:00
|
|
|
->setDbEnds($this->nowDT)
|
2012-03-29 19:20:02 +02:00
|
|
|
->save($this->con);
|
2012-07-16 03:17:13 +02:00
|
|
|
} else {
|
2012-03-27 12:44:48 +02:00
|
|
|
$removedItem->delete($this->con);
|
|
|
|
}
|
2013-02-01 23:47:07 +01:00
|
|
|
|
|
|
|
// update is_scheduled in cc_files but only if
|
|
|
|
// the file is not scheduled somewhere else
|
|
|
|
$fileId = $removedItem->getDbFileId();
|
|
|
|
// check if the removed item is scheduled somewhere else
|
|
|
|
$futureScheduledFiles = Application_Model_Schedule::getAllFutureScheduledFiles();
|
|
|
|
if (!is_null($fileId) && !in_array($fileId, $futureScheduledFiles)) {
|
|
|
|
$db_file = CcFilesQuery::create()->findPk($fileId, $this->con);
|
|
|
|
$db_file->setDbIsScheduled(false)->save($this->con);
|
|
|
|
}
|
2012-02-15 18:27:59 +01:00
|
|
|
}
|
|
|
|
|
2012-01-31 18:59:27 +01:00
|
|
|
if ($adjustSched === true) {
|
|
|
|
//get the show instances of the shows we must adjust times for.
|
|
|
|
foreach ($removedItems as $item) {
|
|
|
|
|
|
|
|
$instance = $item->getDBInstanceId();
|
|
|
|
if (!in_array($instance, $showInstances)) {
|
|
|
|
$showInstances[] = $instance;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-01 14:59:06 +01:00
|
|
|
foreach ($showInstances as $instance) {
|
2012-02-01 18:47:08 +01:00
|
|
|
$this->removeGaps($instance);
|
2012-01-31 18:59:27 +01:00
|
|
|
}
|
|
|
|
}
|
2012-01-31 21:39:34 +01:00
|
|
|
|
2012-03-01 14:59:06 +01:00
|
|
|
//update the status flag in cc_schedule.
|
|
|
|
$instances = CcShowInstancesQuery::create()
|
|
|
|
->filterByPrimaryKeys($showInstances)
|
|
|
|
->find($this->con);
|
|
|
|
|
|
|
|
foreach ($instances as $instance) {
|
|
|
|
$instance->updateScheduleStatus($this->con);
|
|
|
|
}
|
|
|
|
|
2012-02-16 19:46:14 +01:00
|
|
|
//update the last scheduled timestamp.
|
|
|
|
CcShowInstancesQuery::create()
|
|
|
|
->filterByPrimaryKeys($showInstances)
|
|
|
|
->update(array('DbLastScheduled' => new DateTime("now", new DateTimeZone("UTC"))), $this->con);
|
|
|
|
|
2012-01-31 21:39:34 +01:00
|
|
|
$this->con->commit();
|
2012-02-09 21:19:21 +01:00
|
|
|
|
|
|
|
Application_Model_RabbitMq::PushSchedule();
|
2012-07-16 03:17:13 +02:00
|
|
|
} catch (Exception $e) {
|
2012-01-31 18:59:27 +01:00
|
|
|
$this->con->rollback();
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-02-02 18:38:56 +01:00
|
|
|
/*
|
2012-04-02 15:20:13 +02:00
|
|
|
* Used for cancelling the current show instance.
|
2012-07-11 00:51:32 +02:00
|
|
|
*
|
2012-04-02 15:20:13 +02:00
|
|
|
* @param $p_id id of the show instance to cancel.
|
2012-02-02 18:38:56 +01:00
|
|
|
*/
|
2012-07-16 03:17:13 +02:00
|
|
|
public function cancelShow($p_id)
|
|
|
|
{
|
2012-04-02 15:20:13 +02:00
|
|
|
$this->con->beginTransaction();
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-04-02 15:20:13 +02:00
|
|
|
try {
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-04-02 15:20:13 +02:00
|
|
|
$instance = CcShowInstancesQuery::create()->findPK($p_id);
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-04-02 15:31:02 +02:00
|
|
|
if (!$instance->getDbRecord()) {
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-07-16 03:17:13 +02:00
|
|
|
$items = CcScheduleQuery::create()
|
|
|
|
->filterByDbInstanceId($p_id)
|
|
|
|
->filterByDbEnds($this->nowDT, Criteria::GREATER_THAN)
|
|
|
|
->find($this->con);
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-04-03 18:26:04 +02:00
|
|
|
if (count($items) > 0) {
|
2012-07-16 03:17:13 +02:00
|
|
|
$remove = array();
|
|
|
|
$ts = $this->nowDT->format('U');
|
|
|
|
|
|
|
|
for ($i = 0; $i < count($items); $i++) {
|
|
|
|
$remove[$i]["instance"] = $p_id;
|
|
|
|
$remove[$i]["timestamp"] = $ts;
|
|
|
|
$remove[$i]["id"] = $items[$i]->getDbId();
|
|
|
|
}
|
|
|
|
|
2012-04-03 18:26:04 +02:00
|
|
|
$this->removeItems($remove, false);
|
2012-05-15 14:56:31 +02:00
|
|
|
}
|
2012-07-16 03:17:13 +02:00
|
|
|
} else {
|
2012-05-15 15:07:42 +02:00
|
|
|
$rebroadcasts = $instance->getCcShowInstancessRelatedByDbId(null, $this->con);
|
|
|
|
$rebroadcasts->delete($this->con);
|
2012-07-16 03:17:13 +02:00
|
|
|
}
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-05-15 14:56:31 +02:00
|
|
|
$instance->setDbEnds($this->nowDT);
|
|
|
|
$instance->save($this->con);
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-04-02 15:20:13 +02:00
|
|
|
$this->con->commit();
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2012-04-03 18:43:13 +02:00
|
|
|
if ($instance->getDbRecord()) {
|
|
|
|
Application_Model_RabbitMq::SendMessageToShowRecorder("cancel_recording");
|
|
|
|
}
|
2012-07-16 03:17:13 +02:00
|
|
|
} catch (Exception $e) {
|
|
|
|
$this->con->rollback();
|
|
|
|
throw $e;
|
2012-07-11 00:51:32 +02:00
|
|
|
}
|
2012-01-27 21:06:04 +01:00
|
|
|
}
|
2012-02-16 16:29:11 +01:00
|
|
|
}
|
|
|
|
|
2012-04-16 18:04:35 +02:00
|
|
|
class OutDatedScheduleException extends Exception {}
|