freeze the contact field in non standalone context
[civicrm-core.git] / CRM / Core / Invoke.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
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
6b83d5bd 34 * @copyright CiviCRM LLC (c) 2004-2019
6a488035
TO
35 */
36class CRM_Core_Invoke {
37
38 /**
be7dea3f
TO
39 * This is the main front-controller that integrates with the CMS. Any
40 * page-request that is sent to the CMS and intended for CiviCRM should
41 * be processed by invoke().
6a488035 42 *
be7dea3f
TO
43 * @param array $args
44 * The parts of the URL which identify the intended CiviCRM page
45 * (e.g. array('civicrm', 'event', 'register')).
46 * @return string
47 * HTML. For non-HTML content, invoke() may call print() and exit().
6a488035 48 *
6a488035 49 */
00be9182 50 public static function invoke($args) {
6a488035
TO
51 try {
52 return self::_invoke($args);
dcc4f6a7 53 }
dcc4f6a7 54 catch (Exception $e) {
be7dea3f 55 CRM_Core_Error::handleUnhandledException($e);
6a488035
TO
56 }
57 }
58
a0ee3941 59 /**
be7dea3f
TO
60 * This is the same as invoke(), but it does *not* include exception
61 * handling.
62 *
63 * @param array $args
64 * The parts of the URL which identify the intended CiviCRM page
65 * (e.g. array('civicrm', 'event', 'register')).
66 * @return string
67 * HTML. For non-HTML content, invoke() may call print() and exit().
a0ee3941 68 */
be7dea3f 69 public static function _invoke($args) {
6a488035 70 if ($args[0] !== 'civicrm') {
408b79bf 71 return NULL;
6a488035 72 }
1ebbf8bf
NG
73 // CRM-15901: Turn off PHP errors display for all ajax calls
74 if (CRM_Utils_Array::value(1, $args) == 'ajax' || CRM_Utils_Array::value('snippet', $_REQUEST)) {
75 ini_set('display_errors', 0);
76 }
6a488035
TO
77
78 if (!defined('CIVICRM_SYMFONY_PATH')) {
be7dea3f 79 // Traditional Civi invocation path
518fa0ee
SL
80 // may exit
81 self::hackMenuRebuild($args);
be7dea3f 82 self::init($args);
be7dea3f
TO
83 $item = self::getItem($args);
84 return self::runItem($item);
0db6c3e1
TO
85 }
86 else {
6a488035
TO
87 // Symfony-based invocation path
88 require_once CIVICRM_SYMFONY_PATH . '/app/bootstrap.php.cache';
89 require_once CIVICRM_SYMFONY_PATH . '/app/AppKernel.php';
4eeb9a5b 90 $kernel = new AppKernel('dev', TRUE);
6a488035
TO
91 $kernel->loadClassCache();
92 $response = $kernel->handle(Symfony\Component\HttpFoundation\Request::createFromGlobals());
c24c4679
TO
93 if (preg_match(':^text/html:', $response->headers->get('Content-Type'))) {
94 // let the CMS handle the trappings
95 return $response->getContent();
0db6c3e1
TO
96 }
97 else {
c24c4679
TO
98 $response->send();
99 exit();
100 }
6a488035
TO
101 }
102 }
353ffa53 103
6a488035
TO
104 /**
105 * Hackish support /civicrm/menu/rebuild
106 *
6a0b768e
TO
107 * @param array $args
108 * List of path parts.
6a488035
TO
109 * @void
110 */
518fa0ee 111 public static function hackMenuRebuild($args) {
be2fb01f 112 if (['civicrm', 'menu', 'rebuild'] == $args || ['civicrm', 'clearcache'] == $args) {
6a488035
TO
113 // ensure that the user has a good privilege level
114 if (CRM_Core_Permission::check('administer CiviCRM')) {
115 self::rebuildMenuAndCaches();
116 CRM_Core_Session::setStatus(ts('Cleared all CiviCRM caches (database, menu, templates)'), ts('Complete'), 'success');
518fa0ee
SL
117 // exits
118 return CRM_Utils_System::redirect();
6a488035
TO
119 }
120 else {
121 CRM_Core_Error::fatal('You do not have permission to execute this url');
122 }
123 }
124 }
125
126 /**
d09edf64 127 * Perform general setup.
6a488035 128 *
6a0b768e
TO
129 * @param array $args
130 * List of path parts.
6a488035
TO
131 * @void
132 */
518fa0ee 133 public static function init($args) {
6a488035
TO
134 // first fire up IDS and check for bad stuff
135 $config = CRM_Core_Config::singleton();
6a488035
TO
136
137 // also initialize the i18n framework
138 require_once 'CRM/Core/I18n.php';
139 $i18n = CRM_Core_I18n::singleton();
140 }
141
6a488035
TO
142 /**
143 * Determine which menu $item corresponds to $args
144 *
6a0b768e
TO
145 * @param array $args
146 * List of path parts.
6a488035
TO
147 * @return array; see CRM_Core_Menu
148 */
518fa0ee 149 public static function getItem($args) {
6a488035
TO
150 if (is_array($args)) {
151 // get the menu items
152 $path = implode('/', $args);
0db6c3e1
TO
153 }
154 else {
6a488035
TO
155 $path = $args;
156 }
157 $item = CRM_Core_Menu::get($path);
158
159 // we should try to compute menus, if item is empty and stay on the same page,
160 // rather than compute and redirect to dashboard.
161 if (!$item) {
162 CRM_Core_Menu::store(FALSE);
163 $item = CRM_Core_Menu::get($path);
164 }
165
166 return $item;
167 }
168
169 /**
170 * Given a menu item, call the appropriate controller and return the response
171 *
6a0b768e
TO
172 * @param array $item
173 * See CRM_Core_Menu.
6a488035
TO
174 * @return string, HTML
175 */
518fa0ee 176 public static function runItem($item) {
76adcecc
TO
177 $ids = new CRM_Core_IDS();
178 $ids->check($item);
179
6a488035
TO
180 $config = CRM_Core_Config::singleton();
181 if ($config->userFramework == 'Joomla' && $item) {
182 $config->userFrameworkURLVar = 'task';
183
184 // joomla 1.5RC1 seems to push this in the POST variable, which messes
185 // QF and checkboxes
186 unset($_POST['option']);
187 CRM_Core_Joomla::sidebarLeft();
188 }
189
190 // set active Component
191 $template = CRM_Core_Smarty::singleton();
192 $template->assign('activeComponent', 'CiviCRM');
193 $template->assign('formTpl', 'default');
194
195 if ($item) {
196 // CRM-7656 - make sure we send a clean sanitized path to create printer friendly url
197 $printerFriendly = CRM_Utils_System::makeURL(
353ffa53
TO
198 'snippet', FALSE, FALSE,
199 CRM_Utils_Array::value('path', $item)
200 ) . '2';
6a488035
TO
201 $template->assign('printerFriendly', $printerFriendly);
202
203 if (!array_key_exists('page_callback', $item)) {
204 CRM_Core_Error::debug('Bad item', $item);
205 CRM_Core_Error::fatal(ts('Bad menu record in database'));
206 }
207
208 // check that we are permissioned to access this page
209 if (!CRM_Core_Permission::checkMenuItem($item)) {
210 CRM_Utils_System::permissionDenied();
408b79bf 211 return NULL;
6a488035
TO
212 }
213
214 // check if ssl is set
a7488080 215 if (!empty($item['is_ssl'])) {
6a488035
TO
216 CRM_Utils_System::redirectToSSL();
217 }
218
219 if (isset($item['title'])) {
220 CRM_Utils_System::setTitle($item['title']);
221 }
222
223 if (isset($item['breadcrumb']) && !isset($item['is_public'])) {
224 CRM_Utils_System::appendBreadCrumb($item['breadcrumb']);
225 }
226
227 $pageArgs = NULL;
a7488080 228 if (!empty($item['page_arguments'])) {
6a488035
TO
229 $pageArgs = CRM_Core_Menu::getArrayForPathArgs($item['page_arguments']);
230 }
231
232 $template = CRM_Core_Smarty::singleton();
233 if (!empty($item['is_public'])) {
234 $template->assign('urlIsPublic', TRUE);
235 }
236 else {
237 $template->assign('urlIsPublic', FALSE);
06576a03 238 self::statusCheck($template);
6a488035
TO
239 }
240
241 if (isset($item['return_url'])) {
242 $session = CRM_Core_Session::singleton();
243 $args = CRM_Utils_Array::value(
244 'return_url_args',
245 $item,
246 'reset=1'
247 );
248 $session->pushUserContext(CRM_Utils_System::url($item['return_url'], $args));
249 }
250
251 $result = NULL;
c8074a93
TO
252 // WISHLIST: Refactor this. Instead of pattern-matching on page_callback, lookup
253 // page_callback via Civi\Core\Resolver and check the implemented interfaces. This
254 // would require rethinking the default constructor.
255 if (is_array($item['page_callback']) || strpos($item['page_callback'], ':')) {
256 $result = call_user_func(Civi\Core\Resolver::singleton()->get($item['page_callback']));
6a488035
TO
257 }
258 elseif (strstr($item['page_callback'], '_Form')) {
259 $wrapper = new CRM_Utils_Wrapper();
260 $result = $wrapper->run(
261 CRM_Utils_Array::value('page_callback', $item),
262 CRM_Utils_Array::value('title', $item),
263 isset($pageArgs) ? $pageArgs : NULL
264 );
265 }
266 else {
267 $newArgs = explode('/', $_GET[$config->userFrameworkURLVar]);
6a488035
TO
268 $mode = 'null';
269 if (isset($pageArgs['mode'])) {
270 $mode = $pageArgs['mode'];
271 unset($pageArgs['mode']);
272 }
273 $title = CRM_Utils_Array::value('title', $item);
99218b4b 274 if (strstr($item['page_callback'], '_Page') || strstr($item['page_callback'], '\\Page\\')) {
408b79bf 275 $object = new $item['page_callback']($title, $mode);
6c2473d5 276 $object->urlPath = explode('/', $_GET[$config->userFrameworkURLVar]);
6a488035 277 }
99218b4b 278 elseif (strstr($item['page_callback'], '_Controller') || strstr($item['page_callback'], '\\Controller\\')) {
6a488035
TO
279 $addSequence = 'false';
280 if (isset($pageArgs['addSequence'])) {
281 $addSequence = $pageArgs['addSequence'];
282 $addSequence = $addSequence ? 'true' : 'false';
283 unset($pageArgs['addSequence']);
284 }
408b79bf 285 $object = new $item['page_callback']($title, TRUE, $mode, NULL, $addSequence);
6a488035
TO
286 }
287 else {
288 CRM_Core_Error::fatal();
289 }
290 $result = $object->run($newArgs, $pageArgs);
291 }
292
293 CRM_Core_Session::storeSessionObjects();
294 return $result;
295 }
296
297 CRM_Core_Menu::store();
298 CRM_Core_Session::setStatus(ts('Menu has been rebuilt'), ts('Complete'), 'success');
299 return CRM_Utils_System::redirect();
300 }
301
302 /**
d09edf64 303 * This function contains the default action.
6a488035
TO
304 *
305 * @param $action
306 *
77b97be7
EM
307 * @param $contact_type
308 * @param $contact_sub_type
309 *
6a488035 310 */
00be9182 311 public static function form($action, $contact_type, $contact_sub_type) {
be2fb01f 312 CRM_Utils_System::setUserContext(['civicrm/contact/search/basic', 'civicrm/contact/view']);
6a488035
TO
313 $wrapper = new CRM_Utils_Wrapper();
314
315 $properties = CRM_Core_Component::contactSubTypeProperties($contact_sub_type, 'Edit');
316 if ($properties) {
be2fb01f 317 $wrapper->run($properties['class'], ts('New %1', [1 => $contact_sub_type]), $action, TRUE);
6a488035
TO
318 }
319 else {
320 $wrapper->run('CRM_Contact_Form_Contact', ts('New Contact'), $action, TRUE);
321 }
322 }
323
6a488035 324 /**
f55dd135 325 * Show status in the footer (admin only)
6a488035 326 *
fa8dc18c 327 * @param CRM_Core_Smarty $template
6a488035 328 */
097c681e 329 public static function statusCheck($template) {
f55dd135 330 if (CRM_Core_Config::isUpgradeMode() || !CRM_Core_Permission::check('administer CiviCRM')) {
097c681e
AH
331 return;
332 }
f55dd135 333 // always use cached results - they will be refreshed by the session timer
b1fc1ab0 334 $status = Civi::cache('checks')->get('systemStatusCheckResult');
f55dd135 335 $template->assign('footer_status_severity', $status);
f608a24a 336 $template->assign('footer_status_message', CRM_Utils_Check::toStatusLabel($status));
097c681e 337 }
6a488035 338
a0ee3941
EM
339 /**
340 * @param bool $triggerRebuild
341 * @param bool $sessionReset
342 *
343 * @throws Exception
344 */
00be9182 345 public static function rebuildMenuAndCaches($triggerRebuild = FALSE, $sessionReset = FALSE) {
6a488035
TO
346 $config = CRM_Core_Config::singleton();
347 $config->clearModuleList();
348
ae2cab23
TO
349 // also cleanup all caches
350 $config->cleanupCaches($sessionReset || CRM_Utils_Request::retrieve('sessionReset', 'Boolean', CRM_Core_DAO::$_nullObject, FALSE, 0, 'GET'));
351
6a488035
TO
352 CRM_Core_Menu::store();
353
354 // also reset navigation
355 CRM_Core_BAO_Navigation::resetNavigation();
356
6a488035
TO
357 // also cleanup module permissions
358 $config->cleanupPermissions();
359
9762f6ff
CW
360 // rebuild word replacement cache - pass false to prevent operations redundant with this fn
361 CRM_Core_BAO_WordReplacement::rebuild(FALSE);
76dca235 362
76bd16ab 363 Civi::service('settings_manager')->flush();
9762f6ff 364 // Clear js caches
4cc9b813 365 CRM_Core_Resources::singleton()->flushStrings()->resetCacheCode();
ab89fdde 366 CRM_Case_XMLRepository::singleton(TRUE);
1fcf16cc 367
6a488035
TO
368 // also rebuild triggers if requested explicitly
369 if (
370 $triggerRebuild ||
371 CRM_Utils_Request::retrieve('triggerRebuild', 'Boolean', CRM_Core_DAO::$_nullObject, FALSE, 0, 'GET')
372 ) {
373 CRM_Core_DAO::triggerRebuild();
374 }
95a90cba 375 CRM_Core_DAO_AllCoreTables::reinitializeCache(TRUE);
6a488035
TO
376 CRM_Core_ManagedEntities::singleton(TRUE)->reconcile();
377 }
96025800 378
6a488035 379}