| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
| 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-2015 |
| 42 | * $Id$ |
| 43 | * |
| 44 | */ |
| 45 | |
| 46 | require_once 'HTML/QuickForm/Controller.php'; |
| 47 | require_once 'HTML/QuickForm/Action/Direct.php'; |
| 48 | |
| 49 | /** |
| 50 | * Class CRM_Core_Controller |
| 51 | */ |
| 52 | class CRM_Core_Controller extends HTML_QuickForm_Controller { |
| 53 | |
| 54 | /** |
| 55 | * The title associated with this controller. |
| 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 | /** |
| 69 | * The name of the session scope where values are stored. |
| 70 | * |
| 71 | * @var object |
| 72 | */ |
| 73 | protected $_scope; |
| 74 | |
| 75 | /** |
| 76 | * The state machine associated with this controller. |
| 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 | * @var string |
| 122 | */ |
| 123 | public $_QFResponseType = 'html'; |
| 124 | |
| 125 | /** |
| 126 | * Cache the smarty template for efficiency reasons. |
| 127 | * |
| 128 | * @var CRM_Core_Smarty |
| 129 | */ |
| 130 | static protected $_template; |
| 131 | |
| 132 | /** |
| 133 | * Cache the session for efficiency reasons. |
| 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 | |
| 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 | |
| 161 | /** |
| 162 | * All CRM single or multi page pages should inherit from this class. |
| 163 | * |
| 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. |
| 173 | * @param bool $ignoreKey |
| 174 | * Should we not set a qfKey for this controller (for standalone forms). |
| 175 | */ |
| 176 | public function __construct( |
| 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 | |
| 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 |
| 196 | $this->_entryURL |
| 197 | = CRM_Utils_Request::retrieve('entryURL', 'String', $this); |
| 198 | |
| 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 | // mode deprecated in favor of json |
| 240 | // still used by dashlets, probably nothing else |
| 241 | $this->_print = CRM_Core_Smarty::PRINT_NOFORM; |
| 242 | } |
| 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; |
| 246 | $this->_QFResponseType = 'json'; |
| 247 | } |
| 248 | else { |
| 249 | $this->_print = CRM_Core_Smarty::PRINT_SNIPPET; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | // if the request has a reset value, initialize the controller session |
| 254 | if (!empty($_GET['reset'])) { |
| 255 | $this->reset(); |
| 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); |
| 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 | |
| 268 | // also retrieve and store destination in session |
| 269 | $this->_destination = CRM_Utils_Request::retrieve( |
| 270 | 'civicrmDestination', |
| 271 | 'String', |
| 272 | $this, |
| 273 | FALSE, |
| 274 | NULL, |
| 275 | $_REQUEST |
| 276 | ); |
| 277 | } |
| 278 | |
| 279 | public 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 | /** |
| 289 | * @param string $name |
| 290 | * @param bool $addSequence |
| 291 | * @param bool $ignoreKey |
| 292 | * |
| 293 | * @return mixed|string |
| 294 | */ |
| 295 | public function key($name, $addSequence = FALSE, $ignoreKey = FALSE) { |
| 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) { |
| 314 | $this->invalidKey(); |
| 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 | * |
| 333 | * @return mixed |
| 334 | */ |
| 335 | public function run() { |
| 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 | |
| 356 | /** |
| 357 | * @return bool |
| 358 | */ |
| 359 | public function validate() { |
| 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 | * |
| 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) |
| 384 | * |
| 385 | * |
| 386 | * @return void |
| 387 | */ |
| 388 | public function addActions($uploadDirectory = NULL, $uploadNames = NULL) { |
| 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 | /** |
| 411 | * Getter method for stateMachine. |
| 412 | * |
| 413 | * @return CRM_Core_StateMachine |
| 414 | */ |
| 415 | public function getStateMachine() { |
| 416 | return $this->_stateMachine; |
| 417 | } |
| 418 | |
| 419 | /** |
| 420 | * Setter method for stateMachine. |
| 421 | * |
| 422 | * @param CRM_Core_StateMachine $stateMachine |
| 423 | */ |
| 424 | public function setStateMachine($stateMachine) { |
| 425 | $this->_stateMachine = $stateMachine; |
| 426 | } |
| 427 | |
| 428 | /** |
| 429 | * Add pages to the controller. Note that the controller does not really care |
| 430 | * the order in which the pages are added |
| 431 | * |
| 432 | * @param CRM_Core_StateMachine $stateMachine |
| 433 | * @param \const|int $action the mode in which the state machine is operating |
| 434 | * typically this will be add/view/edit |
| 435 | * |
| 436 | * @return void |
| 437 | */ |
| 438 | public function addPages(&$stateMachine, $action = CRM_Core_Action::NONE) { |
| 439 | $pages = $stateMachine->getPages(); |
| 440 | foreach ($pages as $name => $value) { |
| 441 | $className = CRM_Utils_Array::value('className', $value, $name); |
| 442 | $title = CRM_Utils_Array::value('title', $value); |
| 443 | $options = CRM_Utils_Array::value('options', $value); |
| 444 | $stateName = CRM_Utils_String::getClassName($className); |
| 445 | if (!empty($value['className'])) { |
| 446 | $formName = $name; |
| 447 | } |
| 448 | else { |
| 449 | $formName = CRM_Utils_String::getClassName($name); |
| 450 | } |
| 451 | |
| 452 | $ext = CRM_Extension_System::singleton()->getMapper(); |
| 453 | if ($ext->isExtensionClass($className)) { |
| 454 | require_once $ext->classToPath($className); |
| 455 | } |
| 456 | else { |
| 457 | require_once str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php'; |
| 458 | } |
| 459 | $$stateName = new $className($stateMachine->find($className), $action, 'post', $formName); |
| 460 | if ($title) { |
| 461 | $$stateName->setTitle($title); |
| 462 | } |
| 463 | if ($options) { |
| 464 | $$stateName->setOptions($options); |
| 465 | } |
| 466 | if (property_exists($$stateName, 'urlPath') && isset($_GET[CRM_Core_Config::singleton()->userFrameworkURLVar])) { |
| 467 | $$stateName->urlPath = explode('/', $_GET[CRM_Core_Config::singleton()->userFrameworkURLVar]); |
| 468 | } |
| 469 | $this->addPage($$stateName); |
| 470 | $this->addAction($stateName, new HTML_QuickForm_Action_Direct()); |
| 471 | |
| 472 | //CRM-6342 -we need kill the reference here, |
| 473 | //as we have deprecated reference object creation. |
| 474 | unset($$stateName); |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | /** |
| 479 | * QFC does not provide native support to have different 'submit' buttons. |
| 480 | * We introduce this notion to QFC by using button specific data. Thus if |
| 481 | * we have two submit buttons, we could have one displayed as a button and |
| 482 | * the other as an image, both are of type 'submit'. |
| 483 | * |
| 484 | * @return string |
| 485 | * the name of the button that has been pressed by the user |
| 486 | */ |
| 487 | public function getButtonName() { |
| 488 | $data = &$this->container(); |
| 489 | return CRM_Utils_Array::value('_qf_button_name', $data); |
| 490 | } |
| 491 | |
| 492 | /** |
| 493 | * Destroy all the session state of the controller. |
| 494 | * |
| 495 | * |
| 496 | * @return void |
| 497 | */ |
| 498 | public function reset() { |
| 499 | $this->container(TRUE); |
| 500 | self::$_session->resetScope($this->_scope); |
| 501 | } |
| 502 | |
| 503 | /** |
| 504 | * Virtual function to do any processing of data. |
| 505 | * Sometimes it is useful for the controller to actually process data. |
| 506 | * This is typically used when we need the controller to figure out |
| 507 | * what pages are potentially involved in this wizard. (this is dynamic |
| 508 | * and can change based on the arguments |
| 509 | * |
| 510 | * @return void |
| 511 | */ |
| 512 | public function process() { |
| 513 | } |
| 514 | |
| 515 | /** |
| 516 | * Store the variable with the value in the form scope. |
| 517 | * |
| 518 | * @param string|array $name name of the variable or an assoc array of name/value pairs |
| 519 | * @param mixed $value |
| 520 | * Value of the variable if string. |
| 521 | * |
| 522 | * |
| 523 | * @return void |
| 524 | */ |
| 525 | public function set($name, $value = NULL) { |
| 526 | self::$_session->set($name, $value, $this->_scope); |
| 527 | } |
| 528 | |
| 529 | /** |
| 530 | * Get the variable from the form scope. |
| 531 | * |
| 532 | * @param string $name |
| 533 | * name of the variable. |
| 534 | * |
| 535 | * |
| 536 | * @return mixed |
| 537 | */ |
| 538 | public function get($name) { |
| 539 | return self::$_session->get($name, $this->_scope); |
| 540 | } |
| 541 | |
| 542 | /** |
| 543 | * Create the header for the wizard from the list of pages. |
| 544 | * Store the created header in smarty |
| 545 | * |
| 546 | * @param string $currentPageName |
| 547 | * Name of the page being displayed. |
| 548 | * |
| 549 | * @return array |
| 550 | */ |
| 551 | public function wizardHeader($currentPageName) { |
| 552 | $wizard = array(); |
| 553 | $wizard['steps'] = array(); |
| 554 | $count = 0; |
| 555 | foreach ($this->_pages as $name => $page) { |
| 556 | $count++; |
| 557 | $wizard['steps'][] = array( |
| 558 | 'name' => $name, |
| 559 | 'title' => $page->getTitle(), |
| 560 | //'link' => $page->getLink ( ), |
| 561 | 'link' => NULL, |
| 562 | 'step' => TRUE, |
| 563 | 'valid' => TRUE, |
| 564 | 'stepNumber' => $count, |
| 565 | 'collapsed' => FALSE, |
| 566 | ); |
| 567 | |
| 568 | if ($name == $currentPageName) { |
| 569 | $wizard['currentStepNumber'] = $count; |
| 570 | $wizard['currentStepName'] = $name; |
| 571 | $wizard['currentStepTitle'] = $page->getTitle(); |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | $wizard['stepCount'] = $count; |
| 576 | |
| 577 | $this->addWizardStyle($wizard); |
| 578 | |
| 579 | $this->assign('wizard', $wizard); |
| 580 | return $wizard; |
| 581 | } |
| 582 | |
| 583 | /** |
| 584 | * @param array $wizard |
| 585 | */ |
| 586 | public function addWizardStyle(&$wizard) { |
| 587 | $wizard['style'] = array( |
| 588 | 'barClass' => '', |
| 589 | 'stepPrefixCurrent' => '»', |
| 590 | 'stepPrefixPast' => '✔', |
| 591 | 'stepPrefixFuture' => ' ', |
| 592 | 'subStepPrefixCurrent' => ' ', |
| 593 | 'subStepPrefixPast' => ' ', |
| 594 | 'subStepPrefixFuture' => ' ', |
| 595 | 'showTitle' => 1, |
| 596 | ); |
| 597 | } |
| 598 | |
| 599 | /** |
| 600 | * Assign value to name in template. |
| 601 | * |
| 602 | * @param string $var |
| 603 | * @param mixed $value |
| 604 | * Value of varaible. |
| 605 | * |
| 606 | * @return void |
| 607 | */ |
| 608 | public function assign($var, $value = NULL) { |
| 609 | self::$_template->assign($var, $value); |
| 610 | } |
| 611 | |
| 612 | /** |
| 613 | * Assign value to name in template by reference. |
| 614 | * |
| 615 | * @param string $var |
| 616 | * @param mixed $value |
| 617 | * (reference) value of varaible. |
| 618 | * |
| 619 | * @return void |
| 620 | */ |
| 621 | public function assign_by_ref($var, &$value) { |
| 622 | self::$_template->assign_by_ref($var, $value); |
| 623 | } |
| 624 | |
| 625 | /** |
| 626 | * Appends values to template variables. |
| 627 | * |
| 628 | * @param array|string $tpl_var the template variable name(s) |
| 629 | * @param mixed $value |
| 630 | * The value to append. |
| 631 | * @param bool $merge |
| 632 | */ |
| 633 | public function append($tpl_var, $value = NULL, $merge = FALSE) { |
| 634 | self::$_template->append($tpl_var, $value, $merge); |
| 635 | } |
| 636 | |
| 637 | /** |
| 638 | * Returns an array containing template variables. |
| 639 | * |
| 640 | * @param string $name |
| 641 | * |
| 642 | * @return array |
| 643 | */ |
| 644 | public function get_template_vars($name = NULL) { |
| 645 | return self::$_template->get_template_vars($name); |
| 646 | } |
| 647 | |
| 648 | /** |
| 649 | * Setter for embedded. |
| 650 | * |
| 651 | * @param bool $embedded |
| 652 | * |
| 653 | * @return void |
| 654 | */ |
| 655 | public function setEmbedded($embedded) { |
| 656 | $this->_embedded = $embedded; |
| 657 | } |
| 658 | |
| 659 | /** |
| 660 | * Getter for embedded. |
| 661 | * |
| 662 | * @return bool |
| 663 | * return the embedded value |
| 664 | */ |
| 665 | public function getEmbedded() { |
| 666 | return $this->_embedded; |
| 667 | } |
| 668 | |
| 669 | /** |
| 670 | * Setter for skipRedirection. |
| 671 | * |
| 672 | * @param bool $skipRedirection |
| 673 | * |
| 674 | * @return void |
| 675 | */ |
| 676 | public function setSkipRedirection($skipRedirection) { |
| 677 | $this->_skipRedirection = $skipRedirection; |
| 678 | } |
| 679 | |
| 680 | /** |
| 681 | * Getter for skipRedirection. |
| 682 | * |
| 683 | * @return bool |
| 684 | * return the skipRedirection value |
| 685 | */ |
| 686 | public function getSkipRedirection() { |
| 687 | return $this->_skipRedirection; |
| 688 | } |
| 689 | |
| 690 | /** |
| 691 | * @param null $fileName |
| 692 | */ |
| 693 | public function setWord($fileName = NULL) { |
| 694 | //Mark as a CSV file. |
| 695 | CRM_Utils_System::setHttpHeader('Content-Type', 'application/vnd.ms-word'); |
| 696 | |
| 697 | //Force a download and name the file using the current timestamp. |
| 698 | if (!$fileName) { |
| 699 | $fileName = 'Contacts_' . $_SERVER['REQUEST_TIME'] . '.doc'; |
| 700 | } |
| 701 | CRM_Utils_System::setHttpHeader("Content-Disposition", "attachment; filename=Contacts_$fileName"); |
| 702 | } |
| 703 | |
| 704 | /** |
| 705 | * @param null $fileName |
| 706 | */ |
| 707 | public function setExcel($fileName = NULL) { |
| 708 | //Mark as an excel file. |
| 709 | CRM_Utils_System::setHttpHeader('Content-Type', 'application/vnd.ms-excel'); |
| 710 | |
| 711 | //Force a download and name the file using the current timestamp. |
| 712 | if (!$fileName) { |
| 713 | $fileName = 'Contacts_' . $_SERVER['REQUEST_TIME'] . '.xls'; |
| 714 | } |
| 715 | |
| 716 | CRM_Utils_System::setHttpHeader("Content-Disposition", "attachment; filename=Contacts_$fileName"); |
| 717 | } |
| 718 | |
| 719 | /** |
| 720 | * Setter for print. |
| 721 | * |
| 722 | * @param bool $print |
| 723 | * |
| 724 | * @return void |
| 725 | */ |
| 726 | public function setPrint($print) { |
| 727 | if ($print == "xls") { |
| 728 | $this->setExcel(); |
| 729 | } |
| 730 | elseif ($print == "doc") { |
| 731 | $this->setWord(); |
| 732 | } |
| 733 | $this->_print = $print; |
| 734 | } |
| 735 | |
| 736 | /** |
| 737 | * Getter for print. |
| 738 | * |
| 739 | * @return bool |
| 740 | * return the print value |
| 741 | */ |
| 742 | public function getPrint() { |
| 743 | return $this->_print; |
| 744 | } |
| 745 | |
| 746 | /** |
| 747 | * @return string |
| 748 | */ |
| 749 | public function getTemplateFile() { |
| 750 | if ($this->_print) { |
| 751 | if ($this->_print == CRM_Core_Smarty::PRINT_PAGE) { |
| 752 | return 'CRM/common/print.tpl'; |
| 753 | } |
| 754 | elseif ($this->_print == 'xls' || $this->_print == 'doc') { |
| 755 | return 'CRM/Contact/Form/Task/Excel.tpl'; |
| 756 | } |
| 757 | else { |
| 758 | return 'CRM/common/snippet.tpl'; |
| 759 | } |
| 760 | } |
| 761 | else { |
| 762 | $config = CRM_Core_Config::singleton(); |
| 763 | return 'CRM/common/' . strtolower($config->userFramework) . '.tpl'; |
| 764 | } |
| 765 | } |
| 766 | |
| 767 | /** |
| 768 | * @param $uploadDir |
| 769 | * @param $uploadNames |
| 770 | */ |
| 771 | public function addUploadAction($uploadDir, $uploadNames) { |
| 772 | if (empty($uploadDir)) { |
| 773 | $config = CRM_Core_Config::singleton(); |
| 774 | $uploadDir = $config->uploadDir; |
| 775 | } |
| 776 | |
| 777 | if (empty($uploadNames)) { |
| 778 | $uploadNames = $this->get('uploadNames'); |
| 779 | if (!empty($uploadNames)) { |
| 780 | $uploadNames = array_merge($uploadNames, |
| 781 | CRM_Core_BAO_File::uploadNames() |
| 782 | ); |
| 783 | } |
| 784 | else { |
| 785 | $uploadNames = CRM_Core_BAO_File::uploadNames(); |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | $action = new CRM_Core_QuickForm_Action_Upload($this->_stateMachine, |
| 790 | $uploadDir, |
| 791 | $uploadNames |
| 792 | ); |
| 793 | $this->addAction('upload', $action); |
| 794 | } |
| 795 | |
| 796 | /** |
| 797 | * @param $parent |
| 798 | */ |
| 799 | public function setParent($parent) { |
| 800 | $this->_parent = $parent; |
| 801 | } |
| 802 | |
| 803 | /** |
| 804 | * @return object |
| 805 | */ |
| 806 | public function getParent() { |
| 807 | return $this->_parent; |
| 808 | } |
| 809 | |
| 810 | /** |
| 811 | * @return string |
| 812 | */ |
| 813 | public function getDestination() { |
| 814 | return $this->_destination; |
| 815 | } |
| 816 | |
| 817 | /** |
| 818 | * @param null $url |
| 819 | * @param bool $setToReferer |
| 820 | */ |
| 821 | public function setDestination($url = NULL, $setToReferer = FALSE) { |
| 822 | if (empty($url)) { |
| 823 | if ($setToReferer) { |
| 824 | $url = $_SERVER['HTTP_REFERER']; |
| 825 | } |
| 826 | else { |
| 827 | $config = CRM_Core_Config::singleton(); |
| 828 | $url = $config->userFrameworkBaseURL; |
| 829 | } |
| 830 | } |
| 831 | |
| 832 | $this->_destination = $url; |
| 833 | $this->set('civicrmDestination', $this->_destination); |
| 834 | } |
| 835 | |
| 836 | /** |
| 837 | * @return mixed |
| 838 | */ |
| 839 | public function cancelAction() { |
| 840 | $actionName = $this->getActionName(); |
| 841 | list($pageName, $action) = $actionName; |
| 842 | return $this->_pages[$pageName]->cancelAction(); |
| 843 | } |
| 844 | |
| 845 | /** |
| 846 | * Write a simple fatal error message. Other controllers can decide to do something else |
| 847 | * and present the user a better message and/or redirect to the same page with a reset url |
| 848 | * |
| 849 | * @return void |
| 850 | */ |
| 851 | public function invalidKey() { |
| 852 | self::invalidKeyCommon(); |
| 853 | } |
| 854 | |
| 855 | public function invalidKeyCommon() { |
| 856 | $msg = ts("We can't load the requested web page. This page requires cookies to be enabled in your browser settings. Please check this setting and enable cookies (if they are not enabled). Then try again. If this error persists, contact the site administrator for assistance.") . '<br /><br />' . ts('Site Administrators: This error may indicate that users are accessing this page using a domain or URL other than the configured Base URL. EXAMPLE: Base URL is http://example.org, but some users are accessing the page via http://www.example.org or a domain alias like http://myotherexample.org.') . '<br /><br />' . ts('Error type: Could not find a valid session key.'); |
| 857 | CRM_Core_Error::fatal($msg); |
| 858 | } |
| 859 | |
| 860 | /** |
| 861 | * Instead of outputting a fatal error message, we'll just redirect |
| 862 | * to the entryURL if present |
| 863 | * |
| 864 | * @return void |
| 865 | */ |
| 866 | public function invalidKeyRedirect() { |
| 867 | if ($this->_entryURL && $url_parts = parse_url($this->_entryURL)) { |
| 868 | // CRM-16832: Ensure local redirects only. |
| 869 | if (!empty($url_parts['path'])) { |
| 870 | // Prepend a slash, but don't duplicate it. |
| 871 | $redirect_url = '/' . ltrim($url_parts['path'], '/'); |
| 872 | if (!empty($url_parts['query'])) { |
| 873 | $redirect_url .= '?' . $url_parts['query']; |
| 874 | } |
| 875 | 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.')); |
| 876 | return CRM_Utils_System::redirect($redirect_url); |
| 877 | } |
| 878 | } |
| 879 | self::invalidKeyCommon(); |
| 880 | } |
| 881 | |
| 882 | } |