3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 |
9 +--------------------------------------------------------------------+
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)
18 * @copyright CiviCRM LLC https://civicrm.org/licensing
22 class CRM_Core_Action
{
25 * Different possible actions are defined here. Keep in sync with the
26 * constant from CRM_Core_Form for various modes.
54 //make sure MAX_ACTION = 2^n - 1 ( n = total number of actions )
57 * Map the action names to the relevant constant. We perform
58 * bit manipulation operations so we can perform multiple
59 * actions on the same object if needed
64 public static $_names = [
66 'update' => self
::UPDATE
,
68 'delete' => self
::DELETE
,
69 'browse' => self
::BROWSE
,
70 'enable' => self
::ENABLE
,
71 'disable' => self
::DISABLE
,
72 'export' => self
::EXPORT
,
73 'preview' => self
::PREVIEW
,
76 'profile' => self
::PROFILE
,
77 'renew' => self
::RENEW
,
78 'detach' => self
::DETACH
,
79 'revert' => self
::REVERT
,
80 'close' => self
::CLOSE
,
81 'reopen' => self
::REOPEN
,
85 * The flipped version of the names array, initialized when used
89 public static $_description;
92 * Called by the request object to translate a string into a mask.
95 * The action to be resolved.
98 * the action mask corresponding to the input string
100 public static function resolve($str) {
103 $items = explode('|', $str);
104 $action = self
::map($items);
110 * Given a string or an array of strings, determine the bitmask
111 * for this set of actions
114 * Either a single string or an array of strings.
117 * the action mask corresponding to the input args
119 public static function map($item) {
122 if (is_array($item)) {
123 foreach ($item as $it) {
124 $mask |
= self
::mapItem($it);
129 return self
::mapItem($item);
134 * Given a string determine the bitmask for this specific string.
136 * @param string $item
137 * The input action to process.
140 * the action mask corresponding to the input string
142 public static function mapItem($item) {
143 $mask = self
::$_names[trim($item)] ??
NULL;
144 return $mask ?
$mask : 0;
149 * Given an action mask, find the corresponding description
155 * the corresponding action description
157 public static function description($mask) {
158 if (!isset(self
::$_description)) {
159 self
::$_description = array_flip(self
::$_names);
162 return CRM_Utils_Array
::value($mask, self
::$_description, 'NO DESCRIPTION SET');
166 * Given a set of links and a mask, return the html action string for
167 * the links associated with the mask
169 * @param array $links
170 * The set of link items.
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.
181 * @param null $objectName
182 * @param int $objectId
187 public static function formLink(
191 $extraULName = 'more',
192 $enclosedAllInSingleUL = FALSE,
201 // make links indexed sequentially instead of by bitmask
202 // otherwise it's next to impossible to reliably add new ones
204 foreach ($links as $bit => $link) {
209 if ($op && $objectName && $objectId) {
210 CRM_Utils_Hook
::links($op, $objectName, $objectId, $seqLinks, $mask, $values);
215 foreach ($seqLinks as $i => $link) {
216 if (!$mask ||
!array_key_exists('bit', $link) ||
($mask & $link['bit'])) {
217 $extra = isset($link['extra']) ? self
::replace($link['extra'], $values) : NULL;
219 $frontend = isset($link['fe']);
221 if (isset($link['qs']) && !CRM_Utils_System
::isNull($link['qs'])) {
222 $urlPath = CRM_Utils_System
::url(self
::replace($link['url'], $values),
223 self
::replace($link['qs'], $values), FALSE, NULL, TRUE, $frontend
227 $urlPath = CRM_Utils_Array
::value('url', $link, '#');
230 $classes = 'action-item crm-hover-button';
231 if (isset($link['ref'])) {
232 $classes .= ' ' . strtolower($link['ref']);
235 //get the user specified classes in.
236 if (isset($link['class'])) {
237 $className = is_array($link['class']) ?
implode(' ', $link['class']) : $link['class'];
238 $classes .= ' ' . strtolower($className);
241 if ($urlPath !== '#' && $frontend) {
242 $extra .= ' target="_blank"';
244 // Hack to make delete dialogs smaller
245 if (strpos($urlPath, '/delete') ||
strpos($urlPath, 'action=delete')) {
246 $classes .= " small-popup";
248 $url[] = sprintf('<a href="%s" class="%s" %s' . $extra . '>%s</a>',
251 !empty($link['title']) ?
"title='{$link['title']}' " : '',
258 if ($enclosedAllInSingleUL) {
260 CRM_Utils_String
::append($allLinks, '</li><li>', $mainLinks);
261 $allLinks = "{$extraULName}<ul class='panel'><li>{$allLinks}</li></ul>";
262 $result = "<span class='btn-slide crm-hover-button'>{$allLinks}</span>";
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>";
273 CRM_Utils_String
::append($resultLinks, '', $mainLinks);
275 $result = "<span>{$resultLinks}</span><span class='btn-slide crm-hover-button'>{$extra}</span>";
278 $result = "<span>{$resultLinks}</span>";
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.
290 * @param array $links
291 * The set of link items.
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.
297 * @param null $objectName
298 * @param int $objectId
301 * The array describing each link
303 public static function filterLinks(
315 // make links indexed sequentially instead of by bitmask
316 // otherwise it's next to impossible to reliably add new ones
318 foreach ($links as $bit => $link) {
323 if ($op && $objectName && $objectId) {
324 CRM_Utils_Hook
::links($op, $objectName, $objectId, $seqLinks, $mask, $values);
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;
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);
337 unset($seqLinks[$i]);
345 * Given a string and an array of values, substitute the real values
346 * in the placeholder in the str in the CiviCRM format
349 * The string to be replaced.
350 * @param array $values
351 * The array of values for parameter substitution in the str.
354 * the substituted string
356 public static function &replace(&$str, &$values) {
357 foreach ($values as $n => $v) {
358 $str = str_replace("%%$n%%", $v, $str);
364 * Get the mask for a permission (view, edit or null)
366 * @param array $permissions
369 * The mask for the above permission
371 public static function mask($permissions) {
373 if (!is_array($permissions) || CRM_Utils_System
::isNull($permissions)) {
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
;
380 if (in_array(CRM_Core_Permission
::DELETE
, $permissions)) {
381 $mask |
= self
::DELETE
;
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
);