Merge pull request #16753 from agh1/consolidated-php-version
[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 // store case id if present
33 $this->_caseId = CRM_Utils_Request::retrieve('caseid', 'String', $this, FALSE);
34 $this->_context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this);
35
36 $cid = CRM_Utils_Request::retrieve('cid', 'String', $this, FALSE);
37
38 // Allow request to specify email id rather than contact id
39 $toEmailId = CRM_Utils_Request::retrieve('email_id', 'String', $this);
40 if ($toEmailId) {
41 $toEmail = civicrm_api('email', 'getsingle', ['version' => 3, 'id' => $toEmailId]);
42 if (!empty($toEmail['email']) && !empty($toEmail['contact_id'])) {
43 $this->_toEmail = $toEmail;
44 }
45 if (!$cid) {
46 $cid = $toEmail['contact_id'];
47 $this->set('cid', $cid);
48 }
49 }
50
51 if ($cid) {
52 $cid = explode(',', $cid);
53 $displayName = [];
54
55 foreach ($cid as $val) {
56 $displayName[] = CRM_Contact_BAO_Contact::displayName($val);
57 }
58
59 CRM_Utils_System::setTitle(implode(',', $displayName) . ' - ' . ts('Email'));
60 }
61 else {
62 CRM_Utils_System::setTitle(ts('New Email'));
63 }
64 if ($this->_context === 'search') {
65 $this->_single = TRUE;
66 }
67 CRM_Contact_Form_Task_EmailCommon::preProcessFromAddress($this);
68
69 if (!$cid && $this->_context !== 'standalone') {
70 parent::preProcess();
71 }
72
73 $this->assign('single', $this->_single);
74 if (CRM_Core_Permission::check('administer CiviCRM')) {
75 $this->assign('isAdmin', 1);
76 }
77 }
78
79 /**
80 * List available tokens for this form.
81 *
82 * @return array
83 */
84 public function listTokens() {
85 $tokens = CRM_Core_SelectValues::contactTokens();
86
87 if (isset($this->_caseId) || isset($this->_caseIds)) {
88 // For a single case, list tokens relevant for only that case type
89 $caseTypeId = isset($this->_caseId) ? CRM_Core_DAO::getFieldValue('CRM_Case_DAO_Case', $this->_caseId, 'case_type_id') : NULL;
90 $tokens += CRM_Core_SelectValues::caseTokens($caseTypeId);
91 }
92
93 return $tokens;
94 }
95
96 }