working relative dates with drop down selects

This commit is contained in:
Robb Ebright 2017-10-14 01:00:06 -04:00
parent 6c6b33b553
commit 311ecd393d
4 changed files with 162 additions and 40 deletions

View File

@ -192,21 +192,23 @@ class Application_Form_SmartBlockCriteria extends Zend_Form_SubForm
if (isset($criteria[$column])) {
foreach ($criteria[$column] as &$constraint) {
$constraint['value'] =
Application_Common_DateHelper::UTCStringToUserTimezoneString($constraint['value']);
if (isset($constraint['extra'])) {
$constraint['extra'] =
Application_Common_DateHelper::UTCStringToUserTimezoneString($constraint['extra']);
}
// convert to appropriate timezone timestamps only if the modifier is not a relative time
if (!in_array($constraint['modifier'], array('before','after','between'))) {
$constraint['value'] =
Application_Common_DateHelper::UTCStringToUserTimezoneString($constraint['value']);
if (isset($constraint['extra'])) {
$constraint['extra'] =
Application_Common_DateHelper::UTCStringToUserTimezoneString($constraint['extra']);
}
}
}
}
}
}
/*
* This function takes a blockID as param and creates the datastructure for the form displayed with the view
* This function takes a blockID as param and creates the data structure for the form displayed with the view
* smart-block-criteria.phtml
*
* A description of the dataflow. First it loads the block and determines if it is a static or dynamic smartblock.
@ -334,6 +336,7 @@ class Application_Form_SmartBlockCriteria extends Zend_Form_SubForm
$this->addElement($criteriaModifers);
/****************** 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'));
@ -341,7 +344,17 @@ class Application_Form_SmartBlockCriteria extends Zend_Form_SubForm
$criteriaValue->setAttrib('disabled', 'disabled');
}
if (isset($criteriaKeys[$i])) {
$criteriaValue->setValue($storedCrit["crit"][$criteriaKeys[$i]][$j]["value"]);
/*
* Need to parse relative dates in a special way to populate select box down below
*/
$modifierTest = (string)$storedCrit["crit"][$criteriaKeys[$i]][$j]["modifier"];
if(isset($criteriaType) && $criteriaType == "d" &&
preg_match('/before|after|between/', $modifierTest) == 1) {
// 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));
} else {
$criteriaValue->setValue($storedCrit["crit"][$criteriaKeys[$i]][$j]["value"]);
}
}
$this->addElement($criteriaValue);
@ -353,18 +366,24 @@ class Application_Form_SmartBlockCriteria extends Zend_Form_SubForm
if ($i != 0 && !isset($criteriaKeys[$i])) {
$criteriaDatetimeSelect->setAttrib('disabled', 'disabled');
}
// need to determine how this is stored in the database if using plaintext need to parse the value
/* @todo figure this out */
if (isset($criteriaKeys[$i]) && isset($storedCrit["crit"][$criteriaKeys[$i]][$j]["extra"])) {
$criteriaDatetimeSelect->setValue($storedCrit["crit"][$criteriaKeys[$i]][$j]["extra"]);
$criteriaDatetimeSelect->setAttrib('class', 'input_text sp_datetime_input_select');
} else {
$criteriaDatetimeSelect->setAttrib('disabled', 'disabled');
// check if the value is stored and it is a relative datetime field
if (isset($criteriaKeys[$i]) && isset($storedCrit["crit"][$criteriaKeys[$i]][$j]["value"])
&& isset($criteriaType) && $criteriaType == "d" &&
preg_match('/before|after|between/', $modifierTest) == 1) {
// need to remove the leading numbers stored in the database
$dateTimeSelectValue = preg_replace('/[0-9]+/', '', $storedCrit["crit"][$criteriaKeys[$i]][$j]["value"]);
// need to strip white from front and ago from the end to match with the value of the time unit select dropdown
$dateTimeSelectValue = trim(preg_replace('/\W\w+\s*(\W*)$/', '$1', $dateTimeSelectValue));
$criteriaDatetimeSelect->setMultiOptions($this->getTimePeriodCriteriaOptions());
$criteriaDatetimeSelect->setValue($dateTimeSelectValue);
$criteriaDatetimeSelect->setAttrib('enabled', 'enabled');
}
else {
$criteriaDatetimeSelect->setMultiOptions(array('0' => _('Select unit of time')));
$criteriaDatetimeSelect->setMultiOptions($this->getTimePeriodCriteriaOptions());
}
/* TODO FIX THIS */
$criteriaDatetimeSelect->setMultiOptions($this->getTimePeriodCriteriaOptions());
// $criteriaDatetimeSelect->setMultiOptions(array('0' => _('Select modifier')));
$this->addElement($criteriaDatetimeSelect);
/****************** EXTRA ***********/
@ -372,13 +391,42 @@ class Application_Form_SmartBlockCriteria extends Zend_Form_SubForm
$criteriaExtra->setAttrib('class', 'input_text sp_extra_input_text')
->setDecorators(array('viewHelper'));
if (isset($criteriaKeys[$i]) && isset($storedCrit["crit"][$criteriaKeys[$i]][$j]["extra"])) {
$criteriaExtra->setValue($storedCrit["crit"][$criteriaKeys[$i]][$j]["extra"]);
// need to check if this is a relative date time value
if(isset($criteriaType) && $criteriaType == "d" && $modifierTest == 'between') {
// the criteria value will be a number followed by time unit and ago so set input to number part
$criteriaExtra->setValue(filter_var($storedCrit["crit"][$criteriaKeys[$i]][$j]["extra"], FILTER_SANITIZE_NUMBER_INT));
}
else {
$criteriaExtra->setValue($storedCrit["crit"][$criteriaKeys[$i]][$j]["extra"]);
}
$criteriaValue->setAttrib('class', 'input_text sp_extra_input_text');
} else {
$criteriaExtra->setAttrib('disabled', 'disabled');
}
$this->addElement($criteriaExtra);
/****************** DATETIME SELECT EXTRA **********/
$criteriaExtraDatetimeSelect = new Zend_Form_Element_Select("sp_criteria_extra_datetime_select_".$i."_".$j);
$criteriaExtraDatetimeSelect->setAttrib('class','input_select sp_input_select')
->setDecorators(array('viewHelper'));
if (isset($criteriaKeys[$i]) && isset($storedCrit["crit"][$criteriaKeys[$i]][$j]["extra"])
&& $modifierTest == 'between') {
// need to remove the leading numbers stored in the database
$extraDateTimeSelectValue = preg_replace('/[0-9]+/', '', $storedCrit["crit"][$criteriaKeys[$i]][$j]["extra"]);
// need to strip white from front and ago from the end to match with the value of the time unit select dropdown
$extraDateTimeSelectValue = trim(preg_replace('/\W\w+\s*(\W*)$/', '$1', $extraDateTimeSelectValue));
$criteriaExtraDatetimeSelect->setMultiOptions($this->getTimePeriodCriteriaOptions());
Logging::info('THIS IS-'.$extraDateTimeSelectValue.'-IT');
$criteriaExtraDatetimeSelect->setValue($extraDateTimeSelectValue);
$criteriaExtraDatetimeSelect->setAttrib('enabled', 'enabled');
} else {
$criteriaExtraDatetimeSelect->setMultiOptions(array('0' => _('Select unit of time')));
$criteriaExtraDatetimeSelect->setMultiOptions($this->getTimePeriodCriteriaOptions());
$criteriaExtraDatetimeSelect->setAttrib('disabled', 'disabled');
}
$this->addElement($criteriaExtraDatetimeSelect);
}//for
}//for
@ -657,7 +705,8 @@ class Application_Form_SmartBlockCriteria extends Zend_Form_SubForm
// this looks up the column type for the criteria the modified time, upload time etc.
} elseif ($column->getType() == PropelColumnTypes::TIMESTAMP) {
// need to check for relative modifiers first - bypassing currently
if (in_array($d['sp_criteria_modifier'], array('before','ago','between'))) {
if (in_array($d['sp_criteria_modifier'], array('before','after','between'))) {
// TODO validate this on numeric input with whatever parsing also do for extra
//if the modifier is before ago or between we skip validation until we confirm format
}
else {

View File

@ -1191,22 +1191,30 @@ SQL;
{
// delete criteria under $p_blockId
CcBlockcriteriaQuery::create()->findByDbBlockId($this->id)->delete();
//Logging::info($p_criteriaData);
// Logging::info($p_criteriaData);
//insert modifier rows
if (isset($p_criteriaData['criteria'])) {
$critKeys = array_keys($p_criteriaData['criteria']);
for ($i = 0; $i < count($critKeys); $i++) {
foreach ($p_criteriaData['criteria'][$critKeys[$i]] as $d) {
Logging::info($d);
$field = $d['sp_criteria_field'];
$value = $d['sp_criteria_value'];
$modifier = $d['sp_criteria_modifier'];
if (isset($d['sp_criteria_extra'])) { $extra = $d['sp_criteria_extra']; }
$datetimeunit = $d['sp_criteria_datetime_select'];
if (isset($d['sp_criteria_extra_datetime_select'])) {$extradatetimeunit = $d['sp_criteria_extra_datetime_select'];}
if ($field == 'utime' || $field == 'mtime' || $field == 'lptime') {
// if the date isn't relative we want to convert the value to a specific UTC date
if (!in_array($modifier,array('before','ago','between'))) {
if (!(in_array($modifier,array('before','after','between')))) {
$value = Application_Common_DateHelper::UserTimezoneStringToUTCString($value);
}
else {
$value = $value . ' ' . $datetimeunit . ' ago';
// Logging::info($value);
}
}
$qry = new CcBlockcriteria();
@ -1218,10 +1226,17 @@ SQL;
if (isset($d['sp_criteria_extra'])) {
if ($field == 'utime' || $field == 'mtime' || $field == 'lptime') {
$d['sp_criteria_extra'] = Application_Common_DateHelper::UserTimezoneStringToUTCString($d['sp_criteria_extra']);
// if the date isn't relative we want to convert the value to a specific UTC date
if (!(in_array($modifier,array('before','after','between')))) {
$extra = Application_Common_DateHelper::UserTimezoneStringToUTCString($extra);
}
else {
$extra = $extra . ' ' . $extradatetimeunit. ' ago';
}
}
$qry->setDbExtra($d['sp_criteria_extra']);
$qry->setDbExtra($extra);
}
$qry->save();
}
@ -1611,7 +1626,7 @@ SQL;
}
public static function organizeSmartPlaylistCriteria($p_criteria)
{
$fieldNames = array('sp_criteria_field', 'sp_criteria_modifier', 'sp_criteria_value', 'sp_criteria_extra');
$fieldNames = array('sp_criteria_field', 'sp_criteria_modifier', 'sp_criteria_value', 'sp_criteria_extra', 'sp_criteria_datetime_select', 'sp_criteria_extra_datetime_select');
$output = array();
foreach ($p_criteria as $ele) {

View File

@ -103,6 +103,8 @@
</a>
<span class='sp_text_font' id="extra_criteria" <?php echo $this->element->getElement("sp_criteria_extra_".$i."_".$j)->getAttrib("disabled") == "disabled"?'style="display:none;"':""?>><?php echo _(" to "); ?><?php echo $this->element->getElement('sp_criteria_extra_'.$i."_".$j) ?></span>
<span class='sp_text_font' id="extra_datetime_select" <?php echo $this->element->getElement("sp_criteria_extra_datetime_select_".$i."_".$j)->getAttrib("disabled") == "disabled"?'style="display:none;"':""?>><?php echo $this->element->getElement('sp_criteria_extra_datetime_select_'.$i."_".$j) ?><?php echo _(" ago "); ?></span>
<a style='margin-right:3px' class='btn btn-small btn-danger' id='criteria_remove_<?php echo $i ?>'>
<i class='icon-white icon-remove spl-no-r-margin'></i>
</a>

View File

@ -102,6 +102,8 @@ function setSmartBlockEvents() {
if (count == 0) {
disableAndHideDateTimeDropdown(curr.find(':first-child'), index);
disableAndHideExtraDateTimeDropdown(curr.find(':first-child'),index);
disableAndHideExtraField(curr.find(':first-child'),index);
}
/* assign next row to current row for all rows below and including
@ -178,6 +180,29 @@ function setSmartBlockEvents() {
curr.find('[name^="sp_criteria_datetime_select"]').val(criteria_datetime);
}
/* if current and next row have the extra_date_time_select_criteria visible
* then show the current and it from the next row
*/
if (curr.find('[name^="sp_criteria_extra_datetime_select"]').attr("disabled") != "disabled"
&& next.find('#extra_datetime_select').is(':visible')) {
var extra_criteria_datetime = next.find('[name^="sp_criteria_extra_datetime_select"]').val();
curr.find('[name^="sp_criteria_extra_datetime_select"]').val(extra_criteria_datetime);
disableAndHideExtraDateTimeDropdown(next.find('first-child'), getRowIndex(next));
/* if only the current row has the extra criteria value,
* then just remove the current row's extra criteria element
*/
} else if (curr.find('[name^="sp_criteria_extra_datetime_select"]').attr("disabled") != "disabled"
&& next.find('#extra_datetime_select').not(':visible')) {
disableAndHideExtraDateTimeDropdown(curr.find(':first-child'), index);
/* if only the next row has date_time_select then just enable it on the current row
*/
} else if (next.find('#datetime_select').is(':visible')) {
criteria_datetime = next.find('[name^="sp_criteria_extra_datetime_select"]').val();
enableAndShowExtraDateTimeDropdown(curr.find(':first-child'), index);
curr.find('[name^="sp_criteria_extra_datetime_select"]').val(criteria_datetime);
}
/* determine if current row is a modifier row
* if it is, make the criteria select invisible
*/
@ -286,29 +311,29 @@ function setSmartBlockEvents() {
var criteria_value = $(this).next(),
index_num = getRowIndex($(this).parent());
if ($(this).val().match('before|after|between')) {
if ($(this).val().match('before|after')) {
enableAndShowDateTimeDropdown(criteria_value, index_num);
console.log($(this).val());
if ($(this).val() == 'between') {
console.log('yah')
enableAndShowExtraField(criteria_value,index_num);
}
else {
console.log('huh')
disableAndHideExtraField(criteria_value, index_num);
}
}
else {
disableAndHideDateTimeDropdown(criteria_value, index_num);
disableAndHideExtraDateTimeDropdown(criteria_value,index_num);
}
if ($(this).val() == 'is in the range') {
if ($(this).val().match('is in the range')) {
enableAndShowExtraField(criteria_value, index_num);
} else {
disableAndHideExtraField(criteria_value, index_num);
}
if ($(this).val().match('between')) {
enableAndShowExtraField(criteria_value, index_num);
enableAndShowDateTimeDropdown(criteria_value,index_num);
enableAndShowExtraDateTimeDropdown(criteria_value,index_num);
}
else {
disableAndHideExtraDateTimeDropdown(criteria_value,index_num);
}
});
setupUI();
@ -498,8 +523,10 @@ function setupUI() {
* and shows the criteria drop-down
*/
function enableAndShowDateTimeDropdown(valEle, index) {
console.log('datetime show');
var spanDatetime = valEle.nextAll("#datetime_select");
spanDatetime.children('#sp_criteria_datetime_select_'+index).removeAttr("disabled");
spanDatetime.children('#sp_criteria_extra_datetime_select_'+index).removeAttr("disabled");
spanDatetime.show();
//make value input smaller since we have extra element now
@ -513,6 +540,7 @@ function enableAndShowDateTimeDropdown(valEle, index) {
*/
function disableAndHideDateTimeDropdown(valEle, index) {
console.log('datetime hide');
var spanDatetime = valEle.nextAll("#datetime_select");
spanDatetime.children('#sp_criteria_datetime_select_'+index).val("").attr("disabled", "disabled");
spanDatetime.hide();
@ -522,10 +550,37 @@ function disableAndHideDateTimeDropdown(valEle, index) {
sizeTextBoxes(criteria_value, 'sp_extra_input_text', 'sp_input_text');
}
/* Utilizing jQuery this function finds the #datetime_select element on the given row
* and shows the criteria drop-down
*/
function enableAndShowExtraDateTimeDropdown(valEle, index) {
console.log('datetime show');
var spanDatetime = valEle.nextAll("#extra_datetime_select");
spanDatetime.children('#sp_criteria_extra_datetime_select_'+index).removeAttr("disabled");
spanDatetime.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');
}
/* Utilizing jQuery this function finds the #datetime_select element on the given row
* and hides the datetime criteria drop-down
*/
function disableAndHideExtraDateTimeDropdown(valEle, index) {
console.log('datetime hide');
var spanDatetime = valEle.nextAll("#extra_datetime_select");
spanDatetime.children('#sp_criteria_extra_datetime_select_'+index).val("").attr("disabled", "disabled");
spanDatetime.hide();
//make value input larger since 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 enableAndShowExtraField(valEle, index) {
var spanExtra = valEle.nextAll("#extra_criteria");
console.log(spanExtra);
console.log('shown');
spanExtra.children('#sp_criteria_extra_'+index).removeAttr("disabled");
spanExtra.show();
@ -538,6 +593,7 @@ function disableAndHideExtraField(valEle, index) {
var spanExtra = valEle.nextAll("#extra_criteria");
spanExtra.children('#sp_criteria_extra_'+index).val("").attr("disabled", "disabled");
spanExtra.hide();
console.log('hidden');
//make value input larger since we don't have extra field now
var criteria_value = $('#sp_criteria_value_'+index);