Merge pull request #1878 from eileenmcnaughton/4.4
[civicrm-core.git] / CRM / Core / Page.php
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
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
209 if (!defined('CIVICRM_UF_HEAD') && $region = CRM_Core_Region::instance('html-header', FALSE)) {
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 * appends values to template variables
278 *
279 * @param array|string $tpl_var the template variable name(s)
280 * @param mixed $value the value to append
281 * @param bool $merge
282 */
283 function append($tpl_var, $value=NULL, $merge=FALSE) {
284 self::$_template->append($tpl_var, $value, $merge);
285 }
286
287 /**
288 * Returns an array containing template variables
289 *
290 * @param string $name
291 * @param string $type
292 * @return array
293 */
294 function get_template_vars($name=null) {
295 return self::$_template->get_template_vars($name);
296 }
297
298 /**
299 * function to destroy all the session state of this page.
300 *
301 * @access public
302 *
303 * @return void
304 */
305 function reset() {
306 self::$_session->resetScope($this->_name);
307 }
308
309 /**
310 * Use the form name to create the tpl file name
311 *
312 * @return string
313 * @access public
314 */
315 function getTemplateFileName() {
316 return str_replace('_',
317 DIRECTORY_SEPARATOR,
318 CRM_Utils_System::getClassName($this)
319 ) . '.tpl';
320 }
321
322 /**
323 * A wrapper for getTemplateFileName that includes calling the hook to
324 * prevent us from having to copy & paste the logic of calling the hook
325 */
326 function getHookedTemplateFileName() {
327 $pageTemplateFile = $this->getTemplateFileName();
328 CRM_Utils_Hook::alterTemplateFile(get_class($this), $this, 'page', $pageTemplateFile);
329 return $pageTemplateFile;
330 }
331
332 /**
333 * Default extra tpl file basically just replaces .tpl with .extra.tpl
334 * i.e. we dont override
335 *
336 * @return string
337 * @access public
338 */
339 function overrideExtraTemplateFileName() {
340 return NULL;
341 }
342
343 /**
344 * setter for embedded
345 *
346 * @param boolean $embedded
347 *
348 * @return void
349 * @access public
350 */
351 function setEmbedded($embedded) {
352 $this->_embedded = $embedded;
353 }
354
355 /**
356 * getter for embedded
357 *
358 * @return boolean return the embedded value
359 * @access public
360 */
361 function getEmbedded() {
362 return $this->_embedded;
363 }
364
365 /**
366 * setter for print
367 *
368 * @param boolean $print
369 *
370 * @return void
371 * @access public
372 */
373 function setPrint($print) {
374 $this->_print = $print;
375 }
376
377 /**
378 * getter for print
379 *
380 * @return boolean return the print value
381 * @access public
382 */
383 function getPrint() {
384 return $this->_print;
385 }
386
387 static function &getTemplate() {
388 return self::$_template;
389 }
390
391 function getVar($name) {
392 return isset($this->$name) ? $this->$name : NULL;
393 }
394
395 function setVar($name, $value) {
396 $this->$name = $value;
397 }
398 }
399