INFRA-132 - Change "else if" to "elseif"
[civicrm-core.git] / CRM / Core / Page.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
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 /**
100fef9d 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 * @static
89 */
90 static protected $_template;
91
92 /**
100fef9d 93 * Cache the session for efficiency reasons
6a488035
TO
94 *
95 * @var CRM_Core_Session
6a488035
TO
96 * @static
97 */
98 static protected $_session;
99
fc05b8da
CW
100 /**
101 * What to return to the client if in ajax mode (snippet=json)
102 *
103 * @var array
104 */
105 public $ajaxResponse = array();
106
7d93bcc4
CW
107 /**
108 * Url path used to reach this page
109 *
110 * @var array
111 */
112 public $urlPath = array();
113
96f50de2
CW
114 /**
115 * Should crm.livePage.js be added to the page?
116 * @var bool
117 */
118 public $useLivePageJS;
119
6a488035 120 /**
100fef9d 121 * Class constructor
6a488035 122 *
6a0b768e
TO
123 * @param string $title
124 * Title of the page.
125 * @param int $mode
126 * Mode of the page.
6a488035
TO
127 *
128 * @return CRM_Core_Page
129 */
00be9182 130 public function __construct($title = NULL, $mode = NULL) {
6a488035
TO
131 $this->_name = CRM_Utils_System::getClassName($this);
132 $this->_title = $title;
133 $this->_mode = $mode;
134
135 // let the constructor initialize this, should happen only once
136 if (!isset(self::$_template)) {
137 self::$_template = CRM_Core_Smarty::singleton();
138 self::$_session = CRM_Core_Session::singleton();
139 }
140
0e017a41
CW
141 // FIXME - why are we messing with 'snippet'? Why not just pass it directly into $this->_print?
142 if (!empty($_REQUEST['snippet'])) {
03a7ec8f 143 if ($_REQUEST['snippet'] == CRM_Core_Smarty::PRINT_PDF) {
6a488035
TO
144 $this->_print = CRM_Core_Smarty::PRINT_PDF;
145 }
03a7ec8f 146 // FIXME - why does this number not match the constant?
0e017a41 147 elseif ($_REQUEST['snippet'] == 5) {
6a488035
TO
148 $this->_print = CRM_Core_Smarty::PRINT_NOFORM;
149 }
fc05b8da
CW
150 // Support 'json' as well as legacy value '6'
151 elseif (in_array($_REQUEST['snippet'], array(CRM_Core_Smarty::PRINT_JSON, 6))) {
0e017a41
CW
152 $this->_print = CRM_Core_Smarty::PRINT_JSON;
153 }
6a488035
TO
154 else {
155 $this->_print = CRM_Core_Smarty::PRINT_SNIPPET;
156 }
157 }
158
159 // if the request has a reset value, initialize the controller session
a7488080 160 if (!empty($_REQUEST['reset'])) {
6a488035
TO
161 $this->reset();
162 }
163 }
164
165 /**
166 * This function takes care of all the things common to all
167 * pages. This typically involves assigning the appropriate
168 * smarty variable :)
169 *
4bf0b605
AH
170 * @return string
171 * The content generated by running this page
6a488035 172 */
00be9182 173 public function run() {
6a488035
TO
174 if ($this->_embedded) {
175 return;
176 }
177
178 self::$_template->assign('mode', $this->_mode);
179
8aac22c8 180 $pageTemplateFile = $this->getHookedTemplateFileName();
6a488035
TO
181 self::$_template->assign('tplFile', $pageTemplateFile);
182
183 // invoke the pagRun hook, CRM-3906
184 CRM_Utils_Hook::pageRun($this);
185
186 if ($this->_print) {
187 if (in_array( $this->_print, array( CRM_Core_Smarty::PRINT_SNIPPET,
0e017a41 188 CRM_Core_Smarty::PRINT_PDF, CRM_Core_Smarty::PRINT_NOFORM, CRM_Core_Smarty::PRINT_JSON ))) {
6a488035
TO
189 $content = self::$_template->fetch('CRM/common/snippet.tpl');
190 }
191 else {
192 $content = self::$_template->fetch('CRM/common/print.tpl');
193 }
194
195 CRM_Utils_System::appendTPLFile($pageTemplateFile,
196 $content,
197 $this->overrideExtraTemplateFileName()
198 );
199
200 //its time to call the hook.
201 CRM_Utils_Hook::alterContent($content, 'page', $pageTemplateFile, $this);
202
203 if ($this->_print == CRM_Core_Smarty::PRINT_PDF) {
204 CRM_Utils_PDF_Utils::html2pdf($content, "{$this->_name}.pdf", FALSE,
205 array('paper_size' => 'a3', 'orientation' => 'landscape')
206 );
207 }
0e017a41 208 elseif ($this->_print == CRM_Core_Smarty::PRINT_JSON) {
fc05b8da
CW
209 $this->ajaxResponse['content'] = $content;
210 CRM_Core_Page_AJAX::returnJsonResponse($this->ajaxResponse);
0e017a41 211 }
6a488035
TO
212 else {
213 echo $content;
214 }
215 CRM_Utils_System::civiExit();
216 }
217
218 $config = CRM_Core_Config::singleton();
219
96f50de2
CW
220 // Version check and intermittent alert to admins
221 CRM_Utils_VersionCheck::singleton()->versionAlert();
c90a093a 222 CRM_Utils_Check::singleton()->showPeriodicAlerts();
8ef12e64 223
96f50de2 224 if ($this->useLivePageJS &&
53f2643c 225 CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'ajaxPopupsEnabled', NULL, TRUE))
96f50de2 226 {
96ed17aa 227 CRM_Core_Resources::singleton()->addScriptFile('civicrm', 'js/crm.livePage.js', 1, 'html-header');
2e401199 228 $this->assign('includeWysiwygEditor', TRUE);
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);
243 return;
244 }
245
246 /**
247 * Store the variable with the value in the form scope
248 *
6a0b768e
TO
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.
6a488035 252 *
6a488035
TO
253 *
254 * @return void
255 *
256 */
00be9182 257 public function set($name, $value = NULL) {
6a488035
TO
258 self::$_session->set($name, $value, $this->_name);
259 }
260
261 /**
262 * Get the variable from the form scope
263 *
6a0b768e 264 * @param string name : name of the variable
6a488035 265 *
6a488035
TO
266 *
267 * @return mixed
268 *
269 */
00be9182 270 public function get($name) {
6a488035
TO
271 return self::$_session->get($name, $this->_name);
272 }
273
274 /**
100fef9d 275 * Assign value to name in template
6a488035 276 *
c490a46a 277 * @param string $var
6a0b768e
TO
278 * @param mixed $value
279 * Value of varaible.
6a488035
TO
280 *
281 * @return void
6a488035 282 */
00be9182 283 public function assign($var, $value = NULL) {
6a488035
TO
284 self::$_template->assign($var, $value);
285 }
286
287 /**
100fef9d 288 * Assign value to name in template by reference
6a488035 289 *
c490a46a 290 * @param string $var
6a0b768e
TO
291 * @param mixed $value
292 * (reference) value of varaible.
6a488035
TO
293 *
294 * @return void
6a488035 295 */
00be9182 296 public function assign_by_ref($var, &$value) {
6a488035
TO
297 self::$_template->assign_by_ref($var, $value);
298 }
299
4a9538ac 300 /**
100fef9d 301 * Appends values to template variables
4a9538ac
CW
302 *
303 * @param array|string $tpl_var the template variable name(s)
6a0b768e
TO
304 * @param mixed $value
305 * The value to append.
4a9538ac
CW
306 * @param bool $merge
307 */
2aa397bc 308 public function append($tpl_var, $value = NULL, $merge = FALSE) {
4a9538ac
CW
309 self::$_template->append($tpl_var, $value, $merge);
310 }
311
312 /**
313 * Returns an array containing template variables
314 *
315 * @param string $name
fd31fa4c 316 *
4a9538ac
CW
317 * @return array
318 */
2aa397bc 319 public function get_template_vars($name = NULL) {
4a9538ac
CW
320 return self::$_template->get_template_vars($name);
321 }
322
6a488035 323 /**
100fef9d 324 * Destroy all the session state of this page.
6a488035 325 *
6a488035
TO
326 *
327 * @return void
328 */
00be9182 329 public function reset() {
6a488035
TO
330 self::$_session->resetScope($this->_name);
331 }
332
333 /**
334 * Use the form name to create the tpl file name
335 *
336 * @return string
6a488035 337 */
00be9182 338 public function getTemplateFileName() {
6a488035
TO
339 return str_replace('_',
340 DIRECTORY_SEPARATOR,
341 CRM_Utils_System::getClassName($this)
342 ) . '.tpl';
343 }
344
8aac22c8 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 */
00be9182 349 public function getHookedTemplateFileName() {
8aac22c8 350 $pageTemplateFile = $this->getTemplateFileName();
351 CRM_Utils_Hook::alterTemplateFile(get_class($this), $this, 'page', $pageTemplateFile);
352 return $pageTemplateFile;
353 }
354
6a488035
TO
355 /**
356 * Default extra tpl file basically just replaces .tpl with .extra.tpl
357 * i.e. we dont override
358 *
359 * @return string
6a488035 360 */
00be9182 361 public function overrideExtraTemplateFileName() {
6a488035
TO
362 return NULL;
363 }
364
365 /**
100fef9d 366 * Setter for embedded
6a488035 367 *
6a0b768e 368 * @param bool $embedded
6a488035
TO
369 *
370 * @return void
6a488035 371 */
00be9182 372 public function setEmbedded($embedded) {
6a488035
TO
373 $this->_embedded = $embedded;
374 }
375
376 /**
100fef9d 377 * Getter for embedded
6a488035
TO
378 *
379 * @return boolean return the embedded value
6a488035 380 */
00be9182 381 public function getEmbedded() {
6a488035
TO
382 return $this->_embedded;
383 }
384
385 /**
100fef9d 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 /**
100fef9d 397 * Getter for print
6a488035
TO
398 *
399 * @return boolean return the print value
6a488035 400 */
00be9182 401 public function getPrint() {
6a488035
TO
402 return $this->_print;
403 }
404
a0ee3941
EM
405 /**
406 * @return CRM_Core_Smarty
407 */
00be9182 408 public static function &getTemplate() {
6a488035
TO
409 return self::$_template;
410 }
411
a0ee3941 412 /**
100fef9d 413 * @param string $name
a0ee3941
EM
414 *
415 * @return null
416 */
00be9182 417 public function getVar($name) {
6a488035
TO
418 return isset($this->$name) ? $this->$name : NULL;
419 }
420
a0ee3941 421 /**
100fef9d 422 * @param string $name
a0ee3941
EM
423 * @param $value
424 */
00be9182 425 public function setVar($name, $value) {
6a488035
TO
426 $this->$name = $value;
427 }
428}