- Moved all form validation to it's own function in schedule service

- Moved show creation to it's own function in schedule service
This commit is contained in:
denise 2013-02-25 17:31:43 -05:00
parent 3e49421221
commit 9f0baba4f8
8 changed files with 167 additions and 34 deletions

View File

@ -104,7 +104,7 @@ class ScheduleController extends Zend_Controller_Action
$this->view->repeats = $forms["repeats"];
$this->view->live = $forms["live"];
$this->view->rr = $forms["record"];
$this->view->absoluteRebroadcast = $forms["abs_record"];
$this->view->absoluteRebroadcast = $forms["abs_rebroadcast"];
$this->view->rebroadcast = $forms["rebroadcast"];
$this->view->who = $forms["who"];
$this->view->style = $forms["style"];
@ -879,19 +879,26 @@ class ScheduleController extends Zend_Controller_Action
$data['add_show_day_check'] = null;
}
$validateStartDate = true;
$success = Application_Model_Schedule::addUpdateShow($data, $this,
$validateStartDate);
$forms = $this->service_schedule->createShowForms();
if ($success) {
$this->view->addNewShow = true;
$this->view->newForm = $this->view->render(
'schedule/add-show-form.phtml');
$this->view->what = $forms["what"];
$this->view->when = $forms["when"];
$this->view->repeats = $forms["repeats"];
$this->view->live = $forms["live"];
$this->view->rr = $forms["record"];
$this->view->absoluteRebroadcast = $forms["abs_rebroadcast"];
$this->view->rebroadcast = $forms["rebroadcast"];
$this->view->who = $forms["who"];
$this->view->style = $forms["style"];
$this->view->addNewShow = true;
if ($this->service_schedule->validateShowForms($forms, $data)) {
$this->view->newForm = $this->view->render('schedule/add-show-form.phtml');
$this->service_schedule->createShow($data);
Logging::debug("Show creation succeeded");
} else {
$this->view->addNewShow = true;
$this->view->form = $this->view->render(
'schedule/add-show-form.phtml');
$this->view->form = $this->view->render('schedule/add-show-form.phtml');
Logging::debug("Show creation failed");
}
}

View File

@ -40,6 +40,14 @@ class Application_Form_AddShowAbsoluteRebroadcastDates extends Zend_Form_SubForm
}
}
public function isValid($formData) {
if (parent::isValid($formData)) {
return $this->checkReliantFields($formData);
} else {
return false;
}
}
public function checkReliantFields($formData)
{
$noError = true;

View File

@ -45,6 +45,14 @@ class Application_Form_AddShowRebroadcastDates extends Zend_Form_SubForm
}
}
public function isValid($formData) {
if (parent::isValid($formData)) {
return $this->checkReliantFields($formData);
} else {
return false;
}
}
public function checkReliantFields($formData)
{
$noError = true;

View File

@ -66,6 +66,14 @@ class Application_Form_AddShowRepeats extends Zend_Form_SubForm
}
}
public function isValid($formData) {
if (parent::isValid($formData)) {
return $this->checkReliantFields($formData);
} else {
return false;
}
}
public function checkReliantFields($formData)
{
if (!$formData['add_show_no_end']) {

View File

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

View File

@ -1088,7 +1088,7 @@ SQL;
* Another clean-up is to move all the form manipulation to the proper form class.....
* -Martin
*/
public static function addUpdateShow($data, $controller, $validateStartDate,
/*public static function addUpdateShow($data, $controller, $validateStartDate,
$originalStartDate=null, $update=false, $instanceId=null)
{
$userInfo = Zend_Auth::getInstance()->getStorage()->read();
@ -1227,7 +1227,7 @@ SQL;
//$controller->view->form = $controller->view->render('schedule/add-show-form.phtml');
return false;
}
}
}*/
public static function checkOverlappingShows($show_start, $show_end,
$update=false, $instanceId=null, $showId=null)

View File

@ -1113,11 +1113,13 @@ SQL;
*/
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'));
/*$startDateTime = new DateTime($data['add_show_start_date']." ".$data['add_show_start_time']);*/
// these are not used
/*$utcStartDateTime = clone $startDateTime;
$utcStartDateTime->setTimezone(new DateTimeZone('UTC'));*/
if ($data['add_show_no_end']) {
/*if ($data['add_show_no_end']) {
$endDate = NULL;
} elseif ($data['add_show_repeats']) {
$endDateTime = new DateTime($data['add_show_end_date']);
@ -1129,28 +1131,28 @@ SQL;
//$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());
/*$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;
/*$repeatType = ($data['add_show_repeats']) ? $data['add_show_repeat_type'] : -1;*/
if ($data['add_show_id'] == -1) {
$ccShow = new CcShow();
/*$ccShow = new CcShow();*/
} else {
$ccShow = CcShowQuery::create()->findPK($data['add_show_id']);
}
$ccShow->setDbName($data['add_show_name']);
/*$ccShow->setDbName($data['add_show_name']);
$ccShow->setDbDescription($data['add_show_description']);
$ccShow->setDbUrl($data['add_show_url']);
$ccShow->setDbGenre($data['add_show_genre']);
@ -1160,11 +1162,11 @@ SQL;
$ccShow->setDbLiveStreamUsingCustomAuth($data['cb_custom_auth'] == 1);
$ccShow->setDbLiveStreamUser($data['custom_username']);
$ccShow->setDbLiveStreamPass($data['custom_password']);
$ccShow->save();
$ccShow->save();*/
$showId = $ccShow->getDbId();
/*$showId = $ccShow->getDbId();*/
$isRecorded = (isset($data['add_show_record']) && $data['add_show_record']) ? 1 : 0;
/*$isRecorded = (isset($data['add_show_record']) && $data['add_show_record']) ? 1 : 0;*/
if ($data['add_show_id'] != -1) {
$show = new Application_Model_Show($showId);
@ -1178,7 +1180,7 @@ SQL;
}
//don't set day for monthly repeat type, it's invalid.
if ($data['add_show_repeats'] && $data['add_show_repeat_type'] == 2) {
/*if ($data['add_show_repeats'] && $data['add_show_repeat_type'] == 2) {
$showDay = new CcShowDays();
$showDay->setDbFirstShow($startDateTime->format("Y-m-d"));
$showDay->setDbLastShow($endDate);
@ -1215,7 +1217,7 @@ SQL;
$showDay->save();
}
}
}
}*/
//check if we are adding or updating a show, and if updating
//erase all the show's future show_rebroadcast information first.

View File

@ -43,7 +43,7 @@ class Application_Service_ScheduleService
$forms["style"] = $formStyle;
$forms["live"] = $formLive;
$forms["record"] = $formRecord;
$forms["abs_record"] = $formAbsoluteRebroadcast;
$forms["abs_rebroadcast"] = $formAbsoluteRebroadcast;
$forms["rebroadcast"] = $formRebroadcast;
return $forms;
@ -79,21 +79,113 @@ class Application_Service_ScheduleService
*
* Validates show forms
*
* @return array of booleans
* @return boolean
*/
public function validateShowForms($forms)
public function validateShowForms($forms, $formData, $validateStartDate = true)
{
$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);
$repeats = true;
if ($formData["add_show_repeats"]) {
$repeats = $forms["repeats"]->isValid($formData);
/*
* Make the absolute rebroadcast form valid since
* it does not get used if the show is repeating
*/
$forms["abs_rebroadcast"]->reset();
$absRebroadcast = true;
$rebroadcast = true;
if ($formData["add_show_rebroadcast"]) {
$formData["add_show_duration"] = $this->formatShowDuration(
$formData["add_show_duration"]);
$rebroadcast = $forms["rebroadcast"]->isValid($formData);
}
} else {
/*
* Make the rebroadcast form valid since it does
* not get used if the show is not repeating.
* Instead, we use the absolute rebroadcast form
*/
$forms["rebroadcast"]->reset();
$rebroadcast = true;
$absRebroadcast = true;
if ($formData["add_show_rebroadcast"]) {
$formData["add_show_duration"] = $this->formatShowDuration(
$formData["add_show_duration"]);
$absRebroadcast = $forms["abs_rebroadcast"]->isValid($formData);
}
}
if ($what && $live && $record && $who && $style && $when &&
$repeats && $absRebroadcast && $rebroadcast) {
return true;
} else {
return false;
}
}
/*
* Form stuff ends
*/
public function formatShowDuration($duration) {
$hPos = strpos($duration, 'h');
$mPos = strpos($duration, 'm');
$hValue = 0;
$mValue = 0;
if ($hPos !== false) {
$hValue = trim(substr($duration, 0, $hPos));
}
if ($mPos !== false) {
$hPos = $hPos === false ? 0 : $hPos+1;
$mValue = trim(substr($duration, $hPos, -1 ));
}
return $hValue.":".$mValue;
}
/**
*
* Creates a new show if form data is valid
*/
public function createShow()
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));
$repeatType = ($showData['add_show_repeats']) ? $showData['add_show_repeat_type'] : -1;
$isRecorded = (isset($showData['add_show_record']) && $showData['add_show_record']) ? 1 : 0;
$showData["add_show_duration"] = $this->formatShowDuration(
$showData["add_show_duration"]);
if ($isAdminOrPM) {
$service_show = new Application_Service_ShowService();
//create ccShow
$ccShow = new CcShow();
$ccShow = $service_show->setShow($ccShow, $showData);
//create ccShowDay
$service_show->createShowDays(
$showData, $ccShow->getDbId(), $user->getId(), $repeatType, $isRecorded);
//create ccShowHosts
//create ccShowRebroadcast
//populate ccShowInstances
}
}
}