From 00e860183a62f7ff87fca8d3710c6820d2f7afdb Mon Sep 17 00:00:00 2001 From: Codenift Date: Mon, 27 Jan 2020 19:10:54 -0500 Subject: [PATCH] Track Type Select Menu in Smartblock --- .../plugins/PageLayoutInitPlugin.php | 8 ++ .../application/forms/SmartBlockCriteria.php | 87 ++++++++++++++-- .../js/airtime/playlist/smart_blockbuilder.js | 98 ++++++++++++++++++- 3 files changed, 182 insertions(+), 11 deletions(-) diff --git a/airtime_mvc/application/controllers/plugins/PageLayoutInitPlugin.php b/airtime_mvc/application/controllers/plugins/PageLayoutInitPlugin.php index bbb538fdd..37cc9202e 100644 --- a/airtime_mvc/application/controllers/plugins/PageLayoutInitPlugin.php +++ b/airtime_mvc/application/controllers/plugins/PageLayoutInitPlugin.php @@ -141,6 +141,14 @@ class PageLayoutInitPlugin extends Zend_Controller_Plugin_Abstract $view->headScript()->appendScript("var COMPANY_NAME = '" . COMPANY_NAME . "';"); //Each page refresh or tab open has uniqID, not to be used for security $view->headScript()->appendScript("var UNIQID = '" . uniqid() . "';"); + + $track_type_options = array(); + $track_types = Application_Model_Tracktype::getTracktypes(); + foreach ($track_types as $key => $tt) { + $track_type_options[$tt['code']] = $tt['type_name']; + } + $ttarr = json_encode($track_type_options, JSON_FORCE_OBJECT); + $view->headScript()->appendScript("var TRACKTYPES = " . $ttarr . ";"); } protected function _initHeadLink() diff --git a/airtime_mvc/application/forms/SmartBlockCriteria.php b/airtime_mvc/application/forms/SmartBlockCriteria.php index adbcc66b9..e3385bef2 100644 --- a/airtime_mvc/application/forms/SmartBlockCriteria.php +++ b/airtime_mvc/application/forms/SmartBlockCriteria.php @@ -8,6 +8,8 @@ class Application_Form_SmartBlockCriteria extends Zend_Form_SubForm private $timePeriodCriteriaOptions; private $sortOptions; private $limitOptions; + private $isOrNotCriteriaOptions; + private $trackTypeOptions; /* We need to know if the criteria value will be a string * or numeric value in order to populate the modifier @@ -43,7 +45,7 @@ class Application_Form_SmartBlockCriteria extends Zend_Form_SubForm "track_number" => "n", "info_url" => "s", "year" => "n", - "track_type" => "s" + "track_type" => "tt" ); private function getCriteriaOptions($option = null) @@ -174,13 +176,36 @@ class Application_Form_SmartBlockCriteria extends Zend_Form_SubForm "random" => _("Randomly"), "newest" => _("Newest"), "oldest" => _("Oldest"), - "mostrecentplay" => ("Most recently played"), - "leastrecentplay" => ("Least recently played") + "mostrecentplay" => _("Most recently played"), + "leastrecentplay" => _("Least recently played") ); } return $this->sortOptions; } + private function getIsNotOptions() + { + if (!isset($this->isOrNotCriteriaOptions)) { + $this->isOrNotCriteriaOptions = array( + "0" => _("Select modifier"), + "is" => _("is"), + "is not" => _("is not") + ); + } + return $this->isOrNotCriteriaOptions; + } + + private function getTracktypeOptions() + { + if (!isset($this->trackTypeOptions)) { + $tracktypes = Application_Model_Tracktype::getTracktypes(); + $names[] = _("Select Track Type"); + foreach($tracktypes as $arr => $a){ + $names[$a["code"]] = $tracktypes[$arr]["type_name"]; + } + } + return $names; + } public function init() { @@ -339,6 +364,8 @@ class Application_Form_SmartBlockCriteria extends Zend_Form_SubForm $criteriaModifers->setMultiOptions($this->getStringCriteriaOptions()); } elseif ($criteriaType == "d") { $criteriaModifers->setMultiOptions($this->getDateTimeCriteriaOptions()); + } elseif ($criteriaType == "tt") { + $criteriaModifers->setMultiOptions($this->getIsNotOptions()); } else { $criteriaModifers->setMultiOptions($this->getNumericCriteriaOptions()); } @@ -350,11 +377,33 @@ class Application_Form_SmartBlockCriteria extends Zend_Form_SubForm /****************** VALUE ***********/ /* The challenge here is that datetime */ - $criteriaValue = new Zend_Form_Element_Text("sp_criteria_value_" . $i . "_" . $j); - $criteriaValue->setAttrib('class', 'input_text sp_input_text') - ->setDecorators(array('viewHelper')); - if ($i != 0 && !isset($criteriaKeys[$i])) { - $criteriaValue->setAttrib('disabled', 'disabled'); + if (isset($criteriaKeys[$i])) { + $modifierTest = (string)$storedCrit["crit"][$criteriaKeys[$i]][$j]["modifier"]; + if (isset($criteriaType) && $criteriaType == "tt" && + preg_match('/is|is not/', $modifierTest) == 1) { + + $criteriaValue = new Zend_Form_Element_Select("sp_criteria_value_" . $i . "_" . $j); + $criteriaValue->setAttrib('class', 'input_select sp_input_select')->setDecorators(array('viewHelper')); + + if (isset($criteriaKeys[$i]) ) { //do if $relativeTT above + $criteriaValue->setAttrib('enabled', 'enabled'); + } else { + $criteriaValue->setAttrib('disabled', 'disabled'); + } + } else { + $criteriaValue = new Zend_Form_Element_Text("sp_criteria_value_" . $i . "_" . $j); + $criteriaValue->setAttrib('class', 'input_text sp_input_text')->setDecorators(array('viewHelper')); + if ($i != 0 && !isset($criteriaKeys[$i])) { + $criteriaValue->setAttrib('disabled', 'disabled'); + } + } + } else { + + $criteriaValue = new Zend_Form_Element_Text("sp_criteria_value_" . $i . "_" . $j); + $criteriaValue->setAttrib('class', 'input_text sp_input_text')->setDecorators(array('viewHelper')); + if ($i != 0 && !isset($criteriaKeys[$i])) { + $criteriaValue->setAttrib('disabled', 'disabled'); + } } if (isset($criteriaKeys[$i])) { /* @@ -367,8 +416,24 @@ class Application_Form_SmartBlockCriteria extends Zend_Form_SubForm preg_match('/before|after|between/', $modifierTest) == 1) { // set relativeDatetime boolean to true so that the datetime select is displayed below $relativeDateTime = true; - // the criteria value will be a number followed by time unit and ago so set input to number part $criteriaValue->setValue(filter_var($storedCrit["crit"][$criteriaKeys[$i]][$j]["value"], FILTER_SANITIZE_NUMBER_INT)); + } elseif (isset($criteriaType) && $criteriaType == "tt" && + preg_match('/is|is not/', $modifierTest) == 1) { + // set relativeDatetime boolean to true so that the datetime select is displayed below + $relativeDateTime = false; + + $tracktypeSelectValue = preg_replace('/[0-9]+/', '', $storedCrit["crit"][$criteriaKeys[$i]][$j]["value"]); + $tracktypeSelectValue = trim(preg_replace('/\W\w+\s*(\W*)$/', '$1', $tracktypeSelectValue)); + $criteriaValue->setMultiOptions($this->getTracktypeOptions()); + + if ($storedCrit["crit"][$criteriaKeys[$i]][$j]["value"]){ + $criteriaValue->setValue($storedCrit["crit"][$criteriaKeys[$i]][$j]["value"]); + } else { + $criteriaValue->setMultiOptions(array('0' => _('Select track type'))); + $criteriaValue->setMultiOptions($this->getTracktypeOptions()); + $criteriaValue->setValue($tracktypeSelectValue); + } + $criteriaValue->setAttrib('enabled', 'enabled'); } else { $criteriaValue->setValue($storedCrit["crit"][$criteriaKeys[$i]][$j]["value"]); } @@ -554,6 +619,8 @@ class Application_Form_SmartBlockCriteria extends Zend_Form_SubForm $eleMod->setMultiOptions($this->getNumericCriteriaOptions()); } elseif ($criteriaType == "d") { $eleMod->setMultiOptions($this->getDateTimeCriteriaOptions()); + } elseif ($criteriaType == "tt") { + $eleMod->setMultiOptions($this->getIsNotOptions()); } else { $eleMod->setMultiOptions(array('0' => _('Select modifier'))); } @@ -613,6 +680,8 @@ class Application_Form_SmartBlockCriteria extends Zend_Form_SubForm } elseif ($criteriaType == "d") { $criteriaModifers->setMultiOptions($this->getDateTimeCriteriaOptions()); + } elseif ($criteriaType == "tt") { + $criteriaModifers->setMultiOptions($this->getIsNotOptions()); } else { $criteriaModifers->setMultiOptions(array('0' => _('Select modifier'))); } diff --git a/airtime_mvc/public/js/airtime/playlist/smart_blockbuilder.js b/airtime_mvc/public/js/airtime/playlist/smart_blockbuilder.js index abea3a184..a6b0e9fb1 100644 --- a/airtime_mvc/public/js/airtime/playlist/smart_blockbuilder.js +++ b/airtime_mvc/public/js/airtime/playlist/smart_blockbuilder.js @@ -339,7 +339,20 @@ function setSmartBlockEvents() { disableAndHideExtraField($(this), index); disableAndHideDateTimeDropdown($(this), index); disableAndHideExtraDateTimeDropdown($(this),index); - populateModifierSelect(this, true); + + // disable extra field and hide the span + disableAndHideExtraField($(this), index); + disableAndHideDateTimeDropdown($(this), index); + disableAndHideExtraDateTimeDropdown($(this),index); + + if ($( "#sp_criteria_field_" + index +" option:selected" ).val() === 'track_type') { + //console.log("Track Type is set); + populateTracktypeSelect(this, false); + } else { + //console.log("Track Type is not set"); + populateModifierSelect(this, true); + } + }); /********** MODIFIER CHANGE **********/ @@ -370,6 +383,30 @@ function setSmartBlockEvents() { else { disableAndHideExtraDateTimeDropdown(criteria_value,index_num); } + + var get_crit_field = $(this).siblings(':first-child'); + var crit_field = get_crit_field[0]["id"]; + if ($( "#" + crit_field +" option:selected" ).val() === 'track_type') { + + if ($(this).val() == "is" || $(this).val() == "is not") { + console.log("Criteria is set to Track Type, Modifier uses (stringIsNotOptions)"); + enableAndShowTracktypeDropdown(criteria_value, index_num); + } else { + console.log("Criteria is set to Track Type, Modifier not using (stringIsNotOptions)"); + disableAndHideTracktypeDropdown(criteria_value, index_num); + } + //populateTracktypeSelect(this, true); + } + if ($( "#" + crit_field +" option:selected" ).val() !== 'track_type') { + //disableAndHideTracktypeDropdown(criteria_value, index_num); + //populateTracktypeSelect(this, false); + console.log("Criteria is not set to Track Type"); + } + }); + + /********** VALUE CHANGE (if value field is a select menu) **********/ + form.find('select[id^="sp_criteria_value"]').live("change", function(){ + console.log("VALUE CHANGED"); }); setupUI(); @@ -451,6 +488,8 @@ function reindexElements() { $(div).find('select[name^="sp_criteria_modifier"]').attr('id', 'sp_criteria_modifier_'+index+'_'+modIndex); $(div).find('input[name^="sp_criteria_value"]').attr('name', 'sp_criteria_value_'+index+'_'+modIndex); $(div).find('input[name^="sp_criteria_value"]').attr('id', 'sp_criteria_value_'+index+'_'+modIndex); + $(div).find('select[name^="sp_criteria_value"]').attr('name', 'sp_criteria_value_'+index+'_'+modIndex); + $(div).find('select[name^="sp_criteria_value"]').attr('id', 'sp_criteria_value_'+index+'_'+modIndex); $(div).find('input[name^="sp_criteria_extra"]').attr('name', 'sp_criteria_extra_'+index+'_'+modIndex); $(div).find('input[name^="sp_criteria_extra"]').attr('id', 'sp_criteria_extra_'+index+'_'+modIndex); $(div).find('a[name^="modifier_add"]').attr('id', 'modifier_add_'+index); @@ -586,6 +625,19 @@ function setupUI() { }); } +function enableAndShowTracktypeDropdown(valEle, index) { + //console.log("enable tracktype:"+index); + $("#sp_criteria_value_"+index).replaceWith(''); + $.each(stringTracktypeOptions, function(key, value){ + $("#sp_criteria_value_"+index).append($('').attr('value', key).text(value)); + }); +} + +function disableAndHideTracktypeDropdown(valEle, index) { + //console.log("disable tracktype:"+index); + $("#sp_criteria_value_"+index).replaceWith(''); +} + /* Utilizing jQuery this function finds the #datetime_select element on the given row * and shows the criteria drop-down */ @@ -682,6 +734,7 @@ function sizeTextBoxes(ele, classToRemove, classToAdd) { } function populateModifierSelect(e, popAllMods) { + var criteria_type = getCriteriaOptionType(e), index = getRowIndex($(e).parent()), divs; @@ -708,6 +761,13 @@ function populateModifierSelect(e, popAllMods) { .text(value)); }); } + else if(criteria_type == 'tt') { + $.each(stringIsNotOptions, function(key, value){ + $(div).append($('') + .attr('value', key) + .text(value)); + }); + } else { $.each(numericCriteriaOptions, function(key, value){ $(div).append($('') @@ -718,11 +778,37 @@ function populateModifierSelect(e, popAllMods) { }); } +function populateTracktypeSelect(e, popAllMods) { + + var criteria_type = getTracktype(e), + index = getRowIndex($(e).parent()), + divs; + + if (popAllMods) { + index = index.substring(0, 1); + } + divs = $(e).parents().find('select[id^="sp_criteria_modifier_'+index+'"]'); + $.each(divs, function(i, div){ + $(div).children().remove(); + $.each(stringIsNotOptions, function(key, value){ + $(div).append($('') + .attr('value', key) + .text(value)); + }); + + }); +} + function getCriteriaOptionType(e) { var criteria = $(e).val(); return criteriaTypes[criteria]; } +function getTracktype(e) { + var type = $(e).val(); + return stringTracktypeOptions[type]; +} + function callback(json, type) { var dt = $('table[id="library_display"]').dataTable(), form = $('.active-tab .smart-block-form'); @@ -875,7 +961,7 @@ var criteriaTypes = { "track_number" : "n", "info_url" : "s", "year" : "n", - "track_type" : "s" + "track_type" : "tt" }; var stringCriteriaOptions = { @@ -908,3 +994,11 @@ var dateTimeCriteriaOptions = { "is less than" : $.i18n._("is less than"), "is in the range" : $.i18n._("is in the range") }; + +var stringIsNotOptions = { + "0" : $.i18n._("Select modifier"), + "is" : $.i18n._("is"), + "is not" : $.i18n._("is not") +}; + +var stringTracktypeOptions = TRACKTYPES; \ No newline at end of file