Merge pull request #2387 from pratik-joshi/CRM-13973
[civicrm-core.git] / CRM / Core / Page.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
232624b1 4 | CiviCRM version 4.4 |
6a488035
TO
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 */
44class 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
fc05b8da
CW
107 /**
108 * What to return to the client if in ajax mode (snippet=json)
109 *
110 * @var array
111 */
112 public $ajaxResponse = array();
113
7d93bcc4
CW
114 /**
115 * Url path used to reach this page
116 *
117 * @var array
118 */
119 public $urlPath = array();
120
6a488035
TO
121 /**
122 * class constructor
123 *
124 * @param string $title title of the page
125 * @param int $mode mode of the page
126 *
127 * @return CRM_Core_Page
128 */
129 function __construct($title = NULL, $mode = NULL) {
130 $this->_name = CRM_Utils_System::getClassName($this);
131 $this->_title = $title;
132 $this->_mode = $mode;
133
134 // let the constructor initialize this, should happen only once
135 if (!isset(self::$_template)) {
136 self::$_template = CRM_Core_Smarty::singleton();
137 self::$_session = CRM_Core_Session::singleton();
138 }
139
0e017a41
CW
140 // FIXME - why are we messing with 'snippet'? Why not just pass it directly into $this->_print?
141 if (!empty($_REQUEST['snippet'])) {
03a7ec8f 142 if ($_REQUEST['snippet'] == CRM_Core_Smarty::PRINT_PDF) {
6a488035
TO
143 $this->_print = CRM_Core_Smarty::PRINT_PDF;
144 }
03a7ec8f 145 // FIXME - why does this number not match the constant?
0e017a41 146 elseif ($_REQUEST['snippet'] == 5) {
6a488035
TO
147 $this->_print = CRM_Core_Smarty::PRINT_NOFORM;
148 }
fc05b8da
CW
149 // Support 'json' as well as legacy value '6'
150 elseif (in_array($_REQUEST['snippet'], array(CRM_Core_Smarty::PRINT_JSON, 6))) {
0e017a41
CW
151 $this->_print = CRM_Core_Smarty::PRINT_JSON;
152 }
6a488035
TO
153 else {
154 $this->_print = CRM_Core_Smarty::PRINT_SNIPPET;
155 }
156 }
157
158 // if the request has a reset value, initialize the controller session
a7488080 159 if (!empty($_REQUEST['reset'])) {
6a488035
TO
160 $this->reset();
161 }
162 }
163
164 /**
165 * This function takes care of all the things common to all
166 * pages. This typically involves assigning the appropriate
167 * smarty variable :)
168 *
169 * @return string The content generated by running this page
170 */
171 function run() {
172 if ($this->_embedded) {
173 return;
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) {
185 if (in_array( $this->_print, array( CRM_Core_Smarty::PRINT_SNIPPET,
0e017a41 186 CRM_Core_Smarty::PRINT_PDF, CRM_Core_Smarty::PRINT_NOFORM, CRM_Core_Smarty::PRINT_JSON ))) {
6a488035
TO
187 $content = self::$_template->fetch('CRM/common/snippet.tpl');
188 }
189 else {
190 $content = self::$_template->fetch('CRM/common/print.tpl');
191 }
192
193 CRM_Utils_System::appendTPLFile($pageTemplateFile,
194 $content,
195 $this->overrideExtraTemplateFileName()
196 );
197
198 //its time to call the hook.
199 CRM_Utils_Hook::alterContent($content, 'page', $pageTemplateFile, $this);
200
201 if ($this->_print == CRM_Core_Smarty::PRINT_PDF) {
202 CRM_Utils_PDF_Utils::html2pdf($content, "{$this->_name}.pdf", FALSE,
203 array('paper_size' => 'a3', 'orientation' => 'landscape')
204 );
205 }
0e017a41 206 elseif ($this->_print == CRM_Core_Smarty::PRINT_JSON) {
fc05b8da
CW
207 $this->ajaxResponse['content'] = $content;
208 CRM_Core_Page_AJAX::returnJsonResponse($this->ajaxResponse);
0e017a41 209 }
6a488035
TO
210 else {
211 echo $content;
212 }
213 CRM_Utils_System::civiExit();
214 }
215
216 $config = CRM_Core_Config::singleton();
217
218 // TODO: Is there a better way to ensure these actions don't happen during AJAX requests?
219 if (empty($_GET['snippet'])) {
220 // Version check and intermittent alert to admins
221 CRM_Utils_VersionCheck::singleton()->versionAlert();
8ef12e64 222
6a488035
TO
223 // Debug msg once per hour
224 if ($config->debug && CRM_Core_Permission::check('administer CiviCRM') && CRM_Core_Session::singleton()->timer('debug_alert', 3600)) {
225 $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')));
226 CRM_Core_Session::setStatus($msg, ts('Debug Mode'));
227 }
228 }
229
230 $content = self::$_template->fetch('CRM/common/' . strtolower($config->userFramework) . '.tpl');
231
232 // Render page header
9dc21423 233 if (!defined('CIVICRM_UF_HEAD') && $region = CRM_Core_Region::instance('html-header', FALSE)) {
6a488035
TO
234 CRM_Utils_System::addHTMLHead($region->render(''));
235 }
236 CRM_Utils_System::appendTPLFile($pageTemplateFile, $content);
237
238 //its time to call the hook.
239 CRM_Utils_Hook::alterContent($content, 'page', $pageTemplateFile, $this);
240
241 echo CRM_Utils_System::theme($content, $this->_print);
242 return;
243 }
244
245 /**
246 * Store the variable with the value in the form scope
247 *
248 * @param string|array $name name of the variable or an assoc array of name/value pairs
249 * @param mixed $value value of the variable if string
250 *
251 * @access public
252 *
253 * @return void
254 *
255 */
256 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 * @access public
266 *
267 * @return mixed
268 *
269 */
270 function get($name) {
271 return self::$_session->get($name, $this->_name);
272 }
273
274 /**
275 * assign value to name in template
276 *
277 * @param array|string $name name of variable
278 * @param mixed $value value of varaible
279 *
280 * @return void
281 * @access public
282 */
283 function assign($var, $value = NULL) {
284 self::$_template->assign($var, $value);
285 }
286
287 /**
288 * assign value to name in template by reference
289 *
290 * @param array|string $name name of variable
291 * @param mixed $value (reference) value of varaible
292 *
293 * @return void
294 * @access public
295 */
296 function assign_by_ref($var, &$value) {
297 self::$_template->assign_by_ref($var, $value);
298 }
299
4a9538ac
CW
300 /**
301 * appends values to template variables
302 *
303 * @param array|string $tpl_var the template variable name(s)
304 * @param mixed $value the value to append
305 * @param bool $merge
306 */
307 function append($tpl_var, $value=NULL, $merge=FALSE) {
308 self::$_template->append($tpl_var, $value, $merge);
309 }
310
311 /**
312 * Returns an array containing template variables
313 *
314 * @param string $name
315 * @param string $type
316 * @return array
317 */
318 function get_template_vars($name=null) {
319 return self::$_template->get_template_vars($name);
320 }
321
6a488035
TO
322 /**
323 * function to destroy all the session state of this page.
324 *
325 * @access public
326 *
327 * @return void
328 */
329 function reset() {
330 self::$_session->resetScope($this->_name);
331 }
332
333 /**
334 * Use the form name to create the tpl file name
335 *
336 * @return string
337 * @access public
338 */
339 function getTemplateFileName() {
340 return str_replace('_',
341 DIRECTORY_SEPARATOR,
342 CRM_Utils_System::getClassName($this)
343 ) . '.tpl';
344 }
345
8aac22c8 346 /**
347 * A wrapper for getTemplateFileName that includes calling the hook to
348 * prevent us from having to copy & paste the logic of calling the hook
349 */
350 function getHookedTemplateFileName() {
351 $pageTemplateFile = $this->getTemplateFileName();
352 CRM_Utils_Hook::alterTemplateFile(get_class($this), $this, 'page', $pageTemplateFile);
353 return $pageTemplateFile;
354 }
355
6a488035
TO
356 /**
357 * Default extra tpl file basically just replaces .tpl with .extra.tpl
358 * i.e. we dont override
359 *
360 * @return string
361 * @access public
362 */
363 function overrideExtraTemplateFileName() {
364 return NULL;
365 }
366
367 /**
368 * setter for embedded
369 *
370 * @param boolean $embedded
371 *
372 * @return void
373 * @access public
374 */
375 function setEmbedded($embedded) {
376 $this->_embedded = $embedded;
377 }
378
379 /**
380 * getter for embedded
381 *
382 * @return boolean return the embedded value
383 * @access public
384 */
385 function getEmbedded() {
386 return $this->_embedded;
387 }
388
389 /**
390 * setter for print
391 *
392 * @param boolean $print
393 *
394 * @return void
395 * @access public
396 */
397 function setPrint($print) {
398 $this->_print = $print;
399 }
400
401 /**
402 * getter for print
403 *
404 * @return boolean return the print value
405 * @access public
406 */
407 function getPrint() {
408 return $this->_print;
409 }
410
411 static function &getTemplate() {
412 return self::$_template;
413 }
414
415 function getVar($name) {
416 return isset($this->$name) ? $this->$name : NULL;
417 }
418
419 function setVar($name, $value) {
420 $this->$name = $value;
421 }
422}
423