| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.6 | |
| 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 | * |
| 30 | * @package CRM |
| 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | |
| 36 | /** |
| 37 | * A Page is basically data in a nice pretty format. |
| 38 | * |
| 39 | * Pages should not have any form actions / elements in them. If they |
| 40 | * do, make sure you use CRM_Core_Form and the related structures. You can |
| 41 | * embed simple forms in Page and do your own form handling. |
| 42 | * |
| 43 | */ |
| 44 | class CRM_Core_Page { |
| 45 | |
| 46 | /** |
| 47 | * The name of the page (auto generated from class name) |
| 48 | * |
| 49 | * @var string |
| 50 | */ |
| 51 | protected $_name; |
| 52 | |
| 53 | /** |
| 54 | * The title associated with this page. |
| 55 | * |
| 56 | * @var object |
| 57 | */ |
| 58 | protected $_title; |
| 59 | |
| 60 | /** |
| 61 | * A page can have multiple modes. (i.e. displays |
| 62 | * a different set of data based on the input |
| 63 | * @var int |
| 64 | */ |
| 65 | protected $_mode; |
| 66 | |
| 67 | /** |
| 68 | * Is this object being embedded in another object. If |
| 69 | * so the display routine needs to not do any work. (The |
| 70 | * parent object takes care of the display) |
| 71 | * |
| 72 | * @var boolean |
| 73 | */ |
| 74 | protected $_embedded = FALSE; |
| 75 | |
| 76 | /** |
| 77 | * Are we in print mode? if so we need to modify the display |
| 78 | * functionality to do a minimal display :) |
| 79 | * |
| 80 | * @var boolean |
| 81 | */ |
| 82 | protected $_print = FALSE; |
| 83 | |
| 84 | /** |
| 85 | * Cache the smarty template for efficiency reasons |
| 86 | * |
| 87 | * @var CRM_Core_Smarty |
| 88 | */ |
| 89 | static protected $_template; |
| 90 | |
| 91 | /** |
| 92 | * Cache the session for efficiency reasons |
| 93 | * |
| 94 | * @var CRM_Core_Session |
| 95 | */ |
| 96 | static protected $_session; |
| 97 | |
| 98 | /** |
| 99 | * What to return to the client if in ajax mode (snippet=json) |
| 100 | * |
| 101 | * @var array |
| 102 | */ |
| 103 | public $ajaxResponse = array(); |
| 104 | |
| 105 | /** |
| 106 | * Url path used to reach this page |
| 107 | * |
| 108 | * @var array |
| 109 | */ |
| 110 | public $urlPath = array(); |
| 111 | |
| 112 | /** |
| 113 | * Should crm.livePage.js be added to the page? |
| 114 | * @var bool |
| 115 | */ |
| 116 | public $useLivePageJS; |
| 117 | |
| 118 | /** |
| 119 | * Class constructor. |
| 120 | * |
| 121 | * @param string $title |
| 122 | * Title of the page. |
| 123 | * @param int $mode |
| 124 | * Mode of the page. |
| 125 | * |
| 126 | * @return CRM_Core_Page |
| 127 | */ |
| 128 | public function __construct($title = NULL, $mode = NULL) { |
| 129 | $this->_name = CRM_Utils_System::getClassName($this); |
| 130 | $this->_title = $title; |
| 131 | $this->_mode = $mode; |
| 132 | |
| 133 | // let the constructor initialize this, should happen only once |
| 134 | if (!isset(self::$_template)) { |
| 135 | self::$_template = CRM_Core_Smarty::singleton(); |
| 136 | self::$_session = CRM_Core_Session::singleton(); |
| 137 | } |
| 138 | |
| 139 | // FIXME - why are we messing with 'snippet'? Why not just pass it directly into $this->_print? |
| 140 | if (!empty($_REQUEST['snippet'])) { |
| 141 | if ($_REQUEST['snippet'] == CRM_Core_Smarty::PRINT_PDF) { |
| 142 | $this->_print = CRM_Core_Smarty::PRINT_PDF; |
| 143 | } |
| 144 | // FIXME - why does this number not match the constant? |
| 145 | elseif ($_REQUEST['snippet'] == 5) { |
| 146 | $this->_print = CRM_Core_Smarty::PRINT_NOFORM; |
| 147 | } |
| 148 | // Support 'json' as well as legacy value '6' |
| 149 | elseif (in_array($_REQUEST['snippet'], array(CRM_Core_Smarty::PRINT_JSON, 6))) { |
| 150 | $this->_print = CRM_Core_Smarty::PRINT_JSON; |
| 151 | } |
| 152 | else { |
| 153 | $this->_print = CRM_Core_Smarty::PRINT_SNIPPET; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | // if the request has a reset value, initialize the controller session |
| 158 | if (!empty($_REQUEST['reset'])) { |
| 159 | $this->reset(); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * This function takes care of all the things common to all |
| 165 | * pages. This typically involves assigning the appropriate smarty |
| 166 | * variable :) |
| 167 | * |
| 168 | * @return void|string |
| 169 | * The content generated by running this page |
| 170 | */ |
| 171 | public function run() { |
| 172 | if ($this->_embedded) { |
| 173 | return NULL; |
| 174 | } |
| 175 | |
| 176 | self::$_template->assign('mode', $this->_mode); |
| 177 | |
| 178 | $pageTemplateFile = $this->getHookedTemplateFileName(); |
| 179 | self::$_template->assign('tplFile', $pageTemplateFile); |
| 180 | |
| 181 | // invoke the pagRun hook, CRM-3906 |
| 182 | CRM_Utils_Hook::pageRun($this); |
| 183 | |
| 184 | if ($this->_print) { |
| 185 | if (in_array($this->_print, array( |
| 186 | CRM_Core_Smarty::PRINT_SNIPPET, |
| 187 | CRM_Core_Smarty::PRINT_PDF, |
| 188 | CRM_Core_Smarty::PRINT_NOFORM, |
| 189 | CRM_Core_Smarty::PRINT_JSON, |
| 190 | ))) { |
| 191 | $content = self::$_template->fetch('CRM/common/snippet.tpl'); |
| 192 | } |
| 193 | else { |
| 194 | $content = self::$_template->fetch('CRM/common/print.tpl'); |
| 195 | } |
| 196 | |
| 197 | CRM_Utils_System::appendTPLFile($pageTemplateFile, |
| 198 | $content, |
| 199 | $this->overrideExtraTemplateFileName() |
| 200 | ); |
| 201 | |
| 202 | //its time to call the hook. |
| 203 | CRM_Utils_Hook::alterContent($content, 'page', $pageTemplateFile, $this); |
| 204 | |
| 205 | if ($this->_print == CRM_Core_Smarty::PRINT_PDF) { |
| 206 | CRM_Utils_PDF_Utils::html2pdf($content, "{$this->_name}.pdf", FALSE, |
| 207 | array('paper_size' => 'a3', 'orientation' => 'landscape') |
| 208 | ); |
| 209 | } |
| 210 | elseif ($this->_print == CRM_Core_Smarty::PRINT_JSON) { |
| 211 | $this->ajaxResponse['content'] = $content; |
| 212 | CRM_Core_Page_AJAX::returnJsonResponse($this->ajaxResponse); |
| 213 | } |
| 214 | else { |
| 215 | echo $content; |
| 216 | } |
| 217 | CRM_Utils_System::civiExit(); |
| 218 | } |
| 219 | |
| 220 | $config = CRM_Core_Config::singleton(); |
| 221 | |
| 222 | // Version check and intermittent alert to admins |
| 223 | CRM_Utils_VersionCheck::singleton()->versionAlert(); |
| 224 | CRM_Utils_Check::singleton()->showPeriodicAlerts(); |
| 225 | |
| 226 | if ($this->useLivePageJS && |
| 227 | CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'ajaxPopupsEnabled', NULL, TRUE) |
| 228 | ) { |
| 229 | CRM_Core_Resources::singleton()->addScriptFile('civicrm', 'js/crm.livePage.js', 1, 'html-header'); |
| 230 | } |
| 231 | |
| 232 | $content = self::$_template->fetch('CRM/common/' . strtolower($config->userFramework) . '.tpl'); |
| 233 | |
| 234 | // Render page header |
| 235 | if (!defined('CIVICRM_UF_HEAD') && $region = CRM_Core_Region::instance('html-header', FALSE)) { |
| 236 | CRM_Utils_System::addHTMLHead($region->render('')); |
| 237 | } |
| 238 | CRM_Utils_System::appendTPLFile($pageTemplateFile, $content); |
| 239 | |
| 240 | //its time to call the hook. |
| 241 | CRM_Utils_Hook::alterContent($content, 'page', $pageTemplateFile, $this); |
| 242 | |
| 243 | echo CRM_Utils_System::theme($content, $this->_print); |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Store the variable with the value in the form scope. |
| 248 | * |
| 249 | * @param string|array $name name of the variable or an assoc array of name/value pairs |
| 250 | * @param mixed $value |
| 251 | * Value of the variable if string. |
| 252 | * |
| 253 | * |
| 254 | * @return void |
| 255 | */ |
| 256 | public function set($name, $value = NULL) { |
| 257 | self::$_session->set($name, $value, $this->_name); |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Get the variable from the form scope. |
| 262 | * |
| 263 | * @param string $name name of the variable |
| 264 | * |
| 265 | * @return mixed |
| 266 | */ |
| 267 | public function get($name) { |
| 268 | return self::$_session->get($name, $this->_name); |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Assign value to name in template. |
| 273 | * |
| 274 | * @param string $var |
| 275 | * @param mixed $value |
| 276 | * Value of varaible. |
| 277 | * |
| 278 | * @return void |
| 279 | */ |
| 280 | public function assign($var, $value = NULL) { |
| 281 | self::$_template->assign($var, $value); |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * Assign value to name in template by reference. |
| 286 | * |
| 287 | * @param string $var |
| 288 | * @param mixed $value |
| 289 | * (reference) value of varaible. |
| 290 | * |
| 291 | * @return void |
| 292 | */ |
| 293 | public function assign_by_ref($var, &$value) { |
| 294 | self::$_template->assign_by_ref($var, $value); |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Appends values to template variables. |
| 299 | * |
| 300 | * @param array|string $tpl_var the template variable name(s) |
| 301 | * @param mixed $value |
| 302 | * The value to append. |
| 303 | * @param bool $merge |
| 304 | */ |
| 305 | public function append($tpl_var, $value = NULL, $merge = FALSE) { |
| 306 | self::$_template->append($tpl_var, $value, $merge); |
| 307 | } |
| 308 | |
| 309 | /** |
| 310 | * Returns an array containing template variables. |
| 311 | * |
| 312 | * @param string $name |
| 313 | * |
| 314 | * @return array |
| 315 | */ |
| 316 | public function get_template_vars($name = NULL) { |
| 317 | return self::$_template->get_template_vars($name); |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * Destroy all the session state of this page. |
| 322 | * |
| 323 | * |
| 324 | * @return void |
| 325 | */ |
| 326 | public function reset() { |
| 327 | self::$_session->resetScope($this->_name); |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * Use the form name to create the tpl file name. |
| 332 | * |
| 333 | * @return string |
| 334 | */ |
| 335 | public function getTemplateFileName() { |
| 336 | return strtr( |
| 337 | CRM_Utils_System::getClassName($this), |
| 338 | array( |
| 339 | '_' => DIRECTORY_SEPARATOR, |
| 340 | '\\' => DIRECTORY_SEPARATOR, |
| 341 | ) |
| 342 | ) . '.tpl'; |
| 343 | } |
| 344 | |
| 345 | /** |
| 346 | * A wrapper for getTemplateFileName that includes calling the hook to |
| 347 | * prevent us from having to copy & paste the logic of calling the hook |
| 348 | */ |
| 349 | public function getHookedTemplateFileName() { |
| 350 | $pageTemplateFile = $this->getTemplateFileName(); |
| 351 | CRM_Utils_Hook::alterTemplateFile(get_class($this), $this, 'page', $pageTemplateFile); |
| 352 | return $pageTemplateFile; |
| 353 | } |
| 354 | |
| 355 | /** |
| 356 | * Default extra tpl file basically just replaces .tpl with .extra.tpl |
| 357 | * i.e. we dont override |
| 358 | * |
| 359 | * @return string |
| 360 | */ |
| 361 | public function overrideExtraTemplateFileName() { |
| 362 | return NULL; |
| 363 | } |
| 364 | |
| 365 | /** |
| 366 | * Setter for embedded. |
| 367 | * |
| 368 | * @param bool $embedded |
| 369 | * |
| 370 | * @return void |
| 371 | */ |
| 372 | public function setEmbedded($embedded) { |
| 373 | $this->_embedded = $embedded; |
| 374 | } |
| 375 | |
| 376 | /** |
| 377 | * Getter for embedded. |
| 378 | * |
| 379 | * @return bool |
| 380 | * return the embedded value |
| 381 | */ |
| 382 | public function getEmbedded() { |
| 383 | return $this->_embedded; |
| 384 | } |
| 385 | |
| 386 | /** |
| 387 | * Setter for print. |
| 388 | * |
| 389 | * @param bool $print |
| 390 | * |
| 391 | * @return void |
| 392 | */ |
| 393 | public function setPrint($print) { |
| 394 | $this->_print = $print; |
| 395 | } |
| 396 | |
| 397 | /** |
| 398 | * Getter for print. |
| 399 | * |
| 400 | * @return bool |
| 401 | * return the print value |
| 402 | */ |
| 403 | public function getPrint() { |
| 404 | return $this->_print; |
| 405 | } |
| 406 | |
| 407 | /** |
| 408 | * @return CRM_Core_Smarty |
| 409 | */ |
| 410 | public static function &getTemplate() { |
| 411 | return self::$_template; |
| 412 | } |
| 413 | |
| 414 | /** |
| 415 | * @param string $name |
| 416 | * |
| 417 | * @return null |
| 418 | */ |
| 419 | public function getVar($name) { |
| 420 | return isset($this->$name) ? $this->$name : NULL; |
| 421 | } |
| 422 | |
| 423 | /** |
| 424 | * @param string $name |
| 425 | * @param $value |
| 426 | */ |
| 427 | public function setVar($name, $value) { |
| 428 | $this->$name = $value; |
| 429 | } |
| 430 | |
| 431 | } |