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