From e94f30115825863118ccf5069aa8368c2053638e Mon Sep 17 00:00:00 2001 From: eileen Date: Wed, 1 Apr 2020 15:14:37 +1300 Subject: [PATCH] [REF] Update Contact email form to use the trait for EmailCommon functions --- CRM/Contact/Form/Task/Email.php | 24 +++++++++------ CRM/Contact/Form/Task/EmailTrait.php | 46 +++++++++++++++++++++++++++- 2 files changed, 60 insertions(+), 10 deletions(-) diff --git a/CRM/Contact/Form/Task/Email.php b/CRM/Contact/Form/Task/Email.php index afd39cd3a3..c89033b337 100644 --- a/CRM/Contact/Form/Task/Email.php +++ b/CRM/Contact/Form/Task/Email.php @@ -29,6 +29,8 @@ class CRM_Contact_Form_Task_Email extends CRM_Contact_Form_Task { * @throws \CRM_Core_Exception */ public function preProcess() { + // @todo - more of the handling in this function should be move to the trait. Notably the title part is + // not set on other forms that share the trait. // store case id if present $this->_caseId = CRM_Utils_Request::retrieve('caseid', 'String', $this, FALSE); $this->_context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this); @@ -64,18 +66,22 @@ class CRM_Contact_Form_Task_Email extends CRM_Contact_Form_Task { if ($this->_context === 'search') { $this->_single = TRUE; } - CRM_Contact_Form_Task_EmailCommon::preProcessFromAddress($this); - - if (!$cid && $this->_context !== 'standalone') { - parent::preProcess(); - } - - $this->assign('single', $this->_single); - if (CRM_Core_Permission::check('administer CiviCRM')) { - $this->assign('isAdmin', 1); + if ($cid || $this->_context === 'standalone') { + // When search context is false the parent pre-process is not set. That avoids it changing the + // redirect url & attempting to set the search params of the form. It may have only + // historical significance. + $this->setIsSearchContext(FALSE); } + $this->traitPreProcess(); } + /** + * Stub function as EmailTrait calls this. + * + * @todo move some code from preProcess into here. + */ + public function setContactIDs() {} + /** * List available tokens for this form. * diff --git a/CRM/Contact/Form/Task/EmailTrait.php b/CRM/Contact/Form/Task/EmailTrait.php index 02602753dd..80424cba1b 100644 --- a/CRM/Contact/Form/Task/EmailTrait.php +++ b/CRM/Contact/Form/Task/EmailTrait.php @@ -68,6 +68,31 @@ trait CRM_Contact_Form_Task_EmailTrait { */ public $_bccContactIds = []; + /** + * Is the form being loaded from a search action. + * + * @var bool + */ + public $isSearchContext = TRUE; + + /** + * Getter for isSearchContext. + * + * @return bool + */ + public function isSearchContext(): bool { + return $this->isSearchContext; + } + + /** + * Setter for isSearchContext. + * + * @param bool $isSearchContext + */ + public function setIsSearchContext(bool $isSearchContext) { + $this->isSearchContext = $isSearchContext; + } + /** * Build all the data structures needed to build the form. * @@ -75,10 +100,29 @@ trait CRM_Contact_Form_Task_EmailTrait { * @throws \CRM_Core_Exception */ public function preProcess() { + $this->traitPreProcess(); + } + + /** + * Call trait preProcess function. + * + * This function exists as a transitional arrangement so classes overriding + * preProcess can still call it. Ideally it will be melded into preProcess later. + * + * @throws \CiviCRM_API3_Exception + * @throws \CRM_Core_Exception + */ + protected function traitPreProcess() { CRM_Contact_Form_Task_EmailCommon::preProcessFromAddress($this); - parent::preProcess(); + if ($this->isSearchContext()) { + // Currently only the contact email form is callable outside search context. + parent::preProcess(); + } $this->setContactIDs(); $this->assign('single', $this->_single); + if (CRM_Core_Permission::check('administer CiviCRM')) { + $this->assign('isAdmin', 1); + } } /** -- 2.25.1