Merge pull request #1876 from colemanw/4.4
[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
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
8aac22c8 158 $pageTemplateFile = $this->getHookedTemplateFileName();
6a488035
TO
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();
8ef12e64 198
6a488035
TO
199 // Debug msg once per hour
200 if ($config->debug && CRM_Core_Permission::check('administer CiviCRM') && CRM_Core_Session::singleton()->timer('debug_alert', 3600)) {
201 $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')));
202 CRM_Core_Session::setStatus($msg, ts('Debug Mode'));
203 }
204 }
205
206 $content = self::$_template->fetch('CRM/common/' . strtolower($config->userFramework) . '.tpl');
207
208 // Render page header
9dc21423 209 if (!defined('CIVICRM_UF_HEAD') && $region = CRM_Core_Region::instance('html-header', FALSE)) {
6a488035
TO
210 CRM_Utils_System::addHTMLHead($region->render(''));
211 }
212 CRM_Utils_System::appendTPLFile($pageTemplateFile, $content);
213
214 //its time to call the hook.
215 CRM_Utils_Hook::alterContent($content, 'page', $pageTemplateFile, $this);
216
217 echo CRM_Utils_System::theme($content, $this->_print);
218 return;
219 }
220
221 /**
222 * Store the variable with the value in the form scope
223 *
224 * @param string|array $name name of the variable or an assoc array of name/value pairs
225 * @param mixed $value value of the variable if string
226 *
227 * @access public
228 *
229 * @return void
230 *
231 */
232 function set($name, $value = NULL) {
233 self::$_session->set($name, $value, $this->_name);
234 }
235
236 /**
237 * Get the variable from the form scope
238 *
239 * @param string name : name of the variable
240 *
241 * @access public
242 *
243 * @return mixed
244 *
245 */
246 function get($name) {
247 return self::$_session->get($name, $this->_name);
248 }
249
250 /**
251 * assign value to name in template
252 *
253 * @param array|string $name name of variable
254 * @param mixed $value value of varaible
255 *
256 * @return void
257 * @access public
258 */
259 function assign($var, $value = NULL) {
260 self::$_template->assign($var, $value);
261 }
262
263 /**
264 * assign value to name in template by reference
265 *
266 * @param array|string $name name of variable
267 * @param mixed $value (reference) value of varaible
268 *
269 * @return void
270 * @access public
271 */
272 function assign_by_ref($var, &$value) {
273 self::$_template->assign_by_ref($var, $value);
274 }
275
276 /**
277 * function to destroy all the session state of this page.
278 *
279 * @access public
280 *
281 * @return void
282 */
283 function reset() {
284 self::$_session->resetScope($this->_name);
285 }
286
287 /**
288 * Use the form name to create the tpl file name
289 *
290 * @return string
291 * @access public
292 */
293 function getTemplateFileName() {
294 return str_replace('_',
295 DIRECTORY_SEPARATOR,
296 CRM_Utils_System::getClassName($this)
297 ) . '.tpl';
298 }
299
8aac22c8 300 /**
301 * A wrapper for getTemplateFileName that includes calling the hook to
302 * prevent us from having to copy & paste the logic of calling the hook
303 */
304 function getHookedTemplateFileName() {
305 $pageTemplateFile = $this->getTemplateFileName();
306 CRM_Utils_Hook::alterTemplateFile(get_class($this), $this, 'page', $pageTemplateFile);
307 return $pageTemplateFile;
308 }
309
6a488035
TO
310 /**
311 * Default extra tpl file basically just replaces .tpl with .extra.tpl
312 * i.e. we dont override
313 *
314 * @return string
315 * @access public
316 */
317 function overrideExtraTemplateFileName() {
318 return NULL;
319 }
320
321 /**
322 * setter for embedded
323 *
324 * @param boolean $embedded
325 *
326 * @return void
327 * @access public
328 */
329 function setEmbedded($embedded) {
330 $this->_embedded = $embedded;
331 }
332
333 /**
334 * getter for embedded
335 *
336 * @return boolean return the embedded value
337 * @access public
338 */
339 function getEmbedded() {
340 return $this->_embedded;
341 }
342
343 /**
344 * setter for print
345 *
346 * @param boolean $print
347 *
348 * @return void
349 * @access public
350 */
351 function setPrint($print) {
352 $this->_print = $print;
353 }
354
355 /**
356 * getter for print
357 *
358 * @return boolean return the print value
359 * @access public
360 */
361 function getPrint() {
362 return $this->_print;
363 }
364
365 static function &getTemplate() {
366 return self::$_template;
367 }
368
369 function getVar($name) {
370 return isset($this->$name) ? $this->$name : NULL;
371 }
372
373 function setVar($name, $value) {
374 $this->$name = $value;
375 }
376}
377