| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.4 | |
| 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 | * |
| 30 | * @package CRM |
| 31 | * @copyright CiviCRM LLC (c) 2004-2013 |
| 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 | * @access protected |
| 51 | */ |
| 52 | protected $_name; |
| 53 | |
| 54 | /** |
| 55 | * the title associated with this page |
| 56 | * |
| 57 | * @var object |
| 58 | * @access protected |
| 59 | */ |
| 60 | protected $_title; |
| 61 | |
| 62 | /** |
| 63 | * A page can have multiple modes. (i.e. displays |
| 64 | * a different set of data based on the input |
| 65 | * @var int |
| 66 | * @access protected |
| 67 | */ |
| 68 | protected $_mode; |
| 69 | |
| 70 | /** |
| 71 | * Is this object being embedded in another object. If |
| 72 | * so the display routine needs to not do any work. (The |
| 73 | * parent object takes care of the display) |
| 74 | * |
| 75 | * @var boolean |
| 76 | * @access protected |
| 77 | */ |
| 78 | protected $_embedded = FALSE; |
| 79 | |
| 80 | /** |
| 81 | * Are we in print mode? if so we need to modify the display |
| 82 | * functionality to do a minimal display :) |
| 83 | * |
| 84 | * @var boolean |
| 85 | * @access protected |
| 86 | */ |
| 87 | protected $_print = FALSE; |
| 88 | |
| 89 | /** |
| 90 | * cache the smarty template for efficiency reasons |
| 91 | * |
| 92 | * @var CRM_Core_Smarty |
| 93 | * @access protected |
| 94 | * @static |
| 95 | */ |
| 96 | static protected $_template; |
| 97 | |
| 98 | /** |
| 99 | * cache the session for efficiency reasons |
| 100 | * |
| 101 | * @var CRM_Core_Session |
| 102 | * @access protected |
| 103 | * @static |
| 104 | */ |
| 105 | static protected $_session; |
| 106 | |
| 107 | /** |
| 108 | * class constructor |
| 109 | * |
| 110 | * @param string $title title of the page |
| 111 | * @param int $mode mode of the page |
| 112 | * |
| 113 | * @return CRM_Core_Page |
| 114 | */ |
| 115 | function __construct($title = NULL, $mode = NULL) { |
| 116 | $this->_name = CRM_Utils_System::getClassName($this); |
| 117 | $this->_title = $title; |
| 118 | $this->_mode = $mode; |
| 119 | |
| 120 | // let the constructor initialize this, should happen only once |
| 121 | if (!isset(self::$_template)) { |
| 122 | self::$_template = CRM_Core_Smarty::singleton(); |
| 123 | self::$_session = CRM_Core_Session::singleton(); |
| 124 | } |
| 125 | |
| 126 | if (isset($_REQUEST['snippet']) && $_REQUEST['snippet']) { |
| 127 | if ($_REQUEST['snippet'] == 3) { |
| 128 | $this->_print = CRM_Core_Smarty::PRINT_PDF; |
| 129 | } |
| 130 | else if ($_REQUEST['snippet'] == 5) { |
| 131 | $this->_print = CRM_Core_Smarty::PRINT_NOFORM; |
| 132 | } |
| 133 | else { |
| 134 | $this->_print = CRM_Core_Smarty::PRINT_SNIPPET; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | // if the request has a reset value, initialize the controller session |
| 139 | if (CRM_Utils_Array::value('reset', $_REQUEST)) { |
| 140 | $this->reset(); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * This function takes care of all the things common to all |
| 146 | * pages. This typically involves assigning the appropriate |
| 147 | * smarty variable :) |
| 148 | * |
| 149 | * @return string The content generated by running this page |
| 150 | */ |
| 151 | function run() { |
| 152 | if ($this->_embedded) { |
| 153 | return; |
| 154 | } |
| 155 | |
| 156 | self::$_template->assign('mode', $this->_mode); |
| 157 | |
| 158 | $pageTemplateFile = $this->getHookedTemplateFileName(); |
| 159 | self::$_template->assign('tplFile', $pageTemplateFile); |
| 160 | |
| 161 | // invoke the pagRun hook, CRM-3906 |
| 162 | CRM_Utils_Hook::pageRun($this); |
| 163 | |
| 164 | if ($this->_print) { |
| 165 | if (in_array( $this->_print, array( CRM_Core_Smarty::PRINT_SNIPPET, |
| 166 | CRM_Core_Smarty::PRINT_PDF, CRM_Core_Smarty::PRINT_NOFORM ))) { |
| 167 | $content = self::$_template->fetch('CRM/common/snippet.tpl'); |
| 168 | } |
| 169 | else { |
| 170 | $content = self::$_template->fetch('CRM/common/print.tpl'); |
| 171 | } |
| 172 | |
| 173 | CRM_Utils_System::appendTPLFile($pageTemplateFile, |
| 174 | $content, |
| 175 | $this->overrideExtraTemplateFileName() |
| 176 | ); |
| 177 | |
| 178 | //its time to call the hook. |
| 179 | CRM_Utils_Hook::alterContent($content, 'page', $pageTemplateFile, $this); |
| 180 | |
| 181 | if ($this->_print == CRM_Core_Smarty::PRINT_PDF) { |
| 182 | CRM_Utils_PDF_Utils::html2pdf($content, "{$this->_name}.pdf", FALSE, |
| 183 | array('paper_size' => 'a3', 'orientation' => 'landscape') |
| 184 | ); |
| 185 | } |
| 186 | else { |
| 187 | echo $content; |
| 188 | } |
| 189 | CRM_Utils_System::civiExit(); |
| 190 | } |
| 191 | |
| 192 | $config = CRM_Core_Config::singleton(); |
| 193 | |
| 194 | // TODO: Is there a better way to ensure these actions don't happen during AJAX requests? |
| 195 | if (empty($_GET['snippet'])) { |
| 196 | // Version check and intermittent alert to admins |
| 197 | CRM_Utils_VersionCheck::singleton()->versionAlert(); |
| 198 | CRM_Utils_Check_Security::singleton()->showPeriodicAlerts(); |
| 199 | |
| 200 | // Debug msg once per hour |
| 201 | if ($config->debug && CRM_Core_Permission::check('administer CiviCRM') && CRM_Core_Session::singleton()->timer('debug_alert', 3600)) { |
| 202 | $msg = ts('Warning: Debug is enabled in <a href="%1">system settings</a>. This should not be enabled on production servers.', array(1 => CRM_Utils_System::url('civicrm/admin/setting/debug', 'reset=1'))); |
| 203 | CRM_Core_Session::setStatus($msg, ts('Debug Mode')); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | $content = self::$_template->fetch('CRM/common/' . strtolower($config->userFramework) . '.tpl'); |
| 208 | |
| 209 | // Render page header |
| 210 | if (!defined('CIVICRM_UF_HEAD') && $region = CRM_Core_Region::instance('html-header', FALSE)) { |
| 211 | CRM_Utils_System::addHTMLHead($region->render('')); |
| 212 | } |
| 213 | CRM_Utils_System::appendTPLFile($pageTemplateFile, $content); |
| 214 | |
| 215 | //its time to call the hook. |
| 216 | CRM_Utils_Hook::alterContent($content, 'page', $pageTemplateFile, $this); |
| 217 | |
| 218 | echo CRM_Utils_System::theme($content, $this->_print); |
| 219 | return; |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Store the variable with the value in the form scope |
| 224 | * |
| 225 | * @param string|array $name name of the variable or an assoc array of name/value pairs |
| 226 | * @param mixed $value value of the variable if string |
| 227 | * |
| 228 | * @access public |
| 229 | * |
| 230 | * @return void |
| 231 | * |
| 232 | */ |
| 233 | function set($name, $value = NULL) { |
| 234 | self::$_session->set($name, $value, $this->_name); |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Get the variable from the form scope |
| 239 | * |
| 240 | * @param string name : name of the variable |
| 241 | * |
| 242 | * @access public |
| 243 | * |
| 244 | * @return mixed |
| 245 | * |
| 246 | */ |
| 247 | function get($name) { |
| 248 | return self::$_session->get($name, $this->_name); |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * assign value to name in template |
| 253 | * |
| 254 | * @param array|string $name name of variable |
| 255 | * @param mixed $value value of varaible |
| 256 | * |
| 257 | * @return void |
| 258 | * @access public |
| 259 | */ |
| 260 | function assign($var, $value = NULL) { |
| 261 | self::$_template->assign($var, $value); |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * assign value to name in template by reference |
| 266 | * |
| 267 | * @param array|string $name name of variable |
| 268 | * @param mixed $value (reference) value of varaible |
| 269 | * |
| 270 | * @return void |
| 271 | * @access public |
| 272 | */ |
| 273 | function assign_by_ref($var, &$value) { |
| 274 | self::$_template->assign_by_ref($var, $value); |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * appends values to template variables |
| 279 | * |
| 280 | * @param array|string $tpl_var the template variable name(s) |
| 281 | * @param mixed $value the value to append |
| 282 | * @param bool $merge |
| 283 | */ |
| 284 | function append($tpl_var, $value=NULL, $merge=FALSE) { |
| 285 | self::$_template->append($tpl_var, $value, $merge); |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * Returns an array containing template variables |
| 290 | * |
| 291 | * @param string $name |
| 292 | * @param string $type |
| 293 | * @return array |
| 294 | */ |
| 295 | function get_template_vars($name=null) { |
| 296 | return self::$_template->get_template_vars($name); |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * function to destroy all the session state of this page. |
| 301 | * |
| 302 | * @access public |
| 303 | * |
| 304 | * @return void |
| 305 | */ |
| 306 | function reset() { |
| 307 | self::$_session->resetScope($this->_name); |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Use the form name to create the tpl file name |
| 312 | * |
| 313 | * @return string |
| 314 | * @access public |
| 315 | */ |
| 316 | function getTemplateFileName() { |
| 317 | return str_replace('_', |
| 318 | DIRECTORY_SEPARATOR, |
| 319 | CRM_Utils_System::getClassName($this) |
| 320 | ) . '.tpl'; |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * A wrapper for getTemplateFileName that includes calling the hook to |
| 325 | * prevent us from having to copy & paste the logic of calling the hook |
| 326 | */ |
| 327 | function getHookedTemplateFileName() { |
| 328 | $pageTemplateFile = $this->getTemplateFileName(); |
| 329 | CRM_Utils_Hook::alterTemplateFile(get_class($this), $this, 'page', $pageTemplateFile); |
| 330 | return $pageTemplateFile; |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * Default extra tpl file basically just replaces .tpl with .extra.tpl |
| 335 | * i.e. we dont override |
| 336 | * |
| 337 | * @return string |
| 338 | * @access public |
| 339 | */ |
| 340 | function overrideExtraTemplateFileName() { |
| 341 | return NULL; |
| 342 | } |
| 343 | |
| 344 | /** |
| 345 | * setter for embedded |
| 346 | * |
| 347 | * @param boolean $embedded |
| 348 | * |
| 349 | * @return void |
| 350 | * @access public |
| 351 | */ |
| 352 | function setEmbedded($embedded) { |
| 353 | $this->_embedded = $embedded; |
| 354 | } |
| 355 | |
| 356 | /** |
| 357 | * getter for embedded |
| 358 | * |
| 359 | * @return boolean return the embedded value |
| 360 | * @access public |
| 361 | */ |
| 362 | function getEmbedded() { |
| 363 | return $this->_embedded; |
| 364 | } |
| 365 | |
| 366 | /** |
| 367 | * setter for print |
| 368 | * |
| 369 | * @param boolean $print |
| 370 | * |
| 371 | * @return void |
| 372 | * @access public |
| 373 | */ |
| 374 | function setPrint($print) { |
| 375 | $this->_print = $print; |
| 376 | } |
| 377 | |
| 378 | /** |
| 379 | * getter for print |
| 380 | * |
| 381 | * @return boolean return the print value |
| 382 | * @access public |
| 383 | */ |
| 384 | function getPrint() { |
| 385 | return $this->_print; |
| 386 | } |
| 387 | |
| 388 | static function &getTemplate() { |
| 389 | return self::$_template; |
| 390 | } |
| 391 | |
| 392 | function getVar($name) { |
| 393 | return isset($this->$name) ? $this->$name : NULL; |
| 394 | } |
| 395 | |
| 396 | function setVar($name, $value) { |
| 397 | $this->$name = $value; |
| 398 | } |
| 399 | } |
| 400 | |