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