From 27cedb985f9e7ba86bd9069eacef9e715d6bfb21 Mon Sep 17 00:00:00 2001 From: eileen Date: Sat, 2 Mar 2019 18:48:47 +1300 Subject: [PATCH] Convert activity_date_time field to datepicker and add support for url input This converts the form to being a metadata defined field & a date-picker field as well as adding standardised url support for input. The input format is activity_date_time_high=20100101 I stripped out the complex and probably broken based on feedback & not used in core existing hard-coded url support. Note that there are some outstanding items 1) the upgrade script item needs to be added - this includes dealing with renaming of the field since I made it more consistent 2) I stripped out the check to ensure the high date is greater than the low date - if we still want this we should re-add as a generic thing. My guess is it's a bit optional --- CRM/Activity/BAO/Query.php | 31 +++---------------- CRM/Activity/Form/Search.php | 25 --------------- CRM/Contact/BAO/Query.php | 2 ++ CRM/Core/Form.php | 6 ++-- CRM/Core/Form/Search.php | 18 +++++++++-- templates/CRM/Activity/Form/Search/Common.tpl | 7 ++--- 6 files changed, 28 insertions(+), 61 deletions(-) diff --git a/CRM/Activity/BAO/Query.php b/CRM/Activity/BAO/Query.php index 1bba431fa2..3477ffef6a 100644 --- a/CRM/Activity/BAO/Query.php +++ b/CRM/Activity/BAO/Query.php @@ -288,8 +288,10 @@ class CRM_Activity_BAO_Query { case 'activity_date': case 'activity_date_low': case 'activity_date_high': + case 'activity_date_time_low': + case 'activity_date_time_high': $query->dateQueryBuilder($values, - 'civicrm_activity', 'activity_date', 'activity_date_time', ts('Activity Date') + 'civicrm_activity', str_replace(['_high', '_low'], '', $name), 'activity_date_time', ts('Activity Date') ); break; @@ -440,7 +442,7 @@ class CRM_Activity_BAO_Query { * rather than a static function. */ public static function getSearchFieldMetadata() { - $fields = ['activity_type_id']; + $fields = ['activity_type_id', 'activity_date_time']; $metadata = civicrm_api3('Activity', 'getfields', [])['values']; return array_intersect_key($metadata, array_flip($fields)); } @@ -454,10 +456,6 @@ class CRM_Activity_BAO_Query { $form->addSearchFieldMetadata(['Activity' => self::getSearchFieldMetadata()]); $form->addFormFieldsFromMetadata(); - CRM_Core_Form_Date::buildDateRange($form, 'activity_date', 1, '_low', '_high', ts('From'), FALSE, FALSE); - $form->addElement('hidden', 'activity_date_range_error'); - $form->addFormRule(array('CRM_Activity_BAO_Query', 'formRule'), $form); - $followUpActivity = array( 1 => ts('Yes'), 2 => ts('No'), @@ -631,27 +629,6 @@ class CRM_Activity_BAO_Query { return $properties; } - /** - * Custom form rules. - * - * @param array $fields - * @param array $files - * @param CRM_Core_Form $form - * - * @return bool|array - */ - public static function formRule($fields, $files, $form) { - $errors = array(); - - if (empty($fields['activity_date_low']) || empty($fields['activity_date_high'])) { - return TRUE; - } - - CRM_Utils_Rule::validDateRange($fields, 'activity_date', $errors, ts('Activity Date')); - - return empty($errors) ? TRUE : $errors; - } - /** * Where/qill clause for notes * diff --git a/CRM/Activity/Form/Search.php b/CRM/Activity/Form/Search.php index 93a2ae631c..d598dab992 100644 --- a/CRM/Activity/Form/Search.php +++ b/CRM/Activity/Form/Search.php @@ -322,31 +322,6 @@ class CRM_Activity_Form_Search extends CRM_Core_Form_Search { } } - $dateLow = CRM_Utils_Request::retrieve('dateLow', 'String'); - - if ($dateLow) { - $dateLow = date('m/d/Y', strtotime($dateLow)); - $this->_formValues['activity_date_relative'] = 0; - $this->_defaults['activity_date_relative'] = 0; - $this->_formValues['activity_date_low'] = $dateLow; - $this->_defaults['activity_date_low'] = $dateLow; - } - - $dateHigh = CRM_Utils_Request::retrieve('dateHigh', 'String'); - - if ($dateHigh) { - // Activity date time assumes midnight at the beginning of the date - // This sets it to almost midnight at the end of the date - /* if ($dateHigh <= 99999999) { - $dateHigh = 1000000 * $dateHigh + 235959; - } */ - $dateHigh = date('m/d/Y', strtotime($dateHigh)); - $this->_formValues['activity_date_relative'] = 0; - $this->_defaults['activity_date_relative'] = 0; - $this->_formValues['activity_date_high'] = $dateHigh; - $this->_defaults['activity_date_high'] = $dateHigh; - } - // Enable search activity by custom value // @todo this is not good security practice. Instead define entity fields in metadata & // use getEntity Defaults diff --git a/CRM/Contact/BAO/Query.php b/CRM/Contact/BAO/Query.php index be480e7e77..085b4dfc52 100644 --- a/CRM/Contact/BAO/Query.php +++ b/CRM/Contact/BAO/Query.php @@ -1899,6 +1899,8 @@ class CRM_Contact_BAO_Query { case 'activity_date': case 'activity_date_low': case 'activity_date_high': + case 'activity_date_time_low': + case 'activity_date_time_high': case 'activity_role': case 'activity_status_id': case 'activity_status': diff --git a/CRM/Core/Form.php b/CRM/Core/Form.php index 597e543788..a85cbb69dd 100644 --- a/CRM/Core/Form.php +++ b/CRM/Core/Form.php @@ -1295,11 +1295,13 @@ class CRM_Core_Form extends HTML_QuickForm_Page { * * @param string $fieldName * @param string $label + * @param bool $isDateTime + * Is this a date-time field (not just date). * @param bool $required * @param string $fromLabel * @param string $toLabel */ - public function addDatePickerRange($fieldName, $label, $required = FALSE, $fromLabel = 'From', $toLabel = 'To') { + public function addDatePickerRange($fieldName, $label, $isDateTime = FALSE, $required = FALSE, $fromLabel = 'From', $toLabel = 'To') { $options = array( '' => ts('- any -'), @@ -1314,7 +1316,7 @@ class CRM_Core_Form extends HTML_QuickForm_Page { NULL ); $attributes = ['format' => 'searchDate']; - $extra = ['time' => FALSE]; + $extra = ['time' => $isDateTime]; $this->add('datepicker', $fieldName . '_low', ts($fromLabel), $attributes, $required, $extra); $this->add('datepicker', $fieldName . '_high', ts($toLabel), $attributes, $required, $extra); } diff --git a/CRM/Core/Form/Search.php b/CRM/Core/Form/Search.php index 5d4ed98a0a..f793ae6f3a 100644 --- a/CRM/Core/Form/Search.php +++ b/CRM/Core/Form/Search.php @@ -154,9 +154,8 @@ class CRM_Core_Form_Search extends CRM_Core_Form { $this->_action = CRM_Core_Action::ADVANCED; foreach ($this->getSearchFieldMetadata() as $entity => $fields) { foreach ($fields as $fieldName => $fieldSpec) { - if ($fieldSpec['type'] === CRM_Utils_Type::T_DATE) { - // Assuming time is false for now as we are not checking for date-time fields as yet. - $this->addDatePickerRange($fieldName, $fieldSpec['title'], FALSE); + if ($fieldSpec['type'] === CRM_Utils_Type::T_DATE || $fieldSpec['type'] === (CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME)) { + $this->addDatePickerRange($fieldName, $fieldSpec['title'], ($fieldSpec['type'] === (CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME))); } else { $this->addField($fieldName, ['entity' => $entity]); @@ -184,6 +183,10 @@ class CRM_Core_Form_Search extends CRM_Core_Form { case CRM_Utils_Type::T_INT: return 'CommaSeparatedIntegers'; + case CRM_Utils_Type::T_DATE: + case CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME: + return 'Timestamp'; + default: return 'Alphanumeric'; } @@ -204,6 +207,15 @@ class CRM_Core_Form_Search extends CRM_Core_Form { if ($value !== FALSE) { $defaults[$fieldSpec['name']] = $value; } + if ($fieldSpec['type'] === CRM_Utils_Type::T_DATE || ($fieldSpec['type'] === CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME)) { + $low = CRM_Utils_Request::retrieveValue($fieldSpec['name'] . '_low', 'Timestamp', FALSE, NULL, 'GET'); + $high = CRM_Utils_Request::retrieveValue($fieldSpec['name'] . '_high', 'Timestamp', FALSE, NULL, 'GET'); + if ($low !== FALSE || $high !== FALSE) { + $defaults[$fieldSpec['name'] . '_relative'] = 0; + $defaults[$fieldSpec['name'] . '_low'] = $low ? date('Y-m-d H:i:s', strtotime($low)) : NULL; + $defaults[$fieldSpec['name'] . '_high'] = $high ? date('Y-m-d H:i:s', strtotime($high)) : NULL; + } + } } } return $defaults; diff --git a/templates/CRM/Activity/Form/Search/Common.tpl b/templates/CRM/Activity/Form/Search/Common.tpl index b260c49bb2..9df96580da 100644 --- a/templates/CRM/Activity/Form/Search/Common.tpl +++ b/templates/CRM/Activity/Form/Search/Common.tpl @@ -89,10 +89,9 @@ - - - - {include file="CRM/Core/DateRange.tpl" fieldName="activity_date" from='_low' to='_high'} + + {include file="CRM/Core/DatePickerRange.tpl" fieldName="activity_date_time" from='_low' to='_high'} + -- 2.25.1