3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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.
72 //make sure MAX_ACTION = 2^n - 1 ( n = total number of actions )
75 * map the action names to the relevant constant. We perform
76 * bit manipulation operations so we can perform multiple
77 * actions on the same object if needed
79 * @var array _names tupe of variable name to action constant
85 static $_names = array(
87 'update' => self
::UPDATE
,
89 'delete' => self
::DELETE
,
90 'browse' => self
::BROWSE
,
91 'enable' => self
::ENABLE
,
92 'disable' => self
::DISABLE
,
93 'export' => self
::EXPORT
,
94 'preview' => self
::PREVIEW
,
97 'profile' => self
::PROFILE
,
98 'renew' => self
::RENEW
,
99 'detach' => self
::DETACH
,
100 'revert' => self
::REVERT
,
101 'close' => self
::CLOSE
,
102 'reopen' => self
::REOPEN
,
106 * the flipped version of the names array, initialized when used
111 static $_description;
115 * called by the request object to translate a string into a mask
117 * @param string $action the action to be resolved
119 * @return int the action mask corresponding to the input string
124 static function resolve($str) {
127 $items = explode('|', $str);
128 $action = self
::map($items);
134 * Given a string or an array of strings, determine the bitmask
135 * for this set of actions
137 * @param mixed either a single string or an array of strings
139 * @return int the action mask corresponding to the input args
144 static function map($item) {
147 if (is_array($item)) {
148 foreach ($item as $it) {
149 $mask |
= self
::mapItem($it);
154 return self
::mapItem($item);
159 * Given a string determine the bitmask for this specific string
161 * @param string the input action to process
163 * @return int the action mask corresponding to the input string
168 static function mapItem($item) {
169 $mask = CRM_Utils_Array
::value(trim($item), self
::$_names);
170 return $mask ?
$mask : 0;
175 * Given an action mask, find the corresponding description
177 * @param int the action mask
179 * @return string the corresponding action description
184 static function description($mask) {
185 if (!isset($_description)) {
186 self
::$_description = array_flip(self
::$_names);
189 return CRM_Utils_Array
::value($mask, self
::$_description, 'NO DESCRIPTION SET');
193 * given a set of links and a mask, return the html action string for
194 * the links associated with the mask
196 * @param array $links the set of link items
197 * @param int $mask the mask to be used. a null mask means all items
198 * @param array $values the array of values for parameter substitution in the link items
199 * @param string $extraULName enclosed extra links in this UL.
200 * @param boolean $enclosedAllInSingleUL force to enclosed all links in single UL.
202 * @return string the html string
206 static function formLink($links,
209 $extraULName = 'more',
210 $enclosedAllInSingleUL = FALSE,
215 $config = CRM_Core_Config
::singleton();
221 if ($op && $objectName && $objectId) {
222 CRM_Utils_Hook
::links($op, $objectName, $objectId, $links, $mask);
228 foreach ($links as $m => $link) {
229 if (!$mask ||
($mask & $m)) {
230 $extra = isset($link['extra']) ? self
::replace($link['extra'], $values) : NULL;
232 $frontend = (isset($link['fe'])) ?
TRUE : FALSE;
235 if (CRM_Utils_Array
::value('qs', $link) &&
236 !CRM_Utils_System
::isNull($link['qs'])
238 $urlPath = CRM_Utils_System
::url(self
::replace($link['url'], $values),
239 self
::replace($link['qs'], $values), TRUE, NULL, TRUE, $frontend
243 $urlPath = CRM_Utils_Array
::value('url', $link);
246 $classes = 'action-item';
249 $classes .= " action-item-first";
251 if (isset($link['ref'])) {
252 $classes .= ' ' . strtolower($link['ref']);
255 //get the user specified classes in.
256 if (isset($link['class'])) {
257 $className = $link['class'];
258 if (is_array($className)) {
259 $className = implode(' ', $className);
261 $classes .= ' ' . strtolower($className);
264 $linkClasses = 'class = "' . $classes . '"';
268 $extra .= "target=_blank";
270 $url[] = sprintf('<a href="%s" %s title="%s"' . $extra . '>%s</a>',
273 CRM_Utils_Array
::value('title', $link),
278 $url[] = sprintf('<a title="%s" %s ' . $extra . '>%s</a>',
279 CRM_Utils_Array
::value('title', $link),
290 if ($enclosedAllInSingleUL) {
292 CRM_Utils_String
::append($allLinks, '</li><li>', $mainLinks);
293 $allLinks = "{$extraULName}<ul class='panel'><li>{$allLinks}</li></ul>";
294 $result = "<span class='btn-slide'>{$allLinks}</span>";
298 $extraLinks = array_splice($url, 2);
299 if (count($extraLinks) > 1) {
300 $mainLinks = array_slice($url, 0, 2);
301 CRM_Utils_String
::append($extra, '</li><li>', $extraLinks);
302 $extra = "{$extraULName}<ul class='panel'><li>{$extra}</li></ul>";
305 CRM_Utils_String
::append($resultLinks, '', $mainLinks);
307 $result = "<span>{$resultLinks}</span><span class='btn-slide'>{$extra}</span>";
310 $result = "<span>{$resultLinks}</span>";
318 * given a string and an array of values, substitute the real values
319 * in the placeholder in the str in the CiviCRM format
321 * @param string $str the string to be replaced
322 * @param array $values the array of values for parameter substitution in the str
324 * @return string the substituted string
328 static function &replace(&$str, &$values) {
329 foreach ($values as $n => $v) {
330 $str = str_replace("%%$n%%", $v, $str);
336 * get the mask for a permission (view, edit or null)
338 * @param string the permission
340 * @return int the mask for the above permission
344 static function mask($permissions) {
346 if (!is_array($permissions) || CRM_Utils_System
::isNull($permissions)) {
349 //changed structure since we are handling delete separately - CRM-4418
350 if (in_array(CRM_Core_Permission
::VIEW
, $permissions)) {
351 $mask |
= self
::VIEW | self
::EXPORT | self
::BASIC | self
::ADVANCED | self
::BROWSE | self
::MAP | self
::PROFILE
;
353 if (in_array(CRM_Core_Permission
::DELETE
, $permissions)) {
354 $mask |
= self
::DELETE
;
356 if (in_array(CRM_Core_Permission
::EDIT
, $permissions)) {
357 //make sure we make self::MAX_ACTION = 2^n - 1
358 //if we add more actions; ( n = total number of actions )
359 $mask |
= (self
::MAX_ACTION
& ~self
::DELETE
);