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