From adde8f5eb1443a5048e5bf01dcb3fb7035fe557c Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Thu, 18 Nov 2021 16:56:18 -0500 Subject: [PATCH] CiviGrant - Migrate legacy search query code This fixes up the `CRM_Contact_BAO_Query_Interface` class which had incorrectly omitted `static` from some static functions. Adds defaultReurnProperties to search hook interface. --- CRM/Contact/BAO/Query.php | 1 - CRM/Contact/BAO/Query/Hook.php | 17 +++- CRM/Contact/BAO/Query/Interface.php | 27 +++---- CRM/Contact/Form/Search/Advanced.php | 1 + CRM/Contact/Form/Search/Builder.php | 1 - CRM/Core/Component.php | 3 + ext/civigrant/CRM/Grant/BAO/Query.php | 103 +++++++++++++++++------- ext/civigrant/CRM/Grant/Form/Search.php | 3 +- ext/civigrant/civigrant.php | 14 ++++ 9 files changed, 119 insertions(+), 51 deletions(-) diff --git a/CRM/Contact/BAO/Query.php b/CRM/Contact/BAO/Query.php index 09281671fc..ab13518415 100644 --- a/CRM/Contact/BAO/Query.php +++ b/CRM/Contact/BAO/Query.php @@ -5700,7 +5700,6 @@ civicrm_relationship.start_date > {$today} ts('Activities') => 'civicrm_activity', ts('Pledges') => 'civicrm_pledge', ts('Cases') => 'civicrm_case', - ts('Grants') => 'civicrm_grant', ts('Address Fields') => 'civicrm_address', ts('Notes') => 'civicrm_note', ts('Change Log') => 'civicrm_log', diff --git a/CRM/Contact/BAO/Query/Hook.php b/CRM/Contact/BAO/Query/Hook.php index 9b9493c782..ae6ee974a3 100644 --- a/CRM/Contact/BAO/Query/Hook.php +++ b/CRM/Contact/BAO/Query/Hook.php @@ -43,8 +43,7 @@ class CRM_Contact_BAO_Query_Hook { /** * Get or build the list of search objects (via hook). * - * @return array - * Array of CRM_Contact_BAO_Query_Interface objects + * @return CRM_Contact_BAO_Query_Interface[] */ public function getSearchQueryObjects() { if ($this->_queryObjects === NULL) { @@ -150,4 +149,18 @@ class CRM_Contact_BAO_Query_Hook { } } + /** + * @param $mode + * @return array|null + */ + public function getDefaultReturnProperties($mode) { + foreach (self::getSearchQueryObjects() as $obj) { + $properties = $obj::defaultReturnProperties($mode); + if ($properties) { + return $properties; + } + } + return NULL; + } + } diff --git a/CRM/Contact/BAO/Query/Interface.php b/CRM/Contact/BAO/Query/Interface.php index 4e34cdd449..2aa1ba29e8 100644 --- a/CRM/Contact/BAO/Query/Interface.php +++ b/CRM/Contact/BAO/Query/Interface.php @@ -29,42 +29,30 @@ abstract class CRM_Contact_BAO_Query_Interface { * * @return mixed */ - abstract public function from($fieldName, $mode, $side); + abstract public static function from($fieldName, $mode, $side); /** * @param $query - * - * @return null */ - public function select(&$query) { - return NULL; + public static function select(&$query) { } /** * @param $query - * - * @return null */ - public function where(&$query) { - return NULL; + public static function where(&$query) { } /** * @param $tables - * - * @return null */ public function setTableDependency(&$tables) { - return NULL; } /** * @param $panes - * - * @return null */ public function registerAdvancedSearchPane(&$panes) { - return NULL; } /** @@ -105,4 +93,13 @@ abstract class CRM_Contact_BAO_Query_Interface { public function alterSearchBuilderOptions(&$apiEntities, &$fieldOptions) { } + /** + * @param $mode + * @param $includeCustomFields + * @return array|null + */ + public static function defaultReturnProperties($mode) { + return NULL; + } + } diff --git a/CRM/Contact/Form/Search/Advanced.php b/CRM/Contact/Form/Search/Advanced.php index 73ddd7dfc8..40900d5aa3 100644 --- a/CRM/Contact/Form/Search/Advanced.php +++ b/CRM/Contact/Form/Search/Advanced.php @@ -130,6 +130,7 @@ class CRM_Contact_Form_Search_Advanced extends CRM_Contact_Form_Search { $this->_paneTemplatePath[$type] = $c->getAdvancedSearchPaneTemplatePath(); } elseif (in_array($type, $hookPanes)) { + $this->add('hidden', "hidden_$type", 1); CRM_Contact_BAO_Query_Hook::singleton()->buildAdvancedSearchPaneForm($this, $type); CRM_Contact_BAO_Query_Hook::singleton()->setAdvancedSearchPaneTemplatePath($this->_paneTemplatePath, $type); } diff --git a/CRM/Contact/Form/Search/Builder.php b/CRM/Contact/Form/Search/Builder.php index 1eef8b3633..312a01b41e 100644 --- a/CRM/Contact/Form/Search/Builder.php +++ b/CRM/Contact/Form/Search/Builder.php @@ -449,7 +449,6 @@ class CRM_Contact_Form_Search_Builder extends CRM_Contact_Form_Search { 'member', 'contribution', 'case', - 'grant', ]; CRM_Contact_BAO_Query_Hook::singleton()->alterSearchBuilderOptions($entities, $options); $fkEntities = []; diff --git a/CRM/Core/Component.php b/CRM/Core/Component.php index 7661eb3e04..ea28963554 100644 --- a/CRM/Core/Component.php +++ b/CRM/Core/Component.php @@ -313,6 +313,9 @@ class CRM_Core_Component { } } } + if (!$properties) { + $properties = CRM_Contact_BAO_Query_Hook::singleton()->getDefaultReturnProperties($mode); + } return $properties; } diff --git a/ext/civigrant/CRM/Grant/BAO/Query.php b/ext/civigrant/CRM/Grant/BAO/Query.php index 2e814b2a77..2eeb0d0443 100644 --- a/ext/civigrant/CRM/Grant/BAO/Query.php +++ b/ext/civigrant/CRM/Grant/BAO/Query.php @@ -9,17 +9,19 @@ +--------------------------------------------------------------------+ */ +use CRM_Grant_ExtensionUtil as E; + /** * Class CRM_Grant_BAO_Query */ -class CRM_Grant_BAO_Query extends CRM_Core_BAO_Query { +class CRM_Grant_BAO_Query extends CRM_Contact_BAO_Query_Interface { /** * Get grant fields. * * @return array */ - public static function &getFields() { + public function &getFields() { return CRM_Grant_BAO_Grant::exportableFields(); } @@ -27,8 +29,6 @@ class CRM_Grant_BAO_Query extends CRM_Core_BAO_Query { * Build select for CiviGrant. * * @param $query - * - * @return void */ public static function select(&$query) { if (!empty($query->_returnProperties['grant_status_id'])) { @@ -84,8 +84,6 @@ class CRM_Grant_BAO_Query extends CRM_Core_BAO_Query { * where clause * * @param $query - * - * @return void */ public static function where(&$query) { foreach ($query->_params as $id => $values) { @@ -104,7 +102,7 @@ class CRM_Grant_BAO_Query extends CRM_Core_BAO_Query { * @param \CRM_Contact_BAO_Query $query */ public static function whereClauseSingle(&$values, &$query) { - list($name, $op, $value, $grouping, $wildcard) = $values; + [$name, $op, $value, $grouping, $wildcard] = $values; switch ($name) { case 'grant_money_transfer_date_low': case 'grant_money_transfer_date_high': @@ -116,7 +114,7 @@ class CRM_Grant_BAO_Query extends CRM_Core_BAO_Query { case 'grant_money_transfer_date_notset': $query->_where[$grouping][] = "civicrm_grant.money_transfer_date IS NULL"; - $query->_qill[$grouping][] = ts("Grant Money Transfer Date is NULL"); + $query->_qill[$grouping][] = E::ts("Grant Money Transfer Date is NULL"); $query->_tables['civicrm_grant'] = $query->_whereTables['civicrm_grant'] = 1; return; @@ -130,7 +128,7 @@ class CRM_Grant_BAO_Query extends CRM_Core_BAO_Query { 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->_qill[$grouping][] = E::ts("Grant Application Received Date is NULL"); $query->_tables['civicrm_grant'] = $query->_whereTables['civicrm_grant'] = 1; return; @@ -138,13 +136,13 @@ class CRM_Grant_BAO_Query extends CRM_Core_BAO_Query { case 'grant_due_date_high': $query->dateQueryBuilder($values, 'civicrm_grant', 'grant_due_date', - 'grant_due_date', ts('Grant Due Date') + 'grant_due_date', E::ts('Grant Due Date') ); return; case 'grant_due_date_notset': $query->_where[$grouping][] = "civicrm_grant.grant_due_date IS NULL"; - $query->_qill[$grouping][] = ts("Grant Due Date is NULL"); + $query->_qill[$grouping][] = E::ts("Grant Due Date is NULL"); $query->_tables['civicrm_grant'] = $query->_whereTables['civicrm_grant'] = 1; return; @@ -152,13 +150,13 @@ class CRM_Grant_BAO_Query extends CRM_Core_BAO_Query { case 'grant_decision_date_high': $query->dateQueryBuilder($values, 'civicrm_grant', 'grant_decision_date', - 'decision_date', ts('Grant Decision Date') + 'decision_date', E::ts('Grant Decision Date') ); return; case 'grant_decision_date_notset': $query->_where[$grouping][] = "civicrm_grant.decision_date IS NULL"; - $query->_qill[$grouping][] = ts("Grant Decision Date is NULL"); + $query->_qill[$grouping][] = E::ts("Grant Decision Date is NULL"); $query->_tables['civicrm_grant'] = $query->_whereTables['civicrm_grant'] = 1; return; @@ -169,17 +167,17 @@ class CRM_Grant_BAO_Query extends CRM_Core_BAO_Query { if (strstr($name, 'type')) { $name = 'grant_type_id'; - $label = ts('Grant Type(s)'); + $label = E::ts('Grant Type(s)'); } else { $name = 'status_id'; - $label = ts('Grant Status(s)'); + $label = E::ts('Grant Status(s)'); } $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_grant.$name", $op, $value, "Integer"); list($qillop, $qillVal) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Grant_DAO_Grant', $name, $value, $op); - $query->_qill[$grouping][] = ts("%1 %2 %3", [1 => $label, 2 => $qillop, 3 => $qillVal]); + $query->_qill[$grouping][] = E::ts("%1 %2 %3", [1 => $label, 2 => $qillop, 3 => $qillVal]); $query->_tables['civicrm_grant'] = $query->_whereTables['civicrm_grant'] = 1; return; @@ -187,15 +185,15 @@ class CRM_Grant_BAO_Query extends CRM_Core_BAO_Query { case 'grant_report_received': if ($value == 1) { - $yesNo = ts('Yes'); + $yesNo = E::ts('Yes'); $query->_where[$grouping][] = "civicrm_grant.grant_report_received $op $value"; } elseif ($value == 0) { - $yesNo = ts('No'); + $yesNo = E::ts('No'); $query->_where[$grouping][] = "civicrm_grant.grant_report_received IS NULL"; } - $query->_qill[$grouping][] = ts('Grant Report Received = %1', [1 => $yesNo]); + $query->_qill[$grouping][] = E::ts('Grant Report Received = %1', [1 => $yesNo]); $query->_tables['civicrm_grant'] = $query->_whereTables['civicrm_grant'] = 1; return; @@ -305,37 +303,80 @@ class CRM_Grant_BAO_Query extends CRM_Core_BAO_Query { * * * @param \CRM_Grant_Form_Search $form + * @param $type * * @return void */ - public static function buildSearchForm(&$form) { + public function buildAdvancedSearchPaneForm(&$form, $type) { + if ($type !== 'CiviGrant') { + return; + } $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, + $form->add('select', 'grant_type_id', E::ts('Grant Type'), $grantType, FALSE, ['id' => 'grant_type_id', 'multiple' => 'multiple', 'class' => 'crm-select2'] ); $grantStatus = CRM_Core_OptionGroup::values('grant_status'); - $form->add('select', 'grant_status_id', ts('Grant Status'), $grantStatus, FALSE, + $form->add('select', 'grant_status_id', E::ts('Grant Status'), $grantStatus, FALSE, ['id' => 'grant_status_id', 'multiple' => 'multiple', 'class' => 'crm-select2'] ); - $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); + $form->addElement('checkbox', 'grant_application_received_date_notset', E::ts('Date is not set'), NULL); + $form->addElement('checkbox', 'grant_money_transfer_date_notset', E::ts('Date is not set'), NULL); + $form->addElement('checkbox', 'grant_due_date_notset', E::ts('Date is not set'), NULL); + $form->addElement('checkbox', 'grant_decision_date_notset', E::ts('Date is not set'), NULL); - $form->add('text', 'grant_amount_low', ts('Minimum Amount'), ['size' => 8, 'maxlength' => 8]); - $form->addRule('grant_amount_low', ts('Please enter a valid money value (e.g. %1).', [1 => CRM_Utils_Money::formatLocaleNumericRoundedForDefaultCurrency('9.99')]), 'money'); + $form->add('text', 'grant_amount_low', E::ts('Minimum Amount'), ['size' => 8, 'maxlength' => 8]); + $form->addRule('grant_amount_low', E::ts('Please enter a valid money value (e.g. %1).', [1 => CRM_Utils_Money::formatLocaleNumericRoundedForDefaultCurrency('9.99')]), 'money'); - $form->add('text', 'grant_amount_high', ts('Maximum Amount'), ['size' => 8, 'maxlength' => 8]); - $form->addRule('grant_amount_high', ts('Please enter a valid money value (e.g. %1).', [1 => CRM_Utils_Money::formatLocaleNumericRoundedForDefaultCurrency('99.99')]), 'money'); + $form->add('text', 'grant_amount_high', E::ts('Maximum Amount'), ['size' => 8, 'maxlength' => 8]); + $form->addRule('grant_amount_high', E::ts('Please enter a valid money value (e.g. %1).', [1 => CRM_Utils_Money::formatLocaleNumericRoundedForDefaultCurrency('99.99')]), 'money'); - self::addCustomFormFields($form, ['Grant']); + CRM_Core_BAO_Query::addCustomFormFields($form, ['Grant']); $form->assign('validGrant', TRUE); } + /** + * @param string[] $panes + */ + public function registerAdvancedSearchPane(&$panes) { + $panes[E::ts('Grants')] = 'CiviGrant'; + } + + /** + * @param string[] $panes + */ + public function getPanesMapper(&$panes) { + $panes[E::ts('Grants')] = 'civicrm_grant'; + } + + /** + * @param $tables + */ + public function setTableDependency(&$tables) { + // $tables['civicrm_grant'] = 1; + } + + /** + * @param $paneTemplatePathArray + * @param $type + */ + public function setAdvancedSearchPaneTemplatePath(&$paneTemplatePathArray, $type) { + $paneTemplatePathArray['CiviGrant'] = 'CRM/Grant/Form/Search/AdvancedSearchPane.tpl'; + } + + /** + * Describe options for available for use in the search-builder. + * + * @param array $apiEntities + * @param array $fieldOptions + */ + public function alterSearchBuilderOptions(&$apiEntities, &$fieldOptions) { + $apiEntities[] = 'grant'; + } + } diff --git a/ext/civigrant/CRM/Grant/Form/Search.php b/ext/civigrant/CRM/Grant/Form/Search.php index b997f2cab7..9104bce36a 100644 --- a/ext/civigrant/CRM/Grant/Form/Search.php +++ b/ext/civigrant/CRM/Grant/Form/Search.php @@ -119,7 +119,8 @@ class CRM_Grant_Form_Search extends CRM_Core_Form_Search { parent::buildQuickForm(); $this->addSortNameField(); - CRM_Grant_BAO_Query::buildSearchForm($this); + (new CRM_Grant_BAO_Query()) + ->buildAdvancedSearchPaneForm($this, 'CiviGrant'); $rows = $this->get('rows'); if (is_array($rows)) { diff --git a/ext/civigrant/civigrant.php b/ext/civigrant/civigrant.php index ba8748d5eb..5a4e1fdf78 100644 --- a/ext/civigrant/civigrant.php +++ b/ext/civigrant/civigrant.php @@ -87,3 +87,17 @@ function civigrant_civicrm_tabSet($tabSetName, &$tabs, $context) { ]; } } + +/** + * Implements hook_civicrm_queryObjects(). + * + * Adds query object for legacy screens like advanced search, search builder, etc. + */ +function civigrant_civicrm_queryObjects(&$queryObjects, $type) { + if ($type == 'Contact') { + $queryObjects[] = new CRM_Grant_BAO_Query(); + } + elseif ($type == 'Report') { + // Do we need to do something here? + } +} -- 2.25.1