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