/**
  * Exclude object from result
  *
  * @param     CcShowDays $ccShowDays Object to remove from the list of results
  *
  * @return    CcShowDaysQuery The current query, for fluid interface
  */
 public function prune($ccShowDays = null)
 {
     if ($ccShowDays) {
         $this->addUsingAlias(CcShowDaysPeer::ID, $ccShowDays->getDbId(), Criteria::NOT_EQUAL);
     }
     return $this;
 }
Example #2
0
 private function checkToDeleteShow($showId)
 {
     //UTC DateTime object
     $showsPopUntil = Application_Model_Preference::GetShowsPopulatedUntil();
     $showDays = CcShowDaysQuery::create()->filterByDbShowId($showId)->findOne();
     $showEnd = $showDays->getDbLastShow();
     //there will always be more shows populated.
     if (is_null($showEnd)) {
         return false;
     }
     $lastShowStartDateTime = new DateTime("{$showEnd} {$showDays->getDbStartTime()}", new DateTimeZone($showDays->getDbTimezone()));
     //end dates were non inclusive.
     $lastShowStartDateTime = self::addDeltas($lastShowStartDateTime, -1, 0);
     //there's still some shows left to be populated.
     if ($lastShowStartDateTime->getTimestamp() > $showsPopUntil->getTimestamp()) {
         return false;
     }
     // check if there are any non deleted show instances remaining.
     $showInstances = CcShowInstancesQuery::create()->filterByDbShowId($showId)->filterByDbModifiedInstance(false)->filterByDbRebroadcast(0)->find();
     if (is_null($showInstances)) {
         return true;
     } else {
         if (count($showInstances) === 1) {
             $showInstance = $showInstances[0];
             $showDaysOld = CcShowDaysQuery::create()->filterByDbShowId($showId)->find();
             $tz = $showDaysOld[0]->getDbTimezone();
             $startDate = new DateTime($showInstance->getDbStarts(), new DateTimeZone("UTC"));
             $startDate->setTimeZone(new DateTimeZone($tz));
             $endDate = self::addDeltas($startDate, 1, 0);
             //make a new rule for a non repeating show.
             $showDayNew = new CcShowDays();
             $showDayNew->setDbFirstShow($startDate->format("Y-m-d"));
             $showDayNew->setDbLastShow($endDate->format("Y-m-d"));
             $showDayNew->setDbStartTime($startDate->format("H:i:s"));
             $showDayNew->setDbTimezone($tz);
             $showDayNew->setDbDay($startDate->format('w'));
             $showDayNew->setDbDuration($showDaysOld[0]->getDbDuration());
             $showDayNew->setDbRepeatType(-1);
             $showDayNew->setDbShowId($showDaysOld[0]->getDbShowId());
             $showDayNew->setDbRecord($showDaysOld[0]->getDbRecord());
             $showDayNew->save();
             //delete the old rules for repeating shows
             $showDaysOld->delete();
             //remove the old repeating deleted instances.
             $showInstances = CcShowInstancesQuery::create()->filterByDbShowId($showId)->filterByDbModifiedInstance(true)->delete();
         }
     }
     return false;
 }
 /**
  * Adds an object to the instance pool.
  *
  * Propel keeps cached copies of objects in an instance pool when they are retrieved
  * from the database.  In some cases -- especially when you override doSelect*()
  * methods in your stub classes -- you may need to explicitly add objects
  * to the cache in order to ensure that the same objects are always returned by doSelect*()
  * and retrieveByPK*() calls.
  *
  * @param      CcShowDays $value A CcShowDays object.
  * @param      string $key (optional) key to use for instance map (for performance boost if key was already calculated externally).
  */
 public static function addInstanceToPool(CcShowDays $obj, $key = null)
 {
     if (Propel::isInstancePoolingEnabled()) {
         if ($key === null) {
             $key = (string) $obj->getDbId();
         }
         // if key === null
         self::$instances[$key] = $obj;
     }
 }
Example #4
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;
 }
 /**
  * Filter the query by a related CcShowDays object
  *
  * @param     CcShowDays $ccShowDays  the related object to use as filter
  * @param     string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL
  *
  * @return    CcShowQuery The current query, for fluid interface
  */
 public function filterByCcShowDays($ccShowDays, $comparison = null)
 {
     return $this->addUsingAlias(CcShowPeer::ID, $ccShowDays->getDbShowId(), $comparison);
 }
Example #6
0
    /**
     * Create a show.
     *
     * Note: end dates are non inclusive.
     *
     * @param  array $data
     * @return int
     *     Show ID
     */
    public static function create($data)
    {
        $startDateTime = new DateTime($data['add_show_start_date'] . " " . $data['add_show_start_time']);
        $utcStartDateTime = clone $startDateTime;
        $utcStartDateTime->setTimezone(new DateTimeZone('UTC'));
        if ($data['add_show_no_end']) {
            $endDate = NULL;
        } elseif ($data['add_show_repeats']) {
            $endDateTime = new DateTime($data['add_show_end_date']);
            //$endDateTime->setTimezone(new DateTimeZone('UTC'));
            $endDateTime->add(new DateInterval("P1D"));
            $endDate = $endDateTime->format("Y-m-d");
        } else {
            $endDateTime = new DateTime($data['add_show_start_date']);
            //$endDateTime->setTimezone(new DateTimeZone('UTC'));
            $endDateTime->add(new DateInterval("P1D"));
            $endDate = $endDateTime->format("Y-m-d");
        }
        //What we are doing here is checking if the show repeats or if
        //any repeating days have been checked. If not, then by default
        //the "selected" DOW is the initial day.
        //DOW in local time.
        $startDow = date("w", $startDateTime->getTimestamp());
        if (!$data['add_show_repeats']) {
            $data['add_show_day_check'] = array($startDow);
        } elseif ($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->setDbLiveStreamUsingAirtimeAuth($data['cb_airtime_auth'] == 1);
        $ccShow->setDbLiveStreamUsingCustomAuth($data['cb_custom_auth'] == 1);
        $ccShow->setDbLiveStreamUser($data['custom_username']);
        $ccShow->setDbLiveStreamPass($data['custom_password']);
        $ccShow->save();
        $showId = $ccShow->getDbId();
        $isRecorded = isset($data['add_show_record']) && $data['add_show_record'] ? 1 : 0;
        if ($data['add_show_id'] != -1) {
            $show = new Application_Model_Show($showId);
            $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($startDateTime->format("Y-m-d"));
            $showDay->setDbLastShow($endDate);
            $showDay->setDbStartTime($startDateTime->format("H:i:s"));
            $showDay->setDbTimezone(date_default_timezone_get());
            $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) {
                $daysAdd = 0;
                $startDateTimeClone = clone $startDateTime;
                if ($startDow !== $day) {
                    if ($startDow > $day) {
                        $daysAdd = 6 - $startDow + 1 + $day;
                    } else {
                        $daysAdd = $day - $startDow;
                    }
                    $startDateTimeClone->add(new DateInterval("P" . $daysAdd . "D"));
                }
                if (is_null($endDate) || $startDateTimeClone->getTimestamp() <= $endDateTime->getTimestamp()) {
                    $showDay = new CcShowDays();
                    $showDay->setDbFirstShow($startDateTimeClone->format("Y-m-d"));
                    $showDay->setDbLastShow($endDate);
                    $showDay->setDbStartTime($startDateTimeClone->format("H:i"));
                    $showDay->setDbTimezone(date_default_timezone_get());
                    $showDay->setDbDuration($data['add_show_duration']);
                    $showDay->setDbDay($day);
                    $showDay->setDbRepeatType($repeatType);
                    $showDay->setDbShowId($showId);
                    $showDay->setDbRecord($isRecorded);
                    $showDay->save();
                }
            }
        }
        //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 && isset($data['add_show_rebroadcast']) && $data['add_show_rebroadcast']) {
            CcShowRebroadcastQuery::create()->filterByDbShowId($data['add_show_id'])->delete();
        }
        //adding rows to cc_show_rebroadcast
        /* TODO: Document magic constant 10 and define it properly somewhere
           --RG */
        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();
                }
            }
        } elseif ($isRecorded && $data['add_show_rebroadcast'] && $repeatType == -1) {
            for ($i = 1; $i <= 10; $i++) {
                if ($data['add_show_rebroadcast_date_absolute_' . $i]) {
                    //$con = Propel::getConnection(CcShowPeer::DATABASE_NAME);
                    //$sql = "SELECT date '{$data['add_show_rebroadcast_date_absolute_'.$i]}' - date '{$data['add_show_start_date']}' ";
                    $sql = <<<SQL
SELECT :rebroadcast::date - :start::date
SQL;
                    $offset_days = Application_Common_Database::prepareAndExecute($sql, array('rebroadcast' => $data["add_show_rebroadcast_date_absolute_{$i}"], 'start' => $data['add_show_start_date']), "column");
                    //$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();
            }
        }
        if ($data['add_show_id'] != -1) {
            $con = Propel::getConnection(CcSchedulePeer::DATABASE_NAME);
            $con->beginTransaction();
            //current timesamp in UTC.
            $current_timestamp = gmdate("Y-m-d H:i:s");
            try {
                //update the status flag in cc_schedule.
                $instances = CcShowInstancesQuery::create()->filterByDbEnds($current_timestamp, Criteria::GREATER_THAN)->filterByDbShowId($data['add_show_id'])->find($con);
                foreach ($instances as $instance) {
                    $instance->updateScheduleStatus($con);
                }
                $con->commit();
            } catch (Exception $e) {
                $con->rollback();
                Logging::info("Couldn't update schedule status.");
                Logging::info($e->getMessage());
            }
        }
        Application_Model_Show::populateShowUntil($showId);
        Application_Model_RabbitMq::PushSchedule();
        return $showId;
    }
 /**
  *
  * Sets the fields for a cc_show_days table row
  * @param $showData
  * @param $showId
  * @param $userId
  * @param $repeatType
  * @param $isRecorded
  * @param $showDay ccShowDay object we are setting values on
  */
 private function setCcShowDays($showData)
 {
     $showId = $this->ccShow->getDbId();
     $startDateTime = new DateTime($showData['add_show_start_date'] . " " . $showData['add_show_start_time'], new DateTimeZone($showData['add_show_timezone']));
     $endDateTime = $this->calculateEndDate($showData);
     if (!is_null($endDateTime)) {
         $endDate = $endDateTime->format("Y-m-d");
     } else {
         $endDate = null;
     }
     //Our calculated start DOW must be used for non repeating since a day has not been selected.
     //For all repeating shows, only the selected days of the week will be repeated on.
     $startDow = $startDateTime->format("w");
     if (!$showData['add_show_repeats']) {
         $showData['add_show_day_check'] = array($startDow);
     }
     // Don't set day for monthly repeat type, it's invalid
     if ($showData['add_show_repeats'] && $showData['add_show_repeat_type'] == 2) {
         if ($this->isUpdate) {
             $showDay = CcShowDaysQuery::create()->filterByDbShowId($showId)->filterByDbRepeatType($showData['add_show_repeat_type'])->findOne();
         } else {
             $showDay = new CcShowDays();
         }
         $showDay->setDbFirstShow($startDateTime->format("Y-m-d"));
         $showDay->setDbLastShow($endDate);
         $showDay->setDbStartTime($startDateTime->format("H:i:s"));
         $showDay->setDbTimezone($showData['add_show_timezone']);
         $showDay->setDbDuration($showData['add_show_duration']);
         $showDay->setDbRepeatType($this->repeatType);
         $showDay->setDbShowId($showId);
         $showDay->setDbRecord($this->isRecorded);
         //in case we are editing a show we need to set this to the first show
         //so when editing, the date period iterator will start from the beginning
         $showDay->setDbNextPopDate($startDateTime->format("Y-m-d"));
         $showDay->save();
     } else {
         foreach ($showData['add_show_day_check'] as $day) {
             $daysAdd = 0;
             $startDateTimeClone = clone $startDateTime;
             if ($startDow !== $day) {
                 if ($startDow > $day) {
                     $daysAdd = 6 - $startDow + 1 + $day;
                 } else {
                     $daysAdd = $day - $startDow;
                 }
                 $startDateTimeClone->add(new DateInterval("P" . $daysAdd . "D"));
             }
             if (is_null($endDate) || $startDateTimeClone->getTimestamp() <= $endDateTime->getTimestamp()) {
                 if ($this->isUpdate) {
                     $showDay = CcShowDaysQuery::create()->filterByDbShowId($showId)->filterByDbRepeatType($this->repeatType)->filterByDbDay($day)->findOne();
                     if (!$showDay) {
                         //if no show day object was found it is because a new
                         //repeating day of the week was added
                         $showDay = new CcShowDays();
                     }
                 } else {
                     $showDay = new CcShowDays();
                 }
                 $showDay->setDbFirstShow($startDateTimeClone->format("Y-m-d"));
                 $showDay->setDbLastShow($endDate);
                 $showDay->setDbStartTime($startDateTimeClone->format("H:i"));
                 $showDay->setDbTimezone($showData['add_show_timezone']);
                 $showDay->setDbDuration($showData['add_show_duration']);
                 $showDay->setDbDay($day);
                 $showDay->setDbRepeatType($this->repeatType);
                 $showDay->setDbShowId($showId);
                 $showDay->setDbRecord($this->isRecorded);
                 //in case we are editing a show we need to set this to the first show
                 //so when editing, the date period iterator will start from the beginning
                 $showDay->setDbNextPopDate($startDateTimeClone->format("Y-m-d"));
                 $showDay->save();
             }
         }
     }
 }