Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
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 | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 TO |
32 | */ |
33 | ||
34 | /** | |
b6c94f42 | 35 | * This class provides the functionality to email a group of contacts. |
6a488035 TO |
36 | */ |
37 | class 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. |
6a488035 TO |
48 | */ |
49 | protected $_maxActivities = 100; | |
50 | ||
51 | /** | |
fe482240 | 52 | * Variable to store redirect path. |
6a488035 TO |
53 | */ |
54 | protected $_userContext; | |
55 | ||
56 | /** | |
fe482240 | 57 | * Variable to store contact Ids. |
6a488035 TO |
58 | */ |
59 | public $_contacts; | |
60 | ||
61 | /** | |
fe482240 | 62 | * Build all the data structures needed to build the form. |
6a488035 | 63 | */ |
00be9182 | 64 | public function preProcess() { |
f813f78e | 65 | |
7808aae6 | 66 | // initialize the task and row fields. |
6a488035 TO |
67 | parent::preProcess(); |
68 | $session = CRM_Core_Session::singleton(); | |
69 | $this->_userContext = $session->readUserContext(); | |
70 | ||
71 | CRM_Utils_System::setTitle(ts('Send Email to Contacts')); | |
72 | ||
73 | $validate = FALSE; | |
74 | //validations | |
75 | if (count($this->_activityHolderIds) > $this->_maxActivities) { | |
353ffa53 | 76 | 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( |
c5c263ca AH |
77 | 1 => $this->_maxActivities, |
78 | 2 => count($this->_activityHolderIds), | |
79 | )), ts("Maximum Exceeded"), "error"); | |
6a488035 TO |
80 | $validate = TRUE; |
81 | } | |
82 | // then redirect | |
83 | if ($validate) { | |
84 | CRM_Utils_System::redirect($this->_userContext); | |
85 | } | |
86 | } | |
87 | ||
88 | /** | |
fe482240 | 89 | * Build the form object. |
6a488035 | 90 | */ |
00be9182 | 91 | public function buildQuickForm() { |
6a488035 TO |
92 | $this->addElement('checkbox', 'with_contact', ts('With Contact')); |
93 | $this->addElement('checkbox', 'assigned_to', ts('Assigned to Contact')); | |
94 | $this->addElement('checkbox', 'created_by', ts('Created by')); | |
95 | $this->setDefaults(array('with_contact' => 1)); | |
f212d37d | 96 | $this->addDefaultButtons(ts('Continue')); |
6a488035 TO |
97 | } |
98 | ||
99 | /** | |
fe482240 | 100 | * Add local and global form rules. |
6a488035 | 101 | */ |
00be9182 | 102 | public function addRules() { |
6a488035 TO |
103 | $this->addFormRule(array('CRM_Activity_Form_Task_PickOption', 'formRule')); |
104 | } | |
105 | ||
106 | /** | |
fe482240 | 107 | * Global validation rules for the form. |
6a488035 | 108 | * |
041ab3d1 TO |
109 | * @param array $fields |
110 | * Posted values of the form. | |
6a488035 | 111 | * |
a6c01b45 CW |
112 | * @return array |
113 | * list of errors to be posted back to the form | |
6a488035 | 114 | */ |
00be9182 | 115 | public static function formRule($fields) { |
481a74f4 | 116 | if (!isset($fields['with_contact']) && |
6a488035 TO |
117 | !isset($fields['assigned_to']) && |
118 | !isset($fields['created_by']) | |
119 | ) { | |
120 | return array('with_contact' => ts('You must select at least one email recipient type.')); | |
121 | } | |
122 | return TRUE; | |
123 | } | |
124 | ||
125 | /** | |
fe482240 | 126 | * Process the form after the input has been submitted and validated. |
6a488035 TO |
127 | */ |
128 | public function postProcess() { | |
129 | // Clear any formRule errors from Email form in case they came back here via Cancel button | |
130 | $this->controller->resetPage('Email'); | |
131 | $params = $this->exportValues(); | |
132 | $this->_contacts = array(); | |
f813f78e | 133 | |
e7e657f0 | 134 | $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name'); |
034500d4 | 135 | $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts); |
f813f78e | 136 | $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts); |
7808aae6 | 137 | // Get assignee contacts. |
6a488035 TO |
138 | if (!empty($params['assigned_to'])) { |
139 | foreach ($this->_activityHolderIds as $key => $id) { | |
034500d4 | 140 | $ids = array_keys(CRM_Activity_BAO_ActivityContact::getNames($id, $assigneeID)); |
6a488035 TO |
141 | $this->_contacts = array_merge($this->_contacts, $ids); |
142 | } | |
143 | } | |
7808aae6 | 144 | // Get target contacts. |
6a488035 TO |
145 | if (!empty($params['with_contact'])) { |
146 | foreach ($this->_activityHolderIds as $key => $id) { | |
034500d4 | 147 | $ids = array_keys(CRM_Activity_BAO_ActivityContact::getNames($id, $targetID)); |
6a488035 TO |
148 | $this->_contacts = array_merge($this->_contacts, $ids); |
149 | } | |
150 | } | |
7808aae6 | 151 | // Get 'Added by' contacts. |
6a488035 TO |
152 | if (!empty($params['created_by'])) { |
153 | parent::setContactIDs(); | |
154 | if (!empty($this->_contactIds)) { | |
155 | $this->_contacts = array_merge($this->_contacts, $this->_contactIds); | |
156 | } | |
157 | } | |
158 | $this->_contacts = array_unique($this->_contacts); | |
159 | ||
7808aae6 | 160 | // Bounce to pick option if no contacts to send to. |
481a74f4 | 161 | if (empty($this->_contacts)) { |
6a488035 TO |
162 | $urlParams = "_qf_PickOption_display=true&qfKey={$params['qfKey']}"; |
163 | $urlRedirect = CRM_Utils_System::url('civicrm/activity/search', $urlParams); | |
164 | CRM_Core_Error::statusBounce( | |
7f82e636 | 165 | ts('It appears you have no contacts with email addresses from the selected recipients.'), |
6a488035 TO |
166 | $urlRedirect |
167 | ); | |
168 | } | |
169 | ||
170 | $this->set('contacts', $this->_contacts); | |
171 | } | |
96025800 | 172 | |
6a488035 | 173 | } |