From 9cc152c0ae4fca81dcd0dc2d32d47fe8c537cd07 Mon Sep 17 00:00:00 2001 From: Martin Konecny Date: Thu, 2 Aug 2012 15:45:15 -0400 Subject: [PATCH] CC-1665: Scheduled stream rebroadcasting and recording -validate form fields of new webstream --- .../controllers/WebstreamController.php | 11 ++- airtime_mvc/application/models/Webstream.php | 78 +++++++++++++++++-- .../views/scripts/webstream/webstream.phtml | 3 + airtime_mvc/public/js/airtime/library/spl.js | 23 +++++- 4 files changed, 100 insertions(+), 15 deletions(-) diff --git a/airtime_mvc/application/controllers/WebstreamController.php b/airtime_mvc/application/controllers/WebstreamController.php index deaddfca7..2b38f50aa 100644 --- a/airtime_mvc/application/controllers/WebstreamController.php +++ b/airtime_mvc/application/controllers/WebstreamController.php @@ -33,9 +33,14 @@ class WebstreamController extends Zend_Controller_Action { $request = $this->getRequest(); - Application_Model_Webstream::save($request); + $analysis = Application_Model_Webstream::analyzeFormData($request); - $this->view->statusMessage = "
Webstream saved.
"; - + if (Application_Model_Webstream::isValid($analysis)) { + Application_Model_Webstream::save($request); + $this->view->statusMessage = "
Webstream saved.
"; + } else { + $this->view->statusMessage = "
Invalid form values.
"; + $this->view->analysis = $analysis; + } } } diff --git a/airtime_mvc/application/models/Webstream.php b/airtime_mvc/application/models/Webstream.php index 3663b489d..8ff6be973 100644 --- a/airtime_mvc/application/models/Webstream.php +++ b/airtime_mvc/application/models/Webstream.php @@ -75,20 +75,82 @@ class Application_Model_Webstream{ return $leftOvers; } +/* +Array +( + [controller] => Webstream + [action] => save + [module] => default + [format] => json + [description] => desc + [url] => http:// + [length] => 00h 20m + [name] => Default +) + */ + + + public static function analyzeFormData($request) + { + $valid = array("length" => array(true, ''), + "url" => array(true, '')); + + $length = trim($request->getParam("length")); + $result = preg_match("/^([0-9]{1,2})h ([0-5][0-9])m$/", $length, $matches); + if (!$result == 1 || !count($matches) == 3) { + $valid['length'][0] = false; + $valid['length'][1] = 'Length should be of form "00h 00m"'; + } + + + $url = trim($request->getParam("url")); + //simple validator that checks to make sure that the url starts with http(s), + //and that the domain is at least 1 letter long followed by a period. + $result = preg_match("/^(http|https):\/\/.+\./", $url, $matches); + + if ($result == 0) { + $valid['url'][0] = false; + $valid['url'][1] = 'URL should be of form "http://www.domain.com/mount"'; + } + + + $name = trim($request->getParam("name")); + if (strlen($name) == 0) { + $valid['name'][0] = false; + $valid['name'][1] = 'Webstream name cannot be empty'; + } + + return $valid; + } + + public static function isValid($analysis) + { + foreach ($analysis as $k => $v) { + if ($v[0] == false) { + return false; + } + } + + return true; + } public static function save($request) { - Logging::log($request->getParams()); $userInfo = Zend_Auth::getInstance()->getStorage()->read(); - Logging::log($userInfo); $length = trim($request->getParam("length")); - preg_match("/^([0-9]{1,2})h ([0-5][0-9])m$/", $length, $matches); - $hours = $matches[1]; - $minutes = $matches[2]; - $di = new DateInterval("PT{$hours}H{$minutes}M"); - $dblength = $di->format("%H:%I"); - + $result = preg_match("/^([0-9]{1,2})h ([0-5][0-9])m$/", $length, $matches); + if ($result == 1 && count($matches) == 3) { + $hours = $matches[1]; + $minutes = $matches[2]; + $di = new DateInterval("PT{$hours}H{$minutes}M"); + $dblength = $di->format("%H:%I"); + } else { + //This should never happen because we should have already validated + //in the controller + throw new Exception("Invalid date format: $length"); + } + #TODO: These should be validated by a Zend Form. $webstream = new CcWebstream(); $webstream->setDbName($request->getParam("name")); diff --git a/airtime_mvc/application/views/scripts/webstream/webstream.phtml b/airtime_mvc/application/views/scripts/webstream/webstream.phtml index 7e3228d70..4761f2ac9 100644 --- a/airtime_mvc/application/views/scripts/webstream/webstream.phtml +++ b/airtime_mvc/application/views/scripts/webstream/webstream.phtml @@ -8,6 +8,7 @@
+

ws->getName(); ?>

@@ -22,10 +23,12 @@ +
+
diff --git a/airtime_mvc/public/js/airtime/library/spl.js b/airtime_mvc/public/js/airtime/library/spl.js index 8c256ca8b..6a9f41b99 100644 --- a/airtime_mvc/public/js/airtime/library/spl.js +++ b/airtime_mvc/public/js/airtime/library/spl.js @@ -532,15 +532,30 @@ var AIRTIME = (function(AIRTIME){ var streamurl = $pl.find("#streamurl-element input").val(); var length = $pl.find("#streamlength-element input").val(); var name = $pl.find("#playlist_name_display").text(); + + //hide any previous errors (if any) + $("#side_playlist .errors").empty().hide(); var url = 'Webstream/save'; $.post(url, {format: "json", description: description, url:streamurl, length: length, name: name}, function(json){ - var $status = $("#side_playlist .status"); - $status.html(json.statusMessage); - $status.show(); - setTimeout(function(){$status.fadeOut("slow", function(){$status.empty()})}, 5000); + if (json.analysis){ + for (var s in json.analysis){ + var field = json.analysis[s]; + + if (!field[0]) { + var elemId = "#"+s+"-error"; + var $div = $("#side_playlist " + elemId).text(field[1]).show(); + } + } + } else { + var $status = $("#side_playlist .status"); + $status.html(json.statusMessage); + $status.show(); + setTimeout(function(){$status.fadeOut("slow", function(){$status.empty()})}, 5000); + } + });