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