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