send right number of parameters to function
[civicrm-core.git] / CRM / Core / Invoke.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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 *
30 * Given an argument list, invoke the appropriate CRM function
31 * Serves as a wrapper between the UserFrameWork and Core CRM
32 *
33 * @package CRM
34 * @copyright CiviCRM LLC (c) 2004-2013
35 * $Id$
36 *
37 */
38 class CRM_Core_Invoke {
39
40 /**
41 * This is the main function that is called on every click action and based on the argument
42 * respective functions are called
43 *
44 * @param $args array this array contains the arguments of the url
45 * @return string, HTML
46 *
47 * @static
48 * @access public
49 */
50 static function invoke($args) {
51 try {
52 return self::_invoke($args);
53 } catch (Exception $e) {
54 return CRM_Core_Error::handleUnhandledException($e);
55 }
56 }
57
58 protected static function _invoke($args) {
59 if ($args[0] !== 'civicrm') {
60 return;
61 }
62
63 if (!defined('CIVICRM_SYMFONY_PATH')) {
64 try {
65 // Traditional Civi invocation path
66 self::hackMenuRebuild($args); // may exit
67 self::init($args);
68 self::hackStandalone($args);
69 $item = self::getItem($args);
70 return self::runItem($item);
71 } catch (Exception $e) {
72 // Recall: CRM_Core_Config is initialized before calling CRM_Core_Invoke
73 $config = CRM_Core_Config::singleton();
74 return CRM_Core_Error::handleUnhandledException($e);
75 /*
76 if ($config->backtrace) {
77 return CRM_Core_Error::formatHtmlException($e);
78 } else {
79 // TODO
80 }*/
81 }
82 } else {
83 // Symfony-based invocation path
84 require_once CIVICRM_SYMFONY_PATH . '/app/bootstrap.php.cache';
85 require_once CIVICRM_SYMFONY_PATH . '/app/AppKernel.php';
86 $kernel = new AppKernel('dev', true);
87 $kernel->loadClassCache();
88 $response = $kernel->handle(Symfony\Component\HttpFoundation\Request::createFromGlobals());
89 // $response->send();
90 return $response->getContent();
91 }
92 }
93 /**
94 * Hackish support /civicrm/menu/rebuild
95 *
96 * @param array $args list of path parts
97 * @void
98 */
99 static public function hackMenuRebuild($args) {
100 if (array('civicrm','menu','rebuild') == $args || array('civicrm', 'clearcache') == $args) {
101 // ensure that the user has a good privilege level
102 if (CRM_Core_Permission::check('administer CiviCRM')) {
103 self::rebuildMenuAndCaches();
104 CRM_Core_Session::setStatus(ts('Cleared all CiviCRM caches (database, menu, templates)'), ts('Complete'), 'success');
105 return CRM_Utils_System::redirect(); // exits
106 }
107 else {
108 CRM_Core_Error::fatal('You do not have permission to execute this url');
109 }
110 }
111 }
112
113 /**
114 * Perform general setup
115 *
116 * @param array $args list of path parts
117 * @void
118 */
119 static public function init($args) {
120 // first fire up IDS and check for bad stuff
121 $config = CRM_Core_Config::singleton();
122 if ($config->useIDS) {
123 $ids = new CRM_Core_IDS();
124 $ids->check($args);
125 }
126
127 // also initialize the i18n framework
128 require_once 'CRM/Core/I18n.php';
129 $i18n = CRM_Core_I18n::singleton();
130 }
131
132 /**
133 * Hackish support for /standalone/*
134 *
135 * @param array $args list of path parts
136 * @void
137 */
138 static public function hackStandalone($args) {
139 $config = CRM_Core_Config::singleton();
140 if ($config->userFramework == 'Standalone') {
141 $session = CRM_Core_Session::singleton();
142 if ($session->get('new_install') !== TRUE) {
143 CRM_Core_Standalone::sidebarLeft();
144 }
145 elseif ($args[1] == 'standalone' && $args[2] == 'register') {
146 CRM_Core_Menu::store();
147 }
148 }
149 }
150
151 /**
152 * Determine which menu $item corresponds to $args
153 *
154 * @param array $args list of path parts
155 * @return array; see CRM_Core_Menu
156 */
157 static public function getItem($args) {
158 if (is_array($args)) {
159 // get the menu items
160 $path = implode('/', $args);
161 } else {
162 $path = $args;
163 }
164 $item = CRM_Core_Menu::get($path);
165
166 // we should try to compute menus, if item is empty and stay on the same page,
167 // rather than compute and redirect to dashboard.
168 if (!$item) {
169 CRM_Core_Menu::store(FALSE);
170 $item = CRM_Core_Menu::get($path);
171 }
172
173 return $item;
174 }
175
176 /**
177 * Given a menu item, call the appropriate controller and return the response
178 *
179 * @param array $item see CRM_Core_Menu
180 * @return string, HTML
181 */
182 static public function runItem($item) {
183 $config = CRM_Core_Config::singleton();
184 if ($config->userFramework == 'Joomla' && $item) {
185 $config->userFrameworkURLVar = 'task';
186
187 // joomla 1.5RC1 seems to push this in the POST variable, which messes
188 // QF and checkboxes
189 unset($_POST['option']);
190 CRM_Core_Joomla::sidebarLeft();
191 }
192
193 // set active Component
194 $template = CRM_Core_Smarty::singleton();
195 $template->assign('activeComponent', 'CiviCRM');
196 $template->assign('formTpl', 'default');
197
198 if ($item) {
199 // CRM-7656 - make sure we send a clean sanitized path to create printer friendly url
200 $printerFriendly = CRM_Utils_System::makeURL(
201 'snippet', FALSE, FALSE,
202 CRM_Utils_Array::value('path', $item)
203 ) . '2';
204 $template->assign('printerFriendly', $printerFriendly);
205
206 if (!array_key_exists('page_callback', $item)) {
207 CRM_Core_Error::debug('Bad item', $item);
208 CRM_Core_Error::fatal(ts('Bad menu record in database'));
209 }
210
211 // check that we are permissioned to access this page
212 if (!CRM_Core_Permission::checkMenuItem($item)) {
213 CRM_Utils_System::permissionDenied();
214 return;
215 }
216
217 // check if ssl is set
218 if (CRM_Utils_Array::value('is_ssl', $item)) {
219 CRM_Utils_System::redirectToSSL();
220 }
221
222 if (isset($item['title'])) {
223 CRM_Utils_System::setTitle($item['title']);
224 }
225
226 if (isset($item['breadcrumb']) && !isset($item['is_public'])) {
227 CRM_Utils_System::appendBreadCrumb($item['breadcrumb']);
228 }
229
230 $pageArgs = NULL;
231 if (CRM_Utils_Array::value('page_arguments', $item)) {
232 $pageArgs = CRM_Core_Menu::getArrayForPathArgs($item['page_arguments']);
233 }
234
235 $template = CRM_Core_Smarty::singleton();
236 if (!empty($item['is_public'])) {
237 $template->assign('urlIsPublic', TRUE);
238 }
239 else {
240 $template->assign('urlIsPublic', FALSE);
241 self::versionCheck($template);
242 }
243
244 if (isset($item['return_url'])) {
245 $session = CRM_Core_Session::singleton();
246 $args = CRM_Utils_Array::value(
247 'return_url_args',
248 $item,
249 'reset=1'
250 );
251 $session->pushUserContext(CRM_Utils_System::url($item['return_url'], $args));
252 }
253
254 $result = NULL;
255 if (is_array($item['page_callback'])) {
256 $newArgs = explode('/', $_GET[$config->userFrameworkURLVar]);
257 require_once (str_replace('_', DIRECTORY_SEPARATOR, $item['page_callback'][0]) . '.php');
258 $result = call_user_func($item['page_callback'], $newArgs);
259 }
260 elseif (strstr($item['page_callback'], '_Form')) {
261 $wrapper = new CRM_Utils_Wrapper();
262 $result = $wrapper->run(
263 CRM_Utils_Array::value('page_callback', $item),
264 CRM_Utils_Array::value('title', $item),
265 isset($pageArgs) ? $pageArgs : NULL
266 );
267 }
268 else {
269 $newArgs = explode('/', $_GET[$config->userFrameworkURLVar]);
270 require_once (str_replace('_', DIRECTORY_SEPARATOR, $item['page_callback']) . '.php');
271 $mode = 'null';
272 if (isset($pageArgs['mode'])) {
273 $mode = $pageArgs['mode'];
274 unset($pageArgs['mode']);
275 }
276 $title = CRM_Utils_Array::value('title', $item);
277 if (strstr($item['page_callback'], '_Page')) {
278 $object = new $item['page_callback'] ($title, $mode );
279 }
280 elseif (strstr($item['page_callback'], '_Controller')) {
281 $addSequence = 'false';
282 if (isset($pageArgs['addSequence'])) {
283 $addSequence = $pageArgs['addSequence'];
284 $addSequence = $addSequence ? 'true' : 'false';
285 unset($pageArgs['addSequence']);
286 }
287 $object = new $item['page_callback'] ($title, true, $mode, null, $addSequence );
288 }
289 else {
290 CRM_Core_Error::fatal();
291 }
292 $result = $object->run($newArgs, $pageArgs);
293 }
294
295 CRM_Core_Session::storeSessionObjects();
296 return $result;
297 }
298
299 CRM_Core_Menu::store();
300 CRM_Core_Session::setStatus(ts('Menu has been rebuilt'), ts('Complete'), 'success');
301 return CRM_Utils_System::redirect();
302 }
303
304 /**
305 * This function contains the default action
306 *
307 * @param $action
308 *
309 * @static
310 * @access public
311 */
312 static function form($action, $contact_type, $contact_sub_type) {
313 CRM_Utils_System::setUserContext(array('civicrm/contact/search/basic', 'civicrm/contact/view'));
314 $wrapper = new CRM_Utils_Wrapper();
315
316 $properties = CRM_Core_Component::contactSubTypeProperties($contact_sub_type, 'Edit');
317 if ($properties) {
318 $wrapper->run($properties['class'], ts('New %1', array(1 => $contact_sub_type)), $action, TRUE);
319 }
320 else {
321 $wrapper->run('CRM_Contact_Form_Contact', ts('New Contact'), $action, TRUE);
322 }
323 }
324
325 /**
326 * This function contains the actions for profile arguments
327 *
328 * @param $args array this array contains the arguments of the url
329 *
330 * @static
331 * @access public
332 */
333 static function profile($args) {
334 if ($args[1] !== 'profile') {
335 return;
336 }
337
338 $secondArg = CRM_Utils_Array::value(2, $args, '');
339
340 if ($secondArg == 'map') {
341 $controller = new CRM_Core_Controller_Simple(
342 'CRM_Contact_Form_Task_Map',
343 ts('Map Contact'),
344 NULL, FALSE, FALSE, TRUE
345 );
346
347 $gids = explode(',', CRM_Utils_Request::retrieve('gid', 'String', CRM_Core_DAO::$_nullObject, FALSE, 0, 'GET'));
348
349 if (count($gids) > 1) {
350 foreach ($gids as $pfId) {
351 $profileIds[] = CRM_Utils_Type::escape($pfId, 'Positive');
352 }
353 $controller->set('gid', $profileIds[0]);
354 $profileGID = $profileIds[0];
355 }
356 else {
357 $profileGID = CRM_Utils_Request::retrieve('gid', 'Integer', $controller, TRUE);
358 }
359
360
361 // make sure that this profile enables mapping
362 // CRM-8609
363 $isMap =
364 CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $profileGID, 'is_map');
365 if (!$isMap) {
366 CRM_Core_Error::statusBounce(ts('This profile does not have the map feature turned on.'));
367 }
368
369 $profileView = CRM_Utils_Request::retrieve('pv', 'Integer', $controller, FALSE);
370
371 // set the userContext stack
372 $session = CRM_Core_Session::singleton();
373 if ($profileView) {
374 $session->pushUserContext(CRM_Utils_System::url('civicrm/profile/view'));
375 }
376 else {
377 $session->pushUserContext(CRM_Utils_System::url('civicrm/profile', 'force=1'));
378 }
379
380 $controller->set('profileGID', $profileGID);
381 $controller->process();
382 return $controller->run();
383 }
384
385 if ($secondArg == 'edit' || $secondArg == 'create') {
386 // set the userContext stack
387 $session = CRM_Core_Session::singleton();
388 $session->pushUserContext(CRM_Utils_System::url('civicrm/profile', 'reset=1'));
389
390 $buttonType = CRM_Utils_Array::value('_qf_Edit_cancel', $_POST);
391 // CRM-5849: we should actually check the button *type*, but we get the *value*, potentially translated;
392 // we should keep both English and translated checks just to make sure we also handle untranslated Cancels
393 if ($buttonType == 'Cancel' or $buttonType == ts('Cancel')) {
394 $cancelURL = CRM_Utils_Request::retrieve('cancelURL',
395 'String',
396 CRM_Core_DAO::$_nullObject,
397 FALSE,
398 NULL,
399 $_POST
400 );
401 if ($cancelURL) {
402 CRM_Utils_System::redirect($cancelURL);
403 }
404 }
405
406 if ($secondArg == 'edit') {
407 $controller = new CRM_Core_Controller_Simple('CRM_Profile_Form_Edit',
408 ts('Create Profile'),
409 CRM_Core_Action::UPDATE,
410 FALSE, FALSE, TRUE
411 );
412 $controller->set('edit', 1);
413 $controller->process();
414 return $controller->run();
415 }
416 else {
417 $wrapper = new CRM_Utils_Wrapper();
418 return $wrapper->run('CRM_Profile_Form_Edit',
419 ts('Create Profile'),
420 array(
421 'mode' => CRM_Core_Action::ADD,
422 'ignoreKey' => TRUE,
423 )
424 );
425 }
426 }
427
428 $page = new CRM_Profile_Page_Listings();
429 return $page->run();
430 }
431
432 /**
433 * Show the message about CiviCRM versions
434 *
435 * @param obj: $template (reference)
436 */
437 static function versionCheck($template) {
438 if (CRM_Core_Config::isUpgradeMode()) {
439 return;
440 }
441 $versionCheck = CRM_Utils_VersionCheck::singleton();
442 $newerVersion = $versionCheck->newerVersion();
443 $template->assign('newer_civicrm_version', $newerVersion);
444 }
445
446 static function rebuildMenuAndCaches($triggerRebuild = FALSE, $sessionReset = FALSE) {
447 $config = CRM_Core_Config::singleton();
448 $config->clearModuleList();
449
450 CRM_Core_Menu::store();
451
452 // also reset navigation
453 CRM_Core_BAO_Navigation::resetNavigation();
454
455 // also cleanup all caches
456 $config->cleanupCaches($sessionReset || CRM_Utils_Request::retrieve('sessionReset', 'Boolean', CRM_Core_DAO::$_nullObject, FALSE, 0, 'GET'));
457
458 // also cleanup module permissions
459 $config->cleanupPermissions();
460
461 CRM_Core_BAO_SETTING::updateSettingsFromMetaData();
462 CRM_Core_Resources::singleton()->resetCacheCode();
463
464 // also rebuild triggers if requested explicitly
465 if (
466 $triggerRebuild ||
467 CRM_Utils_Request::retrieve('triggerRebuild', 'Boolean', CRM_Core_DAO::$_nullObject, FALSE, 0, 'GET')
468 ) {
469 CRM_Core_DAO::triggerRebuild();
470 }
471
472 CRM_Core_ManagedEntities::singleton(TRUE)->reconcile();
473 }
474 }
475