Created a new service for show days

Created a new user service
Started refactoring edit show action
This commit is contained in:
denise 2013-03-11 16:18:40 -04:00
parent 1cc823ef0e
commit 7347be35b1
8 changed files with 286 additions and 137 deletions

View File

@ -795,42 +795,14 @@ class ScheduleController extends Zend_Controller_Action
$data['add_show_day_check'] = null;
}
/*$show = new Application_Model_Show($data['add_show_id']);*/
$forms = $this->createShowFormAction();
//------- PRE EDIT SHOW CHECK ---------------//
/*$validateStartDate = true;
$validateStartTime = true;
if (!array_key_exists('add_show_start_date', $data)) {
//Changing the start date was disabled, since the
//array key does not exist. We need to repopulate this entry from the db.
//The start date will be returned in UTC time, so lets convert it to local time.
$dt = Application_Common_DateHelper::ConvertToLocalDateTime($show->getStartDateAndTime());
$data['add_show_start_date'] = $dt->format("Y-m-d");
list($data, $validateStartDate, $validateStartTime, $originalShowStartDateTime) =
$this->service_schedule->preEditShowValidationCheck($data);
if (!array_key_exists('add_show_start_time', $data)) {
$data['add_show_start_time'] = $dt->format("H:i");
$validateStartTime = false;
}
$validateStartDate = false;
}
$data['add_show_record'] = $show->isRecorded();*/
// -------------------------------------------//
if ($this->service_schedule->validateShowForms($forms, $data, $validateStartDate,
$originalShowStartDateTime, true, $data["add_show_instance_id"])) {
//-------- ADJUST ORIGINAL START DATE -------//
/*if ($show->isRepeating()) {
$nextFutureRepeatShow = $show->getNextFutureRepeatShowTime();
$originalShowStartDateTime = $nextFutureRepeatShow["starts"];
} else {
$originalShowStartDateTime = Application_Common_DateHelper::ConvertToLocalDateTime(
$show->getStartDateAndTime());
}*/
//-----------------------------------------//
$success = Application_Model_Schedule::addUpdateShow($data, $this,
$validateStartDate, $originalShowStartDateTime, true,
$data['add_show_instance_id']);
if ($success) {
$scheduler = new Application_Model_Scheduler();
$showInstances = CcShowInstancesQuery::create()->filterByDbShowId($data['add_show_id'])->find();
foreach ($showInstances as $si) {

View File

@ -87,9 +87,11 @@ class Application_Form_AddShowWhen extends Zend_Form_SubForm
}
public function isWhenFormValid($formData, $validateStartDate) {
public function isWhenFormValid($formData, $validateStartDate, $originalStartDate,
$update, $instanceId) {
if (parent::isValid($formData)) {
return self::checkReliantFields($formData, $validateStartDate);
return self::checkReliantFields($formData, $validateStartDate,
$originalStartDate, $update, $instanceId);
} else {
return false;
}

View File

@ -15,4 +15,8 @@
*/
class CcSubjs extends BaseCcSubjs {
public function isAdminOrPM()
{
return $this->type === UTYPE_ADMIN || $this->type === UTYPE_PROGRAM_MANAGER;
}
} // CcSubjs

View File

@ -3,12 +3,15 @@
class Application_Service_ScheduleService
{
private $service_show;
private $service_showDays;
private $service_showInstances;
private $service_user;
public function __construct()
{
$this->service_show = new Application_Service_ShowService();
$this->service_showInstances = new Application_Service_ShowInstanceService();
$this->service_user = new Application_Service_UserService();
}
/*
* Form stuff begins here
@ -89,14 +92,16 @@ class Application_Service_ScheduleService
*
* @return boolean
*/
public function validateShowForms($forms, $formData, $validateStartDate = true)
public function validateShowForms($forms, $formData, $validateStartDate = true,
$originalStartDate=null, $editShow=false, $instanceId=null)
{
$what = $forms["what"]->isValid($formData);
$live = $forms["live"]->isValid($formData);
$record = $forms["record"]->isValid($formData);
$who = $forms["who"]->isValid($formData);
$style = $forms["style"]->isValid($formData);
$when = $forms["when"]->isWhenFormValid($formData, $validateStartDate);
$when = $forms["when"]->isWhenFormValid($formData, $validateStartDate,
$originalStartDate, $editShow, $instanceId);
$repeats = true;
if ($formData["add_show_repeats"]) {
@ -163,13 +168,18 @@ class Application_Service_ScheduleService
/**
*
* Creates a new show if form data is valid
* Creates a new show, which entails creating entries in
* the following tables:
* cc_show
* cc_show_days
* cc_show_hosts
* cc_show_rebroadcast
* cc_show_instances
*/
public function createShow($showData)
{
$userInfo = Zend_Auth::getInstance()->getStorage()->read();
$user = new Application_Model_User($userInfo->id);
$isAdminOrPM = $user->isUserType(array(UTYPE_ADMIN, UTYPE_PROGRAM_MANAGER));
//CcSubj object
$currentUser = $this->service_user->getCurrentUser();
$repeatType = ($showData['add_show_repeats']) ? $showData['add_show_repeat_type'] : -1;
$isRecorded = (isset($showData['add_show_record']) && $showData['add_show_record']) ? 1 : 0;
@ -178,15 +188,16 @@ class Application_Service_ScheduleService
$showData["add_show_duration"] = $this->formatShowDuration(
$showData["add_show_duration"]);
if ($isAdminOrPM) {
if ($currentUser->isAdminOrPM()) {
//create ccShow
$ccShow = new CcShow();
$ccShow = $this->service_show->setShow($ccShow, $showData);
$showId = $ccShow->getDbId();
//create ccShowDays
$this->service_show->createShowDays(
$showData, $showId, $user->getId(), $repeatType, $isRecorded);
$this->service_showDays = new Application_Service_ShowDaysService($showId);
$this->service_showDays->createShowDays(
$showData, $currentUser->getDbId(), $repeatType, $isRecorded);
//create ccShowRebroadcasts
$this->service_show->createShowRebroadcasts($showData, $showId, $repeatType, $isRecorded);
@ -199,4 +210,61 @@ class Application_Service_ScheduleService
}
}
public function editShow($formData)
{
//CcSubj object
$currentUser = $this->service_user->getCurrentUser();
}
/**
*
* Before we send the form data in for validation, there
* are a few fields we may need to adjust first
* @param $formData
*/
public function preEditShowValidationCheck($formData) {
$validateStartDate = true;
$validateStartTime = true;
$this->service_showDays = new Application_Service_ShowDaysService(
$formData["add_show_id"]);
//CcShowDays object of the show currently being edited
$currentShowDay = $this->service_showDays->getCurrentShowDay();
if (!array_key_exists('add_show_start_date', $formData)) {
//Changing the start date was disabled, since the
//array key does not exist. We need to repopulate this entry from the db.
//The start date will be returned in UTC time, so lets convert it to local time.
$dt = Application_Common_DateHelper::ConvertToLocalDateTime(
$this->service_showDays->getStartDateAndTime());
$formData['add_show_start_date'] = $dt->format("Y-m-d");
if (!array_key_exists('add_show_start_time', $formData)) {
$formData['add_show_start_time'] = $dt->format("H:i");
$validateStartTime = false;
}
$validateStartDate = false;
}
$formData['add_show_record'] = $currentShowDay->getDbRecord();
//if the show is repeating, set the start date to the next
//repeating instance in the future
if ($currentShowDay->getDbRepeatType() != -1) {
$nextFutureRepeatShow = $this->service_showInstances
->getNextFutureRepeatShowTime($formData["add_show_id"]);
$originalShowStartDateTime = $nextFutureRepeatShow["starts"];
} else {
$originalShowStartDateTime = Application_Common_DateHelper::ConvertToLocalDateTime(
$this->service_showDays->getStartDateAndTime());
}
return array($formData, $validateStartDate, $validateStartTime, $originalShowStartDateTime);
}
public function editShow($showData)
{
//CcSubj object
$currentUser = $this->service_user->getCurrentUser();
}
}

View File

@ -0,0 +1,125 @@
<?php
class Application_Service_ShowDaysService
{
private $showId;
public function __construct($id)
{
$this->showId = $id;
}
/**
*
* Sets the fields for a cc_show_days table row
* @param $showData
* @param $showId
* @param $userId
* @param $repeatType
* @param $isRecorded
*/
public function createShowDays($showData, $userId, $repeatType, $isRecorded)
{
$startDateTime = new DateTime($showData['add_show_start_date']." ".$showData['add_show_start_time']);
if ($showData['add_show_no_end']) {
$endDate = NULL;
} elseif ($showData['add_show_repeats']) {
$endDateTime = new DateTime($showData['add_show_end_date']);
$endDateTime->add(new DateInterval("P1D"));
$endDate = $endDateTime->format("Y-m-d");
} else {
$endDateTime = new DateTime($showData['add_show_start_date']);
$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 (!$showData['add_show_repeats']) {
$showData['add_show_day_check'] = array($startDow);
} elseif ($showData['add_show_repeats'] && $showData['add_show_day_check'] == "") {
$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) {
$showDay = new CcShowDays();
$showDay->setDbFirstShow($startDateTime->format("Y-m-d"));
$showDay->setDbLastShow($endDate);
$showDay->setDbStartTime($startDateTime->format("H:i:s"));
$showDay->setDbTimezone(Application_Model_Preference::GetUserTimezone($userId));
$showDay->setDbDuration($showData['add_show_duration']);
$showDay->setDbRepeatType($repeatType);
$showDay->setDbShowId($this->showId);
$showDay->setDbRecord($isRecorded);
$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()) {
$showDay = new CcShowDays();
$showDay->setDbFirstShow($startDateTimeClone->format("Y-m-d"));
$showDay->setDbLastShow($endDate);
$showDay->setDbStartTime($startDateTimeClone->format("H:i"));
$showDay->setDbTimezone(Application_Model_Preference::GetUserTimezone($userId));
$showDay->setDbDuration($showData['add_show_duration']);
$showDay->setDbDay($day);
$showDay->setDbRepeatType($repeatType);
$showDay->setDbShowId($this->showId);
$showDay->setDbRecord($isRecorded);
$showDay->save();
}
}
}
}
/**
*
* Gets the cc_show_days entries for a specific show
*
* @return array of ccShowDays objects
*/
public function getShowDays()
{
$sql = "SELECT * FROM cc_show_days WHERE show_id = :show_id";
return Application_Common_Database::prepareAndExecute(
$sql, array(":show_id" => $this->showId), 'all');
}
public function getStartDateAndTime()
{
//CcShowDays object
$showDay = $this->getCurrentShowDay();
$dt = new DateTime($showDay->getDbFirstShow()." ".$showDay->getDbStartTime(),
new DateTimeZone($showDay->getDbTimezone()));
$dt->setTimezone(new DateTimeZone("UTC"));
return $dt->format("Y-m-d H:i");
}
/**
*
* Returns a CcShowDays object of the show that
* is currently being edited.
*/
public function getCurrentShowDay()
{
return CcShowDaysQuery::create()->filterByDbShowId($this->showId)
->findOne();
}
}

View File

@ -2,6 +2,9 @@
class Application_Service_ShowInstanceService
{
private $service_show;
private $service_showDays;
private $service_user;
const NO_REPEAT = -1;
const REPEAT_WEEKLY = 0;
const REPEAT_BI_WEEKLY = 1;
@ -11,6 +14,7 @@ class Application_Service_ShowInstanceService
public function __construct()
{
$this->service_show = new Application_Service_ShowService();
$this->service_user = new Application_Service_UserService();
}
/**
@ -22,7 +26,9 @@ class Application_Service_ShowInstanceService
{
$populateUntil = $this->service_show->getPopulateShowUntilDateTIme();
$showDays = $this->service_show->getShowDays($showId);
$this->service_showDays = new Application_Service_ShowDaysService($showId);
$showDays = $this->service_showDays->getShowDays();
foreach ($showDays as $day) {
switch ($day["repeat_type"]) {
case self::NO_REPEAT:
@ -192,4 +198,35 @@ class Application_Service_ShowInstanceService
return new DatePeriod(new DateTime($start, new DateTimeZone($timezone)),
new DateInterval($repeatInterval), $endDatePeriod);
}
/**
*
* Returns 2 DateTime objects, in the user's local time,
* of the next future repeat show instance start and end time
*/
public function getNextFutureRepeatShowTime($showId)
{
$sql = <<<SQL
SELECT starts, ends FROM cc_show_instances
WHERE ends > now() at time zone 'UTC'
AND show_id = :showId
ORDER BY starts
LIMIT 1
SQL;
$result = Application_Common_Database::prepareAndExecute( $sql,
array( 'showId' => $showId ), 'all' );
foreach ($result as $r) {
$show["starts"] = new DateTime($r["starts"], new DateTimeZone('UTC'));
$show["ends"] = new DateTime($r["ends"], new DateTimeZone('UTC'));
}
$userTimezone = Application_Model_Preference::GetUserTimezone(
$this->service_user->getCurrentUser()->getDbId());
$show["starts"]->setTimezone(new DateTimeZone($userTimezone));
$show["ends"]->setTimezone(new DateTimeZone($userTimezone));
return $show;
}
}

View File

@ -27,84 +27,6 @@ class Application_Service_ShowService
return $ccShow;
}
/**
*
* Sets the fields for a cc_show_days table row
* @param $showData
* @param $showId
* @param $userId
* @param $repeatType
* @param $isRecorded
*/
public function createShowDays($showData, $showId, $userId, $repeatType, $isRecorded)
{
$startDateTime = new DateTime($showData['add_show_start_date']." ".$showData['add_show_start_time']);
if ($showData['add_show_no_end']) {
$endDate = NULL;
} elseif ($showData['add_show_repeats']) {
$endDateTime = new DateTime($showData['add_show_end_date']);
$endDateTime->add(new DateInterval("P1D"));
$endDate = $endDateTime->format("Y-m-d");
} else {
$endDateTime = new DateTime($showData['add_show_start_date']);
$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 (!$showData['add_show_repeats']) {
$showData['add_show_day_check'] = array($startDow);
} elseif ($showData['add_show_repeats'] && $showData['add_show_day_check'] == "") {
$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) {
$showDay = new CcShowDays();
$showDay->setDbFirstShow($startDateTime->format("Y-m-d"));
$showDay->setDbLastShow($endDate);
$showDay->setDbStartTime($startDateTime->format("H:i:s"));
$showDay->setDbTimezone(Application_Model_Preference::GetUserTimezone($userId));
$showDay->setDbDuration($showData['add_show_duration']);
$showDay->setDbRepeatType($repeatType);
$showDay->setDbShowId($showId);
$showDay->setDbRecord($isRecorded);
$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()) {
$showDay = new CcShowDays();
$showDay->setDbFirstShow($startDateTimeClone->format("Y-m-d"));
$showDay->setDbLastShow($endDate);
$showDay->setDbStartTime($startDateTimeClone->format("H:i"));
$showDay->setDbTimezone(Application_Model_Preference::GetUserTimezone($userId));
$showDay->setDbDuration($showData['add_show_duration']);
$showDay->setDbDay($day);
$showDay->setDbRepeatType($repeatType);
$showDay->setDbShowId($showId);
$showDay->setDbRecord($isRecorded);
$showDay->save();
}
}
}
}
/**
*
* Sets the fields for a cc_show_rebroadcast table row
@ -178,20 +100,6 @@ class Application_Service_ShowService
return $populateUntil;
}
/**
*
* Gets the cc_show_days entries for a specific show
*
* @return array of ccShowDays objects
*/
public function getShowDays($showId)
{
$sql = "SELECT * FROM cc_show_days WHERE show_id = :show_id";
return Application_Common_Database::prepareAndExecute(
$sql, array(":show_id" => $showId), 'all');
}
/**
*
* Enter description here ...

View File

@ -0,0 +1,33 @@
<?php
/*define('UTYPE_HOST' , 'H');
define('UTYPE_ADMIN' , 'A');
define('UTYPE_GUEST' , 'G');
define('UTYPE_PROGRAM_MANAGER' , 'P');*/
class Application_Service_UserService
{
private $currentUser;
public function __construct()
{
$userInfo = Zend_Auth::getInstance()->getStorage()->read();
if (!is_null($userInfo->id)) {
$this->currentUser = CcSubjsQuery::create()->findPK($userInfo->id);
}
}
/**
*
* Returns a CcSubjs object
*/
public function getCurrentUser()
{
if (is_null($this->currentUser)) {
throw new Exception();
}
return $this->currentUser;
}
}