Merge pull request #11486 from jitendrapurohit/CRM-21622fix
[civicrm-core.git] / CRM / Core / 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 * @package CRM
30 * @copyright CiviCRM LLC (c) 2004-2017
31 */
32
33 /**
34 * Class to represent the actions that can be performed on a group of contacts used by the search forms.
35 */
36 abstract class CRM_Core_Task {
37
38 /**
39 * These constants are only used as enumerators for each of the batch tasks.
40 */
41 const
42 // General (Implemented by more than one entity)
43 GROUP_REMOVE = 1,
44 GROUP_ADD = 2,
45 PDF_LETTER = 3,
46 TASK_DELETE = 4,
47 TASK_PRINT = 5,
48 BATCH_UPDATE = 6,
49 TASK_SMS = 7,
50 TASK_EXPORT = 8,
51 TASK_EMAIL = 9,
52 TAG_ADD = 10,
53 TAG_REMOVE = 11,
54 // Contact tasks
55 SAVE_SEARCH = 12,
56 SAVE_SEARCH_UPDATE = 13,
57 CREATE_MAILING = 14,
58 DELETE_PERMANENTLY = 15,
59 LABEL_CONTACTS = 16;
60
61 /**
62 * The task array
63 *
64 * @var array
65 */
66 public static $_tasks = NULL;
67
68 /**
69 * @var string
70 * This must be defined in each child class. It is passed to the searchTasks hook.
71 * Example: $objectType = 'event';
72 */
73 static $objectType = NULL;
74
75 /**
76 * Generates a list of batch tasks available for the current entities.
77 * Each child class should populate $_tasks array and then call this parent function for shared functionality.
78 * * @return array The set of tasks for a group of contacts
79 * [ 'title' => The Task title,
80 * 'class' => The Task Form class name,
81 * 'result => Boolean. FIXME: Not sure what this is for
82 * ]
83 */
84 public static function tasks() {
85 CRM_Utils_Hook::searchTasks(self::$objectType, self::$_tasks);
86 asort(self::$_tasks);
87
88 return self::$_tasks;
89 }
90
91 /**
92 * These tasks are the core set of tasks that the user can perform
93 * on a contact / group of contacts
94 *
95 * @return array
96 * the set of tasks for a group of contacts
97 */
98 public static function taskTitles() {
99 static::tasks();
100
101 $titles = array();
102 foreach (self::$_tasks as $id => $value) {
103 $titles[$id] = $value['title'];
104 }
105
106 if (!CRM_Utils_Mail::validOutBoundMail()) {
107 unset($titles[self::TASK_EMAIL]);
108 unset($titles[self::CREATE_MAILING]);
109 }
110
111 // Users who do not have 'access deleted contacts' should NOT have the 'Delete Permanently' option in search result tasks
112 if (!CRM_Core_Permission::check('access deleted contacts') ||
113 !CRM_Core_Permission::check('delete contacts')
114 ) {
115 unset($titles[self::DELETE_PERMANENTLY]);
116 }
117 return $titles;
118 }
119
120 /**
121 * Show tasks selectively based on the permission level
122 * of the user
123 * This function should be call parent::corePermissionedTaskTitles
124 *
125 * @param int $permission
126 * @param array $params
127 * "ssID: Saved Search ID": If !empty we are in saved search context
128 *
129 * @return array
130 * set of tasks that are valid for the user
131 */
132 abstract public static function permissionedTaskTitles($permission, $params);
133
134 /**
135 * Show tasks selectively based on the permission level
136 * of the user
137 * This function should be called by permissionedTaskTitles in children
138 *
139 * @param array $tasks The array of tasks generated by permissionedTaskTitles
140 * @param int $permission
141 * @param array $params
142 * "ssID: Saved Search ID": If !empty we are in saved search context
143 *
144 * @return array
145 * set of tasks that are valid for the user
146 */
147 public static function corePermissionedTaskTitles($tasks, $permission, $params) {
148 // Only offer the "Update Smart Group" task if a smart group/saved search is already in play and we have edit permissions
149 if (!empty($params['ssID']) && ($permission == CRM_Core_Permission::EDIT)) {
150 $tasks[self::SAVE_SEARCH_UPDATE] = self::$_tasks[self::SAVE_SEARCH_UPDATE]['title'];
151 }
152 else {
153 unset($tasks[self::SAVE_SEARCH_UPDATE]);
154 }
155
156 asort($tasks);
157 return $tasks;
158 }
159
160 /**
161 * These tasks are the core set of tasks that the user can perform
162 * on participants
163 *
164 * @param int $value
165 *
166 * @return array
167 * the set of tasks for a group of participants
168 */
169 public static function getTask($value) {
170 static::tasks();
171
172 if (!CRM_Utils_Array::value($value, self::$_tasks)) {
173 // Children can specify a default task (eg. print), we don't here
174 return array();
175 }
176 return array(
177 CRM_Utils_Array::value('class', self::$_tasks[$value]),
178 CRM_Utils_Array::value('result', self::$_tasks[$value]),
179 );
180 }
181
182 /**
183 * Function to return the task information on basis of provided task's form name
184 *
185 * @param string $className
186 *
187 * @return array [ 0 => Task ID, 1 => Task Title ]
188 */
189 public static function getTaskAndTitleByClass($className) {
190 static::tasks();
191
192 foreach (self::$_tasks as $task => $value) {
193 if ((!empty($value['url']) || $task == self::TASK_EXPORT)
194 && ((is_array($value['class']) && in_array($className, $value['class']))
195 || ($value['class'] == $className))) {
196 return array($task, CRM_Utils_Array::value('title', $value));
197 }
198 }
199 return array();
200 }
201
202 }