From e8978897fe78273597ddd1520387b691781a9a36 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 | 28 +++++++++++ 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, 107 insertions(+), 47 deletions(-) create mode 100644 templates/CRM/Core/DatePickerRange.tpl diff --git a/CRM/Core/Form.php b/CRM/Core/Form.php index 21839f108e2c..241bd56e858d 100644 --- a/CRM/Core/Form.php +++ b/CRM/Core/Form.php @@ -1286,6 +1286,34 @@ public function addDateRange($name, $from = '_from', $to = '_to', $label = 'From } } + /** + * Add a search for a range using date picker fields. + * + * @param string $fieldName + * @param string $label + * @param string $dateFormat + * @param bool $required + */ + public function addDatePickerRange($fieldName, $label, $required = FALSE, $fromLabel = 'From', $toLabel = 'To', $dateFormat ='') { + + $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 30e5a5de4589..5eeb046a1fad 100644 --- a/CRM/Core/Form/Search.php +++ b/CRM/Core/Form/Search.php @@ -156,8 +156,7 @@ public function addFormFieldsFromMetadata() { 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 b8f47c67a4c1..8825dc72e5b3 100644 --- a/CRM/Grant/BAO/Query.php +++ b/CRM/Grant/BAO/Query.php @@ -120,7 +120,7 @@ public static function where(&$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 @@ public static function whereClauseSingle(&$values, &$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 @@ public static function getSearchFieldMetadata() { 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 @@ public static function buildSearchForm(&$form) { $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 @@ public static function buildSearchForm(&$form) { $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 000000000000..cbf512f006ea --- /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 fcf512a80247..9fcd6992269b 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}