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