From be654b78c360a776e0203e37ed9fbe7cb4d22bd9 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 --- CRM/Contact/BAO/Query.php | 1 - CRM/Contact/Form/Search/Builder.php | 1 - ext/civigrant/CRM/Grant/BAO/Query.php | 105 +++++++++++++++++------- ext/civigrant/CRM/Grant/Form/Search.php | 2 +- ext/civigrant/civigrant.php | 14 ++++ 5 files changed, 90 insertions(+), 33 deletions(-) diff --git a/CRM/Contact/BAO/Query.php b/CRM/Contact/BAO/Query.php index 09281671fc6d..ab13518415ce 100644 --- a/CRM/Contact/BAO/Query.php +++ b/CRM/Contact/BAO/Query.php @@ -5700,7 +5700,6 @@ public function openedSearchPanes($reset = FALSE) { 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/Form/Search/Builder.php b/CRM/Contact/Form/Search/Builder.php index 1eef8b3633f2..312a01b41ece 100644 --- a/CRM/Contact/Form/Search/Builder.php +++ b/CRM/Contact/Form/Search/Builder.php @@ -449,7 +449,6 @@ public static function fieldOptions() { 'member', 'contribution', 'case', - 'grant', ]; CRM_Contact_BAO_Query_Hook::singleton()->alterSearchBuilderOptions($entities, $options); $fkEntities = []; diff --git a/ext/civigrant/CRM/Grant/BAO/Query.php b/ext/civigrant/CRM/Grant/BAO/Query.php index 2e814b2a7755..572dc8fd7c48 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(); } @@ -30,7 +32,7 @@ public static function &getFields() { * * @return void */ - public static function select(&$query) { + public function select(&$query) { if (!empty($query->_returnProperties['grant_status_id'])) { $query->_select['grant_status_id'] = 'grant_status.id as grant_status_id'; $query->_element['grant_status'] = 1; @@ -87,7 +89,7 @@ public static function select(&$query) { * * @return void */ - public static function where(&$query) { + public function where(&$query) { foreach ($query->_params as $id => $values) { if (!is_array($values) || count($values) != 5) { continue; @@ -104,7 +106,7 @@ public static function where(&$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 +118,7 @@ public static function whereClauseSingle(&$values, &$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 +132,7 @@ public static function whereClauseSingle(&$values, &$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 +140,13 @@ public static function whereClauseSingle(&$values, &$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 +154,13 @@ public static function whereClauseSingle(&$values, &$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 +171,17 @@ public static function whereClauseSingle(&$values, &$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 +189,15 @@ public static function whereClauseSingle(&$values, &$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; @@ -216,7 +218,7 @@ public static function whereClauseSingle(&$values, &$query) { * * @return null|string */ - public static function from($name, $mode, $side) { + public function from($name, $mode, $side) { $from = NULL; switch ($name) { case 'civicrm_grant': @@ -305,37 +307,80 @@ public static function getTemplateHandlableSearchFields() { * * * @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 b997f2cab733..f1ad95424996 100644 --- a/ext/civigrant/CRM/Grant/Form/Search.php +++ b/ext/civigrant/CRM/Grant/Form/Search.php @@ -119,7 +119,7 @@ public function buildQuickForm() { parent::buildQuickForm(); $this->addSortNameField(); - CRM_Grant_BAO_Query::buildSearchForm($this); + 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 a7548a3fb43d..d69d9c425ba9 100644 --- a/ext/civigrant/civigrant.php +++ b/ext/civigrant/civigrant.php @@ -176,3 +176,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? + } +}