CC-84: Smart Playlists

- code cleanup
- adds 'Add' button to last row, always
This commit is contained in:
denise 2012-07-16 11:01:34 -04:00
parent 601263b7a0
commit 2dae5f30f7
6 changed files with 81 additions and 91 deletions

View File

@ -129,7 +129,7 @@ class Application_Form_SmartPlaylistCriteria extends Zend_Form_SubForm
for ($i = 0; $i < $numElements; $i++) { for ($i = 0; $i < $numElements; $i++) {
$criteriaType = ""; $criteriaType = "";
$criteria = new Zend_Form_Element_Select('sp_criteria_field_'.$i); $criteria = new Zend_Form_Element_Select('sp_criteria_field_'.$i);
$criteria->setAttrib('class', 'input_select') $criteria->setAttrib('class', 'input_select sp_input_select')
->setValue('Select criteria') ->setValue('Select criteria')
->setDecorators(array('viewHelper')) ->setDecorators(array('viewHelper'))
->setMultiOptions($criteriaOptions); ->setMultiOptions($criteriaOptions);
@ -144,7 +144,7 @@ class Application_Form_SmartPlaylistCriteria extends Zend_Form_SubForm
$criteriaModifers = new Zend_Form_Element_Select('sp_criteria_modifier_'.$i); $criteriaModifers = new Zend_Form_Element_Select('sp_criteria_modifier_'.$i);
$criteriaModifers->setValue('Select modifier') $criteriaModifers->setValue('Select modifier')
->setAttrib('class', 'input_select') ->setAttrib('class', 'input_select sp_input_select')
->setDecorators(array('viewHelper')); ->setDecorators(array('viewHelper'));
if ($i != 0 && !isset($storedCrit["crit"][$i])){ if ($i != 0 && !isset($storedCrit["crit"][$i])){
$criteriaModifers->setAttrib('disabled', 'disabled'); $criteriaModifers->setAttrib('disabled', 'disabled');
@ -162,7 +162,7 @@ class Application_Form_SmartPlaylistCriteria extends Zend_Form_SubForm
$this->addElement($criteriaModifers); $this->addElement($criteriaModifers);
$criteriaValue = new Zend_Form_Element_Text('sp_criteria_value_'.$i); $criteriaValue = new Zend_Form_Element_Text('sp_criteria_value_'.$i);
$criteriaValue->setAttrib('class', 'input_text') $criteriaValue->setAttrib('class', 'input_text sp_input_text')
->setDecorators(array('viewHelper')); ->setDecorators(array('viewHelper'));
if ($i != 0 && !isset($storedCrit["crit"][$i])){ if ($i != 0 && !isset($storedCrit["crit"][$i])){
$criteriaValue->setAttrib('disabled', 'disabled'); $criteriaValue->setAttrib('disabled', 'disabled');
@ -173,10 +173,11 @@ class Application_Form_SmartPlaylistCriteria extends Zend_Form_SubForm
$this->addElement($criteriaValue); $this->addElement($criteriaValue);
$criteriaExtra = new Zend_Form_Element_Text('sp_criteria_extra_'.$i); $criteriaExtra = new Zend_Form_Element_Text('sp_criteria_extra_'.$i);
$criteriaExtra->setAttrib('class', 'input_text') $criteriaExtra->setAttrib('class', 'input_text sp_extra_input_text')
->setDecorators(array('viewHelper')); ->setDecorators(array('viewHelper'));
if (isset($storedCrit["crit"][$i]["extra"])) { if (isset($storedCrit["crit"][$i]["extra"])) {
$criteriaExtra->setValue($storedCrit["crit"][$i]["extra"]); $criteriaExtra->setValue($storedCrit["crit"][$i]["extra"]);
$criteriaValue->setAttrib('class', 'input_text sp_extra_input_text');
}else{ }else{
$criteriaExtra->setAttrib('disabled', 'disabled'); $criteriaExtra->setAttrib('disabled', 'disabled');
} }
@ -189,8 +190,6 @@ class Application_Form_SmartPlaylistCriteria extends Zend_Form_SubForm
$limit->setMultiOptions($limitOptions); $limit->setMultiOptions($limitOptions);
if (isset($storedCrit["limit"])) { if (isset($storedCrit["limit"])) {
$limit->setValue($storedCrit["limit"]["modifier"]); $limit->setValue($storedCrit["limit"]["modifier"]);
}else{
$limit->setAttrib('disabled', 'disabled');
} }
$this->addElement($limit); $this->addElement($limit);
@ -201,8 +200,6 @@ class Application_Form_SmartPlaylistCriteria extends Zend_Form_SubForm
$this->addElement($limitValue); $this->addElement($limitValue);
if (isset($storedCrit["limit"])) { if (isset($storedCrit["limit"])) {
$limitValue->setValue($storedCrit["limit"]["value"]); $limitValue->setValue($storedCrit["limit"]["value"]);
}else{
$limitValue->setAttrib('disabled', 'disabled');
} }
$save = new Zend_Form_Element_Button('save_button'); $save = new Zend_Form_Element_Button('save_button');
@ -212,29 +209,5 @@ class Application_Form_SmartPlaylistCriteria extends Zend_Form_SubForm
$this->addElement($save); $this->addElement($save);
} }
public function loadCriteria($p_playlistId)
{
$c = new Criteria();
$c->add(CcPlaylistcriteriaPeer::PLAYLIST_ID, $p_playlistId);
$out = CcPlaylistcriteriaPeer::doSelect($c);
$i = 0;
foreach ($out as $crit) {
$criteria = $crit->getDbCriteria();
$modifier = $crit->getDbModifier();
$value = $crit->getDbValue();
$extra = $crit->getDbExtra();
if($criteria == "limit"){
Zend_Form::getElement("sp_limit_options")->setValue($modifier);
Zend_Form::getElement("sp_limit_value")->setValue($value);
}else{
Zend_Form::getElement("sp_criteria_$i")->setValue($criteria);
Zend_Form::getElement("sp_criteria_modifier_$i")->setValue($criteria);
Zend_Form::getElement("sp_criteria_value_$i")->setValue($criteria);
Zend_Form::getElement("sp_criteria_extra_$i")->setValue($criteria);
$i++;
}
}
}
} }

View File

@ -870,11 +870,11 @@ class Application_Model_Playlist {
} }
if ($data['etc']['sp_limit_options'] == 'hours' || $data['etc']['sp_limit_options'] == 'mins') { if ($data['etc']['sp_limit_options'] == 'hours' || $data['etc']['sp_limit_options'] == 'mins') {
if ( $data['etc']['sp_limit_value'] == "" || intval($data['etc']['sp_limit_value']) == 0) { if ( $data['etc']['sp_limit_value'] == "" || intval($data['etc']['sp_limit_value']) == 0) {
$error[] = "Litmit cannot be empty or 0"; $error[] = "Limit cannot be empty or 0";
} else { } else {
$mins = $data['etc']['sp_limit_value'] * $multiplier; $mins = $data['etc']['sp_limit_value'] * $multiplier;
if ($mins > 14400) { if ($mins > 14400) {
$error[] = "Litmit cannot be more than 24 hrs"; $error[] = "Limit cannot be more than 24 hrs";
} }
} }
if (count($error) > 0){ if (count($error) > 0){
@ -886,11 +886,11 @@ class Application_Model_Playlist {
foreach ($data['criteria'] as $key=>$d){ foreach ($data['criteria'] as $key=>$d){
$error = array(); $error = array();
// check for not selected select box // check for not selected select box
if ($d['sp_criteria'] == "0" || $d['sp_criteria_modifier'] == "0"){ if ($d['sp_criteria_field'] == "0" || $d['sp_criteria_modifier'] == "0"){
$error[] = "You must select Criteria and Modifier"; $error[] = "You must select Criteria and Modifier";
} else { } else {
// we need to take care 'length' specially since the column type is varchar // we need to take care 'length' specially since the column type is varchar
if ($d['sp_criteria'] == 'length') { if ($d['sp_criteria_field'] == 'length') {
if (!preg_match("/(\d{2}):(\d{2}):(\d{2})/", $d['sp_criteria_value'])) { if (!preg_match("/(\d{2}):(\d{2}):(\d{2})/", $d['sp_criteria_value'])) {
$error[] = "'Length' should be in '00:00:00' format"; $error[] = "'Length' should be in '00:00:00' format";
} }
@ -976,7 +976,7 @@ class Application_Model_Playlist {
$c = new Criteria(); $c = new Criteria();
foreach ($p_data as $criteria) { foreach ($p_data as $criteria) {
$spCriteria = self::$criteria2PeerMap[$criteria['sp_criteria']]; $spCriteria = self::$criteria2PeerMap[$criteria['sp_criteria_field']];
$spCriteriaModifier = $criteria['sp_criteria_modifier']; $spCriteriaModifier = $criteria['sp_criteria_modifier'];
$spCriteriaValue = $criteria['sp_criteria_value']; $spCriteriaValue = $criteria['sp_criteria_value'];
if ($spCriteriaModifier == "starts with") { if ($spCriteriaModifier == "starts with") {

View File

@ -27,8 +27,6 @@
<?php echo $this->element->getElement('sp_criteria_value_'.$i) ?><span id="extra_criteria" <?php echo $this->element->getElement('sp_criteria_extra_'.$i)->getAttrib("disabled") == "disabled"?'style="display:none;"':""?>> to <?php echo $this->element->getElement('sp_criteria_extra_'.$i) ?></span> <?php echo $this->element->getElement('sp_criteria_value_'.$i) ?><span id="extra_criteria" <?php echo $this->element->getElement('sp_criteria_extra_'.$i)->getAttrib("disabled") == "disabled"?'style="display:none;"':""?>> to <?php echo $this->element->getElement('sp_criteria_extra_'.$i) ?></span>
<?php if ($i != 0) { ?> <?php if ($i != 0) { ?>
<a href='#' id='criteria_remove_<?php echo $i ?>'>Remove&nbsp;</a> <a href='#' id='criteria_remove_<?php echo $i ?>'>Remove&nbsp;</a>
<?php } else { ?>
<a href='#' id='criteria_add' class='criteria_add'>Add</a>
<?php } ?> <?php } ?>
<br /> <br />
</div> </div>

View File

@ -416,6 +416,18 @@ input[type="text"]:focus, input[type="password"]:focus, textarea:focus, .input_t
padding: 2px 2px 2px 0; padding: 2px 2px 2px 0;
vertical-align: top; vertical-align: top;
} }
.sp_input_select{
width: 130px;
}
input.input_text.sp_input_text{
width: 200px !important;
}
input.input_text.sp_extra_input_text{
width: 85px !important;
}
label { label {
font-size:13px; font-size:13px;
color:#5b5b5b; color:#5b5b5b;
@ -1409,7 +1421,7 @@ div.success{
border:1px solid #488214; border:1px solid #488214;
} }
div.errors{ div.errors, span.errors{
color:#902d2d; color:#902d2d;
font-size:11px; font-size:11px;
padding:2px 4px; padding:2px 4px;
@ -1418,14 +1430,9 @@ div.errors{
border:1px solid #c83f3f; border:1px solid #c83f3f;
} }
span.sp-errors{ span.errors.sp-errors{
color:#902d2d;
font-size:11px;
padding:2px 4px;
background:#c6b4b4;
margin-bottom:2px;
border:1px solid #c83f3f;
width: 400px; width: 400px;
display: block;
} }
.collapsible-header, .collapsible-header-disabled { .collapsible-header, .collapsible-header-disabled {

View File

@ -320,6 +320,8 @@ var AIRTIME = (function(AIRTIME){
.append(json.html); .append(json.html);
setUpPlaylist(); setUpPlaylist();
var form = $('#smart-playlist-form');
appendAddButton(form);
} }
//sets events dynamically for playlist entries (each row in the playlist) //sets events dynamically for playlist entries (each row in the playlist)

View File

@ -1,5 +1,7 @@
$(document).ready(function() { $(document).ready(function() {
setSmartPlaylistEvents(); setSmartPlaylistEvents();
var form = $('#smart-playlist-form');
appendAddButton(form);
}); });
function setSmartPlaylistEvents() { function setSmartPlaylistEvents() {
@ -26,6 +28,7 @@ function setSmartPlaylistEvents() {
var count = list_length - curr_pos; var count = list_length - curr_pos;
var next = curr.next(); var next = curr.next();
var add_button = form.find('a[id="criteria_add"]'); var add_button = form.find('a[id="criteria_add"]');
var item_to_hide;
//remove error message from current row, if any //remove error message from current row, if any
var error_element = curr.find('span[class="errors sp-errors"]'); var error_element = curr.find('span[class="errors sp-errors"]');
@ -60,9 +63,6 @@ function setSmartPlaylistEvents() {
var criteria_extra = next.find('[name^="sp_criteria_extra"]').val(); var criteria_extra = next.find('[name^="sp_criteria_extra"]').val();
curr.find('[name^="sp_criteria_extra"]').val(criteria_extra); curr.find('[name^="sp_criteria_extra"]').val(criteria_extra);
disableAndHideExtraField(next.find(':first-child'), index+1); disableAndHideExtraField(next.find(':first-child'), index+1);
/*next.find('[name^="sp_criteria_extra"]').remove();
next.find('span[id="sp_criteria_extra_label"]').remove();*/
/* if only the current row has the extra criteria value, /* if only the current row has the extra criteria value,
* then just remove the current row's extra criteria element * then just remove the current row's extra criteria element
@ -70,8 +70,6 @@ function setSmartPlaylistEvents() {
} else if (curr.find('[name^="sp_criteria_extra"]').attr("disabled") != "disabled" } else if (curr.find('[name^="sp_criteria_extra"]').attr("disabled") != "disabled"
&& next.find('[name^="sp_criteria_extra"]').attr("disabled") == "disabled") { && next.find('[name^="sp_criteria_extra"]').attr("disabled") == "disabled") {
disableAndHideExtraField(curr.find(':first-child'), index); disableAndHideExtraField(curr.find(':first-child'), index);
/*curr.find('[name^="sp_criteria_extra"]').remove();
curr.find('span[id="sp_criteria_extra_label"]').remove();*/
/* if only the next row has the extra criteria value, /* if only the next row has the extra criteria value,
* then add the extra criteria element to current row * then add the extra criteria element to current row
@ -80,10 +78,6 @@ function setSmartPlaylistEvents() {
} else if (next.find('[name^="sp_criteria_extra"]').attr("disabled") != "disabled") { } else if (next.find('[name^="sp_criteria_extra"]').attr("disabled") != "disabled") {
criteria_extra = next.find('[name^="sp_criteria_extra"]').val(); criteria_extra = next.find('[name^="sp_criteria_extra"]').val();
enableAndShowExtraField(curr.find(':first-child'), index); enableAndShowExtraField(curr.find(':first-child'), index);
/*curr.find('[name^="sp_criteria_value"]')
.after($('<input type="text" class="input_text">')
.attr('id', 'sp_criteria_extra_'+index_num)
.attr('name', 'sp_criteria_extra_'+index_num)).after('<span id="sp_criteria_extra_label"> to </span>');*/
curr.find('[name^="sp_criteria_extra"]').val(criteria_extra); curr.find('[name^="sp_criteria_extra"]').val(criteria_extra);
} }
@ -91,27 +85,27 @@ function setSmartPlaylistEvents() {
next = curr.next(); next = curr.next();
} }
list.find('div:visible:last').children().attr('disabled', 'disabled'); /* Disable the last visible row since it holds the values the user removed
list.find('div:visible:last') * Reset the values to empty and resize the criteria value textbox
.find('[name^="sp_criteria"]').val(0).end() * in case the row had the extra criteria textbox
.find('[name^="sp_criteria_modifier"]').val(0).end() */
.find('[name^="sp_criteria_value"]').val(''); item_to_hide = list.find('div:visible:last');
item_to_hide.children().attr('disabled', 'disabled');
item_to_hide.find('[name^="sp_criteria_field"]').val(0).end()
.find('[name^="sp_criteria_modifier"]').val(0).end()
.find('[name^="sp_criteria_value"]').val('');
if (list.find('div:visible:last').children().hasClass('criteria_add')) { if (item_to_hide.children().hasClass('criteria_add')) {
list.find('div:visible:last').find('.criteria_add').remove(); item_to_hide.find('.criteria_add').remove();
} }
list.find('div:visible:last').hide();
sizeTextBoxes(item_to_hide.find('[name^="sp_criteria_value"]'), 'sp_extra_input_text', 'sp_input_text');
item_to_hide.hide();
list.next().show(); list.next().show();
// always put 'add' button on the last row // always put 'add' button on the last row
if (list.find('div:visible').length > 1) { appendAddButton(list);
list.find('div:visible:last').find('a[id^="criteria_remove"]')
.after("<a href='#' id='criteria_add' class='criteria_add'>Add</a>");
} else {
list.find('div:visible:last').find('[name^="sp_criteria_value"]')
.after("<a href='#' id='criteria_add' class='criteria_add'>Add</a>");
}
}); });
form.find('button[id="save_button"]').live("click", function(event){ form.find('button[id="save_button"]').live("click", function(event){
@ -148,12 +142,8 @@ function setSmartPlaylistEvents() {
var index_name = $(this).attr('id'), var index_name = $(this).attr('id'),
index_num = index_name.charAt(index_name.length-1); index_num = index_name.charAt(index_name.length-1);
// disalbe extra field and hide the span // disable extra field and hide the span
disableAndHideExtraField($(this), index_num); disableAndHideExtraField($(this), index_num);
/*if ($('#sp_criteria_extra_'+index_num).length > 0) {
$('#sp_criteria_extra_'+index_num).remove();
$('#sp_criteria_extra_label_'+index_num).remove();
}*/
populateModifierSelect(this); populateModifierSelect(this);
}); });
@ -164,16 +154,8 @@ function setSmartPlaylistEvents() {
if ($(this).val() == 'is in the range') { if ($(this).val() == 'is in the range') {
enableAndShowExtraField(criteria_value, index_num); enableAndShowExtraField(criteria_value, index_num);
/*after($('<input type="text" class="input_text">')
.attr('id', 'sp_criteria_extra_'+index_num)
.attr('name', 'sp_criteria_extra_'+index_num)).after('<span id="sp_criteria_extra_label_'+index_num+'"> to </span>');*/
} else { } else {
disableAndHideExtraField(criteria_value, index_num); disableAndHideExtraField(criteria_value, index_num);
/*if ($('#sp_criteria_extra_'+index_num).length > 0) {
$('#sp_criteria_extra_'+index_num).remove();
$('#sp_criteria_extra_label_'+index_num).remove();
}*/
} }
}); });
@ -183,12 +165,26 @@ function enableAndShowExtraField(valEle, index) {
var spanExtra = valEle.nextAll("#extra_criteria"); var spanExtra = valEle.nextAll("#extra_criteria");
spanExtra.children('#sp_criteria_extra_'+index).removeAttr("disabled"); spanExtra.children('#sp_criteria_extra_'+index).removeAttr("disabled");
spanExtra.show(); spanExtra.show();
//make value input smaller since we have extra element now
var criteria_val = $('#sp_criteria_value_'+index);
sizeTextBoxes(criteria_val, 'sp_input_text', 'sp_extra_input_text');
} }
function disableAndHideExtraField(valEle, index) { function disableAndHideExtraField(valEle, index) {
var spanExtra = valEle.nextAll("#extra_criteria"); var spanExtra = valEle.nextAll("#extra_criteria");
spanExtra.children('#sp_criteria_extra_'+index).val("").attr("disabled", "disabled"); spanExtra.children('#sp_criteria_extra_'+index).val("").attr("disabled", "disabled");
spanExtra.hide(); spanExtra.hide();
//make value input larger because we don't have extra field now
var criteria_value = $('#sp_criteria_value_'+index);
sizeTextBoxes(criteria_value, 'sp_extra_input_text', 'sp_input_text');
}
function sizeTextBoxes(ele, classToRemove, classToAdd) {
if (ele.hasClass(classToRemove)) {
ele.removeClass(classToRemove).addClass(classToAdd);
}
} }
function populateModifierSelect(e) { function populateModifierSelect(e) {
@ -239,6 +235,20 @@ function dynamicCallback(json) {
} }
} }
function appendAddButton(rows) {
var add_button = "<a href='#' id='criteria_add' class='criteria_add'>Add</a>";
if (rows.find('select[name^="sp_criteria_field"]:enabled').length > 1) {
rows.find('select[name^="sp_criteria_field"]:enabled:last')
.siblings('a[id^="criteria_remove"]')
.after(add_button);
} else {
rows.find('select[name^="sp_criteria_field"]:enabled')
.siblings('[name^="sp_criteria_value"]')
.after(add_button);
}
}
var criteriaTypes = { var criteriaTypes = {
0 : "", 0 : "",
"album_title" : "s", "album_title" : "s",