2012-01-27 21:06:04 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
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-03-28 10:50:02 +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-04-12 18:00:38 +02:00
|
|
|
|
|
|
|
private $checkUserPermissions = true;
|
2012-01-27 21:06:04 +01:00
|
|
|
|
2012-04-12 18:00:38 +02:00
|
|
|
public function __construct() {
|
2012-01-27 21:06:04 +01:00
|
|
|
|
2012-02-02 23:15:39 +01:00
|
|
|
$this->con = Propel::getConnection(CcSchedulePeer::DATABASE_NAME);
|
2012-03-29 19:20:02 +02:00
|
|
|
|
2012-03-30 12:24:55 +02:00
|
|
|
$this->epochNow = microtime(true);
|
2012-03-29 19:20:02 +02:00
|
|
|
$this->nowDT = DateTime::createFromFormat("U.u", $this->epochNow, new DateTimeZone("UTC"));
|
|
|
|
|
2012-03-28 14:23:25 +02:00
|
|
|
$this->user = Application_Model_User::GetCurrentUser();
|
|
|
|
}
|
|
|
|
|
2012-04-12 18:00:38 +02:00
|
|
|
public function setCheckUserPermissions($value) {
|
|
|
|
$this->checkUserPermissions = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-28 14:23:25 +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-03-28 14:23:25 +02:00
|
|
|
|
|
|
|
for ($i = 0; $i < count($items); $i++) {
|
|
|
|
$id = $items[$i]["id"];
|
|
|
|
|
|
|
|
//could be added to the beginning of a show, which sends id = 0;
|
|
|
|
if ($id > 0) {
|
|
|
|
$schedInfo[$id] = $items[$i]["instance"];
|
|
|
|
}
|
|
|
|
|
|
|
|
$instanceInfo[$items[$i]["instance"]] = $items[$i]["timestamp"];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($instanceInfo) === 0) {
|
|
|
|
throw new Exception("Invalid Request.");
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
//an item has been deleted
|
|
|
|
if (count($schedIds) !== count($schedItems)) {
|
|
|
|
throw new OutDatedScheduleException("The schedule you're viewing is out of date! (sched mismatch)");
|
|
|
|
}
|
|
|
|
|
|
|
|
//a show has been deleted
|
|
|
|
if (count($instanceIds) !== count($showInstances)) {
|
|
|
|
throw new OutDatedScheduleException("The schedule you're viewing is out of date! (instance mismatch)");
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($schedItems as $schedItem) {
|
|
|
|
$id = $schedItem->getDbId();
|
|
|
|
$instance = $schedItem->getCcShowInstances($this->con);
|
|
|
|
|
2012-04-03 17:56:50 +02:00
|
|
|
if (intval($schedInfo[$id]) !== $instance->getDbId()) {
|
2012-03-28 14:23:25 +02:00
|
|
|
throw new OutDatedScheduleException("The schedule you're viewing is out of date!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($showInstances as $instance) {
|
|
|
|
|
|
|
|
$id = $instance->getDbId();
|
|
|
|
$show = $instance->getCcShow($this->con);
|
|
|
|
|
2012-04-12 18:00:38 +02:00
|
|
|
if ($this->checkUserPermissions && $this->user->canSchedule($show->getDbId()) === false) {
|
2012-03-28 14:23:25 +02:00
|
|
|
throw new Exception("You are not allowed to schedule show {$show->getDbName()}.");
|
|
|
|
}
|
|
|
|
|
2012-03-29 19:20:02 +02:00
|
|
|
$showEndEpoch = floatval($instance->getDbEnds("U.u"));
|
2012-03-28 14:23:25 +02:00
|
|
|
|
|
|
|
if ($showEndEpoch < $nowEpoch) {
|
|
|
|
throw new OutDatedScheduleException("The show {$show->getDbName()} is over and cannot be scheduled.");
|
|
|
|
}
|
|
|
|
|
2012-04-02 15:20:13 +02:00
|
|
|
$ts = intval($instanceInfo[$id]);
|
|
|
|
$lastSchedTs = intval($instance->getDbLastScheduled("U")) ? : 0;
|
|
|
|
if ($ts < $lastSchedTs) {
|
|
|
|
Logging::log("ts {$ts} last sched {$lastSchedTs}");
|
2012-03-28 14:23:25 +02:00
|
|
|
throw new OutDatedScheduleException("The show {$show->getDbName()} has been previously updated!");
|
|
|
|
}
|
|
|
|
}
|
2012-01-27 21:06:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-01-31 18:59:27 +01:00
|
|
|
* @param $id
|
|
|
|
* @param $type
|
|
|
|
*
|
|
|
|
* @return $files
|
|
|
|
*/
|
2012-02-01 18:47:08 +01: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);
|
2012-01-31 18:59:27 +01:00
|
|
|
|
2012-02-16 19:46:14 +01:00
|
|
|
if (is_null($file) || !$file->getDbFileExists()) {
|
|
|
|
throw new Exception("A selected File does not exist!");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$data = $this->fileInfo;
|
|
|
|
$data["id"] = $id;
|
|
|
|
$data["cliplength"] = $file->getDbLength();
|
2012-02-24 16:18:15 +01:00
|
|
|
$data["cueout"] = $file->getDbLength();
|
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-01-31 18:59:27 +01:00
|
|
|
}
|
|
|
|
else if ($type === "playlist") {
|
|
|
|
|
2012-02-02 23:15:39 +01:00
|
|
|
$contents = CcPlaylistcontentsQuery::create()
|
|
|
|
->orderByDbPosition()
|
|
|
|
->filterByDbPlaylistId($id)
|
|
|
|
->find($this->con);
|
|
|
|
|
2012-02-16 19:46:14 +01:00
|
|
|
if (is_null($contents)) {
|
|
|
|
throw new Exception("A selected Playlist does not exist!");
|
|
|
|
}
|
|
|
|
|
2012-02-02 23:15:39 +01:00
|
|
|
foreach ($contents as $plItem) {
|
|
|
|
|
2012-02-16 19:46:14 +01:00
|
|
|
$file = $plItem->getCcFiles($this->con);
|
|
|
|
if (isset($file) && $file->getDbFileExists()) {
|
2012-02-02 23:15:39 +01:00
|
|
|
|
2012-02-16 19:46:14 +01:00
|
|
|
$data = $this->fileInfo;
|
|
|
|
$data["id"] = $plItem->getDbFileId();
|
|
|
|
$data["cliplength"] = $plItem->getDbCliplength();
|
|
|
|
$data["cuein"] = $plItem->getDbCuein();
|
|
|
|
$data["cueout"] = $plItem->getDbCueout();
|
|
|
|
$data["fadein"] = $plItem->getDbFadein();
|
|
|
|
$data["fadeout"] = $plItem->getDbFadeout();
|
|
|
|
|
|
|
|
$files[] = $data;
|
|
|
|
}
|
2012-02-02 23:15:39 +01: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-03-29 19:20:02 +02:00
|
|
|
private function findEndTime($p_startDT, $p_duration) {
|
2012-02-07 17:29:52 +01:00
|
|
|
|
|
|
|
$startEpoch = $p_startDT->format("U.u");
|
|
|
|
$durationSeconds = Application_Model_Playlist::playlistTimeToSeconds($p_duration);
|
|
|
|
|
|
|
|
//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"));
|
|
|
|
return $dt;
|
2012-02-01 18:47:08 +01:00
|
|
|
}
|
2012-03-26 19:08:52 +02:00
|
|
|
|
|
|
|
private function findNextStartTime($DT, $instance) {
|
|
|
|
|
2012-03-30 12:24:55 +02:00
|
|
|
$sEpoch = $DT->format("U.u");
|
|
|
|
$nEpoch = $this->epochNow;
|
2012-03-28 14:23:25 +02:00
|
|
|
|
2012-03-26 19:08:52 +02:00
|
|
|
//check for if the show has started.
|
2012-03-30 12:24:55 +02:00
|
|
|
if (bccomp( $nEpoch , $sEpoch , 6) === 1) {
|
2012-03-26 19:08:52 +02:00
|
|
|
//need some kind of placeholder for cc_schedule.
|
|
|
|
//playout_status will be -1.
|
2012-03-30 12:24:55 +02:00
|
|
|
$nextDT = $this->nowDT;
|
|
|
|
|
|
|
|
$length = bcsub($nEpoch , $sEpoch , 6);
|
2012-03-26 19:08:52 +02:00
|
|
|
$cliplength = Application_Model_Playlist::secondsToPlaylistTime($length);
|
|
|
|
|
|
|
|
//fillers are for only storing a chunk of time space that has already passed.
|
|
|
|
$filler = new CcSchedule();
|
|
|
|
$filler->setDbStarts($DT)
|
2012-03-28 14:23:25 +02:00
|
|
|
->setDbEnds($this->nowDT)
|
2012-03-27 12:44:48 +02:00
|
|
|
->setDbClipLength($cliplength)
|
|
|
|
->setDbPlayoutStatus(-1)
|
|
|
|
->setDbInstanceId($instance->getDbId())
|
|
|
|
->save($this->con);
|
2012-03-26 19:08:52 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$nextDT = $DT;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $nextDT;
|
|
|
|
}
|
2012-04-02 15:20:13 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* @param int $showInstance
|
|
|
|
* @param array $exclude
|
|
|
|
* ids of sched items to remove from the calulation.
|
|
|
|
*/
|
|
|
|
private function removeGaps($showInstance, $exclude=null) {
|
|
|
|
|
|
|
|
Logging::log("removing gaps from show instance #".$showInstance);
|
|
|
|
|
|
|
|
$instance = CcShowInstancesQuery::create()->findPK($showInstance, $this->con);
|
|
|
|
if (is_null($instance)) {
|
|
|
|
throw new OutDatedScheduleException("The schedule you're viewing is out of date!");
|
|
|
|
}
|
|
|
|
|
|
|
|
$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)
|
2012-04-16 16:33:32 +02:00
|
|
|
->setDbEnds($itemEndDT);
|
2012-04-02 15:20:13 +02:00
|
|
|
|
|
|
|
$itemStartDT = $itemEndDT;
|
2012-04-16 16:33:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$schedule->save($this->con);
|
2012-04-02 15:20:13 +02:00
|
|
|
}
|
2012-02-01 18:47:08 +01:00
|
|
|
|
2012-02-02 18:13:20 +01:00
|
|
|
/*
|
|
|
|
* @param array $scheduledIds
|
|
|
|
* @param array $fileIds
|
|
|
|
* @param array $playlistIds
|
|
|
|
*/
|
|
|
|
private function insertAfter($scheduleItems, $schedFiles, $adjustSched = true) {
|
|
|
|
|
|
|
|
try {
|
2012-01-31 18:59:27 +01:00
|
|
|
|
2012-02-16 19:46:14 +01:00
|
|
|
$affectedShowInstances = array();
|
|
|
|
|
2012-02-06 01:51:22 +01:00
|
|
|
//dont want to recalculate times for moved items.
|
|
|
|
$excludeIds = array();
|
|
|
|
foreach ($schedFiles as $file) {
|
|
|
|
if (isset($file["sched_id"])) {
|
|
|
|
$excludeIds[] = intval($file["sched_id"]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-16 16:33:32 +02:00
|
|
|
$startProfile = microtime(true);
|
|
|
|
|
2012-02-01 18:47:08 +01:00
|
|
|
foreach ($scheduleItems as $schedule) {
|
|
|
|
$id = intval($schedule["id"]);
|
2012-03-28 14:23:25 +02:00
|
|
|
|
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);
|
2012-02-16 19:46:14 +01:00
|
|
|
$instance = $schedItem->getCcShowInstances($this->con);
|
2012-03-28 14:23:25 +02:00
|
|
|
|
2012-03-26 19:08:52 +02:00
|
|
|
$schedItemEndDT = $schedItem->getDbEnds(null);
|
|
|
|
$nextStartDT = $this->findNextStartTime($schedItemEndDT, $instance);
|
2012-02-01 18:47:08 +01:00
|
|
|
}
|
|
|
|
//selected empty row to add after
|
|
|
|
else {
|
2012-03-26 19:08:52 +02:00
|
|
|
|
2012-03-28 14:23:25 +02:00
|
|
|
$instance = CcShowInstancesQuery::create()->findPK($schedule["instance"], $this->con);
|
|
|
|
|
2012-03-26 19:08:52 +02:00
|
|
|
$showStartDT = $instance->getDbStarts(null);
|
|
|
|
$nextStartDT = $this->findNextStartTime($showStartDT, $instance);
|
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
|
|
|
}
|
|
|
|
|
2012-02-06 01:51:22 +01:00
|
|
|
if ($adjustSched === true) {
|
2012-04-16 16:33:32 +02:00
|
|
|
|
|
|
|
$pstart = microtime(true);
|
|
|
|
|
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-04-16 16:33:32 +02:00
|
|
|
|
|
|
|
$pend = microtime(true);
|
|
|
|
Logging::debug("finding all following items.");
|
|
|
|
Logging::debug(floatval($pend) - floatval($pstart));
|
2012-01-31 18:59:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach($schedFiles as $file) {
|
|
|
|
|
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-02-02 18:38:56 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$sched = new CcSchedule();
|
2012-02-02 18:13:20 +01:00
|
|
|
}
|
2012-04-13 18:05:59 +02:00
|
|
|
Logging::log(print_r($file,true));
|
2012-04-02 15:20:13 +02:00
|
|
|
$sched->setDbStarts($nextStartDT)
|
|
|
|
->setDbEnds($endTimeDT)
|
|
|
|
->setDbFileId($file['id'])
|
|
|
|
->setDbCueIn($file['cuein'])
|
|
|
|
->setDbCueOut($file['cueout'])
|
|
|
|
->setDbFadeIn($file['fadein'])
|
|
|
|
->setDbFadeOut($file['fadeout'])
|
|
|
|
->setDbClipLength($file['cliplength'])
|
|
|
|
->setDbInstanceId($instance->getDbId())
|
|
|
|
->save($this->con);
|
2012-01-31 18:59:27 +01:00
|
|
|
|
2012-02-01 18:47:08 +01:00
|
|
|
$nextStartDT = $endTimeDT;
|
2012-01-31 18:59:27 +01:00
|
|
|
}
|
|
|
|
|
2012-02-06 01:51:22 +01:00
|
|
|
if ($adjustSched === true) {
|
2012-04-16 16:33:32 +02:00
|
|
|
|
|
|
|
$pstart = microtime(true);
|
|
|
|
|
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-04-16 16:33:32 +02:00
|
|
|
|
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);
|
|
|
|
$item->save($this->con);
|
2012-04-16 16:33:32 +02:00
|
|
|
$nextStartDT = $endTimeDT;
|
2012-01-31 18:59:27 +01:00
|
|
|
}
|
2012-04-16 16:33:32 +02:00
|
|
|
|
|
|
|
$pend = microtime(true);
|
|
|
|
Logging::debug("adjusting all following items.");
|
|
|
|
Logging::debug(floatval($pend) - floatval($pstart));
|
2012-01-31 18:59:27 +01:00
|
|
|
}
|
|
|
|
}
|
2012-04-16 16:33:32 +02:00
|
|
|
|
|
|
|
$endProfile = microtime(true);
|
|
|
|
Logging::debug("finished adding scheduled items.");
|
|
|
|
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-03-01 14:59:06 +01:00
|
|
|
foreach ($instances as $instance) {
|
|
|
|
$instance->updateScheduleStatus($this->con);
|
|
|
|
}
|
2012-04-16 16:33:32 +02:00
|
|
|
|
|
|
|
$endProfile = microtime(true);
|
|
|
|
Logging::debug("updating show instances status.");
|
|
|
|
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-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-04-16 16:33:32 +02:00
|
|
|
|
|
|
|
$endProfile = microtime(true);
|
|
|
|
Logging::debug("updating last scheduled timestamp.");
|
|
|
|
Logging::debug(floatval($endProfile) - floatval($startProfile));
|
2012-02-02 18:13:20 +01: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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-02 18:38:56 +01:00
|
|
|
/*
|
2012-02-27 14:33:56 +01:00
|
|
|
* @param array $scheduleItems
|
|
|
|
* @param array $mediaItems
|
2012-02-02 18:38:56 +01:00
|
|
|
*/
|
|
|
|
public function scheduleAfter($scheduleItems, $mediaItems, $adjustSched = true) {
|
|
|
|
|
|
|
|
$this->con->beginTransaction();
|
|
|
|
|
|
|
|
$schedFiles = array();
|
|
|
|
|
|
|
|
try {
|
2012-03-28 14:23:25 +02:00
|
|
|
|
|
|
|
$this->validateRequest($scheduleItems);
|
2012-02-02 18:38:56 +01:00
|
|
|
|
2012-02-28 17:16:43 +01:00
|
|
|
foreach ($mediaItems as $media) {
|
2012-02-02 18:38:56 +01:00
|
|
|
$schedFiles = array_merge($schedFiles, $this->retrieveMediaFiles($media["id"], $media["type"]));
|
|
|
|
}
|
|
|
|
$this->insertAfter($scheduleItems, $schedFiles, $adjustSched);
|
|
|
|
|
|
|
|
$this->con->commit();
|
2012-02-09 21:19:21 +01:00
|
|
|
|
|
|
|
Application_Model_RabbitMq::PushSchedule();
|
2012-02-02 18:38:56 +01:00
|
|
|
}
|
|
|
|
catch (Exception $e) {
|
|
|
|
$this->con->rollback();
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-02 18:13:20 +01:00
|
|
|
/*
|
|
|
|
* @param array $selectedItem
|
|
|
|
* @param array $afterItem
|
|
|
|
*/
|
2012-02-16 19:46:14 +01:00
|
|
|
public function moveItem($selectedItems, $afterItems, $adjustSched = true) {
|
2012-02-02 18:13:20 +01:00
|
|
|
|
2012-04-16 16:33:32 +02:00
|
|
|
$startProfile = microtime(true);
|
|
|
|
|
2012-02-02 18:13:20 +01:00
|
|
|
$this->con->beginTransaction();
|
2012-04-16 16:33:32 +02:00
|
|
|
$this->con->useDebug(true);
|
2012-03-20 17:55:35 +01:00
|
|
|
|
2012-02-02 18:13:20 +01:00
|
|
|
try {
|
2012-03-20 17:55:35 +01:00
|
|
|
|
2012-03-28 14:23:25 +02:00
|
|
|
$this->validateRequest($selectedItems);
|
|
|
|
$this->validateRequest($afterItems);
|
2012-04-16 16:33:32 +02:00
|
|
|
|
|
|
|
$endProfile = microtime(true);
|
|
|
|
Logging::debug("validating move request took:");
|
|
|
|
Logging::debug(floatval($endProfile) - floatval($startProfile));
|
2012-03-28 14:23:25 +02:00
|
|
|
|
|
|
|
$afterInstance = CcShowInstancesQuery::create()->findPK($afterItems[0]["instance"], $this->con);
|
|
|
|
|
2012-03-20 17:55:35 +01:00
|
|
|
//map show instances to cc_schedule primary keys.
|
|
|
|
$modifiedMap = array();
|
|
|
|
$movedData = array();
|
|
|
|
|
|
|
|
//prepare each of the selected items.
|
|
|
|
for ($i = 0; $i < count($selectedItems); $i++) {
|
|
|
|
|
|
|
|
$selected = CcScheduleQuery::create()->findPk($selectedItems[$i]["id"], $this->con);
|
|
|
|
$selectedInstance = $selected->getCcShowInstances($this->con);
|
|
|
|
|
|
|
|
$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();
|
|
|
|
$data["sched_id"] = $selected->getDbId();
|
|
|
|
|
|
|
|
$movedData[] = $data;
|
|
|
|
|
|
|
|
//figure out which items must be removed from calculated show times.
|
|
|
|
$showInstanceId = $selectedInstance->getDbId();
|
|
|
|
$schedId = $selected->getDbId();
|
|
|
|
if (isset($modifiedMap[$showInstanceId])) {
|
|
|
|
array_push($modifiedMap[$showInstanceId], $schedId);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$modifiedMap[$showInstanceId] = array($schedId);
|
|
|
|
}
|
|
|
|
}
|
2012-02-02 18:13:20 +01:00
|
|
|
|
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-03-20 17:55:35 +01:00
|
|
|
$this->removeGaps($instance, $schedIds);
|
2012-04-16 16:33:32 +02:00
|
|
|
|
|
|
|
$endProfile = microtime(true);
|
|
|
|
Logging::debug("removing gaps from instance $instance:");
|
|
|
|
Logging::debug(floatval($endProfile) - floatval($startProfile));
|
2012-02-02 18:13:20 +01:00
|
|
|
}
|
2012-03-20 17:55:35 +01:00
|
|
|
|
2012-04-16 16:33:32 +02:00
|
|
|
$startProfile = microtime(true);
|
|
|
|
|
2012-03-20 17:55:35 +01:00
|
|
|
$this->insertAfter($afterItems, $movedData, $adjustSched);
|
2012-04-16 16:33:32 +02:00
|
|
|
|
|
|
|
$endProfile = microtime(true);
|
|
|
|
Logging::debug("inserting after removing gaps.");
|
|
|
|
Logging::debug(floatval($endProfile) - floatval($startProfile));
|
|
|
|
|
|
|
|
$afterInstanceId = $afterInstance->getDbId();
|
|
|
|
$modified = array_keys($modifiedMap);
|
|
|
|
//need to adjust shows we have moved items from.
|
|
|
|
foreach($modified as $instanceId) {
|
|
|
|
|
|
|
|
$instance = CcShowInstancesQuery::create()->findPK($instanceId, $this->con);
|
|
|
|
$instance->updateScheduleStatus($this->con);
|
|
|
|
}
|
|
|
|
|
|
|
|
$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-01-31 18:59:27 +01: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-02-15 18:27:59 +01:00
|
|
|
public function removeItems($scheduledItems, $adjustSched = true) {
|
2012-01-31 18:59:27 +01:00
|
|
|
|
|
|
|
$showInstances = array();
|
|
|
|
$this->con->beginTransaction();
|
|
|
|
|
|
|
|
try {
|
2012-03-28 14:23:25 +02:00
|
|
|
|
|
|
|
$this->validateRequest($scheduledItems);
|
|
|
|
|
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-03-28 14:23:25 +02:00
|
|
|
|
2012-02-16 16:29:11 +01:00
|
|
|
$instance = $removedItem->getCcShowInstances($this->con);
|
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-03-27 12:44:48 +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');
|
|
|
|
|
|
|
|
$length = bcsub($nEpoch , $sEpoch , 6);
|
2012-03-27 12:44:48 +02:00
|
|
|
$cliplength = Application_Model_Playlist::secondsToPlaylistTime($length);
|
|
|
|
|
2012-03-29 19:20:02 +02:00
|
|
|
$cueinSec = Application_Model_Playlist::playlistTimeToSeconds($removedItem->getDbCueIn());
|
2012-03-30 12:24:55 +02:00
|
|
|
$cueOutSec = bcadd($cueinSec , $length, 6);
|
|
|
|
$cueout = Application_Model_Playlist::secondsToPlaylistTime($cueOutSec);
|
2012-03-30 14:45:30 +02:00
|
|
|
|
2012-03-29 19:20:02 +02:00
|
|
|
$removedItem->setDbCueOut($cueout)
|
|
|
|
->setDbClipLength($cliplength)
|
2012-03-30 12:24:55 +02:00
|
|
|
->setDbEnds($this->nowDT)
|
2012-03-29 19:20:02 +02:00
|
|
|
->save($this->con);
|
2012-03-27 12:44:48 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$removedItem->delete($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-01-31 18:59:27 +01:00
|
|
|
}
|
|
|
|
catch (Exception $e) {
|
|
|
|
$this->con->rollback();
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|
2012-04-02 15:20:13 +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.
|
|
|
|
*
|
|
|
|
* @param $p_id id of the show instance to cancel.
|
2012-02-02 18:38:56 +01:00
|
|
|
*/
|
2012-04-02 15:20:13 +02:00
|
|
|
public function cancelShow($p_id) {
|
|
|
|
|
|
|
|
$this->con->beginTransaction();
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
$instance = CcShowInstancesQuery::create()->findPK($p_id);
|
|
|
|
|
2012-04-02 15:31:02 +02:00
|
|
|
if (!$instance->getDbRecord()) {
|
|
|
|
|
|
|
|
$items = CcScheduleQuery::create()
|
|
|
|
->filterByDbInstanceId($p_id)
|
|
|
|
->filterByDbEnds($this->nowDT, Criteria::GREATER_THAN)
|
|
|
|
->find($this->con);
|
2012-04-03 18:26:04 +02:00
|
|
|
|
|
|
|
if (count($items) > 0) {
|
|
|
|
$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();
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->removeItems($remove, false);
|
2012-04-02 15:31:02 +02:00
|
|
|
}
|
2012-04-03 18:26:04 +02:00
|
|
|
|
2012-04-03 11:15:22 +02:00
|
|
|
$instance->setDbEnds($this->nowDT);
|
|
|
|
$instance->save($this->con);
|
2012-04-02 15:20:13 +02:00
|
|
|
}
|
2012-04-03 11:15:22 +02:00
|
|
|
else {
|
|
|
|
$instance->delete($this->con);
|
|
|
|
}
|
|
|
|
|
2012-04-02 15:20:13 +02:00
|
|
|
$this->con->commit();
|
2012-04-03 18:43:13 +02:00
|
|
|
|
|
|
|
if ($instance->getDbRecord()) {
|
|
|
|
Application_Model_RabbitMq::SendMessageToShowRecorder("cancel_recording");
|
|
|
|
}
|
2012-01-31 18:59:27 +01:00
|
|
|
}
|
2012-04-02 15:20:13 +02:00
|
|
|
catch (Exception $e) {
|
|
|
|
$this->con->rollback();
|
|
|
|
throw $e;
|
|
|
|
}
|
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 {}
|