From e2123607899657ec2aadc948ec782d168bc228f7 Mon Sep 17 00:00:00 2001 From: eileen Date: Tue, 4 Dec 2018 23:15:20 +1300 Subject: [PATCH] Migrate to a datepicker range --- CRM/Core/Form.php | 29 +++++++++++ CRM/Core/Form/Search.php | 3 +- CRM/Grant/BAO/Query.php | 14 ++++-- templates/CRM/Core/DatePickerRange.tpl | 58 ++++++++++++++++++++++ templates/CRM/Grant/Form/Search/Common.tpl | 51 ++++--------------- 5 files changed, 108 insertions(+), 47 deletions(-) create mode 100644 templates/CRM/Core/DatePickerRange.tpl diff --git a/CRM/Core/Form.php b/CRM/Core/Form.php index 21839f108e..8c0b84b291 100644 --- a/CRM/Core/Form.php +++ b/CRM/Core/Form.php @@ -1286,6 +1286,35 @@ class CRM_Core_Form extends HTML_QuickForm_Page { } } + /** + * Add a search for a range using date picker fields. + * + * @param string $fieldName + * @param string $label + * @param bool $required + * @param string $fromLabel + * @param string $toLabel + */ + public function addDatePickerRange($fieldName, $label, $required = FALSE, $fromLabel = 'From', $toLabel = 'To') { + + $options = array( + '' => ts('- any -'), + 0 => ts('Choose Date Range'), + ) + CRM_Core_OptionGroup::values('relative_date_filters'); + + $this->add('select', + "{$fieldName}_relative", + $label, + $options, + $required, + NULL + ); + $attributes = ['format' => 'searchDate']; + $extra = ['time' => FALSE]; + $this->add('datepicker', $fieldName . '_low', ts($fromLabel), $attributes, $required, $extra); + $this->add('datepicker', $fieldName . '_high', ts($toLabel), $attributes, $required, $extra); + } + /** * Based on form action, return a string representing the api action. * Used by addField method. diff --git a/CRM/Core/Form/Search.php b/CRM/Core/Form/Search.php index 30e5a5de45..5d4ed98a0a 100644 --- a/CRM/Core/Form/Search.php +++ b/CRM/Core/Form/Search.php @@ -156,8 +156,7 @@ class CRM_Core_Form_Search extends CRM_Core_Form { 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->add('datepicker', $fieldName . '_low', $fieldSpec['title'], [], FALSE, ['time' => FALSE]); - $this->add('datepicker', $fieldName . '_high', $fieldSpec['title'], [], FALSE, ['time' => FALSE]); + $this->addDatePickerRange($fieldName, $fieldSpec['title'], FALSE); } else { $this->addField($fieldName, ['entity' => $entity]); diff --git a/CRM/Grant/BAO/Query.php b/CRM/Grant/BAO/Query.php index b8f47c67a4..8825dc72e5 100644 --- a/CRM/Grant/BAO/Query.php +++ b/CRM/Grant/BAO/Query.php @@ -120,7 +120,7 @@ class CRM_Grant_BAO_Query extends CRM_Core_BAO_Query { /** * @param $values - * @param $query + * @param \CRM_Contact_BAO_Query $query */ public static function whereClauseSingle(&$values, &$query) { list($name, $op, $value, $grouping, $wildcard) = $values; @@ -147,7 +147,7 @@ class CRM_Grant_BAO_Query extends CRM_Core_BAO_Query { ); return; - case 'grant_application_received_notset': + case 'grant_application_received_date_notset': $query->_where[$grouping][] = "civicrm_grant.application_received_date IS NULL"; $query->_qill[$grouping][] = ts("Grant Application Received Date is NULL"); $query->_tables['civicrm_grant'] = $query->_whereTables['civicrm_grant'] = 1; @@ -310,6 +310,13 @@ class CRM_Grant_BAO_Query extends CRM_Core_BAO_Query { return array_intersect_key($metadata, array_flip($fields)); } + /** + * Transitional function for specifying which fields the tpl can iterate through. + */ + public static function getTemplateHandlableSearchFields() { + return array_diff_key(self::getSearchFieldMetadata(), ['grant_report_received' => 1]); + } + /** * Add all the elements shared between grant search and advanaced search. * @@ -323,6 +330,7 @@ class CRM_Grant_BAO_Query extends CRM_Core_BAO_Query { $grantType = CRM_Core_OptionGroup::values('grant_type'); $form->addSearchFieldMetadata(['Grant' => self::getSearchFieldMetadata()]); $form->addFormFieldsFromMetadata(); + $form->assign('grantSearchFields', self::getTemplateHandlableSearchFields()); $form->add('select', 'grant_type_id', ts('Grant Type'), $grantType, FALSE, array('id' => 'grant_type_id', 'multiple' => 'multiple', 'class' => 'crm-select2') ); @@ -331,7 +339,7 @@ class CRM_Grant_BAO_Query extends CRM_Core_BAO_Query { $form->add('select', 'grant_status_id', ts('Grant Status'), $grantStatus, FALSE, array('id' => 'grant_status_id', 'multiple' => 'multiple', 'class' => 'crm-select2') ); - $form->addElement('checkbox', 'grant_application_received_notset', ts('Date is not set'), NULL); + $form->addElement('checkbox', 'grant_application_received_date_notset', ts('Date is not set'), NULL); $form->addElement('checkbox', 'grant_money_transfer_date_notset', ts('Date is not set'), NULL); $form->addElement('checkbox', 'grant_due_date_notset', ts('Date is not set'), NULL); $form->addElement('checkbox', 'grant_decision_date_notset', ts('Date is not set'), NULL); diff --git a/templates/CRM/Core/DatePickerRange.tpl b/templates/CRM/Core/DatePickerRange.tpl new file mode 100644 index 0000000000..cbf512f006 --- /dev/null +++ b/templates/CRM/Core/DatePickerRange.tpl @@ -0,0 +1,58 @@ +{* + +--------------------------------------------------------------------+ + | CiviCRM version 5 | + +--------------------------------------------------------------------+ + | Copyright CiviCRM LLC (c) 2004-2018 | + +--------------------------------------------------------------------+ + | This file is a part of CiviCRM. | + | | + | CiviCRM is free software; you can copy, modify, and distribute it | + | under the terms of the GNU Affero General Public License | + | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | + | | + | CiviCRM is distributed in the hope that it will be useful, but | + | WITHOUT ANY WARRANTY; without even the implied warranty of | + | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | + | See the GNU Affero General Public License for more details. | + | | + | You should have received a copy of the GNU Affero General Public | + | License and the CiviCRM Licensing Exception along | + | with this program; if not, contact CiviCRM LLC | + | at info[AT]civicrm[DOT]org. If you have questions about the | + | GNU Affero General Public License or the licensing of CiviCRM, | + | see the CiviCRM license FAQ at http://civicrm.org/licensing | + +--------------------------------------------------------------------+ +*} +{*this is included inside a table row*} +{assign var=relativeName value=$fieldName|cat:"_relative"} + + {$form.$relativeName.label}
+ {$form.$relativeName.html}
+ + + {assign var=fromName value=$fieldName|cat:$from} + {$form.$fromName.label} + {$form.$fromName.html} + + + {assign var=toName value=$fieldName|cat:$to} + {$form.$toName.label} + {$form.$toName.html} + + + {literal} + + {/literal} + diff --git a/templates/CRM/Grant/Form/Search/Common.tpl b/templates/CRM/Grant/Form/Search/Common.tpl index fcf512a802..9fcd699226 100644 --- a/templates/CRM/Grant/Form/Search/Common.tpl +++ b/templates/CRM/Grant/Form/Search/Common.tpl @@ -49,50 +49,17 @@ {$form.grant_amount_high.html} +{foreach from=$grantSearchFields key=fieldName item=fieldSpec} + {assign var=notSetFieldName value=$fieldName|cat:'_notset'} - - {$form.grant_application_received_date_low.label}
- {$form.grant_application_received_date_low.html} - - - {$form.grant_application_received_date_high.label}
- {$form.grant_application_received_date_high.html} -  {$form.grant_application_received_notset.html}  {$form.grant_application_received_notset.label} - - - - - {$form.grant_decision_date_low.label}
- {$form.grant_decision_date_low.html} - - - {$form.grant_decision_date_high.label}
- {$form.grant_decision_date_high.html} -  {$form.grant_decision_date_notset.html}  {$form.grant_decision_date_notset.label} - - - - - {$form.grant_money_transfer_date_low.label}
- {$form.grant_money_transfer_date_low.html} - - - {$form.grant_money_transfer_date_high.label}
- {$form.grant_money_transfer_date_high.html} -  {$form.grant_money_transfer_date_notset.html}  {$form.grant_money_transfer_date_notset.label} - - - - - {$form.grant_due_date_low.label}
- {$form.grant_due_date_low.html} - - - {$form.grant_due_date_high.label}
- {$form.grant_due_date_high.html} -  {$form.grant_due_date_notset.html}  {$form.grant_due_date_notset.label} - + + {include file="CRM/Core/DatePickerRange.tpl" from='_low' to='_high'} + + +  {$form.$notSetFieldName.html}  {$form.$notSetFieldName.label} + +{/foreach} {if $grantGroupTree} -- 2.25.1