예제 #1
0
 /**
  * Create a show.
  *
  * Note: end dates are non inclusive.
  *
  * @param array $data
  * @return int
  *     Show ID
  */
 public static function create($data)
 {
     $con = Propel::getConnection(CcShowPeer::DATABASE_NAME);
     $sql = "SELECT EXTRACT(DOW FROM TIMESTAMP '{$data['add_show_start_date']} {$data['add_show_start_time']}')";
     $r = $con->query($sql);
     $startDow = $r->fetchColumn(0);
     if ($data['add_show_no_end']) {
         $endDate = NULL;
     } else {
         if ($data['add_show_repeats']) {
             $sql = "SELECT date '{$data['add_show_end_date']}' + INTERVAL '1 day' ";
             $r = $con->query($sql);
             $endDate = $r->fetchColumn(0);
         } else {
             $sql = "SELECT date '{$data['add_show_start_date']}' + INTERVAL '1 day' ";
             $r = $con->query($sql);
             $endDate = $r->fetchColumn(0);
         }
     }
     //only want the day of the week from the start date.
     if (!$data['add_show_repeats']) {
         $data['add_show_day_check'] = array($startDow);
     } else {
         if ($data['add_show_repeats'] && $data['add_show_day_check'] == "") {
             $data['add_show_day_check'] = array($startDow);
         }
     }
     //find repeat type or set to a non repeating show.
     $repeatType = $data['add_show_repeats'] ? $data['add_show_repeat_type'] : -1;
     if ($data['add_show_id'] == -1) {
         $ccShow = new CcShow();
     } else {
         $ccShow = CcShowQuery::create()->findPK($data['add_show_id']);
     }
     $ccShow->setDbName($data['add_show_name']);
     $ccShow->setDbDescription($data['add_show_description']);
     $ccShow->setDbUrl($data['add_show_url']);
     $ccShow->setDbGenre($data['add_show_genre']);
     $ccShow->setDbColor($data['add_show_color']);
     $ccShow->setDbBackgroundColor($data['add_show_background_color']);
     $ccShow->save();
     $showId = $ccShow->getDbId();
     $show = new Show($showId);
     $isRecorded = $data['add_show_record'] ? 1 : 0;
     if ($data['add_show_id'] != -1) {
         $show->deletePossiblyInvalidInstances($data, $endDate, $isRecorded, $repeatType);
     }
     //check if we are adding or updating a show, and if updating
     //erase all the show's show_days information first.
     if ($data['add_show_id'] != -1) {
         CcShowDaysQuery::create()->filterByDbShowId($data['add_show_id'])->delete();
     }
     //don't set day for monthly repeat type, it's invalid.
     if ($data['add_show_repeats'] && $data['add_show_repeat_type'] == 2) {
         $showDay = new CcShowDays();
         $showDay->setDbFirstShow($data['add_show_start_date']);
         $showDay->setDbLastShow($endDate);
         $showDay->setDbStartTime($data['add_show_start_time']);
         $showDay->setDbDuration($data['add_show_duration']);
         $showDay->setDbRepeatType($repeatType);
         $showDay->setDbShowId($showId);
         $showDay->setDbRecord($isRecorded);
         $showDay->save();
     } else {
         foreach ($data['add_show_day_check'] as $day) {
             if ($startDow !== $day) {
                 if ($startDow > $day) {
                     $daysAdd = 6 - $startDow + 1 + $day;
                 } else {
                     $daysAdd = $day - $startDow;
                 }
                 $sql = "SELECT date '{$data['add_show_start_date']}' + INTERVAL '{$daysAdd} day' ";
                 $r = $con->query($sql);
                 $start = $r->fetchColumn(0);
             } else {
                 $start = $data['add_show_start_date'];
             }
             if (strtotime($start) <= strtotime($endDate) || is_null($endDate)) {
                 $showDay = new CcShowDays();
                 $showDay->setDbFirstShow($start);
                 $showDay->setDbLastShow($endDate);
                 $showDay->setDbStartTime($data['add_show_start_time']);
                 $showDay->setDbDuration($data['add_show_duration']);
                 $showDay->setDbDay($day);
                 $showDay->setDbRepeatType($repeatType);
                 $showDay->setDbShowId($showId);
                 $showDay->setDbRecord($isRecorded);
                 $showDay->save();
             }
         }
     }
     $date = new DateHelper();
     $currentTimestamp = $date->getTimestamp();
     //check if we are adding or updating a show, and if updating
     //erase all the show's future show_rebroadcast information first.
     if ($data['add_show_id'] != -1 && $data['add_show_rebroadcast']) {
         CcShowRebroadcastQuery::create()->filterByDbShowId($data['add_show_id'])->delete();
     }
     //adding rows to cc_show_rebroadcast
     if ($isRecorded && $data['add_show_rebroadcast'] && $repeatType != -1) {
         for ($i = 1; $i <= 10; $i++) {
             if ($data['add_show_rebroadcast_date_' . $i]) {
                 $showRebroad = new CcShowRebroadcast();
                 $showRebroad->setDbDayOffset($data['add_show_rebroadcast_date_' . $i]);
                 $showRebroad->setDbStartTime($data['add_show_rebroadcast_time_' . $i]);
                 $showRebroad->setDbShowId($showId);
                 $showRebroad->save();
             }
         }
     } else {
         if ($isRecorded && $data['add_show_rebroadcast'] && $repeatType == -1) {
             for ($i = 1; $i <= 10; $i++) {
                 if ($data['add_show_rebroadcast_date_absolute_' . $i]) {
                     $sql = "SELECT date '{$data['add_show_rebroadcast_date_absolute_' . $i]}' - date '{$data['add_show_start_date']}' ";
                     $r = $con->query($sql);
                     $offset_days = $r->fetchColumn(0);
                     $showRebroad = new CcShowRebroadcast();
                     $showRebroad->setDbDayOffset($offset_days . " days");
                     $showRebroad->setDbStartTime($data['add_show_rebroadcast_time_absolute_' . $i]);
                     $showRebroad->setDbShowId($showId);
                     $showRebroad->save();
                 }
             }
         }
     }
     //check if we are adding or updating a show, and if updating
     //erase all the show's show_rebroadcast information first.
     if ($data['add_show_id'] != -1) {
         CcShowHostsQuery::create()->filterByDbShow($data['add_show_id'])->delete();
     }
     if (is_array($data['add_show_hosts'])) {
         //add selected hosts to cc_show_hosts table.
         foreach ($data['add_show_hosts'] as $host) {
             $showHost = new CcShowHosts();
             $showHost->setDbShow($showId);
             $showHost->setDbHost($host);
             $showHost->save();
         }
     }
     Show::populateShowUntil($showId);
     RabbitMq::PushSchedule();
     return $showId;
 }