CRM-16373 - Setting.getoptions - Read/execute callbacks from *.settings.php
[civicrm-core.git] / CRM / Core / Page.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 *
30 * @package CRM
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
6a488035
TO
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 */
44class CRM_Core_Page {
45
46 /**
47 * The name of the page (auto generated from class name)
48 *
49 * @var string
6a488035
TO
50 */
51 protected $_name;
52
53 /**
fe482240 54 * The title associated with this page.
6a488035
TO
55 *
56 * @var object
6a488035
TO
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
6a488035
TO
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
6a488035
TO
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
6a488035
TO
81 */
82 protected $_print = FALSE;
83
84 /**
100fef9d 85 * Cache the smarty template for efficiency reasons
6a488035
TO
86 *
87 * @var CRM_Core_Smarty
6a488035
TO
88 */
89 static protected $_template;
90
91 /**
100fef9d 92 * Cache the session for efficiency reasons
6a488035
TO
93 *
94 * @var CRM_Core_Session
6a488035
TO
95 */
96 static protected $_session;
97
fc05b8da
CW
98 /**
99 * What to return to the client if in ajax mode (snippet=json)
100 *
101 * @var array
102 */
103 public $ajaxResponse = array();
104
7d93bcc4
CW
105 /**
106 * Url path used to reach this page
107 *
108 * @var array
109 */
110 public $urlPath = array();
111
96f50de2
CW
112 /**
113 * Should crm.livePage.js be added to the page?
114 * @var bool
115 */
116 public $useLivePageJS;
117
6a488035 118 /**
fe482240 119 * Class constructor.
6a488035 120 *
6a0b768e
TO
121 * @param string $title
122 * Title of the page.
123 * @param int $mode
124 * Mode of the page.
6a488035
TO
125 *
126 * @return CRM_Core_Page
127 */
00be9182 128 public function __construct($title = NULL, $mode = NULL) {
353ffa53 129 $this->_name = CRM_Utils_System::getClassName($this);
6a488035 130 $this->_title = $title;
353ffa53 131 $this->_mode = $mode;
6a488035
TO
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
0e017a41
CW
139 // FIXME - why are we messing with 'snippet'? Why not just pass it directly into $this->_print?
140 if (!empty($_REQUEST['snippet'])) {
03a7ec8f 141 if ($_REQUEST['snippet'] == CRM_Core_Smarty::PRINT_PDF) {
6a488035
TO
142 $this->_print = CRM_Core_Smarty::PRINT_PDF;
143 }
03a7ec8f 144 // FIXME - why does this number not match the constant?
0e017a41 145 elseif ($_REQUEST['snippet'] == 5) {
6a488035
TO
146 $this->_print = CRM_Core_Smarty::PRINT_NOFORM;
147 }
fc05b8da
CW
148 // Support 'json' as well as legacy value '6'
149 elseif (in_array($_REQUEST['snippet'], array(CRM_Core_Smarty::PRINT_JSON, 6))) {
0e017a41
CW
150 $this->_print = CRM_Core_Smarty::PRINT_JSON;
151 }
6a488035
TO
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
a7488080 158 if (!empty($_REQUEST['reset'])) {
6a488035
TO
159 $this->reset();
160 }
161 }
162
163 /**
164 * This function takes care of all the things common to all
8d7a9d07
CB
165 * pages. This typically involves assigning the appropriate smarty
166 * variable :)
6a488035 167 *
8d7a9d07 168 * @return void|string
4bf0b605 169 * The content generated by running this page
6a488035 170 */
00be9182 171 public function run() {
6a488035 172 if ($this->_embedded) {
8d7a9d07 173 return NULL;
6a488035
TO
174 }
175
176 self::$_template->assign('mode', $this->_mode);
177
8aac22c8 178 $pageTemplateFile = $this->getHookedTemplateFileName();
6a488035
TO
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) {
353ffa53
TO
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,
8d7a9d07 189 CRM_Core_Smarty::PRINT_JSON,
353ffa53 190 ))) {
6a488035
TO
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 }
0e017a41 210 elseif ($this->_print == CRM_Core_Smarty::PRINT_JSON) {
fc05b8da
CW
211 $this->ajaxResponse['content'] = $content;
212 CRM_Core_Page_AJAX::returnJsonResponse($this->ajaxResponse);
0e017a41 213 }
6a488035
TO
214 else {
215 echo $content;
216 }
217 CRM_Utils_System::civiExit();
218 }
219
220 $config = CRM_Core_Config::singleton();
221
5c33ad28 222 // Intermittent alert to admins
c90a093a 223 CRM_Utils_Check::singleton()->showPeriodicAlerts();
8ef12e64 224
96f50de2 225 if ($this->useLivePageJS &&
353ffa53
TO
226 CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'ajaxPopupsEnabled', NULL, TRUE)
227 ) {
96ed17aa 228 CRM_Core_Resources::singleton()->addScriptFile('civicrm', 'js/crm.livePage.js', 1, 'html-header');
6a488035
TO
229 }
230
231 $content = self::$_template->fetch('CRM/common/' . strtolower($config->userFramework) . '.tpl');
232
233 // Render page header
9dc21423 234 if (!defined('CIVICRM_UF_HEAD') && $region = CRM_Core_Region::instance('html-header', FALSE)) {
6a488035
TO
235 CRM_Utils_System::addHTMLHead($region->render(''));
236 }
237 CRM_Utils_System::appendTPLFile($pageTemplateFile, $content);
238
239 //its time to call the hook.
240 CRM_Utils_Hook::alterContent($content, 'page', $pageTemplateFile, $this);
241
242 echo CRM_Utils_System::theme($content, $this->_print);
6a488035
TO
243 }
244
245 /**
fe482240 246 * Store the variable with the value in the form scope.
6a488035 247 *
6a0b768e
TO
248 * @param string|array $name name of the variable or an assoc array of name/value pairs
249 * @param mixed $value
250 * Value of the variable if string.
6a488035 251 *
6a488035
TO
252 *
253 * @return void
6a488035 254 */
00be9182 255 public function set($name, $value = NULL) {
6a488035
TO
256 self::$_session->set($name, $value, $this->_name);
257 }
258
259 /**
fe482240 260 * Get the variable from the form scope.
6a488035 261 *
8d7a9d07 262 * @param string $name name of the variable
6a488035
TO
263 *
264 * @return mixed
6a488035 265 */
00be9182 266 public function get($name) {
6a488035
TO
267 return self::$_session->get($name, $this->_name);
268 }
269
270 /**
fe482240 271 * Assign value to name in template.
6a488035 272 *
c490a46a 273 * @param string $var
6a0b768e
TO
274 * @param mixed $value
275 * Value of varaible.
6a488035
TO
276 *
277 * @return void
6a488035 278 */
00be9182 279 public function assign($var, $value = NULL) {
6a488035
TO
280 self::$_template->assign($var, $value);
281 }
282
283 /**
fe482240 284 * Assign value to name in template by reference.
6a488035 285 *
c490a46a 286 * @param string $var
6a0b768e
TO
287 * @param mixed $value
288 * (reference) value of varaible.
6a488035
TO
289 *
290 * @return void
6a488035 291 */
00be9182 292 public function assign_by_ref($var, &$value) {
6a488035
TO
293 self::$_template->assign_by_ref($var, $value);
294 }
295
4a9538ac 296 /**
fe482240 297 * Appends values to template variables.
4a9538ac
CW
298 *
299 * @param array|string $tpl_var the template variable name(s)
6a0b768e
TO
300 * @param mixed $value
301 * The value to append.
4a9538ac
CW
302 * @param bool $merge
303 */
2aa397bc 304 public function append($tpl_var, $value = NULL, $merge = FALSE) {
4a9538ac
CW
305 self::$_template->append($tpl_var, $value, $merge);
306 }
307
308 /**
fe482240 309 * Returns an array containing template variables.
4a9538ac
CW
310 *
311 * @param string $name
fd31fa4c 312 *
4a9538ac
CW
313 * @return array
314 */
2aa397bc 315 public function get_template_vars($name = NULL) {
4a9538ac
CW
316 return self::$_template->get_template_vars($name);
317 }
318
6a488035 319 /**
100fef9d 320 * Destroy all the session state of this page.
6a488035 321 *
6a488035
TO
322 *
323 * @return void
324 */
00be9182 325 public function reset() {
6a488035
TO
326 self::$_session->resetScope($this->_name);
327 }
328
329 /**
fe482240 330 * Use the form name to create the tpl file name.
6a488035
TO
331 *
332 * @return string
6a488035 333 */
00be9182 334 public function getTemplateFileName() {
9b591d79
TO
335 return strtr(
336 CRM_Utils_System::getClassName($this),
337 array(
338 '_' => DIRECTORY_SEPARATOR,
339 '\\' => DIRECTORY_SEPARATOR,
340 )
6a488035
TO
341 ) . '.tpl';
342 }
343
8aac22c8 344 /**
345 * A wrapper for getTemplateFileName that includes calling the hook to
346 * prevent us from having to copy & paste the logic of calling the hook
347 */
00be9182 348 public function getHookedTemplateFileName() {
8aac22c8 349 $pageTemplateFile = $this->getTemplateFileName();
350 CRM_Utils_Hook::alterTemplateFile(get_class($this), $this, 'page', $pageTemplateFile);
351 return $pageTemplateFile;
352 }
353
6a488035
TO
354 /**
355 * Default extra tpl file basically just replaces .tpl with .extra.tpl
356 * i.e. we dont override
357 *
358 * @return string
6a488035 359 */
00be9182 360 public function overrideExtraTemplateFileName() {
6a488035
TO
361 return NULL;
362 }
363
364 /**
fe482240 365 * Setter for embedded.
6a488035 366 *
6a0b768e 367 * @param bool $embedded
6a488035
TO
368 *
369 * @return void
6a488035 370 */
00be9182 371 public function setEmbedded($embedded) {
6a488035
TO
372 $this->_embedded = $embedded;
373 }
374
375 /**
fe482240 376 * Getter for embedded.
6a488035 377 *
8d7a9d07 378 * @return bool
a6c01b45 379 * return the embedded value
6a488035 380 */
00be9182 381 public function getEmbedded() {
6a488035
TO
382 return $this->_embedded;
383 }
384
385 /**
fe482240 386 * Setter for print.
6a488035 387 *
6a0b768e 388 * @param bool $print
6a488035
TO
389 *
390 * @return void
6a488035 391 */
00be9182 392 public function setPrint($print) {
6a488035
TO
393 $this->_print = $print;
394 }
395
396 /**
fe482240 397 * Getter for print.
6a488035 398 *
8d7a9d07 399 * @return bool
a6c01b45 400 * return the print value
6a488035 401 */
00be9182 402 public function getPrint() {
6a488035
TO
403 return $this->_print;
404 }
405
a0ee3941
EM
406 /**
407 * @return CRM_Core_Smarty
408 */
00be9182 409 public static function &getTemplate() {
6a488035
TO
410 return self::$_template;
411 }
412
a0ee3941 413 /**
100fef9d 414 * @param string $name
a0ee3941
EM
415 *
416 * @return null
417 */
00be9182 418 public function getVar($name) {
6a488035
TO
419 return isset($this->$name) ? $this->$name : NULL;
420 }
421
a0ee3941 422 /**
100fef9d 423 * @param string $name
a0ee3941
EM
424 * @param $value
425 */
00be9182 426 public function setVar($name, $value) {
6a488035
TO
427 $this->$name = $value;
428 }
96025800 429
6a488035 430}