3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * The core concept of the system is an action performed on an object. Typically this will be a "data model" object
30 * as specified in the API specs. We attempt to keep the number and type of actions consistent
31 * and similar across all objects (thus providing both reuse and standards)
34 * @copyright CiviCRM LLC (c) 2004-2015
38 class CRM_Core_Action
{
41 * Different possible actions are defined here. Keep in sync with the
42 * constant from CRM_Core_Form for various modes.
70 //make sure MAX_ACTION = 2^n - 1 ( n = total number of actions )
73 * Map the action names to the relevant constant. We perform
74 * bit manipulation operations so we can perform multiple
75 * actions on the same object if needed
77 * @var array $_names type of variable name to action constant
80 static $_names = array(
82 'update' => self
::UPDATE
,
84 'delete' => self
::DELETE
,
85 'browse' => self
::BROWSE
,
86 'enable' => self
::ENABLE
,
87 'disable' => self
::DISABLE
,
88 'export' => self
::EXPORT
,
89 'preview' => self
::PREVIEW
,
92 'profile' => self
::PROFILE
,
93 'renew' => self
::RENEW
,
94 'detach' => self
::DETACH
,
95 'revert' => self
::REVERT
,
96 'close' => self
::CLOSE
,
97 'reopen' => self
::REOPEN
,
101 * The flipped version of the names array, initialized when used
105 static $_description;
108 * Called by the request object to translate a string into a mask.
111 * The action to be resolved.
114 * the action mask corresponding to the input string
116 public static function resolve($str) {
119 $items = explode('|', $str);
120 $action = self
::map($items);
126 * Given a string or an array of strings, determine the bitmask
127 * for this set of actions
130 * Either a single string or an array of strings.
133 * the action mask corresponding to the input args
135 public static function map($item) {
138 if (is_array($item)) {
139 foreach ($item as $it) {
140 $mask |
= self
::mapItem($it);
145 return self
::mapItem($item);
150 * Given a string determine the bitmask for this specific string.
152 * @param string $item
153 * The input action to process.
156 * the action mask corresponding to the input string
158 public static function mapItem($item) {
159 $mask = CRM_Utils_Array
::value(trim($item), self
::$_names);
160 return $mask ?
$mask : 0;
165 * Given an action mask, find the corresponding description
171 * the corresponding action description
173 public static function description($mask) {
174 if (!isset($_description)) {
175 self
::$_description = array_flip(self
::$_names);
178 return CRM_Utils_Array
::value($mask, self
::$_description, 'NO DESCRIPTION SET');
182 * Given a set of links and a mask, return the html action string for
183 * the links associated with the mask
185 * @param array $links
186 * The set of link items.
188 * The mask to be used. a null mask means all items.
189 * @param array $values
190 * The array of values for parameter substitution in the link items.
191 * @param string $extraULName
192 * Enclosed extra links in this UL.
193 * @param bool $enclosedAllInSingleUL
194 * Force to enclosed all links in single UL.
197 * @param null $objectName
198 * @param int $objectId
203 public static function formLink(
207 $extraULName = 'more',
208 $enclosedAllInSingleUL = FALSE,
213 $config = CRM_Core_Config
::singleton();
218 // make links indexed sequentially instead of by bitmask
219 // otherwise it's next to impossible to reliably add new ones
221 foreach ($links as $bit => $link) {
226 if ($op && $objectName && $objectId) {
227 CRM_Utils_Hook
::links($op, $objectName, $objectId, $seqLinks, $mask, $values);
232 foreach ($seqLinks as $i => $link) {
233 if (!$mask ||
!array_key_exists('bit', $link) ||
($mask & $link['bit'])) {
234 $extra = isset($link['extra']) ? self
::replace($link['extra'], $values) : NULL;
236 $frontend = (isset($link['fe'])) ?
TRUE : FALSE;
238 if (isset($link['qs']) && !CRM_Utils_System
::isNull($link['qs'])) {
239 $urlPath = CRM_Utils_System
::url(self
::replace($link['url'], $values),
240 self
::replace($link['qs'], $values), TRUE, NULL, TRUE, $frontend
244 $urlPath = CRM_Utils_Array
::value('url', $link, '#');
247 $classes = 'action-item crm-hover-button';
248 if (isset($link['ref'])) {
249 $classes .= ' ' . strtolower($link['ref']);
252 //get the user specified classes in.
253 if (isset($link['class'])) {
254 $className = is_array($link['class']) ?
implode(' ', $link['class']) : $link['class'];
255 $classes .= ' ' . strtolower($className);
258 if ($urlPath !== '#' && $frontend) {
259 $extra .= ' target="_blank"';
261 // Hack to make delete dialogs smaller
262 if (strpos($urlPath, '/delete') ||
strpos($urlPath, 'action=delete')) {
263 $classes .= " small-popup";
265 $url[] = sprintf('<a href="%s" class="%s" %s' . $extra . '>%s</a>',
268 !empty($link['title']) ?
"title='{$link['title']}' " : '',
275 if ($enclosedAllInSingleUL) {
277 CRM_Utils_String
::append($allLinks, '</li><li>', $mainLinks);
278 $allLinks = "{$extraULName}<ul class='panel'><li>{$allLinks}</li></ul>";
279 $result = "<span class='btn-slide crm-hover-button'>{$allLinks}</span>";
283 $extraLinks = array_splice($url, 2);
284 if (count($extraLinks) > 1) {
285 $mainLinks = array_slice($url, 0, 2);
286 CRM_Utils_String
::append($extra, '</li><li>', $extraLinks);
287 $extra = "{$extraULName}<ul class='panel'><li>{$extra}</li></ul>";
290 CRM_Utils_String
::append($resultLinks, '', $mainLinks);
292 $result = "<span>{$resultLinks}</span><span class='btn-slide crm-hover-button'>{$extra}</span>";
295 $result = "<span>{$resultLinks}</span>";
303 * Given a string and an array of values, substitute the real values
304 * in the placeholder in the str in the CiviCRM format
307 * The string to be replaced.
308 * @param array $values
309 * The array of values for parameter substitution in the str.
312 * the substituted string
314 public static function &replace(&$str, &$values) {
315 foreach ($values as $n => $v) {
316 $str = str_replace("%%$n%%", $v, $str);
322 * Get the mask for a permission (view, edit or null)
325 * the mask for the above permission
327 public static function mask($permissions) {
329 if (!is_array($permissions) || CRM_Utils_System
::isNull($permissions)) {
332 //changed structure since we are handling delete separately - CRM-4418
333 if (in_array(CRM_Core_Permission
::VIEW
, $permissions)) {
334 $mask |
= self
::VIEW | self
::EXPORT | self
::BASIC | self
::ADVANCED | self
::BROWSE | self
::MAP | self
::PROFILE
;
336 if (in_array(CRM_Core_Permission
::DELETE
, $permissions)) {
337 $mask |
= self
::DELETE
;
339 if (in_array(CRM_Core_Permission
::EDIT
, $permissions)) {
340 //make sure we make self::MAX_ACTION = 2^n - 1
341 //if we add more actions; ( n = total number of actions )
342 $mask |
= (self
::MAX_ACTION
& ~self
::DELETE
);