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