<commit_before><?php
define("MAX_REBROADCAST_DATES", 10);
define("NO_REPEAT", -1);
define("REPEAT_WEEKLY", 0);
define("REPEAT_BI_WEEKLY", 1);
define("REPEAT_MONTHLY_MONTHLY", 2);
define("REPEAT_MONTHLY_WEEKLY", 3);

class Application_Service_ShowService
{
    private $ccShow;
    private $isRecorded;
    private $isRebroadcast;
    private $repeatType;
    private $isUpdate;
    private $linkedShowContent;

    public function __construct($showId=null, $showData=null, $isUpdate=false)
    {
        if (!is_null($showId)) {
            $this->ccShow = CcShowQuery::create()->findPk($showId);
        }

        if (isset($showData["add_show_repeats"]) && $showData["add_show_repeats"]) {
            $this->repeatType = $showData["add_show_repeat_type"];
            if ($showData["add_show_repeat_type"] == 2) {
                $this->repeatType = $showData["add_show_monthly_repeat_type"];
            }
        } else {
            $this->repeatType = -1;
        }
        $this->isRecorded = (isset($showData['add_show_record']) && $showData['add_show_record']) ? 1 : 0;
        $this->isRebroadcast = (isset($showData['add_show_rebroadcast']) && $showData['add_show_rebroadcast']) ? 1 : 0;
        $this->isUpdate = $isUpdate;
    }

    public function createShowFromRepeatingInstance($showData) {
        $service_user = new Application_Service_UserService();
        $currentUser = $service_user->getCurrentUser();

        $showData["add_show_duration"] = $this->formatShowDuration(
            $showData["add_show_duration"]);

        $con = Propel::getConnection();
        $con->beginTransaction();
        try {
            if (!$currentUser->isAdminOrPM()) {
                throw new Exception("Permission denied");
            }

            /****** UPDATE SCHEDULE START TIME ******/
            //get the ccShow object to which this instance belongs
            //so we can get the original start date and time
            $oldCcShow = CcShowQuery::create()
                ->findPk($showData["add_show_id"]);

            //DateTime in user's local time
            $newStartDateTime = new DateTime($showData["add_show_start_date"]." ".
            $showData["add_show_start_time"],
                new DateTimeZone(Application_Model_Preference::GetTimezone()));

            $ccShowInstanceOrig = CcShowInstancesQuery::create()
                ->findPk($showData["add_show_instance_id"]);
            $diff = $this->calculateShowStartDiff($newStartDateTime,
                $ccShowInstanceOrig->getLocalStartDateTime());

            if ($diff > 0) {
                Application_Service_SchedulerService::updateScheduleStartTime(
                    array($showData["add_show_instance_id"]), $diff);
            }
            /****** UPDATE SCHEDULE START TIME ENDS******/

            $this->setCcShow($showData);
            $this->setCcShowDays($showData);
            $this->setCcShowHosts($showData);
            $this->delegateInstanceCreation();

            //get the new instance id
            $ccShowInstance = CcShowInstancesQuery::create()
                ->filterByDbShowId($this->ccShow->getDbId())
                ->findOne();

            $newInstanceId = $ccShowInstance->getDbId();

            //update cc_schedule with the new instance id
            $ccSchedules = CcScheduleQuery::create()
                ->filterByDbInstanceId($showData["add_show_instance_id"])
                ->find();

            foreach ($ccSchedules as $ccSchedule) {
                $ccSchedule->setDbInstanceId($newInstanceId);
                $ccSchedule->save();
            }

            $con = Propel::getConnection(CcShowInstancesPeer::DATABASE_NAME);
            $ccShowInstance->updateDbTimeFilled($con);

            //delete the edited instance from the repeating sequence
            $ccShowInstanceOrig->setDbModifiedInstance(true)->save();

            $service_showForm = new Application_Service_ShowFormService($showData["add_show_id"]);
            list($start, $end) = $service_showForm->getNextFutureRepeatShowTime();
            $oldCcShowDay = $oldCcShow->getFirstCcShowDay();
            $oldCcShowDay
                ->setDbFirstShow($start->setTimezone(new DateTimeZone("UTC"))->format("Y-m-d"))
                ->save();

            $con->commit();
            Application_Model_RabbitMq::PushSchedule();
        } catch (Exception $e) {
            $con->rollback();
            Logging::info("EXCEPTION: Show update failed.");
            Logging::info($e->getMessage());
        }
    }

    public function addUpdateShow($showData)
    {
        $service_user = new Application_Service_UserService();
        $currentUser = $service_user->getCurrentUser();

        $showData["add_show_duration"] = $this->formatShowDuration(
            $showData["add_show_duration"]);

        $con = Propel::getConnection();
        $con->beginTransaction();
        try {
            if (!$currentUser->isAdminOrPM()) {
                throw new Exception("Permission denied");
            }
            //update ccShow
            $this->setCcShow($showData);

            $daysAdded = array();
            if ($this->isUpdate) {
                $daysAdded = $this->delegateInstanceCleanup($showData);
                // updates cc_show_instances start/end times, and updates
                // schedule start/end times
                $this->applyShowStartEndDifference($showData);
                $this->deleteRebroadcastInstances();
                $this->deleteCcShowDays();
                $this->deleteCcShowHosts();
                if ($this->isRebroadcast) {
                    //delete entry in cc_show_rebroadcast
                    $this->deleteCcShowRebroadcasts();
                }
            }

            //update ccShowDays
            $this->setCcShowDays($showData);

            //update ccShowRebroadcasts
            $this->setCcShowRebroadcasts($showData);

            //update ccShowHosts
            $this->setCcShowHosts($showData);

            //create new ccShowInstances
            $this->delegateInstanceCreation($daysAdded);

            if ($this->isUpdate) {
                $service_scheduler = new Application_Service_SchedulerService();
                $service_scheduler->removeGaps($this->ccShow->getDbId());
            }

            $con->commit();
            Application_Model_RabbitMq::PushSchedule();
        } catch (Exception $e) {
            $con->rollback();
            $this->isUpdate ? $action = "update" : $action = "creation";
            Logging::info("EXCEPTION: Show ".$action." failed.");
            Logging::info($e->getMessage());
        }
    }

    /**
     * 
     * Receives a cc_show id and determines whether to create a 
     * single show instance or repeating show instances
     */
    public function delegateInstanceCreation($daysAdded=null, $end=null, $fillInstances=false)
    {
        $populateUntil = $this->getPopulateShowUntilDateTIme();

        if (is_null($this->ccShow)) {<commit_msg>Removed unnecessary function call when editing a show<commit_after><?php
define("MAX_REBROADCAST_DATES", 10);
define("NO_REPEAT", -1);
define("REPEAT_WEEKLY", 0);
define("REPEAT_BI_WEEKLY", 1);
define("REPEAT_MONTHLY_MONTHLY", 2);
define("REPEAT_MONTHLY_WEEKLY", 3);

class Application_Service_ShowService
{
    private $ccShow;
    private $isRecorded;
    private $isRebroadcast;
    private $repeatType;
    private $isUpdate;
    private $linkedShowContent;

    public function __construct($showId=null, $showData=null, $isUpdate=false)
    {
        if (!is_null($showId)) {
            $this->ccShow = CcShowQuery::create()->findPk($showId);
        }

        if (isset($showData["add_show_repeats"]) && $showData["add_show_repeats"]) {
            $this->repeatType = $showData["add_show_repeat_type"];
            if ($showData["add_show_repeat_type"] == 2) {
                $this->repeatType = $showData["add_show_monthly_repeat_type"];
            }
        } else {
            $this->repeatType = -1;
        }
        $this->isRecorded = (isset($showData['add_show_record']) && $showData['add_show_record']) ? 1 : 0;
        $this->isRebroadcast = (isset($showData['add_show_rebroadcast']) && $showData['add_show_rebroadcast']) ? 1 : 0;
        $this->isUpdate = $isUpdate;
    }

    public function createShowFromRepeatingInstance($showData) {
        $service_user = new Application_Service_UserService();
        $currentUser = $service_user->getCurrentUser();

        $showData["add_show_duration"] = $this->formatShowDuration(
            $showData["add_show_duration"]);

        $con = Propel::getConnection();
        $con->beginTransaction();
        try {
            if (!$currentUser->isAdminOrPM()) {
                throw new Exception("Permission denied");
            }

            /****** UPDATE SCHEDULE START TIME ******/
            //get the ccShow object to which this instance belongs
            //so we can get the original start date and time
            $oldCcShow = CcShowQuery::create()
                ->findPk($showData["add_show_id"]);

            //DateTime in user's local time
            $newStartDateTime = new DateTime($showData["add_show_start_date"]." ".
            $showData["add_show_start_time"],
                new DateTimeZone(Application_Model_Preference::GetTimezone()));

            $ccShowInstanceOrig = CcShowInstancesQuery::create()
                ->findPk($showData["add_show_instance_id"]);
            $diff = $this->calculateShowStartDiff($newStartDateTime,
                $ccShowInstanceOrig->getLocalStartDateTime());

            if ($diff > 0) {
                Application_Service_SchedulerService::updateScheduleStartTime(
                    array($showData["add_show_instance_id"]), $diff);
            }
            /****** UPDATE SCHEDULE START TIME ENDS******/

            $this->setCcShow($showData);
            $this->setCcShowDays($showData);
            $this->setCcShowHosts($showData);
            $this->delegateInstanceCreation();

            //get the new instance id
            $ccShowInstance = CcShowInstancesQuery::create()
                ->filterByDbShowId($this->ccShow->getDbId())
                ->findOne();

            $newInstanceId = $ccShowInstance->getDbId();

            //update cc_schedule with the new instance id
            $ccSchedules = CcScheduleQuery::create()
                ->filterByDbInstanceId($showData["add_show_instance_id"])
                ->find();

            foreach ($ccSchedules as $ccSchedule) {
                $ccSchedule->setDbInstanceId($newInstanceId);
                $ccSchedule->save();
            }

            $con = Propel::getConnection(CcShowInstancesPeer::DATABASE_NAME);
            $ccShowInstance->updateDbTimeFilled($con);

            //delete the edited instance from the repeating sequence
            $ccShowInstanceOrig->setDbModifiedInstance(true)->save();

            $service_showForm = new Application_Service_ShowFormService($showData["add_show_id"]);
            list($start, $end) = $service_showForm->getNextFutureRepeatShowTime();
            $oldCcShowDay = $oldCcShow->getFirstCcShowDay();
            $oldCcShowDay
                ->setDbFirstShow($start->setTimezone(new DateTimeZone("UTC"))->format("Y-m-d"))
                ->save();

            $con->commit();
            Application_Model_RabbitMq::PushSchedule();
        } catch (Exception $e) {
            $con->rollback();
            Logging::info("EXCEPTION: Show update failed.");
            Logging::info($e->getMessage());
        }
    }

    public function addUpdateShow($showData)
    {
        $service_user = new Application_Service_UserService();
        $currentUser = $service_user->getCurrentUser();

        $showData["add_show_duration"] = $this->formatShowDuration(
            $showData["add_show_duration"]);

        $con = Propel::getConnection();
        $con->beginTransaction();
        try {
            if (!$currentUser->isAdminOrPM()) {
                throw new Exception("Permission denied");
            }
            //update ccShow
            $this->setCcShow($showData);

            $daysAdded = array();
            if ($this->isUpdate) {
                $daysAdded = $this->delegateInstanceCleanup($showData);
                // updates cc_show_instances start/end times, and updates
                // schedule start/end times
                $this->applyShowStartEndDifference($showData);
                $this->deleteRebroadcastInstances();
                $this->deleteCcShowDays();
                $this->deleteCcShowHosts();
                if ($this->isRebroadcast) {
                    //delete entry in cc_show_rebroadcast
                    $this->deleteCcShowRebroadcasts();
                }
            }

            //update ccShowDays
            $this->setCcShowDays($showData);

            //update ccShowRebroadcasts
            $this->setCcShowRebroadcasts($showData);

            //update ccShowHosts
            $this->setCcShowHosts($showData);

            //create new ccShowInstances
            $this->delegateInstanceCreation($daysAdded);

            $con->commit();
            Application_Model_RabbitMq::PushSchedule();
        } catch (Exception $e) {
            $con->rollback();
            $this->isUpdate ? $action = "update" : $action = "creation";
            Logging::info("EXCEPTION: Show ".$action." failed.");
            Logging::info($e->getMessage());
        }
    }

    /**
     * 
     * Receives a cc_show id and determines whether to create a 
     * single show instance or repeating show instances
     */
    public function delegateInstanceCreation($daysAdded=null, $end=null, $fillInstances=false)
    {
        $populateUntil = $this->getPopulateShowUntilDateTIme();

        if (is_null($this->ccShow)) {
{'fraction_non_alphanumeric': 0.13005259744938397, 'fraction_numerical': 0.002017436414727286, 'mean_word_length': 2.264346190028222, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 137, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3218754', 'n_tokens_mistral': 3961, 'n_tokens_neox': 3785, 'n_words': 629}