From 7a3978aad68dd0bf964f1b914604bf236079d986 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Frank=20J=2E=20G=C3=B3mez?= Date: Thu, 12 Feb 2015 17:01:13 -0500 Subject: [PATCH] CRM-15965: Refactored for a more object-oriented approach, and moved the task list members and methods further up the inheritance tree to promote consistency and code reuse. --- CRM/Contact/Form/Search.php | 39 ++++++++++--------------- CRM/Contact/Form/Search/Custom.php | 12 +++++--- CRM/Contact/Form/Search/Custom/Base.php | 13 +++++++++ CRM/Contact/Form/Search/Interface.php | 8 +++++ CRM/Core/Form/Search.php | 24 +++++++++++++++ 5 files changed, 69 insertions(+), 27 deletions(-) diff --git a/CRM/Contact/Form/Search.php b/CRM/Contact/Form/Search.php index a9515deae8..79824f368b 100644 --- a/CRM/Contact/Form/Search.php +++ b/CRM/Contact/Form/Search.php @@ -331,31 +331,28 @@ class CRM_Contact_Form_Search extends CRM_Core_Form_Search { /** * Builds the list of tasks or actions that a searcher can perform on a result set. * - * The result is passed to $this->addTaskList, which is responsible for building - * the menu and adding it to the form. - * - * @access public * @return array */ - public function buildTaskList() { - $tasks = array(); - $permission = CRM_Core_Permission::getPermission(); + function buildTaskList() { + if ($this->_context !== 'amtg') { + $permission = CRM_Core_Permission::getPermission(); - if ($this->_componentMode == 1 || $this->_componentMode == 7) { - $tasks += CRM_Contact_Task::permissionedTaskTitles($permission, - CRM_Utils_Array::value('deleted_contacts', $this->_formValues) - ); - } else { - $className = $this->_modeValue['taskClassName']; - $tasks += $className::permissionedTaskTitles($permission, false); - } + if ($this->_componentMode == 1 || $this->_componentMode == 7) { + $this->_taskList += CRM_Contact_Task::permissionedTaskTitles($permission, + CRM_Utils_Array::value('deleted_contacts', $this->_formValues) + ); + } else { + $className = $this->_modeValue['taskClassName']; + $this->_taskList += $className::permissionedTaskTitles($permission, false); + } - // Only offer the "Update Smart Group" task if a smart group/saved search is already in play - if (isset($this->_ssID) && $permission == CRM_Core_Permission::EDIT) { - $tasks = $tasks + CRM_Contact_Task::optionalTaskTitle(); + // Only offer the "Update Smart Group" task if a smart group/saved search is already in play + if (isset($this->_ssID) && $permission == CRM_Core_Permission::EDIT) { + $this->_taskList += CRM_Contact_Task::optionalTaskTitle(); + } } - return $tasks; + return $this->_taskList; } /** @@ -466,10 +463,6 @@ class CRM_Contact_Form_Search extends CRM_Core_Form_Search { $this->assign('ts_sel_id', $selectedRowsRadio->_attributes['id']); $this->assign('ts_all_id', $allRowsRadio->_attributes['id']); } - else { - $tasks = $this->buildTaskList(); - $this->addTaskMenu($tasks); - } $selectedContactIds = array(); $qfKeyParam = CRM_Utils_Array::value('qfKey', $this->_formValues); diff --git a/CRM/Contact/Form/Search/Custom.php b/CRM/Contact/Form/Search/Custom.php index 2f4e910338..1c5baee64d 100644 --- a/CRM/Contact/Form/Search/Custom.php +++ b/CRM/Contact/Form/Search/Custom.php @@ -108,11 +108,15 @@ class CRM_Contact_Form_Search_Custom extends CRM_Contact_Form_Search { return $this->_formValues; } + /** + * Builds the list of tasks or actions that a searcher can perform on a result set. + * + * @return array + */ function buildTaskList() { - if (method_exists($this->_customSearchClass, 'buildTaskList')) { - return $this->_customClass->buildTaskList(); - } - return parent::buildTaskList(); + // call the parent method to populate $this->_taskList for the custom search + parent::buildTaskList(); + return $this->_customClass->buildTaskList($this); } function buildQuickForm() { diff --git a/CRM/Contact/Form/Search/Custom/Base.php b/CRM/Contact/Form/Search/Custom/Base.php index 5ef3c868e1..7484c92b2b 100644 --- a/CRM/Contact/Form/Search/Custom/Base.php +++ b/CRM/Contact/Form/Search/Custom/Base.php @@ -47,6 +47,19 @@ class CRM_Contact_Form_Search_Custom_Base { $this->_formValues = &$formValues; } + /** + * Builds the list of tasks or actions that a searcher can perform on a result set. + * + * The returned array completely replaces the task list, so a child class that + * wants to modify the existing list should manipulate the result of this method. + * + * @param CRM_Core_Form_Search $form + * @return array + */ + function buildTaskList(CRM_Core_Form_Search $form) { + return $form->getVar('_taskList'); + } + /** * @return null|string */ diff --git a/CRM/Contact/Form/Search/Interface.php b/CRM/Contact/Form/Search/Interface.php index 92cf44aba2..b128c87bb2 100644 --- a/CRM/Contact/Form/Search/Interface.php +++ b/CRM/Contact/Form/Search/Interface.php @@ -39,6 +39,14 @@ interface CRM_Contact_Form_Search_Interface { */ function __construct(&$formValues); + /** + * Builds the list of tasks or actions that a searcher can perform on a result set. + * + * @param CRM_Core_Form_Search $form + * @return array + */ + function buildTaskList(CRM_Core_Form_Search $form); + /** * Builds the quickform for this search */ diff --git a/CRM/Core/Form/Search.php b/CRM/Core/Form/Search.php index 9b9cbc0f7e..1e02fde170 100644 --- a/CRM/Core/Form/Search.php +++ b/CRM/Core/Form/Search.php @@ -77,6 +77,25 @@ class CRM_Core_Form_Search extends CRM_Core_Form { */ protected $_context = NULL; + /** + * The list of tasks or actions that a searcher can perform on a result set. + * + * @var array + */ + protected $_taskList = array(); + + /** + * Builds the list of tasks or actions that a searcher can perform on a result set. + * + * To modify the task list, child classes should alter $this->_taskList, + * preferably by extending this method. + * + * @return array + */ + protected function buildTaskList() { + return $this->_taskList; + } + /** * Common buildform tasks required by all searches */ @@ -99,6 +118,11 @@ class CRM_Core_Form_Search extends CRM_Core_Form { )); $this->addClass('crm-search-form'); + + // for backwards compatibility we pass an argument to addTaskMenu even though + // it could just as well access $this->_taskList internally + $tasks = $this->buildTaskList(); + $this->addTaskMenu($tasks); } /** -- 2.25.1