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