ShowBuilder.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. <?php
  2. require_once 'formatters/LengthFormatter.php';
  3. require_once 'formatters/TimeFilledFormatter.php';
  4. class Application_Model_ShowBuilder
  5. {
  6. private $timezone;
  7. //in UTC timezone
  8. private $startDT;
  9. //in UTC timezone
  10. private $endDT;
  11. private $user;
  12. private $opts;
  13. private $pos;
  14. private $contentDT;
  15. private $epoch_now;
  16. private $currentShow;
  17. private $currentShowId;
  18. private $showInstances = array();
  19. private $defaultRowArray = array(
  20. "header" => false,
  21. "footer" => false,
  22. "empty" => false,
  23. "allowed" => false,
  24. "linked_allowed" => true,
  25. "id" => 0,
  26. "instance" => "",
  27. "starts" => "",
  28. "ends" => "",
  29. "runtime" => "",
  30. "title" => "",
  31. "creator" => "",
  32. "album" => "",
  33. "timestamp" => null,
  34. "cuein" => "",
  35. "cueout" => "",
  36. "fadein" => "",
  37. "fadeout" => "",
  38. "image" => false,
  39. "mime" => null,
  40. "color" => "", //in hex without the '#' sign.
  41. "backgroundColor" => "", //in hex without the '#' sign.
  42. );
  43. /*
  44. * @param DateTime $p_startsDT
  45. * @param DateTime $p_endsDT
  46. */
  47. public function __construct($p_startDT, $p_endDT, $p_opts)
  48. {
  49. $this->startDT = $p_startDT;
  50. $this->endDT = $p_endDT;
  51. $this->timezone = Application_Model_Preference::GetUserTimezone();
  52. $this->user = Application_Model_User::getCurrentUser();
  53. $this->opts = $p_opts;
  54. $this->epoch_now = floatval(microtime(true));
  55. $this->currentShow = false;
  56. }
  57. private function getUsersShows()
  58. {
  59. $shows = array();
  60. $host_shows = CcShowHostsQuery::create()
  61. ->setFormatter(ModelCriteria::FORMAT_ON_DEMAND)
  62. ->filterByDbHost($this->user->getId())
  63. ->find();
  64. foreach ($host_shows as $host_show) {
  65. $shows[] = $host_show->getDbShow();
  66. }
  67. return $shows;
  68. }
  69. //check to see if this row should be editable by the user.
  70. private function isAllowed($p_item, &$row)
  71. {
  72. //cannot schedule in a recorded show.
  73. if (intval($p_item["si_record"]) === 1) {
  74. return;
  75. }
  76. if ($this->currentShow) {
  77. $this->currentShowId = $p_item["show_id"];
  78. }
  79. /* If any linked show instance is currently playing
  80. * we have to disable editing, or else the times
  81. * will not make sense for shows scheduled in the future
  82. */
  83. if ($p_item["linked"] && $p_item["show_id"] == $this->currentShowId) {
  84. $row["linked_allowed"] = false;
  85. }
  86. if ($this->user->canSchedule($p_item["show_id"]) == true) {
  87. $row["allowed"] = true;
  88. }
  89. }
  90. private function getItemColor($p_item, &$row)
  91. {
  92. $defaultColor = "ffffff";
  93. $defaultBackground = "3366cc";
  94. $color = $p_item["show_color"];
  95. if ($color === '') {
  96. $color = $defaultColor;
  97. }
  98. $backgroundColor = $p_item["show_background_color"];
  99. if ($backgroundColor === '') {
  100. $backgroundColor = $defaultBackground;
  101. }
  102. $row["color"] = $color;
  103. $row["backgroundColor"] = $backgroundColor;
  104. }
  105. //information about whether a track is inside|boundary|outside a show.
  106. private function getItemStatus($p_item, &$row)
  107. {
  108. $row["status"] = intval($p_item["playout_status"]);
  109. }
  110. private function getRowTimestamp($p_item, &$row)
  111. {
  112. if (is_null($p_item["si_last_scheduled"])) {
  113. $ts = 0;
  114. } else {
  115. $dt = new DateTime($p_item["si_last_scheduled"], new DateTimeZone("UTC"));
  116. $ts = intval($dt->format("U"));
  117. }
  118. $row["timestamp"] = $ts;
  119. }
  120. /*
  121. * marks a row's status.
  122. * 0 = past
  123. * 1 = current
  124. * 2 = future
  125. * TODO : change all of the above to real constants -- RG
  126. */
  127. private function getScheduledStatus($p_epochItemStart, $p_epochItemEnd, &$row)
  128. {
  129. if ($row["footer"] === true && $this->epoch_now > $p_epochItemStart &&
  130. $this->epoch_now > $p_epochItemEnd) {
  131. $row["scheduled"] = 0;
  132. } elseif ($row["footer"] === true && $this->epoch_now < $p_epochItemEnd) {
  133. $row["scheduled"] = 2;
  134. } elseif ($row["header"] === true && $this->epoch_now >= $p_epochItemStart) {
  135. $row["scheduled"] = 0;
  136. } elseif ($row["header"] === true && $this->epoch_now < $p_epochItemEnd) {
  137. $row["scheduled"] = 2;
  138. }
  139. //item is in the past.
  140. else if ($this->epoch_now > $p_epochItemEnd) {
  141. $row["scheduled"] = 0;
  142. }
  143. //item is the currently scheduled item.
  144. else if ($this->epoch_now >= $p_epochItemStart && $this->epoch_now < $p_epochItemEnd) {
  145. $row["scheduled"] = 1;
  146. //how many seconds the view should wait to redraw itself.
  147. $row["refresh"] = $p_epochItemEnd - $this->epoch_now;
  148. }
  149. //item is in the future.
  150. else if ($this->epoch_now < $p_epochItemStart) {
  151. $row["scheduled"] = 2;
  152. } else {
  153. Logging::warn("No-op? is this what should happen...printing
  154. debug just in case");
  155. $d = array(
  156. '$p_epochItemStart' => $p_epochItemStart,
  157. '$p_epochItemEnd' => $p_epochItemEnd,
  158. '$row' => $row);
  159. Logging::warn($d);
  160. }
  161. }
  162. private function makeHeaderRow($p_item)
  163. {
  164. $row = $this->defaultRowArray;
  165. //$this->isAllowed($p_item, $row);
  166. $this->getRowTimestamp($p_item, $row);
  167. $this->getItemColor($p_item, $row);
  168. $showStartDT = new DateTime($p_item["si_starts"], new DateTimeZone("UTC"));
  169. $showStartDT->setTimezone(new DateTimeZone($this->timezone));
  170. $startsEpoch = floatval($showStartDT->format("U.u"));
  171. $showEndDT = new DateTime($p_item["si_ends"], new DateTimeZone("UTC"));
  172. $showEndDT->setTimezone(new DateTimeZone($this->timezone));
  173. $endsEpoch = floatval($showEndDT->format("U.u"));
  174. //is a rebroadcast show
  175. if (intval($p_item["si_rebroadcast"]) === 1) {
  176. $row["rebroadcast"] = true;
  177. $parentInstance = CcShowInstancesQuery::create()->findPk($p_item["parent_show"]);
  178. $name = $parentInstance->getCcShow()->getDbName();
  179. $dt = $parentInstance->getDbStarts(null);
  180. $dt->setTimezone(new DateTimeZone($this->timezone));
  181. $time = $dt->format("Y-m-d H:i");
  182. $row["rebroadcast_title"] = sprintf(_("Rebroadcast of %s from %s"), $name, $time);
  183. } elseif (intval($p_item["si_record"]) === 1) {
  184. $row["record"] = true;
  185. // at the time of creating on show, the recorded file is not in the DB yet.
  186. // therefore, 'si_file_id' is null. So we need to check it.
  187. if (Application_Model_Preference::GetUploadToSoundcloudOption() && isset($p_item['si_file_id'])) {
  188. $file = Application_Model_StoredFile::RecallById($p_item['si_file_id']);
  189. if (isset($file)) {
  190. $sid = $file->getSoundCloudId();
  191. $row['soundcloud_id'] = $sid;
  192. }
  193. }
  194. }
  195. if ($startsEpoch < $this->epoch_now && $endsEpoch > $this->epoch_now) {
  196. $row["currentShow"] = true;
  197. $this->currentShow = true;
  198. } else {
  199. $this->currentShow = false;
  200. }
  201. $this->isAllowed($p_item, $row);
  202. $row["header"] = true;
  203. $row["starts"] = $showStartDT->format("Y-m-d H:i");
  204. $row["startDate"] = $showStartDT->format("Y-m-d");
  205. $row["startTime"] = $showStartDT->format("H:i");
  206. $row["refresh"] = floatval($showStartDT->format("U.u")) - $this->epoch_now;
  207. $row["ends"] = $showEndDT->format("Y-m-d H:i");
  208. $row["endDate"] = $showEndDT->format("Y-m-d");
  209. $row["endTime"] = $showEndDT->format("H:i");
  210. $row["duration"] = floatval($showEndDT->format("U.u")) - floatval($showStartDT->format("U.u"));
  211. $row["title"] = htmlspecialchars($p_item["show_name"]);
  212. $row["instance"] = intval($p_item["si_id"]);
  213. $row["image"] = '';
  214. $this->getScheduledStatus($startsEpoch, $endsEpoch, $row);
  215. $this->contentDT = $showStartDT;
  216. return $row;
  217. }
  218. private function makeScheduledItemRow($p_item)
  219. {
  220. $row = $this->defaultRowArray;
  221. if (isset($p_item["sched_starts"])) {
  222. $schedStartDT = new DateTime($p_item["sched_starts"],
  223. new DateTimeZone("UTC"));
  224. $schedStartDT->setTimezone(new DateTimeZone($this->timezone));
  225. $schedEndDT = new DateTime($p_item["sched_ends"],
  226. new DateTimeZone("UTC"));
  227. $schedEndDT->setTimezone(new DateTimeZone($this->timezone));
  228. $showEndDT = new DateTime($p_item["si_ends"], new DateTimeZone("UTC"));
  229. $this->getItemStatus($p_item, $row);
  230. $startsEpoch = floatval($schedStartDT->format("U.u"));
  231. $endsEpoch = floatval($schedEndDT->format("U.u"));
  232. $showEndEpoch = floatval($showEndDT->format("U.u"));
  233. //don't want an overbooked item to stay marked as current.
  234. $this->getScheduledStatus($startsEpoch, min($endsEpoch, $showEndEpoch), $row);
  235. $row["id"] = intval($p_item["sched_id"]);
  236. $row["image"] = $p_item["file_exists"];
  237. $row["instance"] = intval($p_item["si_id"]);
  238. $row["starts"] = $schedStartDT->format("H:i:s");
  239. $row["ends"] = $schedEndDT->format("H:i:s");
  240. $cue_out = Application_Common_DateHelper::playlistTimeToSeconds($p_item['cue_out']);
  241. $cue_in = Application_Common_DateHelper::playlistTimeToSeconds($p_item['cue_in']);
  242. $run_time = $cue_out-$cue_in;
  243. $formatter = new LengthFormatter(Application_Common_DateHelper::secondsToPlaylistTime($run_time));
  244. $row['runtime'] = $formatter->format();
  245. $row["title"] = htmlspecialchars($p_item["file_track_title"]);
  246. $row["creator"] = htmlspecialchars($p_item["file_artist_name"]);
  247. $row["album"] = htmlspecialchars($p_item["file_album_title"]);
  248. $row["cuein"] = $p_item["cue_in"];
  249. $row["cueout"] = $p_item["cue_out"];
  250. $row["fadein"] = round(substr($p_item["fade_in"], 6), 6);
  251. $row["fadeout"] = round(substr($p_item["fade_out"], 6), 6);
  252. $row["mime"] = $p_item["file_mime"];
  253. $row["pos"] = $this->pos++;
  254. $this->contentDT = $schedEndDT;
  255. }
  256. //show is empty or is a special kind of show (recording etc)
  257. else if (intval($p_item["si_record"]) === 1) {
  258. $row["record"] = true;
  259. $row["instance"] = intval($p_item["si_id"]);
  260. $showStartDT = new DateTime($p_item["si_starts"], new DateTimeZone("UTC"));
  261. $showEndDT = new DateTime($p_item["si_ends"], new DateTimeZone("UTC"));
  262. $startsEpoch = floatval($showStartDT->format("U.u"));
  263. $endsEpoch = floatval($showEndDT->format("U.u"));
  264. $this->getScheduledStatus($startsEpoch, $endsEpoch, $row);
  265. } else {
  266. $row["empty"] = true;
  267. $row["id"] = 0 ;
  268. $row["instance"] = intval($p_item["si_id"]);
  269. }
  270. if (intval($p_item["si_rebroadcast"]) === 1) {
  271. $row["rebroadcast"] = true;
  272. }
  273. if ($this->currentShow === true) {
  274. $row["currentShow"] = true;
  275. }
  276. $this->getItemColor($p_item, $row);
  277. $this->getRowTimestamp($p_item, $row);
  278. $this->isAllowed($p_item, $row);
  279. return $row;
  280. }
  281. private function makeFooterRow($p_item)
  282. {
  283. $row = $this->defaultRowArray;
  284. $row["footer"] = true;
  285. $row["instance"] = intval($p_item["si_id"]);
  286. $this->getRowTimestamp($p_item, $row);
  287. $showEndDT = new DateTime($p_item["si_ends"], new DateTimeZone("UTC"));
  288. $contentDT = $this->contentDT;
  289. $runtime = bcsub($contentDT->format("U.u"), $showEndDT->format("U.u"), 6);
  290. $row["runtime"] = $runtime;
  291. $timeFilled = new TimeFilledFormatter($runtime);
  292. $row["fRuntime"] = $timeFilled->format();
  293. $showStartDT = new DateTime($p_item["si_starts"], new DateTimeZone("UTC"));
  294. $showStartDT->setTimezone(new DateTimeZone($this->timezone));
  295. $startsEpoch = floatval($showStartDT->format("U.u"));
  296. $showEndDT = new DateTime($p_item["si_ends"], new DateTimeZone("UTC"));
  297. $showEndDT->setTimezone(new DateTimeZone($this->timezone));
  298. $endsEpoch = floatval($showEndDT->format("U.u"));
  299. $row["refresh"] = floatval($showEndDT->format("U.u")) - $this->epoch_now;
  300. if ($this->currentShow === true) {
  301. $row["currentShow"] = true;
  302. }
  303. $this->getScheduledStatus($startsEpoch, $endsEpoch, $row);
  304. $this->isAllowed($p_item, $row);
  305. if (intval($p_item["si_record"]) === 1) {
  306. $row["record"] = true;
  307. }
  308. return $row;
  309. }
  310. /*
  311. * @param int $timestamp Unix timestamp in seconds.
  312. *
  313. * @return boolean whether the schedule in the show builder's range has
  314. * been updated.
  315. *
  316. */
  317. public function hasBeenUpdatedSince($timestamp, $instances)
  318. {
  319. $outdated = false;
  320. $shows = Application_Model_Show::getShows($this->startDT, $this->endDT);
  321. $include = array();
  322. if ($this->opts["showFilter"] !== 0) {
  323. $include[] = $this->opts["showFilter"];
  324. } elseif ($this->opts["myShows"] === 1) {
  325. $include = $this->getUsersShows();
  326. }
  327. $currentInstances = array();
  328. foreach ($shows as $show) {
  329. if (empty($include) || in_array($show["show_id"], $include)) {
  330. $currentInstances[] = $show["instance_id"];
  331. if (isset($show["last_scheduled"])) {
  332. $dt = new DateTime($show["last_scheduled"],
  333. new DateTimeZone("UTC"));
  334. } else {
  335. $dt = new DateTime($show["created"],
  336. new DateTimeZone("UTC"));
  337. }
  338. //check if any of the shows have a more recent timestamp.
  339. $showTimeStamp = intval($dt->format("U"));
  340. if ($timestamp < $showTimeStamp) {
  341. $outdated = true;
  342. break;
  343. }
  344. }
  345. }
  346. //see if the displayed show instances have changed. (deleted,
  347. //empty schedule etc)
  348. if ($outdated === false && count($instances)
  349. !== count($currentInstances)) {
  350. Logging::debug("show instances have changed.");
  351. $outdated = true;
  352. }
  353. return $outdated;
  354. }
  355. public function getItems()
  356. {
  357. $current_id = -1;
  358. $display_items = array();
  359. $shows = array();
  360. $showInstance = array();
  361. if ($this->opts["myShows"] === 1) {
  362. $shows = $this->getUsersShows();
  363. } elseif ($this->opts["showFilter"] !== 0) {
  364. $shows[] = $this->opts["showFilter"];
  365. } elseif ($this->opts["showInstanceFilter"] !== 0) {
  366. $showInstance[] = $this->opts["showInstanceFilter"];
  367. }
  368. $scheduled_items = Application_Model_Schedule::GetScheduleDetailItems(
  369. $this->startDT, $this->endDT, $shows, $showInstance);
  370. for ($i = 0, $rows = count($scheduled_items); $i < $rows; $i++) {
  371. $item = $scheduled_items[$i];
  372. //don't send back data for filler rows.
  373. if (isset($item["playout_status"]) &&
  374. $item["playout_status"] < 0) {
  375. continue;
  376. }
  377. //make a header row.
  378. if ($current_id !== $item["si_id"]) {
  379. //make a footer row.
  380. if ($current_id !== -1) {
  381. // pass in the previous row as it's the last row for
  382. // the previous show.
  383. $display_items[] = $this->makeFooterRow(
  384. $scheduled_items[$i-1]);
  385. }
  386. $display_items[] = $this->makeHeaderRow($item);
  387. $current_id = $item["si_id"];
  388. $this->pos = 1;
  389. }
  390. //make a normal data row.
  391. $row = $this->makeScheduledItemRow($item);
  392. //don't display the empty rows.
  393. if (isset($row)) {
  394. $display_items[] = $row;
  395. }
  396. if ($current_id !== -1 &&
  397. !in_array($current_id, $this->showInstances)) {
  398. $this->showInstances[] = $current_id;
  399. }
  400. }
  401. //make the last footer if there were any scheduled items.
  402. if (count($scheduled_items) > 0) {
  403. $display_items[] = $this->makeFooterRow($scheduled_items[
  404. count($scheduled_items)-1]);
  405. }
  406. return array(
  407. "schedule" => $display_items,
  408. "showInstances" => $this->showInstances);
  409. }
  410. }