123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489 |
- <?php
- class Application_Service_SchedulerService
- {
- private $con;
- private $fileInfo = array(
- "id" => "",
- "cliplength" => "",
- "cuein" => "00:00:00",
- "cueout" => "00:00:00",
- "fadein" => "00:00:00",
- "fadeout" => "00:00:00",
- "sched_id" => null,
- "type" => 0
- );
- private $epochNow;
- private $nowDT;
- private $currentUser;
- private $checkUserPermissions = true;
- public function __construct()
- {
- $this->con = Propel::getConnection(CcSchedulePeer::DATABASE_NAME);
-
-
-
-
- $this->epochNow = microtime(true)-1;
- $this->nowDT = DateTime::createFromFormat("U.u", $this->epochNow, new DateTimeZone("UTC"));
- if ($this->nowDT === false) {
-
-
- $this->nowDT = DateTime::createFromFormat("U", time(), new DateTimeZone("UTC"));
- }
- $user_service = new Application_Service_UserService();
- $this->currentUser = $user_service->getCurrentUser();
- }
-
- public static function updateScheduleStartTime($instanceIds, $diff)
- {
- $con = Propel::getConnection();
- if (count($instanceIds) > 0) {
- $showIdList = implode(",", $instanceIds);
- $ccSchedules = CcScheduleQuery::create()
- ->filterByDbInstanceId($instanceIds, Criteria::IN)
- ->find($con);
- $interval = new DateInterval("PT".abs($diff)."S");
- if ($diff < 0) {
- $interval->invert = 1;
- }
- foreach ($ccSchedules as $ccSchedule) {
- $start = $ccSchedule->getDbStarts(null);
- $newStart = $start->add($interval);
- $end = $ccSchedule->getDbEnds(null);
- $newEnd = $end->add($interval);
- $ccSchedule
- ->setDbStarts($newStart)
- ->setDbEnds($newEnd)
- ->save($con);
- }
- }
- }
-
- public function removeGaps($showId, $schedIds=null)
- {
- $ccShowInstances = CcShowInstancesQuery::create()->filterByDbShowId($showId)->find();
- foreach ($ccShowInstances as $instance) {
- Logging::info("Removing gaps from show instance #".$instance->getDbId());
-
- $itemStart = $instance->getDbStarts(null);
- $ccScheduleItems = CcScheduleQuery::create()
- ->filterByDbInstanceId($instance->getDbId())
- ->filterByDbId($schedIds, Criteria::NOT_IN)
- ->orderByDbStarts()
- ->find();
- foreach ($ccScheduleItems as $ccSchedule) {
-
- $itemEnd = $this->findEndTime($itemStart, $ccSchedule->getDbClipLength());
- $ccSchedule->setDbStarts($itemStart)
- ->setDbEnds($itemEnd);
- $itemStart = $itemEnd;
- }
- $ccScheduleItems->save();
- }
- }
-
- private static function findEndTime($instanceStart, $clipLength)
- {
- $startEpoch = $instanceStart->format("U.u");
- $durationSeconds = Application_Common_DateHelper::playlistTimeToSeconds($clipLength);
-
-
- $endEpoch = bcadd($startEpoch , (string) $durationSeconds, 6);
- $dt = DateTime::createFromFormat("U.u", $endEpoch, new DateTimeZone("UTC"));
- if ($dt === false) {
-
- $dt = DateTime::createFromFormat("U", intval($endEpoch), new DateTimeZone("UTC"));
- }
- return $dt;
- }
- private static function findTimeDifference($p_startDT, $p_seconds)
- {
- $startEpoch = $p_startDT->format("U.u");
-
-
-
- $newEpoch = bcsub($startEpoch , (string) $p_seconds, 6);
-
- $dt = DateTime::createFromFormat("U.u", $newEpoch, new DateTimeZone("UTC"));
-
- if ($dt === false) {
-
- $dt = DateTime::createFromFormat("U", intval($newEpoch), new DateTimeZone("UTC"));
- }
-
- return $dt;
- }
-
- private static function getLinkedShowSchedule($showId, $instancsIdsToFill, $instanceId)
- {
- $con = Propel::getConnection();
-
- if (is_null($instanceId)) {
- $showsPopulatedUntil = Application_Model_Preference::GetShowsPopulatedUntil();
- $showInstanceWithMostRecentSchedule = CcShowInstancesQuery::create()
- ->filterByDbShowId($showId)
- ->filterByDbStarts($showsPopulatedUntil->format("Y-m-d H:i:s"), Criteria::LESS_THAN)
- ->filterByDbId($instancsIdsToFill, Criteria::NOT_IN)
- ->orderByDbStarts(Criteria::DESC)
- ->limit(1)
- ->findOne();
- if (is_null($showInstanceWithMostRecentSchedule)) {
- return null;
- }
- $instanceId = $showInstanceWithMostRecentSchedule->getDbId();
- }
- $linkedShowSchedule_sql = $con->prepare(
- "select * from cc_schedule where instance_id = :instance_id ".
- "order by starts");
- $linkedShowSchedule_sql->bindParam(':instance_id', $instanceId);
- $linkedShowSchedule_sql->execute();
- return $linkedShowSchedule_sql->fetchAll();
- }
-
-
- public static function fillLinkedInstances($ccShow, $instanceIdsToFill, $instanceId=null)
- {
-
-
- $linkedShowSchedule = self::getLinkedShowSchedule($ccShow->getDbId(), $instanceIdsToFill, $instanceId);
-
- if (!empty($linkedShowSchedule)) {
- $timeFilled_sql = "SELECT time_filled FROM cc_show_instances ".
- "WHERE id = {$linkedShowSchedule[0]["instance_id"]}";
- $timeFilled = Application_Common_Database::prepareAndExecute(
- $timeFilled_sql, array(), Application_Common_Database::COLUMN);
- } else {
-
-
- $timeFilled = "00:00:00";
- }
-
- $values = array();
- $con = Propel::getConnection();
-
-
-
- try {
- $con->beginTransaction();
- foreach ($instanceIdsToFill as $id)
- {
-
-
- self::clearShowInstanceContents($id);
-
-
- $instanceStart_sql = "SELECT starts FROM cc_show_instances " .
- "WHERE id = {$id} " . "ORDER BY starts";
-
-
-
- $nextStartDT = new DateTime(
- Application_Common_Database::prepareAndExecute(
- $instanceStart_sql, array(),
- Application_Common_Database::COLUMN),
- new DateTimeZone("UTC"));
-
- $defaultCrossfadeDuration = Application_Model_Preference::GetDefaultCrossfadeDuration();
- unset($values);
- $values = array();
- foreach ($linkedShowSchedule as $item) {
- $endTimeDT = self::findEndTime($nextStartDT,
- $item["clip_length"]);
-
- if (is_null($item["file_id"])) {
- $item["file_id"] = "null";
- }
- if (is_null($item["stream_id"])) {
- $item["stream_id"] = "null";
- }
-
- $values[] = "(" . "'{$nextStartDT->format("Y-m-d H:i:s")}', " .
- "'{$endTimeDT->format("Y-m-d H:i:s")}', " .
- "'{$item["clip_length"]}', " .
- "'{$item["fade_in"]}', " . "'{$item["fade_out"]}', " .
- "'{$item["cue_in"]}', " . "'{$item["cue_out"]}', " .
- "{$item["file_id"]}, " . "{$item["stream_id"]}, " .
- "{$id}, " . "{$item["position"]})";
-
- $nextStartDT = self::findTimeDifference($endTimeDT,
- $defaultCrossfadeDuration);
- }
- if (!empty($values)) {
- $insert_sql = "INSERT INTO cc_schedule (starts, ends, ".
- "clip_length, fade_in, fade_out, cue_in, cue_out, ".
- "file_id, stream_id, instance_id, position) VALUES ".
- implode($values, ",");
- Application_Common_Database::prepareAndExecute(
- $insert_sql, array(), Application_Common_Database::EXECUTE);
- }
-
-
- $instance = CcShowInstancesQuery::create()->findPk($id);
- $instance->updateScheduleStatus($con);
- }
-
- $now = gmdate("Y-m-d H:i:s");
- $whereClause = new Criteria();
- $whereClause->add(CcShowInstancesPeer::ID, $instanceIdsToFill, Criteria::IN);
-
- $updateClause = new Criteria();
- $updateClause->add(CcShowInstancesPeer::TIME_FILLED, $timeFilled);
- $updateClause->add(CcShowInstancesPeer::LAST_SCHEDULED, $now);
-
- BasePeer::doUpdate($whereClause, $updateClause, $con);
- $con->commit();
- Logging::info("finished fill");
- } catch (Exception $e) {
- $con->rollback();
- Logging::info("Error filling linked shows: ".$e->getMessage());
- exit();
- }
- }
- public static function fillPreservedLinkedShowContent($ccShow, $showStamp)
- {
- $item = $showStamp->getFirst();
- $timeFilled = $item->getCcShowInstances()->getDbTimeFilled();
- foreach ($ccShow->getCcShowInstancess() as $ccShowInstance) {
- $ccSchedules = CcScheduleQuery::create()
- ->filterByDbInstanceId($ccShowInstance->getDbId())
- ->find();
- if ($ccSchedules->isEmpty()) {
- $nextStartDT = $ccShowInstance->getDbStarts(null);
- foreach ($showStamp as $item) {
- $endTimeDT = self::findEndTime($nextStartDT, $item->getDbClipLength());
- $ccSchedule = new CcSchedule();
- $ccSchedule
- ->setDbStarts($nextStartDT)
- ->setDbEnds($endTimeDT)
- ->setDbFileId($item->getDbFileId())
- ->setDbStreamId($item->getDbStreamId())
- ->setDbClipLength($item->getDbClipLength())
- ->setDbFadeIn($item->getDbFadeIn())
- ->setDbFadeOut($item->getDbFadeOut())
- ->setDbCuein($item->getDbCueIn())
- ->setDbCueOut($item->getDbCueOut())
- ->setDbInstanceId($ccShowInstance->getDbId())
- ->setDbPosition($item->getDbPosition())
- ->save();
- $nextStartDT = self::findTimeDifference($endTimeDT,
- Application_Model_Preference::GetDefaultCrossfadeDuration());
- }
- $ccShowInstance
- ->setDbTimeFilled($timeFilled)
- ->setDbLastScheduled(gmdate("Y-m-d H:i:s"))
- ->save();
- }
- }
- }
-
- private static function clearShowInstanceContents($instanceId)
- {
-
- $con = Propel::getConnection();
- $query = $con->prepare("DELETE FROM cc_schedule WHERE instance_id = :instance_id");
- $query->bindParam(':instance_id', $instanceId);
- $query->execute();
- }
-
-
- public function emptyShowContent($instanceId)
- {
- try {
- $ccShowInstance = CcShowInstancesQuery::create()->findPk($instanceId);
- $instances = array();
- $instanceIds = array();
- if ($ccShowInstance->getCcShow()->isLinked()) {
- foreach ($ccShowInstance->getCcShow()->getFutureCcShowInstancess() as $instance) {
- $instanceIds[] = $instance->getDbId();
- $instances[] = $instance;
- }
- } else {
- $instanceIds[] = $ccShowInstance->getDbId();
- $instances[] = $ccShowInstance;
- }
-
- $ccSchedules = CcScheduleQuery::create()
- ->filterByDbInstanceId($instanceIds, Criteria::IN)
- ->setDistinct(CcSchedulePeer::FILE_ID)
- ->find();
- $fileIds = array();
- foreach ($ccSchedules as $ccSchedule) {
- $fileIds[] = $ccSchedule->getDbFileId();
- }
-
- CcScheduleQuery::create()
- ->filterByDbInstanceId($instanceIds, Criteria::IN)
- ->delete();
-
- $futureScheduledFiles = Application_Model_Schedule::getAllFutureScheduledFiles();
- foreach ($fileIds as $k => $v) {
- if (in_array($v, $futureScheduledFiles)) {
- unset($fileIds[$k]);
- }
- }
- $selectCriteria = new Criteria();
- $selectCriteria->add(CcFilesPeer::ID, $fileIds, Criteria::IN);
- $updateCriteria = new Criteria();
- $updateCriteria->add(CcFilesPeer::IS_SCHEDULED, false);
- BasePeer::doUpdate($selectCriteria, $updateCriteria, Propel::getConnection());
- Application_Model_RabbitMq::PushSchedule();
- $con = Propel::getConnection(CcShowInstancesPeer::DATABASE_NAME);
- foreach ($instances as $instance) {
- $instance->updateDbTimeFilled($con);
- }
- return true;
- } catch (Exception $e) {
- Logging::info($e->getMessage());
- return false;
- }
- }
-
-
- public function updateFutureIsScheduled($scheduleId, $status)
- {
- $sched = CcScheduleQuery::create()->findPk($scheduleId);
- $redraw = false;
-
- if (isset($sched)) {
-
- $fileId = $sched->getDbFileId();
-
- if (isset($fileId)) {
- $redraw = Application_Model_StoredFile::setIsScheduled($fileId, $status);
- }
- }
-
- return $redraw;
- }
- }
|