Merge pull request #18360 from colemanw/fixMultiInProfile
[civicrm-core.git] / CRM / Contact / Form / Task / Email.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * This class provides the functionality to email a group of contacts.
20 */
21 class CRM_Contact_Form_Task_Email extends CRM_Contact_Form_Task {
22
23 use CRM_Contact_Form_Task_EmailTrait;
24
25 /**
26 * Build all the data structures needed to build the form.
27 *
28 * @throws \CiviCRM_API3_Exception
29 * @throws \CRM_Core_Exception
30 */
31 public function preProcess() {
32 // @todo - more of the handling in this function should be move to the trait. Notably the title part is
33 // not set on other forms that share the trait.
34 // store case id if present
35 $this->_caseId = CRM_Utils_Request::retrieve('caseid', 'String', $this, FALSE);
36 $this->_context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this);
37
38 $cid = CRM_Utils_Request::retrieve('cid', 'String', $this, FALSE);
39
40 // Allow request to specify email id rather than contact id
41 $toEmailId = CRM_Utils_Request::retrieve('email_id', 'String', $this);
42 if ($toEmailId) {
43 $toEmail = civicrm_api('email', 'getsingle', ['version' => 3, 'id' => $toEmailId]);
44 if (!empty($toEmail['email']) && !empty($toEmail['contact_id'])) {
45 $this->_toEmail = $toEmail;
46 }
47 if (!$cid) {
48 $cid = $toEmail['contact_id'];
49 $this->set('cid', $cid);
50 }
51 }
52
53 if ($cid) {
54 $cid = explode(',', $cid);
55 $displayName = [];
56
57 foreach ($cid as $val) {
58 $displayName[] = CRM_Contact_BAO_Contact::displayName($val);
59 }
60
61 CRM_Utils_System::setTitle(implode(',', $displayName) . ' - ' . ts('Email'));
62 }
63 else {
64 CRM_Utils_System::setTitle(ts('New Email'));
65 }
66 if ($this->_context === 'search') {
67 $this->_single = TRUE;
68 }
69 if ($cid || $this->_context === 'standalone') {
70 // When search context is false the parent pre-process is not set. That avoids it changing the
71 // redirect url & attempting to set the search params of the form. It may have only
72 // historical significance.
73 $this->setIsSearchContext(FALSE);
74 }
75 $this->traitPreProcess();
76 }
77
78 /**
79 * Stub function as EmailTrait calls this.
80 *
81 * @todo move some code from preProcess into here.
82 */
83 public function setContactIDs() {}
84
85 /**
86 * List available tokens for this form.
87 *
88 * @return array
89 * @throws \CRM_Core_Exception
90 */
91 public function listTokens() {
92 $tokens = CRM_Core_SelectValues::contactTokens();
93
94 if (isset($this->_caseId) || isset($this->_caseIds)) {
95 // For a single case, list tokens relevant for only that case type
96 $caseTypeId = isset($this->_caseId) ? CRM_Core_DAO::getFieldValue('CRM_Case_DAO_Case', $this->_caseId, 'case_type_id') : NULL;
97 $tokens += CRM_Core_SelectValues::caseTokens($caseTypeId);
98 }
99
100 return $tokens;
101 }
102
103 }