Merge pull request #23970 from MegaphoneJon/false-not-zero
[civicrm-core.git] / CRM / Core / Controller.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 * This class acts as our base controller class and adds additional
14 * functionality and smarts to the base QFC. Specifically we create
15 * our own action classes and handle the transitions ourselves by
16 * simulating a state machine. We also create direct jump links to any
17 * page that can be used universally.
18 *
19 * This concept has been discussed on the PEAR list and the QFC FAQ
20 * goes into a few details. Please check
21 * http://pear.php.net/manual/en/package.html.html-quickform-controller.faq.php
22 * for other useful tips and suggestions
23 *
24 * @package CRM
ca5cec67 25 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
26 */
27
28require_once 'HTML/QuickForm/Controller.php';
29require_once 'HTML/QuickForm/Action/Direct.php';
30
28518c90
EM
31/**
32 * Class CRM_Core_Controller
33 */
6a488035
TO
34class CRM_Core_Controller extends HTML_QuickForm_Controller {
35
36 /**
d09edf64 37 * The title associated with this controller.
6a488035
TO
38 *
39 * @var string
40 */
41 protected $_title;
42
43 /**
d09edf64 44 * The key associated with this controller.
6a488035
TO
45 *
46 * @var string
47 */
48 public $_key;
49
50 /**
d09edf64 51 * The name of the session scope where values are stored.
6a488035
TO
52 *
53 * @var object
54 */
55 protected $_scope;
56
57 /**
d09edf64 58 * The state machine associated with this controller.
6a488035
TO
59 *
60 * @var object
61 */
62 protected $_stateMachine;
63
64 /**
65 * Is this object being embedded in another object. If
66 * so the display routine needs to not do any work. (The
67 * parent object takes care of the display)
68 *
d51c6add 69 * @var bool
6a488035
TO
70 */
71 protected $_embedded = FALSE;
72
73 /**
74 * After entire form execution complete,
75 * do we want to skip control redirection.
76 * Default - It get redirect to user context.
77 *
78 * Useful when we run form in non civicrm context
79 * and we need to transfer control back.(eg. drupal)
80 *
d51c6add 81 * @var bool
6a488035
TO
82 */
83 protected $_skipRedirection = FALSE;
84
85 /**
86 * Are we in print mode? if so we need to modify the display
87 * functionality to do a minimal display :)
88 *
2beb47b8
BT
89 * @var int|string
90 * Should match a CRM_Core_Smarty::PRINT_* constant,
91 * or equal 0 if not in print mode
6a488035
TO
92 */
93 public $_print = 0;
94
95 /**
96 * Should we generate a qfKey, true by default
97 *
d51c6add 98 * @var bool
6a488035
TO
99 */
100 public $_generateQFKey = TRUE;
101
102 /**
d09edf64 103 * QF response type.
4bf0b605
AH
104 *
105 * @var string
6a488035
TO
106 */
107 public $_QFResponseType = 'html';
108
109 /**
d09edf64 110 * Cache the smarty template for efficiency reasons.
6a488035
TO
111 *
112 * @var CRM_Core_Smarty
113 */
114 static protected $_template;
115
116 /**
d09edf64 117 * Cache the session for efficiency reasons.
6a488035
TO
118 *
119 * @var CRM_Core_Session
120 */
121 static protected $_session;
122
123 /**
d09edf64 124 * The parent of this form if embedded.
6a488035
TO
125 *
126 * @var object
127 */
128 protected $_parent = NULL;
129
130 /**
d09edf64 131 * The destination if set will override the destination the code wants to send it to.
6a488035 132 *
e97c66ff 133 * @var string
6a488035
TO
134 */
135 public $_destination = NULL;
136
3ab88a8c
DL
137 /**
138 * The entry url for a top level form or wizard. Typically the URL with a reset=1
139 * used to redirect back to when we land into some session wierdness
140 *
141 * @var string
142 */
143 public $_entryURL = NULL;
144
6a488035
TO
145 /**
146 * All CRM single or multi page pages should inherit from this class.
147 *
4bf0b605
AH
148 * @param string $title
149 * Descriptive title of the controller.
150 * @param bool $modal
151 * Whether controller is modal.
152 * @param mixed $mode
153 * @param string $scope
154 * Name of session if we want unique scope, used only by Controller_Simple.
155 * @param bool $addSequence
156 * Should we add a unique sequence number to the end of the key.
2aa397bc 157 * @param bool $ignoreKey
4bf0b605 158 * Should we not set a qfKey for this controller (for standalone forms).
e97c66ff 159 *
160 * @throws \CRM_Core_Exception
6a488035 161 */
7c550ca0 162 public function __construct(
6a488035
TO
163 $title = NULL,
164 $modal = TRUE,
165 $mode = NULL,
166 $scope = NULL,
167 $addSequence = FALSE,
168 $ignoreKey = FALSE
169 ) {
170 // this has to true for multiple tab session fix
171 $addSequence = TRUE;
172
173 // let the constructor initialize this, should happen only once
174 if (!isset(self::$_template)) {
175 self::$_template = CRM_Core_Smarty::singleton();
176 self::$_session = CRM_Core_Session::singleton();
03f46250 177 self::$_template->ensureVariablesAreAssigned(['formTpl']);
6a488035
TO
178 }
179
3ab88a8c
DL
180 // lets try to get it from the session and/or the request vars
181 // we do this early on in case there is a fatal error in retrieving the
182 // key and/or session
7c550ca0
WA
183 $this->_entryURL
184 = CRM_Utils_Request::retrieve('entryURL', 'String', $this);
3ab88a8c 185
6a488035 186 // add a unique validable key to the name
e69299b1 187 $name = CRM_Utils_System::getClassName($this);
6a488035
TO
188 if ($name == 'CRM_Core_Controller_Simple' && !empty($scope)) {
189 // use form name if we have, since its a lot better and
190 // definitely different for different forms
191 $name = $scope;
192 }
353ffa53 193 $name = $name . '_' . $this->key($name, $addSequence, $ignoreKey);
6a488035
TO
194 $this->_title = $title;
195 if ($scope) {
196 $this->_scope = $scope;
197 }
198 else {
199 $this->_scope = CRM_Utils_System::getClassName($this);
200 }
201 $this->_scope = $this->_scope . '_' . $this->_key;
202
203 // only use the civicrm cache if we have a valid key
204 // else we clash with other users CRM-7059
205 if (!empty($this->_key)) {
be2fb01f 206 CRM_Core_Session::registerAndRetrieveSessionObjects([
6a488035 207 "_{$name}_container",
be2fb01f
CW
208 ['CiviCRM', $this->_scope],
209 ]);
6a488035
TO
210 }
211
6ef04c72 212 parent::__construct($name, $modal);
6a488035 213
9c1bc317 214 $snippet = $_REQUEST['snippet'] ?? NULL;
6a488035
TO
215 if ($snippet) {
216 if ($snippet == 3) {
217 $this->_print = CRM_Core_Smarty::PRINT_PDF;
218 }
219 elseif ($snippet == 4) {
220 // this is used to embed fragments of a form
221 $this->_print = CRM_Core_Smarty::PRINT_NOFORM;
222 self::$_template->assign('suppressForm', TRUE);
223 $this->_generateQFKey = FALSE;
224 }
225 elseif ($snippet == 5) {
da8f8f42
CW
226 // mode deprecated in favor of json
227 // still used by dashlets, probably nothing else
6a488035
TO
228 $this->_print = CRM_Core_Smarty::PRINT_NOFORM;
229 }
fc05b8da 230 // Respond with JSON if in AJAX context (also support legacy value '6')
be2fb01f 231 elseif (in_array($snippet, [CRM_Core_Smarty::PRINT_JSON, 6])) {
fc05b8da 232 $this->_print = CRM_Core_Smarty::PRINT_JSON;
6a488035
TO
233 $this->_QFResponseType = 'json';
234 }
235 else {
236 $this->_print = CRM_Core_Smarty::PRINT_SNIPPET;
237 }
238 }
239
2aa397bc 240 // if the request has a reset value, initialize the controller session
a7488080 241 if (!empty($_GET['reset'])) {
6a488035 242 $this->reset();
3ab88a8c
DL
243
244 // in this case we'll also cache the url as a hidden form variable, this allows us to
245 // redirect in case the session has disappeared on us
246 $this->_entryURL = CRM_Utils_System::makeURL(NULL, TRUE, FALSE, NULL, TRUE);
093fd901
SL
247 // In WordPress Shortcodes the standard entryURL generated via makeURL doesn't generally have id=x&reset=1 included so we add them here
248 // This prevents infinite loops caused when the session has timed out.
249 if (stripos($this->_entryURL, 'id') === FALSE && (stripos($this->_entryURL, 'transact') !== FALSE || stripos($this->_entryURL, 'register') !== FALSE)) {
250 $this->_entryURL .= '&id=' . CRM_Utils_Request::retrieveValue('id', 'Positive') . '&reset=1';
251 }
3ab88a8c 252 $this->set('entryURL', $this->_entryURL);
6a488035
TO
253 }
254
255 // set the key in the session
256 // do this at the end so we have initialized the object
257 // and created the scope etc
258 $this->set('qfKey', $this->_key);
259
6a488035 260 // also retrieve and store destination in session
3ab88a8c
DL
261 $this->_destination = CRM_Utils_Request::retrieve(
262 'civicrmDestination',
263 'String',
264 $this,
265 FALSE,
266 NULL,
267 $_REQUEST
6a488035
TO
268 );
269 }
270
00be9182 271 public function fini() {
be2fb01f 272 CRM_Core_BAO_Cache::storeSessionToCache([
518fa0ee
SL
273 "_{$this->_name}_container",
274 ['CiviCRM', $this->_scope],
275 ], TRUE);
6a488035
TO
276 }
277
a0ee3941 278 /**
100fef9d 279 * @param string $name
a0ee3941
EM
280 * @param bool $addSequence
281 * @param bool $ignoreKey
282 *
283 * @return mixed|string
284 */
00be9182 285 public function key($name, $addSequence = FALSE, $ignoreKey = FALSE) {
6a488035
TO
286 $config = CRM_Core_Config::singleton();
287
288 if (
289 $ignoreKey ||
290 (isset($config->keyDisable) && $config->keyDisable)
291 ) {
292 return NULL;
293 }
294
cc59f8f9
RLAR
295 // We need a form key. Check _POST first, then _GET.
296 // @todo Note: we currently have to check $_REQUEST, too, since that
297 // is currently overwritten by civicrm_api3_contribution_page_validate.
298 // It's bad form to use $_REQUEST because it's ambiguous; and it's bad form
299 // to change superglobals anyway. If PR
300 // https://github.com/civicrm/civicrm-core/pull/17324
301 // and/or related get merged, then we should remove the REQUEST reference here.
302 $key = $_POST['qfKey'] ?? $_GET['qfKey'] ?? $_REQUEST['qfKey'] ?? NULL;
2584c49d 303 if (!$key && in_array($_SERVER['REQUEST_METHOD'], ['GET', 'HEAD'])) {
cc59f8f9
RLAR
304 // Generate a key if this is an initial request without one.
305 // We allow HEAD here because it is used by bots to validate URLs, so if
306 // we issue a 500 server error to them they may think the site is broken.
6a488035
TO
307 $key = CRM_Core_Key::get($name, $addSequence);
308 }
309 else {
cc59f8f9
RLAR
310 // Other requests that usually change data (POST, but feasibly DELETE,
311 // PUT, PATCH...) always require a valid key.
6a488035
TO
312 $key = CRM_Core_Key::validate($key, $name, $addSequence);
313 }
314
315 if (!$key) {
c02edd0e 316 $this->invalidKey();
6a488035
TO
317 }
318
319 $this->_key = $key;
320
321 return $key;
322 }
323
324 /**
325 * Process the request, overrides the default QFC run method
326 * This routine actually checks if the QFC is modal and if it
327 * is the first invalid page, if so it call the requested action
328 * if not, it calls the display action on the first invalid page
329 * avoids the issue of users hitting the back button and getting
330 * a broken page
331 *
332 * This run is basically a composition of the original run and the
333 * jump action
334 *
4bf0b605 335 * @return mixed
6a488035 336 */
00be9182 337 public function run() {
6a488035
TO
338 // the names of the action and page should be saved
339 // note that this is split into two, because some versions of
340 // php 5.x core dump on the triple assignment :)
341 $this->_actionName = $this->getActionName();
342 list($pageName, $action) = $this->_actionName;
343
344 if ($this->isModal()) {
345 if (!$this->isValid($pageName)) {
346 $pageName = $this->findInvalid();
347 $action = 'display';
348 }
349 }
350
351 // note that based on action, control might not come back!!
352 // e.g. if action is a valid JUMP, u basically do a redirect
353 // to the appropriate place
354 $this->wizardHeader($pageName);
355 return $this->_pages[$pageName]->handle($action);
356 }
357
a0ee3941
EM
358 /**
359 * @return bool
360 */
00be9182 361 public function validate() {
6a488035
TO
362 $this->_actionName = $this->getActionName();
363 list($pageName, $action) = $this->_actionName;
364
365 $page = &$this->_pages[$pageName];
366
367 $data = &$this->container();
368 $this->applyDefaults($pageName);
369 $page->isFormBuilt() or $page->buildForm();
370 // We use defaults and constants as if they were submitted
371 $data['values'][$pageName] = $page->exportValues();
372 $page->loadValues($data['values'][$pageName]);
373 // Is the page now valid?
374 if (TRUE === ($data['valid'][$pageName] = $page->validate())) {
375 return TRUE;
376 }
377 return $page->_errors;
378 }
379
380 /**
8eedd10a 381 * Helper function to add all the needed default actions.
382 *
383 * Note that the framework redefines all of the default QFC actions.
6a488035 384 *
7c550ca0
WA
385 * @param string $uploadDirectory to store all the uploaded files
386 * @param array $uploadNames for the various upload buttons (note u can have more than 1 upload)
6a488035 387 */
00be9182 388 public function addActions($uploadDirectory = NULL, $uploadNames = NULL) {
be2fb01f 389 $names = [
6a488035
TO
390 'display' => 'CRM_Core_QuickForm_Action_Display',
391 'next' => 'CRM_Core_QuickForm_Action_Next',
392 'back' => 'CRM_Core_QuickForm_Action_Back',
393 'process' => 'CRM_Core_QuickForm_Action_Process',
394 'cancel' => 'CRM_Core_QuickForm_Action_Cancel',
395 'refresh' => 'CRM_Core_QuickForm_Action_Refresh',
396 'reload' => 'CRM_Core_QuickForm_Action_Reload',
397 'done' => 'CRM_Core_QuickForm_Action_Done',
398 'jump' => 'CRM_Core_QuickForm_Action_Jump',
399 'submit' => 'CRM_Core_QuickForm_Action_Submit',
be2fb01f 400 ];
6a488035
TO
401
402 foreach ($names as $name => $classPath) {
403 $action = new $classPath($this->_stateMachine);
404 $this->addAction($name, $action);
405 }
406
407 $this->addUploadAction($uploadDirectory, $uploadNames);
408 }
409
410 /**
0880a9d0 411 * Getter method for stateMachine.
6a488035 412 *
c490a46a 413 * @return CRM_Core_StateMachine
6a488035 414 */
00be9182 415 public function getStateMachine() {
6a488035
TO
416 return $this->_stateMachine;
417 }
418
419 /**
0880a9d0 420 * Setter method for stateMachine.
6a488035 421 *
c490a46a 422 * @param CRM_Core_StateMachine $stateMachine
6a488035 423 */
00be9182 424 public function setStateMachine($stateMachine) {
6a488035
TO
425 $this->_stateMachine = $stateMachine;
426 }
427
428 /**
100fef9d 429 * Add pages to the controller. Note that the controller does not really care
6a488035
TO
430 * the order in which the pages are added
431 *
c490a46a 432 * @param CRM_Core_StateMachine $stateMachine
2a6da8d7 433 * @param \const|int $action the mode in which the state machine is operating
b44e3f84 434 * typically this will be add/view/edit
6a488035 435 */
00be9182 436 public function addPages(&$stateMachine, $action = CRM_Core_Action::NONE) {
6a488035
TO
437 $pages = $stateMachine->getPages();
438 foreach ($pages as $name => $value) {
439 $className = CRM_Utils_Array::value('className', $value, $name);
9c1bc317
CW
440 $title = $value['title'] ?? NULL;
441 $options = $value['options'] ?? NULL;
6a488035 442 $stateName = CRM_Utils_String::getClassName($className);
a7488080 443 if (!empty($value['className'])) {
6a488035
TO
444 $formName = $name;
445 }
446 else {
447 $formName = CRM_Utils_String::getClassName($name);
448 }
449
450 $ext = CRM_Extension_System::singleton()->getMapper();
451 if ($ext->isExtensionClass($className)) {
2aa397bc 452 require_once $ext->classToPath($className);
6a488035
TO
453 }
454 else {
2aa397bc 455 require_once str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
6a488035
TO
456 }
457 $$stateName = new $className($stateMachine->find($className), $action, 'post', $formName);
458 if ($title) {
459 $$stateName->setTitle($title);
460 }
461 if ($options) {
462 $$stateName->setOptions($options);
463 }
42a40a1c 464 if (property_exists($$stateName, 'urlPath')) {
f31f885e 465 $$stateName->urlPath = explode('/', (string) CRM_Utils_System::currentPath());
118e964e 466 }
6a488035
TO
467 $this->addPage($$stateName);
468 $this->addAction($stateName, new HTML_QuickForm_Action_Direct());
469
470 //CRM-6342 -we need kill the reference here,
471 //as we have deprecated reference object creation.
472 unset($$stateName);
473 }
474 }
475
476 /**
477 * QFC does not provide native support to have different 'submit' buttons.
478 * We introduce this notion to QFC by using button specific data. Thus if
479 * we have two submit buttons, we could have one displayed as a button and
480 * the other as an image, both are of type 'submit'.
481 *
a6c01b45
CW
482 * @return string
483 * the name of the button that has been pressed by the user
6a488035 484 */
00be9182 485 public function getButtonName() {
6a488035 486 $data = &$this->container();
f21e34df 487 return $data['_qf_button_name'] ?? '';
6a488035
TO
488 }
489
490 /**
100fef9d 491 * Destroy all the session state of the controller.
6a488035 492 */
00be9182 493 public function reset() {
6a488035
TO
494 $this->container(TRUE);
495 self::$_session->resetScope($this->_scope);
496 }
497
498 /**
100fef9d 499 * Virtual function to do any processing of data.
8eedd10a 500 *
6a488035
TO
501 * Sometimes it is useful for the controller to actually process data.
502 * This is typically used when we need the controller to figure out
503 * what pages are potentially involved in this wizard. (this is dynamic
504 * and can change based on the arguments
6a488035 505 */
2aa397bc
TO
506 public function process() {
507 }
6a488035
TO
508
509 /**
0880a9d0 510 * Store the variable with the value in the form scope.
6a488035 511 *
6a0b768e
TO
512 * @param string|array $name name of the variable or an assoc array of name/value pairs
513 * @param mixed $value
514 * Value of the variable if string.
6a488035 515 */
00be9182 516 public function set($name, $value = NULL) {
6a488035
TO
517 self::$_session->set($name, $value, $this->_scope);
518 }
519
520 /**
0880a9d0 521 * Get the variable from the form scope.
6a488035 522 *
6a0b768e 523 * @param string $name
16b10e64 524 * name of the variable.
6a488035 525 *
6a488035 526 * @return mixed
6a488035 527 */
00be9182 528 public function get($name) {
6a488035
TO
529 return self::$_session->get($name, $this->_scope);
530 }
531
532 /**
0880a9d0 533 * Create the header for the wizard from the list of pages.
6a488035
TO
534 * Store the created header in smarty
535 *
6a0b768e
TO
536 * @param string $currentPageName
537 * Name of the page being displayed.
6a488035
TO
538 *
539 * @return array
6a488035 540 */
00be9182 541 public function wizardHeader($currentPageName) {
be2fb01f
CW
542 $wizard = [];
543 $wizard['steps'] = [];
353ffa53 544 $count = 0;
6a488035
TO
545 foreach ($this->_pages as $name => $page) {
546 $count++;
be2fb01f 547 $wizard['steps'][] = [
6a488035
TO
548 'name' => $name,
549 'title' => $page->getTitle(),
550 //'link' => $page->getLink ( ),
551 'link' => NULL,
552 'step' => TRUE,
553 'valid' => TRUE,
554 'stepNumber' => $count,
555 'collapsed' => FALSE,
be2fb01f 556 ];
6a488035
TO
557
558 if ($name == $currentPageName) {
559 $wizard['currentStepNumber'] = $count;
560 $wizard['currentStepName'] = $name;
561 $wizard['currentStepTitle'] = $page->getTitle();
562 }
563 }
564
565 $wizard['stepCount'] = $count;
566
567 $this->addWizardStyle($wizard);
568
569 $this->assign('wizard', $wizard);
570 return $wizard;
571 }
572
a0ee3941 573 /**
c490a46a 574 * @param array $wizard
a0ee3941 575 */
00be9182 576 public function addWizardStyle(&$wizard) {
be2fb01f 577 $wizard['style'] = [
6a488035 578 'barClass' => '',
dd7e23f2
AH
579 'stepPrefixCurrent' => '<i class="crm-i fa-chevron-right" aria-hidden="true"></i> ',
580 'stepPrefixPast' => '<i class="crm-i fa-check" aria-hidden="true"></i> ',
6a488035
TO
581 'stepPrefixFuture' => ' ',
582 'subStepPrefixCurrent' => '&nbsp;&nbsp;',
583 'subStepPrefixPast' => '&nbsp;&nbsp;',
584 'subStepPrefixFuture' => '&nbsp;&nbsp;',
585 'showTitle' => 1,
be2fb01f 586 ];
6a488035
TO
587 }
588
589 /**
0880a9d0 590 * Assign value to name in template.
6a488035 591 *
c490a46a 592 * @param string $var
6a0b768e 593 * @param mixed $value
8eedd10a 594 * Value of variable.
6a488035 595 */
00be9182 596 public function assign($var, $value = NULL) {
6a488035
TO
597 self::$_template->assign($var, $value);
598 }
599
4a9538ac 600 /**
0880a9d0 601 * Assign value to name in template by reference.
4a9538ac 602 *
c490a46a 603 * @param string $var
6a0b768e 604 * @param mixed $value
8eedd10a 605 * (reference) value of variable.
4a9538ac 606 */
00be9182 607 public function assign_by_ref($var, &$value) {
6a488035
TO
608 self::$_template->assign_by_ref($var, $value);
609 }
610
4a9538ac 611 /**
0880a9d0 612 * Appends values to template variables.
4a9538ac
CW
613 *
614 * @param array|string $tpl_var the template variable name(s)
6a0b768e
TO
615 * @param mixed $value
616 * The value to append.
4a9538ac
CW
617 * @param bool $merge
618 */
2aa397bc 619 public function append($tpl_var, $value = NULL, $merge = FALSE) {
4a9538ac
CW
620 self::$_template->append($tpl_var, $value, $merge);
621 }
622
623 /**
0880a9d0 624 * Returns an array containing template variables.
4a9538ac
CW
625 *
626 * @param string $name
2a6da8d7 627 *
4a9538ac
CW
628 * @return array
629 */
2aa397bc 630 public function get_template_vars($name = NULL) {
4a9538ac
CW
631 return self::$_template->get_template_vars($name);
632 }
633
6a488035 634 /**
0880a9d0 635 * Setter for embedded.
6a488035 636 *
6a0b768e 637 * @param bool $embedded
6a488035 638 */
00be9182 639 public function setEmbedded($embedded) {
6a488035
TO
640 $this->_embedded = $embedded;
641 }
642
643 /**
0880a9d0 644 * Getter for embedded.
6a488035 645 *
7c550ca0 646 * @return bool
a6c01b45 647 * return the embedded value
6a488035 648 */
00be9182 649 public function getEmbedded() {
6a488035
TO
650 return $this->_embedded;
651 }
652
653 /**
0880a9d0 654 * Setter for skipRedirection.
6a488035 655 *
6a0b768e 656 * @param bool $skipRedirection
6a488035 657 */
00be9182 658 public function setSkipRedirection($skipRedirection) {
6a488035
TO
659 $this->_skipRedirection = $skipRedirection;
660 }
661
662 /**
0880a9d0 663 * Getter for skipRedirection.
6a488035 664 *
7c550ca0 665 * @return bool
a6c01b45 666 * return the skipRedirection value
6a488035 667 */
00be9182 668 public function getSkipRedirection() {
6a488035
TO
669 return $this->_skipRedirection;
670 }
671
a0ee3941 672 /**
2beb47b8
BT
673 * Output HTTP headers for Word document
674 * (note .doc, not the newer .docx format)
675 *
676 * @deprecated
677 *
678 * @param string|null $fileName
679 * @return void
a0ee3941 680 */
00be9182 681 public function setWord($fileName = NULL) {
2beb47b8
BT
682 CRM_Core_Error::deprecatedFunctionWarning('no alternative');
683
6a488035 684 //Mark as a CSV file.
d42a224c 685 CRM_Utils_System::setHttpHeader('Content-Type', 'application/vnd.ms-word');
6a488035
TO
686
687 //Force a download and name the file using the current timestamp.
688 if (!$fileName) {
689 $fileName = 'Contacts_' . $_SERVER['REQUEST_TIME'] . '.doc';
690 }
d42a224c 691 CRM_Utils_System::setHttpHeader("Content-Disposition", "attachment; filename=Contacts_$fileName");
6a488035
TO
692 }
693
a0ee3941 694 /**
2beb47b8
BT
695 * Output HTTP headers for Excel document
696 * (note .xls, not the newer .xlsx format)
697 *
698 * @deprecated
699 *
700 * @param string|null $fileName
701 * @return void
a0ee3941 702 */
00be9182 703 public function setExcel($fileName = NULL) {
2beb47b8
BT
704 CRM_Core_Error::deprecatedFunctionWarning('no alternative');
705
6a488035 706 //Mark as an excel file.
d42a224c 707 CRM_Utils_System::setHttpHeader('Content-Type', 'application/vnd.ms-excel');
6a488035
TO
708
709 //Force a download and name the file using the current timestamp.
710 if (!$fileName) {
711 $fileName = 'Contacts_' . $_SERVER['REQUEST_TIME'] . '.xls';
712 }
713
d42a224c 714 CRM_Utils_System::setHttpHeader("Content-Disposition", "attachment; filename=Contacts_$fileName");
6a488035
TO
715 }
716
717 /**
0880a9d0 718 * Setter for print.
6a488035 719 *
2beb47b8
BT
720 * Historically the $print argument has also accepted a string (xls or doc),
721 * but this usage is now deprecated.
722 *
723 * @param int|string $print
724 * Should match a CRM_Core_Smarty::PRINT_* constant,
725 * or equal 0 if not in print mode
726 *
727 * @return void
6a488035 728 */
00be9182 729 public function setPrint($print) {
2beb47b8 730 if ($print === "xls") {
6a488035
TO
731 $this->setExcel();
732 }
2beb47b8 733 elseif ($print === "doc") {
6a488035
TO
734 $this->setWord();
735 }
736 $this->_print = $print;
737 }
738
739 /**
0880a9d0 740 * Getter for print.
6a488035 741 *
2beb47b8
BT
742 * @return int|string
743 * Value matching a CRM_Core_Smarty::PRINT_* constant,
744 * or 0 if not in print mode
6a488035 745 */
00be9182 746 public function getPrint() {
6a488035
TO
747 return $this->_print;
748 }
749
a0ee3941
EM
750 /**
751 * @return string
752 */
00be9182 753 public function getTemplateFile() {
6a488035
TO
754 if ($this->_print) {
755 if ($this->_print == CRM_Core_Smarty::PRINT_PAGE) {
756 return 'CRM/common/print.tpl';
757 }
2beb47b8 758 elseif ($this->_print === 'xls' || $this->_print === 'doc') {
6a488035
TO
759 return 'CRM/Contact/Form/Task/Excel.tpl';
760 }
761 else {
762 return 'CRM/common/snippet.tpl';
763 }
764 }
765 else {
766 $config = CRM_Core_Config::singleton();
767 return 'CRM/common/' . strtolower($config->userFramework) . '.tpl';
768 }
769 }
770
a0ee3941
EM
771 /**
772 * @param $uploadDir
773 * @param $uploadNames
774 */
6a488035
TO
775 public function addUploadAction($uploadDir, $uploadNames) {
776 if (empty($uploadDir)) {
777 $config = CRM_Core_Config::singleton();
778 $uploadDir = $config->uploadDir;
779 }
780
781 if (empty($uploadNames)) {
782 $uploadNames = $this->get('uploadNames');
783 if (!empty($uploadNames)) {
784 $uploadNames = array_merge($uploadNames,
785 CRM_Core_BAO_File::uploadNames()
786 );
787 }
788 else {
789 $uploadNames = CRM_Core_BAO_File::uploadNames();
790 }
791 }
792
793 $action = new CRM_Core_QuickForm_Action_Upload($this->_stateMachine,
794 $uploadDir,
795 $uploadNames
796 );
797 $this->addAction('upload', $action);
798 }
799
a0ee3941
EM
800 /**
801 * @param $parent
802 */
6a488035
TO
803 public function setParent($parent) {
804 $this->_parent = $parent;
805 }
806
a0ee3941
EM
807 /**
808 * @return object
809 */
6a488035
TO
810 public function getParent() {
811 return $this->_parent;
812 }
813
a0ee3941
EM
814 /**
815 * @return string
816 */
6a488035
TO
817 public function getDestination() {
818 return $this->_destination;
819 }
820
a0ee3941
EM
821 /**
822 * @param null $url
823 * @param bool $setToReferer
824 */
6a488035
TO
825 public function setDestination($url = NULL, $setToReferer = FALSE) {
826 if (empty($url)) {
827 if ($setToReferer) {
828 $url = $_SERVER['HTTP_REFERER'];
829 }
830 else {
831 $config = CRM_Core_Config::singleton();
832 $url = $config->userFrameworkBaseURL;
833 }
834 }
835
836 $this->_destination = $url;
837 $this->set('civicrmDestination', $this->_destination);
838 }
839
a0ee3941
EM
840 /**
841 * @return mixed
842 */
6a488035
TO
843 public function cancelAction() {
844 $actionName = $this->getActionName();
845 list($pageName, $action) = $actionName;
846 return $this->_pages[$pageName]->cancelAction();
847 }
6a488035 848
c02edd0e 849 /**
8eedd10a 850 * Write a simple fatal error message.
c02edd0e 851 *
8eedd10a 852 * Other controllers can decide to do something else and present the user a better message
853 * and/or redirect to the same page with a reset url
c02edd0e
DL
854 */
855 public function invalidKey() {
3ab88a8c
DL
856 self::invalidKeyCommon();
857 }
858
859 public function invalidKeyCommon() {
5d6aaf6b 860 $msg = ts("We can't load the requested web page. This page requires cookies to be enabled in your browser settings. Please check this setting and enable cookies (if they are not enabled). Then try again. If this error persists, contact the site administrator for assistance.") . '<br /><br />' . ts('Site Administrators: This error may indicate that users are accessing this page using a domain or URL other than the configured Base URL. EXAMPLE: Base URL is http://example.org, but some users are accessing the page via http://www.example.org or a domain alias like http://myotherexample.org.') . '<br /><br />' . ts('Error type: Could not find a valid session key.');
79e11805 861 throw new CRM_Core_Exception($msg);
c02edd0e
DL
862 }
863
3ab88a8c 864 /**
6ef7bd91
CB
865 * Instead of outputting a fatal error message, we'll just redirect
866 * to the entryURL if present
3ab88a8c
DL
867 */
868 public function invalidKeyRedirect() {
6ef7bd91
CB
869 if ($this->_entryURL && $url_parts = parse_url($this->_entryURL)) {
870 // CRM-16832: Ensure local redirects only.
871 if (!empty($url_parts['path'])) {
872 // Prepend a slash, but don't duplicate it.
873 $redirect_url = '/' . ltrim($url_parts['path'], '/');
874 if (!empty($url_parts['query'])) {
875 $redirect_url .= '?' . $url_parts['query'];
876 }
877 CRM_Core_Session::setStatus(ts('Your browser session has expired and we are unable to complete your form submission. We have returned you to the initial step so you can complete and resubmit the form. If you experience continued difficulties, please contact us for assistance.'));
878 return CRM_Utils_System::redirect($redirect_url);
879 }
3ab88a8c 880 }
6ef7bd91 881 self::invalidKeyCommon();
3ab88a8c
DL
882 }
883
c02edd0e 884}