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