CC-5762 : Playlist options validation

saving rules separately again now. testing a bunch of ways to submit bad values.
This commit is contained in:
Naomi 2014-03-31 16:52:10 -04:00
parent aa53297a9c
commit c0fb7a421f
6 changed files with 166 additions and 82 deletions

View File

@ -20,6 +20,7 @@ class PlaylistController extends Zend_Controller_Action
->addActionContext('shuffle', 'json')
->addActionContext('generate', 'json')
->addActionContext('clear', 'json')
->addActionContext('save-rules', 'json')
->initContext();
@ -172,6 +173,55 @@ class PlaylistController extends Zend_Controller_Action
}
}
public function saveRulesAction()
{
$rules = $this->_getParam('rules');
Logging::info($rules);
$con = Propel::getConnection(PlaylistPeer::DATABASE_NAME);
$con->beginTransaction();
try {
$playlist = $this->getPlaylist();
$form = new Application_Form_PlaylistRules();
if (isset($rules["criteria"])) {
$form->buildCriteriaOptions($rules["criteria"]);
}
$criteriaFields = $form->getPopulateHelp();
$playlistRules = array(
"pl_repeat_tracks" => $rules[Playlist::RULE_REPEAT_TRACKS],
"pl_my_tracks" => $rules[Playlist::RULE_USERS_TRACKS_ONLY],
"pl_order_column" => $rules[Playlist::RULE_ORDER][Playlist::RULE_ORDER_COLUMN],
"pl_order_direction" => $rules[Playlist::RULE_ORDER][Playlist::RULE_ORDER_DIRECTION],
"pl_limit_value" => $rules["limit"]["value"],
"pl_limit_options" => $rules["limit"]["unit"]
);
$data = array_merge($criteriaFields, $playlistRules);
if ($form->isValid($data)) {
Logging::info("playlist rules are valid");
Logging::info($form->getValues());
$playlist->setRules($rules);
}
else {
Logging::info("invalid playlist rules");
Logging::info($form->getMessages());
$this->view->form = $form->render();
}
$con->commit();
}
catch (Exception $e) {
$con->rollBack();
$this->view->error = $e->getMessage();
}
}
public function saveAction()
{
$info = $this->_getParam('serialized');

View File

@ -205,7 +205,12 @@ class Application_Form_PlaylistRules extends Zend_Form
->setAttrib('class', 'sp_input_text_limit')
->setLabel(_('Limit to'))
->setRequired(true)
->setDecorators(array('ViewHelper'));
->setDecorators(array('ViewHelper'))
->setValidators(array(
new Zend_Validate_NotEmpty(),
new Zend_Validate_Int(),
new Zend_Validate_Between(array('min' => 1, 'max' => PHP_INT_MAX))
));
$this->addElement($limitValue);
$orderby = new Zend_Form_Element_Select('pl_order_column');
@ -252,6 +257,7 @@ class Application_Form_PlaylistRules extends Zend_Form
$input->setValidators(array(
new Zend_Validate_NotEmpty(),
new Zend_Validate_Int(),
new Zend_Validate_Between(array('min' => 0, 'max' => PHP_INT_MAX))
));
break;
case "s":
@ -276,7 +282,6 @@ class Application_Form_PlaylistRules extends Zend_Form
$criteria = new Zend_Form_Element_Select("sp_criteria_field_{$suffix}");
$criteria
->setAttrib('class', 'input_select sp_input_select rule_criteria')
->setValue('Select criteria')
->setDecorators(array('viewHelper'))
->setMultiOptions($this->getCriteriaOptions());
@ -348,22 +353,25 @@ class Application_Form_PlaylistRules extends Zend_Form
private function buildRuleCriteriaRow($info = null) {
$suffix = mt_rand(10000, 99999);
if (is_null($info)) {
$criteria = "";
}
else {
$criteria = $info["criteria"];
}
$critType = $this->_criteriaTypes[$criteria];
$critKey = self::buildRuleCriteria($suffix);
if (isset($info)) {
if (isset($info["criteria"])) {
$this->_populateHelp[$critKey] = $info["criteria"];
$criteria = $info["criteria"];
$this->_populateHelp[$critKey] = $criteria;
}
//check that a valid criteria was passed to build the rest of the row with.
try {
$critType = $this->_criteriaTypes[$criteria];
}
catch (Exception $e) {
$this->getElement("sp_criteria_field_{$suffix}")->addError("Invalid criteria {$criteria}");
return $suffix;
}
if (isset($info["modifier"])) {
$options = self::getModifierOptions($criteria);

View File

@ -69,40 +69,6 @@ class Application_Service_PlaylistService
$playlist->setDescription($info["description"]);
}
if (isset($info["rules"])) {
$rules = $info["rules"];
$form = new Application_Form_PlaylistRules();
if (isset($info["rules"]["criteria"])) {
$form->buildCriteriaOptions($info["rules"]["criteria"]);
}
$criteriaFields = $form->getPopulateHelp();
$playlistRules = array(
"pl_repeat_tracks" => $rules[Playlist::RULE_REPEAT_TRACKS],
"pl_my_tracks" => $rules[Playlist::RULE_USERS_TRACKS_ONLY],
"pl_order_column" => $rules[Playlist::RULE_ORDER][Playlist::RULE_ORDER_COLUMN],
"pl_order_direction" => $rules[Playlist::RULE_ORDER][Playlist::RULE_ORDER_DIRECTION],
"pl_limit_value" => $rules["limit"]["value"],
"pl_limit_options" => $rules["limit"]["unit"]
);
$data = array_merge($criteriaFields, $playlistRules);
if ($form->isValid($data)) {
Logging::info("playlist rules are valid");
Logging::info($form->getValues());
$playlist->setRules($info["rules"]);
}
else {
Logging::info("invalid playlist rules");
Logging::info($form->getMessages());
}
}
//only save content for static playlists
if ($playlist->isStatic()) {
$content = isset($info["content"]) ? $info["content"] : array();

View File

@ -1,9 +1,11 @@
<dl class='zend_form search-criteria'>
<dd id='track_rules'>
<dd>
<span class='sp_text_font'><?php echo $this->element->getElement('pl_repeat_tracks')->getLabel() ?></span>
<span class='repeat_tracks_help_icon'></span>
<?php echo $this->element->getElement('pl_repeat_tracks')?>
</dd>
<dd>
<span class='sp_text_font'><?php echo $this->element->getElement('pl_my_tracks')->getLabel() ?></span>
<span class='repeat_tracks_help_icon'></span>
<?php echo $this->element->getElement('pl_my_tracks')?>
@ -24,15 +26,19 @@
$input1 = $this->element->getElement("sp_criteria_value_{$suffix}");
$input2 = $this->element->getElement("sp_criteria_extra_{$suffix}");
$unit1 = $this->element->getElement("sp_rel_date_unit_1_{$suffix}");
$unit2 = $this->element->getElement("sp_rel_date_unit_2_{$suffix}")
$unit2 = $this->element->getElement("sp_rel_date_unit_2_{$suffix}");
$checkErrors = array($criteria, $modifier, $input1, $input2, $unit1, $unit2);
?>
<?php echo $criteria; ?>
<?php echo $modifier; ?>
<?php if (isset($input1)): ?>
<?php echo $input1; ?>
<?php endif; ?>
<?php if (isset($modifier)): ?>
<?php echo $modifier; ?>
<?php endif; ?>
<?php if (isset($unit1)): ?>
<?php echo $unit1; ?>
@ -43,7 +49,7 @@
<?php echo _("to"); ?>
<?php echo $input2; ?>
</span>
<?php elseif (in_array ($modifier->getValue(), array(20, 21))): ?>
<?php elseif (isset($modifier) && in_array ($modifier->getValue(), array(20, 21))): ?>
<a class="btn btn-small btn-range">
<span>TO</span>
</a>
@ -60,6 +66,17 @@
<span><?php echo _("OR"); ?></span>
</a>
</div>
<?php foreach($checkErrors as $field): ?>
<?php if (isset($field) && $field->hasErrors()) : ?>
<?php foreach($field->getMessages() as $error): ?>
<span class='errors sp-errors'>
<?php echo $error; ?>
</span>
<?php endforeach; ?>
<?php endif; ?>
<?php endforeach; ?>
<?php endfor; ?>
</div>
@ -71,7 +88,9 @@
<dd id='rule_order' class="criteria-element">
<?php
$order = $this->element->getElement('pl_order_column');
$direction = $this->element->getElement('pl_order_direction');
$direction = $this->element->getElement('pl_order_direction');
$checkErrors = array($order, $direction);
?>
<span class='sp_text_font'><?php echo $order->getLabel() ?></span>
<?php echo $order ?>
@ -79,18 +98,37 @@
$direction->setAttrib('style', 'display:none;');
} ?>
<?php echo $direction ?>
</dd>
<dd id='rule_limit' class="criteria-element">
<span class='sp_text_font'><?php echo $this->element->getElement('pl_limit_value')->getLabel() ?></span>
<?php echo $this->element->getElement('pl_limit_value')?>
<?php echo $this->element->getElement('pl_limit_options') ?>
<?php if($this->element->getElement("pl_limit_value")->hasErrors()) : ?>
<?php foreach($this->element->getElement("pl_limit_value")->getMessages() as $error): ?>
<?php foreach($checkErrors as $field): ?>
<?php if (isset($field) && $field->hasErrors()) : ?>
<?php foreach($field->getMessages() as $error): ?>
<span class='errors sp-errors'>
<?php echo $error; ?>
</span>
<?php endforeach; ?>
<?php endif; ?>
<?php endforeach; ?>
</dd>
<dd id='rule_limit' class="criteria-element">
<?php
$value = $this->element->getElement('pl_limit_value');
$options = $this->element->getElement('pl_limit_options');
$checkErrors = array($value, $options);
?>
<span class='sp_text_font'><?php echo $value->getLabel() ?></span>
<?php echo $value ?>
<?php echo $options ?>
<?php foreach($checkErrors as $field): ?>
<?php if (isset($field) && $field->hasErrors()) : ?>
<?php foreach($field->getMessages() as $error): ?>
<span class='errors sp-errors'>
<?php echo $error; ?>
</span>
<?php endforeach; ?>
<?php endif; ?>
<?php endforeach; ?>
</dd>
</dl>

View File

@ -33,6 +33,18 @@
<?php if (isset($this->obj)):?>
<input id="playlist_id" type="hidden" value="<?php echo $this->obj->getId(); ?>"></input>
<input id="playlist_lastmod" type="hidden" value="<?php echo $this->obj->getLastModifiedEpoch(); ?>"></input>
<fieldset class="toggle">
<legend style="cursor: pointer;">
<span class="ui-icon ui-icon-triangle-2-n-s"></span>
<?php echo _("Options") ?>
</legend>
<?php echo $this->obj->getRules(); ?>
<div class='btn-group pull-right'>
<button id="spl_rules_save" class="btn" role="button" aria-disabled="false"><?php echo _("Save") ?></button>
</div>
</fieldset>
<div class="playlist_title">
<h3 id="playlist_name">
<a id="playlist_name_display" contenteditable="true">
@ -62,14 +74,6 @@
</dl>
</fieldset>
<fieldset class="toggle">
<legend style="cursor: pointer;">
<span class="ui-icon ui-icon-triangle-2-n-s"></span>
<?php echo _("Options") ?>
</legend>
<?php echo $this->obj->getRules(); ?>
</fieldset>
<?php if($this->obj->hasContent()):?>
<div style="clear:both; float:none; width:100%;">
<ul id="spl_sortable">

View File

@ -439,20 +439,9 @@ var AIRTIME = (function(AIRTIME){
return criteria;
}
function serializePlaylist(order) {
var i, len,
info = {},
entries = [];
function serializeRules() {
for (i = 0, len = order.length; i < len; i++) {
entries.push(getEntryDetails(order[i]));
}
info["name"] = cleanString($("#playlist_name").text());
info["description"] = cleanString($("#playlist_description").val());
info["content"] = entries;
info["rules"] = {
var rules = {
"repeat-tracks": $("#pl_repeat_tracks").is(":checked"),
"my-tracks": $("#pl_my_tracks").is(":checked"),
"limit": {
@ -466,6 +455,22 @@ var AIRTIME = (function(AIRTIME){
}
};
return rules;
}
function serializePlaylist(order) {
var i, len,
info = {},
entries = [];
for (i = 0, len = order.length; i < len; i++) {
entries.push(getEntryDetails(order[i]));
}
info["name"] = cleanString($("#playlist_name").text());
info["description"] = cleanString($("#playlist_description").val());
info["content"] = entries;
return info;
}
@ -799,6 +804,19 @@ var AIRTIME = (function(AIRTIME){
});
});
$playlist.on("click", "#spl_rules_save", function(e) {
var url = baseUrl+"playlist/save-rules",
data;
data = {format: "json", rules: serializeRules()};
$.post(url, data, function(json) {
if (json.form !== undefined) {
$(".search-criteria").replaceWith(json.form);
}
});
});
$playlist.on("click", "#spl_save", function(e) {
var $contents = $("#spl_sortable"),
order = $contents.sortable("toArray"),