Merge pull request #6167 from eileenmcnaughton/master
[civicrm-core.git] / CRM / Contact / Task.php
... / ...
CommitLineData
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
32 * $Id$
33 *
34 */
35
36/**
37 * class to represent the actions that can be performed on a group of contacts
38 * used by the search forms
39 *
40 */
41class CRM_Contact_Task {
42 const
43 GROUP_CONTACTS = 1,
44 REMOVE_CONTACTS = 2,
45 TAG_CONTACTS = 3,
46 REMOVE_TAGS = 4,
47 EXPORT_CONTACTS = 5,
48 EMAIL_CONTACTS = 6,
49 SMS_CONTACTS = 7,
50 DELETE_CONTACTS = 8,
51 HOUSEHOLD_CONTACTS = 9,
52 ORGANIZATION_CONTACTS = 10,
53 RECORD_CONTACTS = 11,
54 MAP_CONTACTS = 12,
55 SAVE_SEARCH = 13,
56 SAVE_SEARCH_UPDATE = 14,
57 PRINT_CONTACTS = 15,
58 LABEL_CONTACTS = 16,
59 BATCH_UPDATE = 17,
60 ADD_EVENT = 18,
61 PRINT_FOR_CONTACTS = 19,
62 CREATE_MAILING = 20,
63 MERGE_CONTACTS = 21,
64 EMAIL_UNHOLD = 22,
65 RESTORE = 23,
66 DELETE_PERMANENTLY = 24,
67 COMMUNICATION_PREFS = 25;
68
69 /**
70 * The task array
71 *
72 * @var array
73 */
74 static $_tasks = NULL;
75
76 /**
77 * The optional task array
78 *
79 * @var array
80 */
81 static $_optionalTasks = NULL;
82
83 public static function initTasks() {
84 if (!self::$_tasks) {
85 self::$_tasks = array(
86 self::GROUP_CONTACTS => array(
87 'title' => ts('Add Contacts to Group'),
88 'class' => 'CRM_Contact_Form_Task_AddToGroup',
89 ),
90 self::REMOVE_CONTACTS => array(
91 'title' => ts('Remove Contacts from Group'),
92 'class' => 'CRM_Contact_Form_Task_RemoveFromGroup',
93 ),
94 self::TAG_CONTACTS => array(
95 'title' => ts('Tag Contacts (assign tags)'),
96 'class' => 'CRM_Contact_Form_Task_AddToTag',
97 ),
98 self::REMOVE_TAGS => array(
99 'title' => ts('Untag Contacts (remove tags)'),
100 'class' => 'CRM_Contact_Form_Task_RemoveFromTag',
101 ),
102 self::EXPORT_CONTACTS => array(
103 'title' => ts('Export Contacts'),
104 'class' => array(
105 'CRM_Export_Form_Select',
106 'CRM_Export_Form_Map',
107 ),
108 'result' => FALSE,
109 ),
110 self::EMAIL_CONTACTS => array(
111 'title' => ts('Send Email to Contacts'),
112 'class' => 'CRM_Contact_Form_Task_Email',
113 'result' => TRUE,
114 ),
115 self::DELETE_CONTACTS => array(
116 'title' => ts('Delete Contacts'),
117 'class' => 'CRM_Contact_Form_Task_Delete',
118 'result' => FALSE,
119 ),
120 self::RECORD_CONTACTS => array(
121 'title' => ts('Record Activity for Contacts'),
122 'class' => 'CRM_Activity_Form_Activity',
123 ),
124 self::SAVE_SEARCH => array(
125 'title' => ts('New Smart Group'),
126 'class' => 'CRM_Contact_Form_Task_SaveSearch',
127 'result' => TRUE,
128 ),
129 self::SAVE_SEARCH_UPDATE => array(
130 'title' => ts('Update Smart Group'),
131 'class' => 'CRM_Contact_Form_Task_SaveSearch_Update',
132 'result' => TRUE,
133 ),
134 self::PRINT_CONTACTS => array(
135 'title' => ts('Print Selected Rows'),
136 'class' => 'CRM_Contact_Form_Task_Print',
137 'result' => FALSE,
138 ),
139 self::LABEL_CONTACTS => array(
140 'title' => ts('Mailing Labels'),
141 'class' => 'CRM_Contact_Form_Task_Label',
142 'result' => TRUE,
143 ),
144 self::BATCH_UPDATE => array(
145 'title' => ts('Batch Update via Profile'),
146 'class' => array(
147 'CRM_Contact_Form_Task_PickProfile',
148 'CRM_Contact_Form_Task_Batch',
149 ),
150 'result' => TRUE,
151 ),
152 self::PRINT_FOR_CONTACTS => array(
153 'title' => ts('Print PDF Letter for Contacts'),
154 'class' => 'CRM_Contact_Form_Task_PDF',
155 'result' => TRUE,
156 ),
157 self::EMAIL_UNHOLD => array(
158 'title' => ts('Unhold Emails'),
159 'class' => 'CRM_Contact_Form_Task_Unhold',
160 ),
161 self::COMMUNICATION_PREFS => array(
162 'title' => ts('Alter Contact Communication Preferences'),
163 'class' => 'CRM_Contact_Form_Task_AlterPreferences',
164 ),
165 self::RESTORE => array(
166 'title' => ts('Restore Contacts'),
167 'class' => 'CRM_Contact_Form_Task_Delete',
168 'result' => FALSE,
169 ),
170 self::DELETE_PERMANENTLY => array(
171 'title' => ts('Delete Permanently'),
172 'class' => 'CRM_Contact_Form_Task_Delete',
173 'result' => FALSE,
174 ),
175 );
176
177 //CRM-16329, if SMS provider is configured show sms action.
178 $providersCount = CRM_SMS_BAO_Provider::activeProviderCount();
179 if ($providersCount) {
180 self::$_tasks[self::SMS_CONTACTS] = array(
181 'title' => ts('Send SMS to Contacts'),
182 'class' => 'CRM_Contact_Form_Task_SMS',
183 'result' => TRUE,
184 );
185 }
186
187 if (CRM_Contact_BAO_ContactType::isActive('Household')) {
188 $label = CRM_Contact_BAO_ContactType::getLabel('Household');
189 self::$_tasks[self::HOUSEHOLD_CONTACTS] = array(
190 'title' => ts('Add Contacts to %1',
191 array(1 => $label)
192 ),
193 'class' => 'CRM_Contact_Form_Task_AddToHousehold',
194 );
195 }
196
197 if (CRM_Contact_BAO_ContactType::isActive('Organization')) {
198 $label = CRM_Contact_BAO_ContactType::getLabel('Organization');
199 self::$_tasks[self::ORGANIZATION_CONTACTS] = array(
200 'title' => ts('Add Contacts to %1',
201 array(1 => $label)
202 ),
203 'class' => 'CRM_Contact_Form_Task_AddToOrganization',
204 );
205 }
206
207 if (CRM_Core_Permission::check('merge duplicate contacts')) {
208 self::$_tasks[self::MERGE_CONTACTS] = array(
209 'title' => ts('Merge Contacts'),
210 'class' => 'CRM_Contact_Form_Task_Merge',
211 'result' => TRUE,
212 );
213 }
214
215 //CRM-4418, check for delete
216 if (!CRM_Core_Permission::check('delete contacts')) {
217 unset(self::$_tasks[self::DELETE_CONTACTS]);
218 }
219
220 //show map action only if map provider and geoprovider are set (Google doesn't need geoprovider)
221 // should fix this to be more flexible as providers are added ??
222 $config = CRM_Core_Config::singleton();
223
224 if ($config->mapProvider &&
225 ($config->mapProvider == 'Google' ||
226 ($config->mapProvider == 'OpenStreetMaps' ||
227 $config->geoProvider == 'Google'
228 )
229 )
230 ) {
231 self::$_tasks[self::MAP_CONTACTS] = array(
232 'title' => ts('Map Contacts'),
233 'class' => 'CRM_Contact_Form_Task_Map',
234 'result' => FALSE,
235 );
236 }
237
238 if (CRM_Core_Permission::access('CiviEvent')) {
239 self::$_tasks[self::ADD_EVENT] = array(
240 'title' => ts('Add Contacts to Event'),
241 'class' => 'CRM_Event_Form_Participant',
242 );
243 }
244
245 if (defined('CIVICRM_CIVIMAIL_UI_LEGACY')) {
246 if (CRM_Core_Permission::access('CiviMail')) {
247 self::$_tasks[self::CREATE_MAILING] = array(
248 'title' => ts('Schedule/Send a Mass Mailing'),
249 'class' => array(
250 'CRM_Mailing_Form_Group',
251 'CRM_Mailing_Form_Settings',
252 'CRM_Mailing_Form_Upload',
253 'CRM_Mailing_Form_Test',
254 'CRM_Mailing_Form_Schedule',
255 ),
256 'result' => FALSE,
257 );
258 }
259 elseif (CRM_Mailing_Info::workflowEnabled() &&
260 CRM_Core_Permission::check('create mailings')
261 ) {
262 self::$_tasks[self::CREATE_MAILING] = array(
263 'title' => ts('Create a Mass Mailing'),
264 'class' => array(
265 'CRM_Mailing_Form_Group',
266 'CRM_Mailing_Form_Settings',
267 'CRM_Mailing_Form_Upload',
268 'CRM_Mailing_Form_Test',
269 ),
270 'result' => FALSE,
271 );
272 }
273 }
274 else {
275 if (CRM_Core_Permission::access('CiviMail')
276 || (CRM_Mailing_Info::workflowEnabled() && CRM_Core_Permission::check('create mailings'))
277 ) {
278 self::$_tasks[self::CREATE_MAILING] = array(
279 'title' => ts('Schedule/Send a Mass Mailing'),
280 'class' => 'CRM_Mailing_Form_Task_AdhocMailing',
281 'result' => FALSE,
282 );
283 }
284 }
285
286 self::$_tasks += CRM_Core_Component::taskList();
287
288 CRM_Utils_Hook::searchTasks('contact', self::$_tasks);
289
290 asort(self::$_tasks);
291 }
292 }
293
294 /**
295 * These tasks are the core set of tasks that the user can perform
296 * on a contact / group of contacts
297 *
298 * @return array
299 * the set of tasks for a group of contacts
300 */
301 public static function &taskTitles() {
302 self::initTasks();
303
304 $titles = array();
305 foreach (self::$_tasks as $id => $value) {
306 $titles[$id] = $value['title'];
307 }
308
309 // hack unset update saved search
310 unset($titles[self::SAVE_SEARCH_UPDATE]);
311
312 if (!CRM_Utils_Mail::validOutBoundMail()) {
313 unset($titles[self::EMAIL_CONTACTS]);
314 unset($titles[self::CREATE_MAILING]);
315 }
316
317 // CRM-6806
318 if (!CRM_Core_Permission::check('access deleted contacts') ||
319 !CRM_Core_Permission::check('delete contacts')
320 ) {
321 unset($titles[self::DELETE_PERMANENTLY]);
322 }
323 asort($titles);
324 return $titles;
325 }
326
327 /**
328 * Show tasks selectively based on the permission level
329 * of the user
330 *
331 * @param int $permission
332 * @param bool $deletedContacts
333 * Are these tasks for operating on deleted contacts?.
334 *
335 * @return array
336 * set of tasks that are valid for the user
337 */
338 public static function &permissionedTaskTitles($permission, $deletedContacts = FALSE) {
339 self::initTasks();
340 $tasks = array();
341 if ($deletedContacts) {
342 if (CRM_Core_Permission::check('access deleted contacts')) {
343 $tasks[self::RESTORE] = self::$_tasks[self::RESTORE]['title'];
344 if (CRM_Core_Permission::check('delete contacts')) {
345 $tasks[self::DELETE_PERMANENTLY] = self::$_tasks[self::DELETE_PERMANENTLY]['title'];
346 }
347 }
348 }
349 elseif ($permission == CRM_Core_Permission::EDIT) {
350 $tasks = self::taskTitles();
351 }
352 else {
353 $tasks = array(
354 self::EXPORT_CONTACTS => self::$_tasks[self::EXPORT_CONTACTS]['title'],
355 self::EMAIL_CONTACTS => self::$_tasks[self::EMAIL_CONTACTS]['title'],
356 self::LABEL_CONTACTS => self::$_tasks[self::LABEL_CONTACTS]['title'],
357 );
358
359 if (isset(self::$_tasks[self::MAP_CONTACTS]) &&
360 !empty(self::$_tasks[self::MAP_CONTACTS]['title'])
361 ) {
362 $tasks[self::MAP_CONTACTS] = self::$_tasks[self::MAP_CONTACTS]['title'];
363 }
364
365 if (isset(self::$_tasks[self::CREATE_MAILING]) &&
366 !empty(self::$_tasks[self::CREATE_MAILING]['title'])
367 ) {
368 $tasks[self::CREATE_MAILING] = self::$_tasks[self::CREATE_MAILING]['title'];
369 }
370 }
371 return $tasks;
372 }
373
374 /**
375 * These tasks get added based on the context the user is in.
376 *
377 * @return array
378 * the set of optional tasks for a group of contacts
379 */
380 public static function &optionalTaskTitle() {
381 $tasks = array(
382 self::SAVE_SEARCH_UPDATE => self::$_tasks[self::SAVE_SEARCH_UPDATE]['title'],
383 );
384 return $tasks;
385 }
386
387 /**
388 * @param $value
389 *
390 * @return array
391 */
392 public static function getTask($value) {
393 self::initTasks();
394
395 if (!CRM_Utils_Array::value($value, self::$_tasks)) {
396 // make it the print task by default
397 $value = self::PRINT_CONTACTS;
398 }
399 return array(
400 CRM_Utils_Array::value('class', self::$_tasks[$value]),
401 CRM_Utils_Array::value('result', self::$_tasks[$value]),
402 );
403 }
404
405}