CC-5762 : Playlist options validation
saving rules separately again now. testing a bunch of ways to submit bad values.
This commit is contained in:
parent
aa53297a9c
commit
c0fb7a421f
|
@ -20,6 +20,7 @@ class PlaylistController extends Zend_Controller_Action
|
||||||
->addActionContext('shuffle', 'json')
|
->addActionContext('shuffle', 'json')
|
||||||
->addActionContext('generate', 'json')
|
->addActionContext('generate', 'json')
|
||||||
->addActionContext('clear', 'json')
|
->addActionContext('clear', 'json')
|
||||||
|
->addActionContext('save-rules', 'json')
|
||||||
->initContext();
|
->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()
|
public function saveAction()
|
||||||
{
|
{
|
||||||
$info = $this->_getParam('serialized');
|
$info = $this->_getParam('serialized');
|
||||||
|
|
|
@ -205,7 +205,12 @@ class Application_Form_PlaylistRules extends Zend_Form
|
||||||
->setAttrib('class', 'sp_input_text_limit')
|
->setAttrib('class', 'sp_input_text_limit')
|
||||||
->setLabel(_('Limit to'))
|
->setLabel(_('Limit to'))
|
||||||
->setRequired(true)
|
->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);
|
$this->addElement($limitValue);
|
||||||
|
|
||||||
$orderby = new Zend_Form_Element_Select('pl_order_column');
|
$orderby = new Zend_Form_Element_Select('pl_order_column');
|
||||||
|
@ -252,6 +257,7 @@ class Application_Form_PlaylistRules extends Zend_Form
|
||||||
$input->setValidators(array(
|
$input->setValidators(array(
|
||||||
new Zend_Validate_NotEmpty(),
|
new Zend_Validate_NotEmpty(),
|
||||||
new Zend_Validate_Int(),
|
new Zend_Validate_Int(),
|
||||||
|
new Zend_Validate_Between(array('min' => 0, 'max' => PHP_INT_MAX))
|
||||||
));
|
));
|
||||||
break;
|
break;
|
||||||
case "s":
|
case "s":
|
||||||
|
@ -276,7 +282,6 @@ class Application_Form_PlaylistRules extends Zend_Form
|
||||||
$criteria = new Zend_Form_Element_Select("sp_criteria_field_{$suffix}");
|
$criteria = new Zend_Form_Element_Select("sp_criteria_field_{$suffix}");
|
||||||
$criteria
|
$criteria
|
||||||
->setAttrib('class', 'input_select sp_input_select rule_criteria')
|
->setAttrib('class', 'input_select sp_input_select rule_criteria')
|
||||||
->setValue('Select criteria')
|
|
||||||
->setDecorators(array('viewHelper'))
|
->setDecorators(array('viewHelper'))
|
||||||
->setMultiOptions($this->getCriteriaOptions());
|
->setMultiOptions($this->getCriteriaOptions());
|
||||||
|
|
||||||
|
@ -348,22 +353,25 @@ class Application_Form_PlaylistRules extends Zend_Form
|
||||||
private function buildRuleCriteriaRow($info = null) {
|
private function buildRuleCriteriaRow($info = null) {
|
||||||
$suffix = mt_rand(10000, 99999);
|
$suffix = mt_rand(10000, 99999);
|
||||||
|
|
||||||
if (is_null($info)) {
|
|
||||||
$criteria = "";
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
$criteria = $info["criteria"];
|
|
||||||
}
|
|
||||||
|
|
||||||
$critType = $this->_criteriaTypes[$criteria];
|
|
||||||
$critKey = self::buildRuleCriteria($suffix);
|
$critKey = self::buildRuleCriteria($suffix);
|
||||||
|
|
||||||
if (isset($info)) {
|
if (isset($info)) {
|
||||||
|
|
||||||
if (isset($info["criteria"])) {
|
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"])) {
|
if (isset($info["modifier"])) {
|
||||||
|
|
||||||
$options = self::getModifierOptions($criteria);
|
$options = self::getModifierOptions($criteria);
|
||||||
|
|
|
@ -69,40 +69,6 @@ class Application_Service_PlaylistService
|
||||||
$playlist->setDescription($info["description"]);
|
$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
|
//only save content for static playlists
|
||||||
if ($playlist->isStatic()) {
|
if ($playlist->isStatic()) {
|
||||||
$content = isset($info["content"]) ? $info["content"] : array();
|
$content = isset($info["content"]) ? $info["content"] : array();
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
<dl class='zend_form search-criteria'>
|
<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='sp_text_font'><?php echo $this->element->getElement('pl_repeat_tracks')->getLabel() ?></span>
|
||||||
<span class='repeat_tracks_help_icon'></span>
|
<span class='repeat_tracks_help_icon'></span>
|
||||||
<?php echo $this->element->getElement('pl_repeat_tracks')?>
|
<?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='sp_text_font'><?php echo $this->element->getElement('pl_my_tracks')->getLabel() ?></span>
|
||||||
<span class='repeat_tracks_help_icon'></span>
|
<span class='repeat_tracks_help_icon'></span>
|
||||||
<?php echo $this->element->getElement('pl_my_tracks')?>
|
<?php echo $this->element->getElement('pl_my_tracks')?>
|
||||||
|
@ -24,15 +26,19 @@
|
||||||
$input1 = $this->element->getElement("sp_criteria_value_{$suffix}");
|
$input1 = $this->element->getElement("sp_criteria_value_{$suffix}");
|
||||||
$input2 = $this->element->getElement("sp_criteria_extra_{$suffix}");
|
$input2 = $this->element->getElement("sp_criteria_extra_{$suffix}");
|
||||||
$unit1 = $this->element->getElement("sp_rel_date_unit_1_{$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 $criteria; ?>
|
||||||
<?php echo $modifier; ?>
|
|
||||||
|
|
||||||
<?php if (isset($input1)): ?>
|
<?php if (isset($input1)): ?>
|
||||||
<?php echo $input1; ?>
|
<?php echo $input1; ?>
|
||||||
<?php endif; ?>
|
<?php endif; ?>
|
||||||
|
|
||||||
|
<?php if (isset($modifier)): ?>
|
||||||
|
<?php echo $modifier; ?>
|
||||||
|
<?php endif; ?>
|
||||||
|
|
||||||
<?php if (isset($unit1)): ?>
|
<?php if (isset($unit1)): ?>
|
||||||
<?php echo $unit1; ?>
|
<?php echo $unit1; ?>
|
||||||
|
@ -43,7 +49,7 @@
|
||||||
<?php echo _("to"); ?>
|
<?php echo _("to"); ?>
|
||||||
<?php echo $input2; ?>
|
<?php echo $input2; ?>
|
||||||
</span>
|
</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">
|
<a class="btn btn-small btn-range">
|
||||||
<span>TO</span>
|
<span>TO</span>
|
||||||
</a>
|
</a>
|
||||||
|
@ -60,6 +66,17 @@
|
||||||
<span><?php echo _("OR"); ?></span>
|
<span><?php echo _("OR"); ?></span>
|
||||||
</a>
|
</a>
|
||||||
</div>
|
</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; ?>
|
<?php endfor; ?>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
@ -71,7 +88,9 @@
|
||||||
<dd id='rule_order' class="criteria-element">
|
<dd id='rule_order' class="criteria-element">
|
||||||
<?php
|
<?php
|
||||||
$order = $this->element->getElement('pl_order_column');
|
$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>
|
<span class='sp_text_font'><?php echo $order->getLabel() ?></span>
|
||||||
<?php echo $order ?>
|
<?php echo $order ?>
|
||||||
|
@ -79,18 +98,37 @@
|
||||||
$direction->setAttrib('style', 'display:none;');
|
$direction->setAttrib('style', 'display:none;');
|
||||||
} ?>
|
} ?>
|
||||||
<?php echo $direction ?>
|
<?php echo $direction ?>
|
||||||
</dd>
|
|
||||||
|
<?php foreach($checkErrors as $field): ?>
|
||||||
<dd id='rule_limit' class="criteria-element">
|
<?php if (isset($field) && $field->hasErrors()) : ?>
|
||||||
<span class='sp_text_font'><?php echo $this->element->getElement('pl_limit_value')->getLabel() ?></span>
|
<?php foreach($field->getMessages() as $error): ?>
|
||||||
<?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): ?>
|
|
||||||
<span class='errors sp-errors'>
|
<span class='errors sp-errors'>
|
||||||
<?php echo $error; ?>
|
<?php echo $error; ?>
|
||||||
</span>
|
</span>
|
||||||
<?php endforeach; ?>
|
<?php endforeach; ?>
|
||||||
<?php endif; ?>
|
<?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>
|
</dd>
|
||||||
</dl>
|
</dl>
|
|
@ -33,6 +33,18 @@
|
||||||
<?php if (isset($this->obj)):?>
|
<?php if (isset($this->obj)):?>
|
||||||
<input id="playlist_id" type="hidden" value="<?php echo $this->obj->getId(); ?>"></input>
|
<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>
|
<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">
|
<div class="playlist_title">
|
||||||
<h3 id="playlist_name">
|
<h3 id="playlist_name">
|
||||||
<a id="playlist_name_display" contenteditable="true">
|
<a id="playlist_name_display" contenteditable="true">
|
||||||
|
@ -62,14 +74,6 @@
|
||||||
</dl>
|
</dl>
|
||||||
</fieldset>
|
</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()):?>
|
<?php if($this->obj->hasContent()):?>
|
||||||
<div style="clear:both; float:none; width:100%;">
|
<div style="clear:both; float:none; width:100%;">
|
||||||
<ul id="spl_sortable">
|
<ul id="spl_sortable">
|
||||||
|
|
|
@ -439,20 +439,9 @@ var AIRTIME = (function(AIRTIME){
|
||||||
return criteria;
|
return criteria;
|
||||||
}
|
}
|
||||||
|
|
||||||
function serializePlaylist(order) {
|
function serializeRules() {
|
||||||
var i, len,
|
|
||||||
info = {},
|
|
||||||
entries = [];
|
|
||||||
|
|
||||||
for (i = 0, len = order.length; i < len; i++) {
|
var rules = {
|
||||||
entries.push(getEntryDetails(order[i]));
|
|
||||||
}
|
|
||||||
|
|
||||||
info["name"] = cleanString($("#playlist_name").text());
|
|
||||||
info["description"] = cleanString($("#playlist_description").val());
|
|
||||||
info["content"] = entries;
|
|
||||||
|
|
||||||
info["rules"] = {
|
|
||||||
"repeat-tracks": $("#pl_repeat_tracks").is(":checked"),
|
"repeat-tracks": $("#pl_repeat_tracks").is(":checked"),
|
||||||
"my-tracks": $("#pl_my_tracks").is(":checked"),
|
"my-tracks": $("#pl_my_tracks").is(":checked"),
|
||||||
"limit": {
|
"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;
|
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) {
|
$playlist.on("click", "#spl_save", function(e) {
|
||||||
var $contents = $("#spl_sortable"),
|
var $contents = $("#spl_sortable"),
|
||||||
order = $contents.sortable("toArray"),
|
order = $contents.sortable("toArray"),
|
||||||
|
|
Loading…
Reference in New Issue