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