Merge pull request #15820 from seamuslee001/dev_core_183_custom_contribsybnt
[civicrm-core.git] / CRM / Activity / Form / Task / PickOption.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_Activity_Form_Task_PickOption extends CRM_Activity_Form_Task {
22
23 /**
24 * The title of the group.
25 *
26 * @var string
27 */
28 protected $_title;
29
30 /**
31 * Maximum Activities that should be allowed to update.
32 * @var int
33 */
34 protected $_maxActivities = 100;
35
36 /**
37 * Variable to store redirect path.
38 * @var int
39 */
40 protected $_userContext;
41
42 /**
43 * Variable to store contact Ids.
44 * @var array
45 */
46 public $_contacts;
47
48 /**
49 * Build all the data structures needed to build the form.
50 */
51 public function preProcess() {
52
53 // initialize the task and row fields.
54 parent::preProcess();
55 $session = CRM_Core_Session::singleton();
56 $this->_userContext = $session->readUserContext();
57
58 CRM_Utils_System::setTitle(ts('Send Email to Contacts'));
59
60 $validate = FALSE;
61 //validations
62 if (count($this->_activityHolderIds) > $this->_maxActivities) {
63 CRM_Core_Session::setStatus(ts("The maximum number of Activities you can select to send an email is %1. You have selected %2. Please select fewer Activities from your search results and try again.", [
64 1 => $this->_maxActivities,
65 2 => count($this->_activityHolderIds),
66 ]), ts("Maximum Exceeded"), "error");
67 $validate = TRUE;
68 }
69 // then redirect
70 if ($validate) {
71 CRM_Utils_System::redirect($this->_userContext);
72 }
73 }
74
75 /**
76 * Build the form object.
77 */
78 public function buildQuickForm() {
79 $this->addElement('checkbox', 'with_contact', ts('With Contact'));
80 $this->addElement('checkbox', 'assigned_to', ts('Assigned to Contact'));
81 $this->addElement('checkbox', 'created_by', ts('Created by'));
82 $this->setDefaults(['with_contact' => 1]);
83 $this->addDefaultButtons(ts('Continue'));
84 }
85
86 /**
87 * Add local and global form rules.
88 */
89 public function addRules() {
90 $this->addFormRule(['CRM_Activity_Form_Task_PickOption', 'formRule']);
91 }
92
93 /**
94 * Global validation rules for the form.
95 *
96 * @param array $fields
97 * Posted values of the form.
98 *
99 * @return array
100 * list of errors to be posted back to the form
101 */
102 public static function formRule($fields) {
103 if (!isset($fields['with_contact']) &&
104 !isset($fields['assigned_to']) &&
105 !isset($fields['created_by'])
106 ) {
107 return ['with_contact' => ts('You must select at least one email recipient type.')];
108 }
109 return TRUE;
110 }
111
112 /**
113 * Process the form after the input has been submitted and validated.
114 */
115 public function postProcess() {
116 // Clear any formRule errors from Email form in case they came back here via Cancel button
117 $this->controller->resetPage('Email');
118 $params = $this->exportValues();
119 $this->_contacts = [];
120
121 $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate');
122 $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
123 $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
124 // Get assignee contacts.
125 if (!empty($params['assigned_to'])) {
126 foreach ($this->_activityHolderIds as $key => $id) {
127 $ids = array_keys(CRM_Activity_BAO_ActivityContact::getNames($id, $assigneeID));
128 $this->_contacts = array_merge($this->_contacts, $ids);
129 }
130 }
131 // Get target contacts.
132 if (!empty($params['with_contact'])) {
133 foreach ($this->_activityHolderIds as $key => $id) {
134 $ids = array_keys(CRM_Activity_BAO_ActivityContact::getNames($id, $targetID));
135 $this->_contacts = array_merge($this->_contacts, $ids);
136 }
137 }
138 // Get 'Added by' contacts.
139 if (!empty($params['created_by'])) {
140 parent::setContactIDs();
141 if (!empty($this->_contactIds)) {
142 $this->_contacts = array_merge($this->_contacts, $this->_contactIds);
143 }
144 }
145 $this->_contacts = array_unique($this->_contacts);
146
147 // Bounce to pick option if no contacts to send to.
148 if (empty($this->_contacts)) {
149 $urlParams = "_qf_PickOption_display=true&qfKey={$params['qfKey']}";
150 $urlRedirect = CRM_Utils_System::url('civicrm/activity/search', $urlParams);
151 CRM_Core_Error::statusBounce(
152 ts('It appears you have no contacts with email addresses from the selected recipients.'),
153 $urlRedirect
154 );
155 }
156
157 $this->set('contacts', $this->_contacts);
158 }
159
160 }