Merge pull request #15046 from 19ATF77/Fix_Activitytpl
[civicrm-core.git] / CRM / Activity / Form / Task / PickOption.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
6b83d5bd 31 * @copyright CiviCRM LLC (c) 2004-2019
6a488035
TO
32 */
33
34/**
b6c94f42 35 * This class provides the functionality to email a group of contacts.
6a488035
TO
36 */
37class CRM_Activity_Form_Task_PickOption extends CRM_Activity_Form_Task {
38
39 /**
fe482240 40 * The title of the group.
6a488035
TO
41 *
42 * @var string
43 */
44 protected $_title;
45
46 /**
fe482240 47 * Maximum Activities that should be allowed to update.
62d3ee27 48 * @var int
6a488035
TO
49 */
50 protected $_maxActivities = 100;
51
52 /**
fe482240 53 * Variable to store redirect path.
62d3ee27 54 * @var int
6a488035
TO
55 */
56 protected $_userContext;
57
58 /**
fe482240 59 * Variable to store contact Ids.
62d3ee27 60 * @var array
6a488035
TO
61 */
62 public $_contacts;
63
64 /**
fe482240 65 * Build all the data structures needed to build the form.
6a488035 66 */
00be9182 67 public function preProcess() {
f813f78e 68
7808aae6 69 // initialize the task and row fields.
6a488035
TO
70 parent::preProcess();
71 $session = CRM_Core_Session::singleton();
72 $this->_userContext = $session->readUserContext();
73
74 CRM_Utils_System::setTitle(ts('Send Email to Contacts'));
75
76 $validate = FALSE;
77 //validations
78 if (count($this->_activityHolderIds) > $this->_maxActivities) {
be2fb01f 79 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
80 1 => $this->_maxActivities,
81 2 => count($this->_activityHolderIds),
be2fb01f 82 ]), ts("Maximum Exceeded"), "error");
6a488035
TO
83 $validate = TRUE;
84 }
85 // then redirect
86 if ($validate) {
87 CRM_Utils_System::redirect($this->_userContext);
88 }
89 }
90
91 /**
fe482240 92 * Build the form object.
6a488035 93 */
00be9182 94 public function buildQuickForm() {
6a488035
TO
95 $this->addElement('checkbox', 'with_contact', ts('With Contact'));
96 $this->addElement('checkbox', 'assigned_to', ts('Assigned to Contact'));
97 $this->addElement('checkbox', 'created_by', ts('Created by'));
be2fb01f 98 $this->setDefaults(['with_contact' => 1]);
f212d37d 99 $this->addDefaultButtons(ts('Continue'));
6a488035
TO
100 }
101
102 /**
fe482240 103 * Add local and global form rules.
6a488035 104 */
00be9182 105 public function addRules() {
be2fb01f 106 $this->addFormRule(['CRM_Activity_Form_Task_PickOption', 'formRule']);
6a488035
TO
107 }
108
109 /**
fe482240 110 * Global validation rules for the form.
6a488035 111 *
041ab3d1
TO
112 * @param array $fields
113 * Posted values of the form.
6a488035 114 *
a6c01b45
CW
115 * @return array
116 * list of errors to be posted back to the form
6a488035 117 */
00be9182 118 public static function formRule($fields) {
481a74f4 119 if (!isset($fields['with_contact']) &&
6a488035
TO
120 !isset($fields['assigned_to']) &&
121 !isset($fields['created_by'])
122 ) {
be2fb01f 123 return ['with_contact' => ts('You must select at least one email recipient type.')];
6a488035
TO
124 }
125 return TRUE;
126 }
127
128 /**
fe482240 129 * Process the form after the input has been submitted and validated.
6a488035
TO
130 */
131 public function postProcess() {
132 // Clear any formRule errors from Email form in case they came back here via Cancel button
133 $this->controller->resetPage('Email');
134 $params = $this->exportValues();
be2fb01f 135 $this->_contacts = [];
f813f78e 136
44f817d4 137 $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate');
034500d4 138 $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
f813f78e 139 $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
7808aae6 140 // Get assignee contacts.
6a488035
TO
141 if (!empty($params['assigned_to'])) {
142 foreach ($this->_activityHolderIds as $key => $id) {
034500d4 143 $ids = array_keys(CRM_Activity_BAO_ActivityContact::getNames($id, $assigneeID));
6a488035
TO
144 $this->_contacts = array_merge($this->_contacts, $ids);
145 }
146 }
7808aae6 147 // Get target contacts.
6a488035
TO
148 if (!empty($params['with_contact'])) {
149 foreach ($this->_activityHolderIds as $key => $id) {
034500d4 150 $ids = array_keys(CRM_Activity_BAO_ActivityContact::getNames($id, $targetID));
6a488035
TO
151 $this->_contacts = array_merge($this->_contacts, $ids);
152 }
153 }
7808aae6 154 // Get 'Added by' contacts.
6a488035
TO
155 if (!empty($params['created_by'])) {
156 parent::setContactIDs();
157 if (!empty($this->_contactIds)) {
158 $this->_contacts = array_merge($this->_contacts, $this->_contactIds);
159 }
160 }
161 $this->_contacts = array_unique($this->_contacts);
162
7808aae6 163 // Bounce to pick option if no contacts to send to.
481a74f4 164 if (empty($this->_contacts)) {
6a488035
TO
165 $urlParams = "_qf_PickOption_display=true&qfKey={$params['qfKey']}";
166 $urlRedirect = CRM_Utils_System::url('civicrm/activity/search', $urlParams);
167 CRM_Core_Error::statusBounce(
7f82e636 168 ts('It appears you have no contacts with email addresses from the selected recipients.'),
6a488035
TO
169 $urlRedirect
170 );
171 }
172
173 $this->set('contacts', $this->_contacts);
174 }
96025800 175
6a488035 176}