Merge pull request #17277 from mattwire/fatalerrorhandler
[civicrm-core.git] / CRM / Core / Action.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 * The core concept of the system is an action performed on an object. Typically this will be a "data model" object
14 * as specified in the API specs. We attempt to keep the number and type of actions consistent
15 * and similar across all objects (thus providing both reuse and standards)
16 *
17 * @package CRM
ca5cec67 18 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
19 */
20class CRM_Core_Action {
21
22 /**
23 * Different possible actions are defined here. Keep in sync with the
24 * constant from CRM_Core_Form for various modes.
25 *
9f266042 26 * @var int
6a488035 27 */
7da04cde 28 const
6a488035
TO
29 NONE = 0,
30 ADD = 1,
31 UPDATE = 2,
32 VIEW = 4,
33 DELETE = 8,
34 BROWSE = 16,
35 ENABLE = 32,
36 DISABLE = 64,
37 EXPORT = 128,
38 BASIC = 256,
39 ADVANCED = 512,
40 PREVIEW = 1024,
41 FOLLOWUP = 2048,
42 MAP = 4096,
43 PROFILE = 8192,
44 COPY = 16384,
45 RENEW = 32768,
46 DETACH = 65536,
47 REVERT = 131072,
353ffa53
TO
48 CLOSE = 262144,
49 REOPEN = 524288,
50 MAX_ACTION = 1048575;
6a488035
TO
51
52 //make sure MAX_ACTION = 2^n - 1 ( n = total number of actions )
53
54 /**
100fef9d 55 * Map the action names to the relevant constant. We perform
6a488035
TO
56 * bit manipulation operations so we can perform multiple
57 * actions on the same object if needed
58 *
518fa0ee 59 * @var array
6a488035 60 *
6a488035 61 */
518fa0ee 62 public static $_names = [
6a488035
TO
63 'add' => self::ADD,
64 'update' => self::UPDATE,
65 'view' => self::VIEW,
66 'delete' => self::DELETE,
67 'browse' => self::BROWSE,
68 'enable' => self::ENABLE,
69 'disable' => self::DISABLE,
70 'export' => self::EXPORT,
71 'preview' => self::PREVIEW,
72 'map' => self::MAP,
73 'copy' => self::COPY,
74 'profile' => self::PROFILE,
75 'renew' => self::RENEW,
76 'detach' => self::DETACH,
77 'revert' => self::REVERT,
353ffa53
TO
78 'close' => self::CLOSE,
79 'reopen' => self::REOPEN,
be2fb01f 80 ];
6a488035
TO
81
82 /**
100fef9d 83 * The flipped version of the names array, initialized when used
6a488035
TO
84 *
85 * @var array
6a488035 86 */
518fa0ee 87 public static $_description;
6a488035
TO
88
89 /**
eceb18cc 90 * Called by the request object to translate a string into a mask.
6a488035 91 *
6a0b768e
TO
92 * @param string $str
93 * The action to be resolved.
6a488035 94 *
a6c01b45
CW
95 * @return int
96 * the action mask corresponding to the input string
6a488035 97 */
00be9182 98 public static function resolve($str) {
6a488035
TO
99 $action = 0;
100 if ($str) {
101 $items = explode('|', $str);
102 $action = self::map($items);
103 }
104 return $action;
105 }
106
107 /**
108 * Given a string or an array of strings, determine the bitmask
109 * for this set of actions
110 *
6a0b768e
TO
111 * @param mixed $item
112 * Either a single string or an array of strings.
6a488035 113 *
a6c01b45
CW
114 * @return int
115 * the action mask corresponding to the input args
6a488035 116 */
00be9182 117 public static function map($item) {
6a488035
TO
118 $mask = 0;
119
120 if (is_array($item)) {
121 foreach ($item as $it) {
122 $mask |= self::mapItem($it);
123 }
124 return $mask;
125 }
126 else {
127 return self::mapItem($item);
128 }
129 }
130
131 /**
eceb18cc 132 * Given a string determine the bitmask for this specific string.
6a488035 133 *
6a0b768e
TO
134 * @param string $item
135 * The input action to process.
6a488035 136 *
a6c01b45
CW
137 * @return int
138 * the action mask corresponding to the input string
6a488035 139 */
00be9182 140 public static function mapItem($item) {
9c1bc317 141 $mask = self::$_names[trim($item)] ?? NULL;
6a488035
TO
142 return $mask ? $mask : 0;
143 }
144
145 /**
146 *
147 * Given an action mask, find the corresponding description
148 *
6a0b768e
TO
149 * @param int $mask
150 * The action mask.
6a488035 151 *
a6c01b45
CW
152 * @return string
153 * the corresponding action description
6a488035 154 */
00be9182 155 public static function description($mask) {
0e02cb01 156 if (!isset(self::$_description)) {
6a488035
TO
157 self::$_description = array_flip(self::$_names);
158 }
159
160 return CRM_Utils_Array::value($mask, self::$_description, 'NO DESCRIPTION SET');
161 }
162
163 /**
100fef9d 164 * Given a set of links and a mask, return the html action string for
6a488035
TO
165 * the links associated with the mask
166 *
6a0b768e
TO
167 * @param array $links
168 * The set of link items.
169 * @param int $mask
170 * The mask to be used. a null mask means all items.
171 * @param array $values
172 * The array of values for parameter substitution in the link items.
173 * @param string $extraULName
174 * Enclosed extra links in this UL.
175 * @param bool $enclosedAllInSingleUL
176 * Force to enclosed all links in single UL.
da6b46f4
EM
177 *
178 * @param null $op
179 * @param null $objectName
100fef9d 180 * @param int $objectId
6a488035 181 *
a6c01b45
CW
182 * @return string
183 * the html string
6a488035 184 */
d5cc0fc2 185 public static function formLink(
f9f40af3 186 $links,
6a488035
TO
187 $mask,
188 $values,
189 $extraULName = 'more',
190 $enclosedAllInSingleUL = FALSE,
191 $op = NULL,
192 $objectName = NULL,
193 $objectId = NULL
194 ) {
6a488035
TO
195 if (empty($links)) {
196 return NULL;
197 }
198
a9b15f31
AH
199 // make links indexed sequentially instead of by bitmask
200 // otherwise it's next to impossible to reliably add new ones
be2fb01f 201 $seqLinks = [];
a9b15f31
AH
202 foreach ($links as $bit => $link) {
203 $link['bit'] = $bit;
204 $seqLinks[] = $link;
205 }
6a488035
TO
206
207 if ($op && $objectName && $objectId) {
a9b15f31 208 CRM_Utils_Hook::links($op, $objectName, $objectId, $seqLinks, $mask, $values);
6a488035
TO
209 }
210
be2fb01f 211 $url = [];
6a488035 212
a1c7d42f 213 foreach ($seqLinks as $i => $link) {
a9b15f31 214 if (!$mask || !array_key_exists('bit', $link) || ($mask & $link['bit'])) {
6a488035
TO
215 $extra = isset($link['extra']) ? self::replace($link['extra'], $values) : NULL;
216
63d76404 217 $frontend = isset($link['fe']);
6a488035 218
18d13c82 219 if (isset($link['qs']) && !CRM_Utils_System::isNull($link['qs'])) {
6a488035 220 $urlPath = CRM_Utils_System::url(self::replace($link['url'], $values),
8b25dde0 221 self::replace($link['qs'], $values), FALSE, NULL, TRUE, $frontend
6a488035
TO
222 );
223 }
224 else {
18d13c82 225 $urlPath = CRM_Utils_Array::value('url', $link, '#');
6a488035
TO
226 }
227
a1c7d42f 228 $classes = 'action-item crm-hover-button';
6a488035
TO
229 if (isset($link['ref'])) {
230 $classes .= ' ' . strtolower($link['ref']);
231 }
232
233 //get the user specified classes in.
234 if (isset($link['class'])) {
a1c7d42f 235 $className = is_array($link['class']) ? implode(' ', $link['class']) : $link['class'];
6a488035
TO
236 $classes .= ' ' . strtolower($className);
237 }
238
18d13c82
CW
239 if ($urlPath !== '#' && $frontend) {
240 $extra .= ' target="_blank"';
6a488035 241 }
a1c7d42f
CW
242 // Hack to make delete dialogs smaller
243 if (strpos($urlPath, '/delete') || strpos($urlPath, 'action=delete')) {
244 $classes .= " small-popup";
245 }
246 $url[] = sprintf('<a href="%s" class="%s" %s' . $extra . '>%s</a>',
18d13c82 247 $urlPath,
a1c7d42f
CW
248 $classes,
249 !empty($link['title']) ? "title='{$link['title']}' " : '',
18d13c82
CW
250 $link['name']
251 );
6a488035
TO
252 }
253 }
254
6a488035
TO
255 $mainLinks = $url;
256 if ($enclosedAllInSingleUL) {
257 $allLinks = '';
258 CRM_Utils_String::append($allLinks, '</li><li>', $mainLinks);
259 $allLinks = "{$extraULName}<ul class='panel'><li>{$allLinks}</li></ul>";
a1c7d42f 260 $result = "<span class='btn-slide crm-hover-button'>{$allLinks}</span>";
6a488035
TO
261 }
262 else {
263 $extra = '';
264 $extraLinks = array_splice($url, 2);
265 if (count($extraLinks) > 1) {
266 $mainLinks = array_slice($url, 0, 2);
267 CRM_Utils_String::append($extra, '</li><li>', $extraLinks);
268 $extra = "{$extraULName}<ul class='panel'><li>{$extra}</li></ul>";
269 }
270 $resultLinks = '';
271 CRM_Utils_String::append($resultLinks, '', $mainLinks);
272 if ($extra) {
a1c7d42f 273 $result = "<span>{$resultLinks}</span><span class='btn-slide crm-hover-button'>{$extra}</span>";
6a488035
TO
274 }
275 else {
276 $result = "<span>{$resultLinks}</span>";
277 }
278 }
279
280 return $result;
281 }
282
29cff47e
MWMC
283 /**
284 * Given a set of links and a mask, return a filtered (by mask) array containing the final links with parsed values
285 * and calling hooks as appropriate.
286 * Use this when passing a set of action links to the API or to the form without adding html formatting.
287 *
288 * @param array $links
289 * The set of link items.
290 * @param int $mask
291 * The mask to be used. a null mask means all items.
292 * @param array $values
293 * The array of values for parameter substitution in the link items.
294 * @param null $op
295 * @param null $objectName
296 * @param int $objectId
297 *
298 * @return array|null
299 * The array describing each link
300 */
301 public static function filterLinks(
302 $links,
303 $mask,
304 $values,
305 $op = NULL,
306 $objectName = NULL,
307 $objectId = NULL
308 ) {
309 if (empty($links)) {
310 return NULL;
311 }
312
313 // make links indexed sequentially instead of by bitmask
314 // otherwise it's next to impossible to reliably add new ones
315 $seqLinks = array();
316 foreach ($links as $bit => $link) {
317 $link['bit'] = $bit;
318 $seqLinks[] = $link;
319 }
320
321 if ($op && $objectName && $objectId) {
322 CRM_Utils_Hook::links($op, $objectName, $objectId, $seqLinks, $mask, $values);
323 }
324
325 foreach ($seqLinks as $i => $link) {
326 if (!$mask || !array_key_exists('bit', $link) || ($mask & $link['bit'])) {
327 $seqLinks[$i]['extra'] = isset($link['extra']) ? self::replace($link['extra'], $values) : NULL;
328
329 if (isset($link['qs']) && !CRM_Utils_System::isNull($link['qs'])) {
330 $seqLinks[$i]['url'] = self::replace($link['url'], $values);
331 $seqLinks[$i]['qs'] = self::replace($link['qs'], $values);
332 }
333 }
334 else {
335 unset($seqLinks[$i]);
336 }
337 }
338
339 return $seqLinks;
340 }
341
6a488035 342 /**
100fef9d 343 * Given a string and an array of values, substitute the real values
6a488035
TO
344 * in the placeholder in the str in the CiviCRM format
345 *
6a0b768e
TO
346 * @param string $str
347 * The string to be replaced.
348 * @param array $values
349 * The array of values for parameter substitution in the str.
6a488035 350 *
a6c01b45
CW
351 * @return string
352 * the substituted string
6a488035 353 */
00be9182 354 public static function &replace(&$str, &$values) {
6a488035
TO
355 foreach ($values as $n => $v) {
356 $str = str_replace("%%$n%%", $v, $str);
357 }
358 return $str;
359 }
360
361 /**
100fef9d 362 * Get the mask for a permission (view, edit or null)
6a488035 363 *
ad37ac8e 364 * @param array $permissions
365 *
a6c01b45 366 * @return int
ad37ac8e 367 * The mask for the above permission
6a488035 368 */
00be9182 369 public static function mask($permissions) {
6a488035
TO
370 $mask = NULL;
371 if (!is_array($permissions) || CRM_Utils_System::isNull($permissions)) {
372 return $mask;
373 }
374 //changed structure since we are handling delete separately - CRM-4418
375 if (in_array(CRM_Core_Permission::VIEW, $permissions)) {
376 $mask |= self::VIEW | self::EXPORT | self::BASIC | self::ADVANCED | self::BROWSE | self::MAP | self::PROFILE;
377 }
378 if (in_array(CRM_Core_Permission::DELETE, $permissions)) {
379 $mask |= self::DELETE;
380 }
381 if (in_array(CRM_Core_Permission::EDIT, $permissions)) {
382 //make sure we make self::MAX_ACTION = 2^n - 1
383 //if we add more actions; ( n = total number of actions )
384 $mask |= (self::MAX_ACTION & ~self::DELETE);
385 }
386
387 return $mask;
388 }
96025800 389
6a488035 390}