Merge pull request #15649 from JMAConsulting/core-1346
[civicrm-core.git] / CRM / Activity / Form / Task / PickOption.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18/**
b6c94f42 19 * This class provides the functionality to email a group of contacts.
6a488035
TO
20 */
21class CRM_Activity_Form_Task_PickOption extends CRM_Activity_Form_Task {
22
23 /**
fe482240 24 * The title of the group.
6a488035
TO
25 *
26 * @var string
27 */
28 protected $_title;
29
30 /**
fe482240 31 * Maximum Activities that should be allowed to update.
62d3ee27 32 * @var int
6a488035
TO
33 */
34 protected $_maxActivities = 100;
35
36 /**
fe482240 37 * Variable to store redirect path.
62d3ee27 38 * @var int
6a488035
TO
39 */
40 protected $_userContext;
41
42 /**
fe482240 43 * Variable to store contact Ids.
62d3ee27 44 * @var array
6a488035
TO
45 */
46 public $_contacts;
47
48 /**
fe482240 49 * Build all the data structures needed to build the form.
6a488035 50 */
00be9182 51 public function preProcess() {
f813f78e 52
7808aae6 53 // initialize the task and row fields.
6a488035
TO
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) {
be2fb01f 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.", [
c5c263ca
AH
64 1 => $this->_maxActivities,
65 2 => count($this->_activityHolderIds),
be2fb01f 66 ]), ts("Maximum Exceeded"), "error");
6a488035
TO
67 $validate = TRUE;
68 }
69 // then redirect
70 if ($validate) {
71 CRM_Utils_System::redirect($this->_userContext);
72 }
73 }
74
75 /**
fe482240 76 * Build the form object.
6a488035 77 */
00be9182 78 public function buildQuickForm() {
6a488035
TO
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'));
be2fb01f 82 $this->setDefaults(['with_contact' => 1]);
f212d37d 83 $this->addDefaultButtons(ts('Continue'));
6a488035
TO
84 }
85
86 /**
fe482240 87 * Add local and global form rules.
6a488035 88 */
00be9182 89 public function addRules() {
be2fb01f 90 $this->addFormRule(['CRM_Activity_Form_Task_PickOption', 'formRule']);
6a488035
TO
91 }
92
93 /**
fe482240 94 * Global validation rules for the form.
6a488035 95 *
041ab3d1
TO
96 * @param array $fields
97 * Posted values of the form.
6a488035 98 *
a6c01b45
CW
99 * @return array
100 * list of errors to be posted back to the form
6a488035 101 */
00be9182 102 public static function formRule($fields) {
481a74f4 103 if (!isset($fields['with_contact']) &&
6a488035
TO
104 !isset($fields['assigned_to']) &&
105 !isset($fields['created_by'])
106 ) {
be2fb01f 107 return ['with_contact' => ts('You must select at least one email recipient type.')];
6a488035
TO
108 }
109 return TRUE;
110 }
111
112 /**
fe482240 113 * Process the form after the input has been submitted and validated.
6a488035
TO
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();
be2fb01f 119 $this->_contacts = [];
f813f78e 120
44f817d4 121 $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate');
034500d4 122 $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
f813f78e 123 $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
7808aae6 124 // Get assignee contacts.
6a488035
TO
125 if (!empty($params['assigned_to'])) {
126 foreach ($this->_activityHolderIds as $key => $id) {
034500d4 127 $ids = array_keys(CRM_Activity_BAO_ActivityContact::getNames($id, $assigneeID));
6a488035
TO
128 $this->_contacts = array_merge($this->_contacts, $ids);
129 }
130 }
7808aae6 131 // Get target contacts.
6a488035
TO
132 if (!empty($params['with_contact'])) {
133 foreach ($this->_activityHolderIds as $key => $id) {
034500d4 134 $ids = array_keys(CRM_Activity_BAO_ActivityContact::getNames($id, $targetID));
6a488035
TO
135 $this->_contacts = array_merge($this->_contacts, $ids);
136 }
137 }
7808aae6 138 // Get 'Added by' contacts.
6a488035
TO
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
7808aae6 147 // Bounce to pick option if no contacts to send to.
481a74f4 148 if (empty($this->_contacts)) {
6a488035
TO
149 $urlParams = "_qf_PickOption_display=true&qfKey={$params['qfKey']}";
150 $urlRedirect = CRM_Utils_System::url('civicrm/activity/search', $urlParams);
151 CRM_Core_Error::statusBounce(
7f82e636 152 ts('It appears you have no contacts with email addresses from the selected recipients.'),
6a488035
TO
153 $urlRedirect
154 );
155 }
156
157 $this->set('contacts', $this->_contacts);
158 }
96025800 159
6a488035 160}