Refactored out of CRM_Core_PseudoConstant: IMProvider(), pcm(), preferredCommunicatio...
[civicrm-core.git] / CRM / Activity / Form / Task / PickOption.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2013
32 * $Id$
33 *
34 */
35
36 /**
37 * This class provides the functionality to email a group of contacts
38 */
39 class CRM_Activity_Form_Task_PickOption extends CRM_Activity_Form_Task {
40
41 /**
42 * the title of the group
43 *
44 * @var string
45 */
46 protected $_title;
47
48 /**
49 * maximum Activities that should be allowed to update
50 *
51 */
52 protected $_maxActivities = 100;
53
54 /**
55 * variable to store redirect path
56 *
57 */
58 protected $_userContext;
59
60 /**
61 * variable to store contact Ids
62 *
63 */
64 public $_contacts;
65
66 /**
67 * build all the data structures needed to build the form
68 *
69 * @return void
70 * @access public
71 */
72 function preProcess() {
73 /*
74 * initialize the task and row fields
75 */
76
77 parent::preProcess();
78 $session = CRM_Core_Session::singleton();
79 $this->_userContext = $session->readUserContext();
80
81 CRM_Utils_System::setTitle(ts('Send Email to Contacts'));
82
83 $validate = FALSE;
84 //validations
85 if (count($this->_activityHolderIds) > $this->_maxActivities) {
86 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.", array(1 => $this->_maxActivities, 2 => count($this->_activityHolderIds))), ts("Maximum Exceeded"), "error");
87 $validate = TRUE;
88 }
89 // then redirect
90 if ($validate) {
91 CRM_Utils_System::redirect($this->_userContext);
92 }
93 }
94
95 /**
96 * Build the form
97 *
98 * @access public
99 *
100 * @return void
101 */
102 function buildQuickForm() {
103 $this->addElement('checkbox', 'with_contact', ts('With Contact'));
104 $this->addElement('checkbox', 'assigned_to', ts('Assigned to Contact'));
105 $this->addElement('checkbox', 'created_by', ts('Created by'));
106 $this->setDefaults(array('with_contact' => 1));
107 $this->addDefaultButtons(ts('Continue >>'));
108 }
109
110 /**
111 * Add local and global form rules
112 *
113 * @access protected
114 *
115 * @return void
116 */
117 function addRules() {
118 $this->addFormRule(array('CRM_Activity_Form_Task_PickOption', 'formRule'));
119 }
120
121 /**
122 * global validation rules for the form
123 *
124 * @param array $fields posted values of the form
125 *
126 * @return array list of errors to be posted back to the form
127 * @static
128 * @access public
129 */
130 static function formRule($fields) {
131 if ( !isset($fields['with_contact']) &&
132 !isset($fields['assigned_to']) &&
133 !isset($fields['created_by'])
134 ) {
135 return array('with_contact' => ts('You must select at least one email recipient type.'));
136 }
137 return TRUE;
138 }
139
140 /**
141 * process the form after the input has been submitted and validated
142 *
143 * @access public
144 *
145 * @return None
146 */
147 public function postProcess() {
148 // Clear any formRule errors from Email form in case they came back here via Cancel button
149 $this->controller->resetPage('Email');
150 $params = $this->exportValues();
151 $this->_contacts = array();
152
153 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
154 $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
155 $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
156 //get assignee contacts
157 if (!empty($params['assigned_to'])) {
158 foreach ($this->_activityHolderIds as $key => $id) {
159 $ids = array_keys(CRM_Activity_BAO_ActivityContact::getNames($id, $assigneeID));
160 $this->_contacts = array_merge($this->_contacts, $ids);
161 }
162 }
163 //get target contacts
164 if (!empty($params['with_contact'])) {
165 foreach ($this->_activityHolderIds as $key => $id) {
166 $ids = array_keys(CRM_Activity_BAO_ActivityContact::getNames($id, $targetID));
167 $this->_contacts = array_merge($this->_contacts, $ids);
168 }
169 }
170 //get 'Added by' contacts
171 if (!empty($params['created_by'])) {
172 parent::setContactIDs();
173 if (!empty($this->_contactIds)) {
174 $this->_contacts = array_merge($this->_contacts, $this->_contactIds);
175 }
176 }
177 $this->_contacts = array_unique($this->_contacts);
178
179 //bounce to pick option if no contacts to send to
180 if ( empty($this->_contacts) ) {
181 $urlParams = "_qf_PickOption_display=true&qfKey={$params['qfKey']}";
182 $urlRedirect = CRM_Utils_System::url('civicrm/activity/search', $urlParams);
183 CRM_Core_Error::statusBounce(
184 ts('It appears you have no contacts with emails from the selected recipients.'),
185 $urlRedirect
186 );
187 }
188
189 $this->set('contacts', $this->_contacts);
190 }
191 }
192