CRM-15172 - Fix profile state/county required options
[civicrm-core.git] / CRM / Core / Invoke.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
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 }
54
55 catch (Exception $e) {
56 return CRM_Core_Error::handleUnhandledException($e);
57 }
58 }
59
60 /**
61 * @param $args
62 */
63 protected static function _invoke($args) {
64 if ($args[0] !== 'civicrm') {
65 return;
66 }
67
68 if (!defined('CIVICRM_SYMFONY_PATH')) {
69 try {
70 // Traditional Civi invocation path
71 self::hackMenuRebuild($args); // may exit
72 self::init($args);
73 self::hackStandalone($args);
74 $item = self::getItem($args);
75 return self::runItem($item);
76 }
77 catch (CRM_Core_EXCEPTION $e) {
78 $params = $e->getErrorData();
79 $message = $e->getMessage();
80 if (isset($params['legacy_status_bounce'])) {
81 //@todo remove this- see comments on
82 //https://github.com/eileenmcnaughton/civicrm-core/commit/ae686b09e2c987091612bb25ba0a58e520a203e7
83 CRM_Core_Error::statusBounce($params['message']);
84 }
85 else {
86 $session = CRM_Core_Session::singleton();
87 $session->setStatus(
88 $message,
89 CRM_Utils_Array::value('message_title', $params),
90 CRM_Utils_Array::value('message_type', $params, 'error')
91 );
92
93 // @todo remove this code - legacy redirect path is an interim measure for moving redirects out of BAO
94 // to somewhere slightly more acceptable. they should not be part of the exception class & should
95 // be managed @ the form level - if you find a form that is triggering this piece of code
96 // you should log a ticket for it to be removed with details about the form you were on.
97 if(!empty($params['legacy_redirect_path'])) {
98 if(CRM_Utils_System::isDevelopment()) {
99 // here we could set a message telling devs to log it per above
100 }
101 CRM_Utils_System::redirect($params['legacy_redirect_path'], $params['legacy_redirect_query']);
102 }
103 }
104 }
105 catch (Exception $e) {
106 // Recall: CRM_Core_Config is initialized before calling CRM_Core_Invoke
107 $config = CRM_Core_Config::singleton();
108 return CRM_Core_Error::handleUnhandledException($e);
109 /*
110 if ($config->backtrace) {
111 return CRM_Core_Error::formatHtmlException($e);
112 } else {
113 // TODO
114 }*/
115 }
116 } else {
117 // Symfony-based invocation path
118 require_once CIVICRM_SYMFONY_PATH . '/app/bootstrap.php.cache';
119 require_once CIVICRM_SYMFONY_PATH . '/app/AppKernel.php';
120 $kernel = new AppKernel('dev', true);
121 $kernel->loadClassCache();
122 $response = $kernel->handle(Symfony\Component\HttpFoundation\Request::createFromGlobals());
123 if (preg_match(':^text/html:', $response->headers->get('Content-Type'))) {
124 // let the CMS handle the trappings
125 return $response->getContent();
126 } else {
127 $response->send();
128 exit();
129 }
130 }
131 }
132 /**
133 * Hackish support /civicrm/menu/rebuild
134 *
135 * @param array $args list of path parts
136 * @void
137 */
138 static public function hackMenuRebuild($args) {
139 if (array('civicrm','menu','rebuild') == $args || array('civicrm', 'clearcache') == $args) {
140 // ensure that the user has a good privilege level
141 if (CRM_Core_Permission::check('administer CiviCRM')) {
142 self::rebuildMenuAndCaches();
143 CRM_Core_Session::setStatus(ts('Cleared all CiviCRM caches (database, menu, templates)'), ts('Complete'), 'success');
144 return CRM_Utils_System::redirect(); // exits
145 }
146 else {
147 CRM_Core_Error::fatal('You do not have permission to execute this url');
148 }
149 }
150 }
151
152 /**
153 * Perform general setup
154 *
155 * @param array $args list of path parts
156 * @void
157 */
158 static public function init($args) {
159 // first fire up IDS and check for bad stuff
160 $config = CRM_Core_Config::singleton();
161 if (!CRM_Core_Permission::check('skip IDS check')) {
162 $ids = new CRM_Core_IDS();
163 $ids->check($args);
164 }
165
166 // also initialize the i18n framework
167 require_once 'CRM/Core/I18n.php';
168 $i18n = CRM_Core_I18n::singleton();
169 }
170
171 /**
172 * Hackish support for /standalone/*
173 *
174 * @param array $args list of path parts
175 * @void
176 */
177 static public function hackStandalone($args) {
178 $config = CRM_Core_Config::singleton();
179 if ($config->userFramework == 'Standalone') {
180 $session = CRM_Core_Session::singleton();
181 if ($session->get('new_install') !== TRUE) {
182 CRM_Core_Standalone::sidebarLeft();
183 }
184 elseif ($args[1] == 'standalone' && $args[2] == 'register') {
185 CRM_Core_Menu::store();
186 }
187 }
188 }
189
190 /**
191 * Determine which menu $item corresponds to $args
192 *
193 * @param array $args list of path parts
194 * @return array; see CRM_Core_Menu
195 */
196 static public function getItem($args) {
197 if (is_array($args)) {
198 // get the menu items
199 $path = implode('/', $args);
200 } else {
201 $path = $args;
202 }
203 $item = CRM_Core_Menu::get($path);
204
205 // we should try to compute menus, if item is empty and stay on the same page,
206 // rather than compute and redirect to dashboard.
207 if (!$item) {
208 CRM_Core_Menu::store(FALSE);
209 $item = CRM_Core_Menu::get($path);
210 }
211
212 return $item;
213 }
214
215 /**
216 * Given a menu item, call the appropriate controller and return the response
217 *
218 * @param array $item see CRM_Core_Menu
219 * @return string, HTML
220 */
221 static public function runItem($item) {
222 $config = CRM_Core_Config::singleton();
223 if ($config->userFramework == 'Joomla' && $item) {
224 $config->userFrameworkURLVar = 'task';
225
226 // joomla 1.5RC1 seems to push this in the POST variable, which messes
227 // QF and checkboxes
228 unset($_POST['option']);
229 CRM_Core_Joomla::sidebarLeft();
230 }
231
232 // set active Component
233 $template = CRM_Core_Smarty::singleton();
234 $template->assign('activeComponent', 'CiviCRM');
235 $template->assign('formTpl', 'default');
236
237 if ($item) {
238 // CRM-7656 - make sure we send a clean sanitized path to create printer friendly url
239 $printerFriendly = CRM_Utils_System::makeURL(
240 'snippet', FALSE, FALSE,
241 CRM_Utils_Array::value('path', $item)
242 ) . '2';
243 $template->assign('printerFriendly', $printerFriendly);
244
245 if (!array_key_exists('page_callback', $item)) {
246 CRM_Core_Error::debug('Bad item', $item);
247 CRM_Core_Error::fatal(ts('Bad menu record in database'));
248 }
249
250 // check that we are permissioned to access this page
251 if (!CRM_Core_Permission::checkMenuItem($item)) {
252 CRM_Utils_System::permissionDenied();
253 return;
254 }
255
256 // check if ssl is set
257 if (!empty($item['is_ssl'])) {
258 CRM_Utils_System::redirectToSSL();
259 }
260
261 if (isset($item['title'])) {
262 CRM_Utils_System::setTitle($item['title']);
263 }
264
265 if (isset($item['breadcrumb']) && !isset($item['is_public'])) {
266 CRM_Utils_System::appendBreadCrumb($item['breadcrumb']);
267 }
268
269 $pageArgs = NULL;
270 if (!empty($item['page_arguments'])) {
271 $pageArgs = CRM_Core_Menu::getArrayForPathArgs($item['page_arguments']);
272 }
273
274 $template = CRM_Core_Smarty::singleton();
275 if (!empty($item['is_public'])) {
276 $template->assign('urlIsPublic', TRUE);
277 }
278 else {
279 $template->assign('urlIsPublic', FALSE);
280 self::versionCheck($template);
281 }
282
283 if (isset($item['return_url'])) {
284 $session = CRM_Core_Session::singleton();
285 $args = CRM_Utils_Array::value(
286 'return_url_args',
287 $item,
288 'reset=1'
289 );
290 $session->pushUserContext(CRM_Utils_System::url($item['return_url'], $args));
291 }
292
293 $result = NULL;
294 if (is_array($item['page_callback'])) {
295 require_once (str_replace('_', DIRECTORY_SEPARATOR, $item['page_callback'][0]) . '.php');
296 $result = call_user_func($item['page_callback']);
297 }
298 elseif (strstr($item['page_callback'], '_Form')) {
299 $wrapper = new CRM_Utils_Wrapper();
300 $result = $wrapper->run(
301 CRM_Utils_Array::value('page_callback', $item),
302 CRM_Utils_Array::value('title', $item),
303 isset($pageArgs) ? $pageArgs : NULL
304 );
305 }
306 else {
307 $newArgs = explode('/', $_GET[$config->userFrameworkURLVar]);
308 require_once (str_replace('_', DIRECTORY_SEPARATOR, $item['page_callback']) . '.php');
309 $mode = 'null';
310 if (isset($pageArgs['mode'])) {
311 $mode = $pageArgs['mode'];
312 unset($pageArgs['mode']);
313 }
314 $title = CRM_Utils_Array::value('title', $item);
315 if (strstr($item['page_callback'], '_Page')) {
316 $object = new $item['page_callback'] ($title, $mode );
317 $object->urlPath = explode('/', $_GET[$config->userFrameworkURLVar]);
318 }
319 elseif (strstr($item['page_callback'], '_Controller')) {
320 $addSequence = 'false';
321 if (isset($pageArgs['addSequence'])) {
322 $addSequence = $pageArgs['addSequence'];
323 $addSequence = $addSequence ? 'true' : 'false';
324 unset($pageArgs['addSequence']);
325 }
326 $object = new $item['page_callback'] ($title, true, $mode, null, $addSequence );
327 }
328 else {
329 CRM_Core_Error::fatal();
330 }
331 $result = $object->run($newArgs, $pageArgs);
332 }
333
334 CRM_Core_Session::storeSessionObjects();
335 return $result;
336 }
337
338 CRM_Core_Menu::store();
339 CRM_Core_Session::setStatus(ts('Menu has been rebuilt'), ts('Complete'), 'success');
340 return CRM_Utils_System::redirect();
341 }
342
343 /**
344 * This function contains the default action
345 *
346 * @param $action
347 *
348 * @param $contact_type
349 * @param $contact_sub_type
350 *
351 * @static
352 * @access public
353 */
354 static function form($action, $contact_type, $contact_sub_type) {
355 CRM_Utils_System::setUserContext(array('civicrm/contact/search/basic', 'civicrm/contact/view'));
356 $wrapper = new CRM_Utils_Wrapper();
357
358 $properties = CRM_Core_Component::contactSubTypeProperties($contact_sub_type, 'Edit');
359 if ($properties) {
360 $wrapper->run($properties['class'], ts('New %1', array(1 => $contact_sub_type)), $action, TRUE);
361 }
362 else {
363 $wrapper->run('CRM_Contact_Form_Contact', ts('New Contact'), $action, TRUE);
364 }
365 }
366
367 /**
368 * Show the message about CiviCRM versions
369 *
370 * @param obj: $template (reference)
371 */
372 static function versionCheck($template) {
373 if (CRM_Core_Config::isUpgradeMode()) {
374 return;
375 }
376 $versionCheck = CRM_Utils_VersionCheck::singleton();
377 $newerVersion = $versionCheck->newerVersion();
378 $template->assign('newer_civicrm_version', $newerVersion);
379 }
380
381 /**
382 * @param bool $triggerRebuild
383 * @param bool $sessionReset
384 *
385 * @throws Exception
386 */
387 static function rebuildMenuAndCaches($triggerRebuild = FALSE, $sessionReset = FALSE) {
388 $config = CRM_Core_Config::singleton();
389 $config->clearModuleList();
390
391 // also cleanup all caches
392 $config->cleanupCaches($sessionReset || CRM_Utils_Request::retrieve('sessionReset', 'Boolean', CRM_Core_DAO::$_nullObject, FALSE, 0, 'GET'));
393
394 CRM_Core_Menu::store();
395
396 // also reset navigation
397 CRM_Core_BAO_Navigation::resetNavigation();
398
399 // also cleanup module permissions
400 $config->cleanupPermissions();
401
402 // also rebuild word replacement cache
403 CRM_Core_BAO_WordReplacement::rebuild();
404
405 // Clear dynamic js files
406 CRM_Utils_File::flushDynamicResources();
407
408 CRM_Core_BAO_Setting::updateSettingsFromMetaData();
409 CRM_Core_Resources::singleton()->resetCacheCode();
410 CRM_Case_XMLRepository::singleton(TRUE);
411
412 // also rebuild triggers if requested explicitly
413 if (
414 $triggerRebuild ||
415 CRM_Utils_Request::retrieve('triggerRebuild', 'Boolean', CRM_Core_DAO::$_nullObject, FALSE, 0, 'GET')
416 ) {
417 CRM_Core_DAO::triggerRebuild();
418 }
419 CRM_Core_DAO_AllCoreTables::reinitializeCache(TRUE);
420 CRM_Core_ManagedEntities::singleton(TRUE)->reconcile();
421 }
422 }