Merge pull request #16914 from eileenmcnaughton/act2
[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 /**
24 * Are we operating in "single mode".
25 *
26 * Single mode means sending email to one specific contact.
27 *
28 * @var bool
29 */
30 public $_single = FALSE;
31
32 /**
33 * Are we operating in "single mode", i.e. sending email to one
34 * specific contact?
35 *
36 * @var bool
37 */
38 public $_noEmails = FALSE;
39
40 /**
41 * All the existing templates in the system.
42 *
43 * @var array
44 */
45 public $_templates = NULL;
46
47 /**
48 * Store "to" contact details.
49 * @var array
50 */
51 public $_toContactDetails = [];
52
53 /**
54 * Store all selected contact id's, that includes to, cc and bcc contacts
55 * @var array
56 */
57 public $_allContactIds = [];
58
59 /**
60 * Store only "to" contact ids.
61 * @var array
62 */
63 public $_toContactIds = [];
64
65 /**
66 * Store only "cc" contact ids.
67 * @var array
68 */
69 public $_ccContactIds = [];
70
71 /**
72 * Store only "bcc" contact ids.
73 * @var array
74 */
75 public $_bccContactIds = [];
76
77 /**
78 * Build all the data structures needed to build the form.
79 *
80 * @throws \CiviCRM_API3_Exception
81 * @throws \CRM_Core_Exception
82 */
83 public function preProcess() {
84 // store case id if present
85 $this->_caseId = CRM_Utils_Request::retrieve('caseid', 'String', $this, FALSE);
86 $this->_context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this);
87
88 $cid = CRM_Utils_Request::retrieve('cid', 'String', $this, FALSE);
89
90 // Allow request to specify email id rather than contact id
91 $toEmailId = CRM_Utils_Request::retrieve('email_id', 'String', $this);
92 if ($toEmailId) {
93 $toEmail = civicrm_api('email', 'getsingle', ['version' => 3, 'id' => $toEmailId]);
94 if (!empty($toEmail['email']) && !empty($toEmail['contact_id'])) {
95 $this->_toEmail = $toEmail;
96 }
97 if (!$cid) {
98 $cid = $toEmail['contact_id'];
99 $this->set('cid', $cid);
100 }
101 }
102
103 if ($cid) {
104 $cid = explode(',', $cid);
105 $displayName = [];
106
107 foreach ($cid as $val) {
108 $displayName[] = CRM_Contact_BAO_Contact::displayName($val);
109 }
110
111 CRM_Utils_System::setTitle(implode(',', $displayName) . ' - ' . ts('Email'));
112 }
113 else {
114 CRM_Utils_System::setTitle(ts('New Email'));
115 }
116 if ($this->_context === 'search') {
117 $this->_single = TRUE;
118 }
119 CRM_Contact_Form_Task_EmailCommon::preProcessFromAddress($this);
120
121 if (!$cid && $this->_context !== 'standalone') {
122 parent::preProcess();
123 }
124
125 $this->assign('single', $this->_single);
126 if (CRM_Core_Permission::check('administer CiviCRM')) {
127 $this->assign('isAdmin', 1);
128 }
129 }
130
131 /**
132 * Build the form object.
133 */
134 public function buildQuickForm() {
135 //enable form element
136 $this->assign('suppressForm', FALSE);
137 $this->assign('emailTask', TRUE);
138
139 CRM_Contact_Form_Task_EmailCommon::buildQuickForm($this);
140 }
141
142 /**
143 * Process the form after the input has been submitted and validated.
144 */
145 public function postProcess() {
146 CRM_Contact_Form_Task_EmailCommon::postProcess($this);
147 }
148
149 /**
150 * List available tokens for this form.
151 *
152 * @return array
153 */
154 public function listTokens() {
155 $tokens = CRM_Core_SelectValues::contactTokens();
156
157 if (isset($this->_caseId) || isset($this->_caseIds)) {
158 // For a single case, list tokens relevant for only that case type
159 $caseTypeId = isset($this->_caseId) ? CRM_Core_DAO::getFieldValue('CRM_Case_DAO_Case', $this->_caseId, 'case_type_id') : NULL;
160 $tokens += CRM_Core_SelectValues::caseTokens($caseTypeId);
161 }
162
163 return $tokens;
164 }
165
166 }