diff --git a/CRM/Core/Form/Search.php b/CRM/Core/Form/Search.php index 7fbf516ac2e5..deb12c7a0b37 100644 --- a/CRM/Core/Form/Search.php +++ b/CRM/Core/Form/Search.php @@ -89,6 +89,20 @@ class CRM_Core_Form_Search extends CRM_Core_Form { */ protected $entityReferenceFields = array(); + /** + * Metadata for fields on the search form. + * + * @var array + */ + protected $searchFieldMetadata = []; + + /** + * @return array + */ + public function getSearchFieldMetadata() { + return $this->searchFieldMetadata; + } + /** * Builds the list of tasks or actions that a searcher can perform on a result set. * @@ -270,4 +284,43 @@ protected function getFormVariables() { } } + /** + * Add any fields described in metadata to the form. + * + * The goal is to describe all fields in metadata and handle from metadata rather + * than existing ad hoc handling. + */ + public function addFormFieldsFromMetadata() { + $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->add('datepicker', $fieldName . '_low', $fieldSpec['title'], [], FALSE, ['time' => FALSE]); + $this->add('datepicker', $fieldName . '_high', $fieldSpec['title'], [], FALSE, ['time' => FALSE]); + } + else { + $this->addField($fieldName, ['entity' => $entity]); + } + } + } + } + + /** + * @param array $searchFieldMetadata + */ + public function addSearchFieldMetadata($searchFieldMetadata) { + $existingFieldsForEntity = isset($this->searchFieldMetadata[$this->getDefaultEntity()]) ? $this->searchFieldMetadata[$this->getDefaultEntity()] : []; + $this->searchFieldMetadata[$this->getDefaultEntity()] = array_merge($existingFieldsForEntity, $searchFieldMetadata); + } + + /** + * Get the metadata for fields to be included on the activity search form. + */ + protected function getEntityMetadata() { + $fields = $this->searchFields; + $metadata = civicrm_api3($this->getDefaultEntity(), 'getfields', [])['values']; + return array_intersect_key($metadata, array_flip($fields)); + } + } diff --git a/CRM/Grant/BAO/Query.php b/CRM/Grant/BAO/Query.php index 6631c6934320..de8977f6c7d8 100644 --- a/CRM/Grant/BAO/Query.php +++ b/CRM/Grant/BAO/Query.php @@ -123,9 +123,7 @@ public static function where(&$query) { * @param $query */ public static function whereClauseSingle(&$values, &$query) { - $strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower'; list($name, $op, $value, $grouping, $wildcard) = $values; - $val = $names = array(); switch ($name) { case 'grant_money_transfer_date_low': case 'grant_money_transfer_date_high': @@ -317,28 +315,6 @@ public static function buildSearchForm(&$form) { array('id' => 'grant_status_id', 'multiple' => 'multiple', 'class' => 'crm-select2') ); - $form->addDate('grant_application_received_date_low', ts('App. Received Date - From'), FALSE, array('formatType' => 'searchDate')); - $form->addDate('grant_application_received_date_high', ts('To'), FALSE, array('formatType' => 'searchDate')); - - $form->addElement('checkbox', 'grant_application_received_notset', ts('Date is not set'), NULL); - - $form->addDate('grant_money_transfer_date_low', ts('Money Sent Date - From'), FALSE, array('formatType' => 'searchDate')); - $form->addDate('grant_money_transfer_date_high', ts('To'), FALSE, array('formatType' => 'searchDate')); - - $form->addElement('checkbox', 'grant_money_transfer_date_notset', ts('Date is not set'), NULL); - - $form->addDate('grant_due_date_low', ts('Report Due Date - From'), FALSE, array('formatType' => 'searchDate')); - $form->addDate('grant_due_date_high', ts('To'), FALSE, array('formatType' => 'searchDate')); - - $form->addElement('checkbox', 'grant_due_date_notset', ts('Date is not set'), NULL); - - $form->addDate('grant_decision_date_low', ts('Grant Decision Date - From'), FALSE, array('formatType' => 'searchDate')); - $form->addDate('grant_decision_date_high', ts('To'), FALSE, array('formatType' => 'searchDate')); - - $form->addElement('checkbox', 'grant_decision_date_notset', ts('Date is not set'), NULL); - - $form->addYesNo('grant_report_received', ts('Grant report received?'), TRUE); - $form->add('text', 'grant_amount_low', ts('Minimum Amount'), array('size' => 8, 'maxlength' => 8)); $form->addRule('grant_amount_low', ts('Please enter a valid money value (e.g. %1).', array(1 => CRM_Utils_Money::format('9.99', ' '))), 'money'); diff --git a/CRM/Grant/DAO/Grant.php b/CRM/Grant/DAO/Grant.php index c04d525034d1..5a695e137ef6 100644 --- a/CRM/Grant/DAO/Grant.php +++ b/CRM/Grant/DAO/Grant.php @@ -6,7 +6,7 @@ * * Generated from xml/schema/CRM/Grant/Grant.xml * DO NOT EDIT. Generated by CRM_Core_CodeGen - * (GenCodeChecksum:43c5de526491b3e385bec6d129dba6e3) + * (GenCodeChecksum:ea60d8cd875ca924d3cc34b4282c8a8a) */ /** @@ -200,7 +200,7 @@ public static function &fields() { 'type' => 'EntityRef', ], ], - 'application_received_date' => [ + 'grant_application_received_date' => [ 'name' => 'application_received_date', 'type' => CRM_Utils_Type::T_DATE, 'title' => ts('Application received date'), @@ -219,7 +219,7 @@ public static function &fields() { 'formatType' => 'activityDate', ], ], - 'decision_date' => [ + 'grant_decision_date' => [ 'name' => 'decision_date', 'type' => CRM_Utils_Type::T_DATE, 'title' => ts('Decision date'), @@ -260,7 +260,7 @@ public static function &fields() { 'grant_due_date' => [ 'name' => 'grant_due_date', 'type' => CRM_Utils_Type::T_DATE, - 'title' => ts('Grant Due Date'), + 'title' => ts('Grant Report Due Date'), 'description' => ts('Date on which grant report is due.'), 'import' => TRUE, 'where' => 'civicrm_grant.grant_due_date', diff --git a/CRM/Grant/Form/Search.php b/CRM/Grant/Form/Search.php index 4d833d9708c1..73ea2ae6270f 100644 --- a/CRM/Grant/Form/Search.php +++ b/CRM/Grant/Form/Search.php @@ -62,7 +62,32 @@ class CRM_Grant_Form_Search extends CRM_Core_Form_Search { */ protected $_prefix = "grant_"; - protected $entity = 'grant'; + /** + * Search fields to include on the form. + * + * @var array + */ + protected $searchFields = [ + 'grant_report_received', + 'grant_application_received_date', + 'grant_decision_date', + 'grant_money_transfer_date', + 'grant_due_date', + ]; + + /** + * Metadata of all fields to include on the form. + * + * @var array + */ + protected $searchFieldMetadata = []; + + /** + * @return string + */ + public function getDefaultEntity() { + return 'Grant'; + } /** * Processing needed for buildForm and later. @@ -81,7 +106,7 @@ public function preProcess() { $this->getUrlVariables(); $this->getFormVariables(); - + $this->addSearchFieldMetadata($this->getEntityMetadata()); if ($this->_force) { $this->postProcess(); $this->set('force', 0); @@ -130,6 +155,7 @@ public function preProcess() { public function buildQuickForm() { parent::buildQuickForm(); $this->addSortNameField(); + $this->addFormFieldsFromMetadata(); CRM_Grant_BAO_Query::buildSearchForm($this); diff --git a/templates/CRM/Grant/Form/Search/Common.tpl b/templates/CRM/Grant/Form/Search/Common.tpl index 9830e19fbc9f..354128a44a0e 100644 --- a/templates/CRM/Grant/Form/Search/Common.tpl +++ b/templates/CRM/Grant/Form/Search/Common.tpl @@ -52,45 +52,41 @@ {$form.grant_application_received_date_low.label}
- {include file="CRM/common/jcalendar.tpl" elementName=grant_application_received_date_low} + {$form.grant_application_received_date_low.html} - {$form.grant_application_received_date_high.label}
- {include file="CRM/common/jcalendar.tpl" elementName=grant_application_received_date_high} -  {$form.grant_application_received_notset.html}  {$form.grant_application_received_notset.label} + {$form.grant_application_received_date_high.label}
+ {$form.grant_application_received_date_high.html} - {$form.grant_decision_date_low.label}
- {include file="CRM/common/jcalendar.tpl" elementName=grant_decision_date_low} + {$form.grant_decision_date_low.label}
+ {$form.grant_decision_date_low.html} {$form.grant_decision_date_high.label}
- {include file="CRM/common/jcalendar.tpl" elementName=grant_decision_date_high} -  {$form.grant_decision_date_notset.html}  {$form.grant_decision_date_notset.label} + {$form.grant_decision_date_high.html} - {$form.grant_money_transfer_date_low.label}
- {include file="CRM/common/jcalendar.tpl" elementName=grant_money_transfer_date_low} + {$form.grant_money_transfer_date_low.label}
+ {$form.grant_money_transfer_date_low.html} - {$form.grant_money_transfer_date_high.label}
- {include file="CRM/common/jcalendar.tpl" elementName=grant_money_transfer_date_high} -  {$form.grant_money_transfer_date_notset.html}  {$form.grant_money_transfer_date_notset.label} + {$form.grant_money_transfer_date_high.label}
+ {$form.grant_money_transfer_date_high.html} - {$form.grant_due_date_low.label}
- {include file="CRM/common/jcalendar.tpl" elementName=grant_due_date_low} + {$form.grant_due_date_low.label}
+ {$form.grant_due_date_low.html} - {$form.grant_due_date_high.label}
- {include file="CRM/common/jcalendar.tpl" elementName=grant_due_date_high} -  {$form.grant_due_date_notset.html}  {$form.grant_due_date_notset.label} + {$form.grant_due_date_high.label}
+ {$form.grant_due_date_high.html} {if $grantGroupTree} diff --git a/xml/schema/Grant/Grant.xml b/xml/schema/Grant/Grant.xml index 9d09f0c13c13..d19965dde64e 100644 --- a/xml/schema/Grant/Grant.xml +++ b/xml/schema/Grant/Grant.xml @@ -43,6 +43,7 @@ application_received_date Application received date + grant_application_received_date date true true @@ -56,6 +57,7 @@ decision_date Decision date + grant_decision_date date Date on which grant decision was made. true @@ -81,7 +83,7 @@ grant_due_date date - Grant Due Date + Grant Report Due Date Date on which grant report is due. 1.8 true