From 06dec0480c079535cff5c12cf03acab586a247c8 Mon Sep 17 00:00:00 2001 From: eileen Date: Fri, 26 Mar 2021 14:37:52 +1300 Subject: [PATCH] [REF] Extract getIDS functionality With this change made we are ready to start exposing some actions to search kit --- CRM/Contribute/Form/Task.php | 18 ++-------- CRM/Contribute/Form/Task/TaskTrait.php | 48 ++++++++++++++++++++++++-- 2 files changed, 48 insertions(+), 18 deletions(-) diff --git a/CRM/Contribute/Form/Task.php b/CRM/Contribute/Form/Task.php index 53df76a8b5..e9b72c8c92 100644 --- a/CRM/Contribute/Form/Task.php +++ b/CRM/Contribute/Form/Task.php @@ -49,21 +49,9 @@ class CRM_Contribute_Form_Task extends CRM_Core_Form_Task { $form->_task = $values['task'] ?? NULL; - $ids = $form->getSelectedIDs($values); - if (!$ids) { - $result = $form->getSearchQueryResults(); - while ($result->fetch()) { - $ids[] = $result->contribution_id; - } - $form->assign('totalSelectedContributions', $form->get('rowCount')); - } - - if (!empty($ids)) { - $form->_componentClause = ' civicrm_contribution.id IN ( ' . implode(',', $ids) . ' ) '; - - $form->assign('totalSelectedContributions', count($ids)); - } - + $ids = $form->getIDs(); + $form->_componentClause = $form->getComponentClause(); + $form->assign('totalSelectedContributions', count($ids)); $form->_contributionIds = $form->_componentIds = $ids; $form->set('contributionIds', $form->_contributionIds); $form->setNextUrl('contribute'); diff --git a/CRM/Contribute/Form/Task/TaskTrait.php b/CRM/Contribute/Form/Task/TaskTrait.php index e08185ace1..6dedf99ec3 100644 --- a/CRM/Contribute/Form/Task/TaskTrait.php +++ b/CRM/Contribute/Form/Task/TaskTrait.php @@ -21,11 +21,11 @@ trait CRM_Contribute_Form_Task_TaskTrait { /** - * Query result object. + * Selected IDs for the action. * - * @var \CRM_Core_DAO + * @var array */ - protected $queryBAO; + protected $ids; /** * Get the results from the BAO_Query object based search. @@ -96,4 +96,46 @@ trait CRM_Contribute_Form_Task_TaskTrait { return (bool) CRM_Contribute_BAO_Query::isSoftCreditOptionEnabled($this->getQueryParams()); } + /** + * Get ids selected for the task. + * + * @return array|bool + * @throws \CRM_Core_Exception + */ + public function getIDs() { + if (!$this->ids) { + $this->ids = $this->calculateIDS(); + } + return $this->ids; + } + + /** + * @return array|bool|string[] + * @throws \CRM_Core_Exception + */ + protected function calculateIDS() { + if ($this->controller->get('id')) { + return explode(',', $this->controller->get('id')); + } + $ids = $this->getSelectedIDs($this->getSearchFormValues()); + if (!$ids) { + $result = $this->getSearchQueryResults(); + while ($result->fetch()) { + $ids[] = $result->contribution_id; + } + } + return $ids; + } + + /** + * Get the clause to add to queries to hone the results. + * + * In practice this generally means the query to limit by selected ids. + * + * @throws \CRM_Core_Exception + */ + public function getComponentClause(): string { + return ' civicrm_contribution.id IN ( ' . implode(',', $this->getIDs()) . ' ) '; + } + } -- 2.25.1