Merge pull request #13809 from sushantpaste/auto-complete-search
[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 public function preProcess() {
81 // store case id if present
82 $this->_caseId = CRM_Utils_Request::retrieve('caseid', 'String', $this, FALSE);
83 $this->_context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this);
84
85 $cid = CRM_Utils_Request::retrieve('cid', 'String', $this, FALSE);
86
87 // Allow request to specify email id rather than contact id
88 $toEmailId = CRM_Utils_Request::retrieve('email_id', 'String', $this);
89 if ($toEmailId) {
90 $toEmail = civicrm_api('email', 'getsingle', ['version' => 3, 'id' => $toEmailId]);
91 if (!empty($toEmail['email']) && !empty($toEmail['contact_id'])) {
92 $this->_toEmail = $toEmail;
93 }
94 if (!$cid) {
95 $cid = $toEmail['contact_id'];
96 $this->set('cid', $cid);
97 }
98 }
99
100 if ($cid) {
101 $cid = explode(',', $cid);
102 $displayName = [];
103
104 foreach ($cid as $val) {
105 $displayName[] = CRM_Contact_BAO_Contact::displayName($val);
106 }
107
108 CRM_Utils_System::setTitle(implode(',', $displayName) . ' - ' . ts('Email'));
109 }
110 else {
111 CRM_Utils_System::setTitle(ts('New Email'));
112 }
113 CRM_Contact_Form_Task_EmailCommon::preProcessFromAddress($this);
114
115 if (!$cid && $this->_context != 'standalone') {
116 parent::preProcess();
117 }
118
119 $this->assign('single', $this->_single);
120 if (CRM_Core_Permission::check('administer CiviCRM')) {
121 $this->assign('isAdmin', 1);
122 }
123 }
124
125 /**
126 * Build the form object.
127 */
128 public function buildQuickForm() {
129 //enable form element
130 $this->assign('suppressForm', FALSE);
131 $this->assign('emailTask', TRUE);
132
133 CRM_Contact_Form_Task_EmailCommon::buildQuickForm($this);
134 }
135
136 /**
137 * Process the form after the input has been submitted and validated.
138 */
139 public function postProcess() {
140 CRM_Contact_Form_Task_EmailCommon::postProcess($this);
141 }
142
143 /**
144 * List available tokens for this form.
145 *
146 * @return array
147 */
148 public function listTokens() {
149 $tokens = CRM_Core_SelectValues::contactTokens();
150
151 if (isset($this->_caseId) || isset($this->_caseIds)) {
152 // For a single case, list tokens relevant for only that case type
153 $caseTypeId = isset($this->_caseId) ? CRM_Core_DAO::getFieldValue('CRM_Case_DAO_Case', $this->_caseId, 'case_type_id') : NULL;
154 $tokens += CRM_Core_SelectValues::caseTokens($caseTypeId);
155 }
156
157 return $tokens;
158 }
159
160 }