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
20 class CRM_Core_Action
{
23 * Different possible actions are defined here. Keep in sync with the
24 * constant from CRM_Core_Form for various modes.
52 //make sure MAX_ACTION = 2^n - 1 ( n = total number of actions )
55 * Map the action names to the relevant constant. We perform
56 * bit manipulation operations so we can perform multiple
57 * actions on the same object if needed
62 public static $_names = [
64 'update' => self
::UPDATE
,
66 'delete' => self
::DELETE
,
67 'browse' => self
::BROWSE
,
68 'enable' => self
::ENABLE
,
69 'disable' => self
::DISABLE
,
70 'export' => self
::EXPORT
,
71 'preview' => self
::PREVIEW
,
74 'profile' => self
::PROFILE
,
75 'renew' => self
::RENEW
,
76 'detach' => self
::DETACH
,
77 'revert' => self
::REVERT
,
78 'close' => self
::CLOSE
,
79 'reopen' => self
::REOPEN
,
83 * The flipped version of the names array, initialized when used
87 public static $_description;
90 * Called by the request object to translate a string into a mask.
93 * The action to be resolved.
96 * the action mask corresponding to the input string
98 public static function resolve($str) {
101 $items = explode('|', $str);
102 $action = self
::map($items);
108 * Given a string or an array of strings, determine the bitmask
109 * for this set of actions
112 * Either a single string or an array of strings.
115 * the action mask corresponding to the input args
117 public static function map($item) {
120 if (is_array($item)) {
121 foreach ($item as $it) {
122 $mask |
= self
::mapItem($it);
127 return self
::mapItem($item);
132 * Given a string determine the bitmask for this specific string.
134 * @param string $item
135 * The input action to process.
138 * the action mask corresponding to the input string
140 public static function mapItem($item) {
141 $mask = self
::$_names[trim($item)] ??
NULL;
142 return $mask ?
$mask : 0;
147 * Given an action mask, find the corresponding description
153 * the corresponding action description
155 public static function description($mask) {
156 if (!isset(self
::$_description)) {
157 self
::$_description = array_flip(self
::$_names);
160 return CRM_Utils_Array
::value($mask, self
::$_description, 'NO DESCRIPTION SET');
164 * Given a set of links and a mask, return the html action string for
165 * the links associated with the mask
167 * @param array $links
168 * The set of link items.
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.
179 * @param null $objectName
180 * @param int $objectId
185 public static function formLink(
189 $extraULName = 'more',
190 $enclosedAllInSingleUL = FALSE,
199 // make links indexed sequentially instead of by bitmask
200 // otherwise it's next to impossible to reliably add new ones
202 foreach ($links as $bit => $link) {
207 if ($op && $objectName && $objectId) {
208 CRM_Utils_Hook
::links($op, $objectName, $objectId, $seqLinks, $mask, $values);
213 foreach ($seqLinks as $i => $link) {
214 if (!$mask ||
!array_key_exists('bit', $link) ||
($mask & $link['bit'])) {
215 $extra = isset($link['extra']) ? self
::replace($link['extra'], $values) : NULL;
217 $frontend = isset($link['fe']);
219 if (isset($link['qs']) && !CRM_Utils_System
::isNull($link['qs'])) {
220 $urlPath = CRM_Utils_System
::url(self
::replace($link['url'], $values),
221 self
::replace($link['qs'], $values), FALSE, NULL, TRUE, $frontend
225 $urlPath = CRM_Utils_Array
::value('url', $link, '#');
228 $classes = 'action-item crm-hover-button';
229 if (isset($link['ref'])) {
230 $classes .= ' ' . strtolower($link['ref']);
233 //get the user specified classes in.
234 if (isset($link['class'])) {
235 $className = is_array($link['class']) ?
implode(' ', $link['class']) : $link['class'];
236 $classes .= ' ' . strtolower($className);
239 if ($urlPath !== '#' && $frontend) {
240 $extra .= ' target="_blank"';
242 // Hack to make delete dialogs smaller
243 if (strpos($urlPath, '/delete') ||
strpos($urlPath, 'action=delete')) {
244 $classes .= " small-popup";
246 $url[] = sprintf('<a href="%s" class="%s" %s' . $extra . '>%s</a>',
249 !empty($link['title']) ?
"title='{$link['title']}' " : '',
250 empty($link['icon']) ?
$link['name'] : CRM_Core_Page
::crmIcon($link['icon'], $link['name'], TRUE, ['title' => ''])
256 if ($enclosedAllInSingleUL) {
258 CRM_Utils_String
::append($allLinks, '</li><li>', $mainLinks);
259 $allLinks = "{$extraULName}<ul class='panel'><li>{$allLinks}</li></ul>";
260 $result = "<span class='btn-slide crm-hover-button'>{$allLinks}</span>";
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>";
271 CRM_Utils_String
::append($resultLinks, '', $mainLinks);
273 $result = "<span>{$resultLinks}</span><span class='btn-slide crm-hover-button'>{$extra}</span>";
276 $result = "<span>{$resultLinks}</span>";
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.
288 * @param array $links
289 * The set of link items.
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.
295 * @param null $objectName
296 * @param int $objectId
299 * The array describing each link
301 public static function filterLinks(
313 // make links indexed sequentially instead of by bitmask
314 // otherwise it's next to impossible to reliably add new ones
316 foreach ($links as $bit => $link) {
321 if ($op && $objectName && $objectId) {
322 CRM_Utils_Hook
::links($op, $objectName, $objectId, $seqLinks, $mask, $values);
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;
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);
335 unset($seqLinks[$i]);
343 * Given a string and an array of values, substitute the real values
344 * in the placeholder in the str in the CiviCRM format
347 * The string to be replaced.
348 * @param array $values
349 * The array of values for parameter substitution in the str.
352 * the substituted string
354 public static function &replace(&$str, &$values) {
355 foreach ($values as $n => $v) {
356 $str = str_replace("%%$n%%", $v, $str);
362 * Get the mask for a permission (view, edit or null)
364 * @param array $permissions
367 * The mask for the above permission
369 public static function mask($permissions) {
371 if (!is_array($permissions) || CRM_Utils_System
::isNull($permissions)) {
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
;
378 if (in_array(CRM_Core_Permission
::DELETE
, $permissions)) {
379 $mask |
= self
::DELETE
;
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
);