CC-1665: Scheduled stream rebroadcasting and recording

-validate form fields of new webstream
This commit is contained in:
Martin Konecny 2012-08-02 15:45:15 -04:00
parent 2ef4fd314c
commit 9cc152c0ae
4 changed files with 100 additions and 15 deletions

View file

@ -33,9 +33,14 @@ class WebstreamController extends Zend_Controller_Action
{ {
$request = $this->getRequest(); $request = $this->getRequest();
Application_Model_Webstream::save($request); $analysis = Application_Model_Webstream::analyzeFormData($request);
$this->view->statusMessage = "<div class='success'>Webstream saved.</div>"; if (Application_Model_Webstream::isValid($analysis)) {
Application_Model_Webstream::save($request);
$this->view->statusMessage = "<div class='success'>Webstream saved.</div>";
} else {
$this->view->statusMessage = "<div class='errors'>Invalid form values.</div>";
$this->view->analysis = $analysis;
}
} }
} }

View file

@ -75,20 +75,82 @@ class Application_Model_Webstream{
return $leftOvers; 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) public static function save($request)
{ {
Logging::log($request->getParams());
$userInfo = Zend_Auth::getInstance()->getStorage()->read(); $userInfo = Zend_Auth::getInstance()->getStorage()->read();
Logging::log($userInfo);
$length = trim($request->getParam("length")); $length = trim($request->getParam("length"));
preg_match("/^([0-9]{1,2})h ([0-5][0-9])m$/", $length, $matches); $result = preg_match("/^([0-9]{1,2})h ([0-5][0-9])m$/", $length, $matches);
$hours = $matches[1]; if ($result == 1 && count($matches) == 3) {
$minutes = $matches[2]; $hours = $matches[1];
$di = new DateInterval("PT{$hours}H{$minutes}M"); $minutes = $matches[2];
$dblength = $di->format("%H:%I"); $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. #TODO: These should be validated by a Zend Form.
$webstream = new CcWebstream(); $webstream = new CcWebstream();
$webstream->setDbName($request->getParam("name")); $webstream->setDbName($request->getParam("name"));

View file

@ -8,6 +8,7 @@
<input id="pl_lastMod" type="hidden" value="<?php echo $this->ws->getLastModified('U'); ?>"></input> <input id="pl_lastMod" type="hidden" value="<?php echo $this->ws->getLastModified('U'); ?>"></input>
<div class="status" style="display:none;"></div> <div class="status" style="display:none;"></div>
<div class="playlist_title"> <div class="playlist_title">
<div id="name-error" class="errors" style="display:none;"></div>
<h3 id="ws_name"> <h3 id="ws_name">
<a id="playlist_name_display" contenteditable="true"><?php echo $this->ws->getName(); ?></a> <a id="playlist_name_display" contenteditable="true"><?php echo $this->ws->getName(); ?></a>
</h3> </h3>
@ -22,10 +23,12 @@
<textarea cols="80" rows="24" id="description" name="description"><?php echo $this->ws->getDescription(); ?></textarea> <textarea cols="80" rows="24" id="description" name="description"><?php echo $this->ws->getDescription(); ?></textarea>
</dd> </dd>
<dt id="submit-label" style="display: none;">&nbsp;</dt> <dt id="submit-label" style="display: none;">&nbsp;</dt>
<div id="url-error" class="errors" style="display:none;"></div>
<dt id="streamurl-label"><label for="streamurl">Stream URL:</label></dt> <dt id="streamurl-label"><label for="streamurl">Stream URL:</label></dt>
<dd id="streamurl-element"> <dd id="streamurl-element">
<input type="text" value="http://" size="40"/> <input type="text" value="http://" size="40"/>
</dd> </dd>
<div id="length-error" class="errors" style="display:none;"></div>
<dt id="streamlength-label"><label for="streamlength">Default Length:</label></dt> <dt id="streamlength-label"><label for="streamlength">Default Length:</label></dt>
<dd id="streamlength-element"> <dd id="streamlength-element">
<input type="text" value="00h 20m"/> <input type="text" value="00h 20m"/>

View file

@ -532,15 +532,30 @@ var AIRTIME = (function(AIRTIME){
var streamurl = $pl.find("#streamurl-element input").val(); var streamurl = $pl.find("#streamurl-element input").val();
var length = $pl.find("#streamlength-element input").val(); var length = $pl.find("#streamlength-element input").val();
var name = $pl.find("#playlist_name_display").text(); var name = $pl.find("#playlist_name_display").text();
//hide any previous errors (if any)
$("#side_playlist .errors").empty().hide();
var url = 'Webstream/save'; var url = 'Webstream/save';
$.post(url, $.post(url,
{format: "json", description: description, url:streamurl, length: length, name: name}, {format: "json", description: description, url:streamurl, length: length, name: name},
function(json){ function(json){
var $status = $("#side_playlist .status"); if (json.analysis){
$status.html(json.statusMessage); for (var s in json.analysis){
$status.show(); var field = json.analysis[s];
setTimeout(function(){$status.fadeOut("slow", function(){$status.empty()})}, 5000);
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);
}
}); });