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