Merge remote-tracking branch 'upstream/4.4' into 4.4-master-2013-11-26-11-46-28
[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 // FIXME - why are we messing with 'snippet'? Why not just pass it directly into $this->_print?
127 if (!empty($_REQUEST['snippet'])) {
128 if ($_REQUEST['snippet'] == CRM_Core_Smarty::PRINT_PDF) {
129 $this->_print = CRM_Core_Smarty::PRINT_PDF;
130 }
131 // FIXME - why does this number not match the constant?
132 elseif ($_REQUEST['snippet'] == 5) {
133 $this->_print = CRM_Core_Smarty::PRINT_NOFORM;
134 }
135 elseif ($_REQUEST['snippet'] == CRM_Core_Smarty::PRINT_JSON) {
136 $this->_print = CRM_Core_Smarty::PRINT_JSON;
137 }
138 else {
139 $this->_print = CRM_Core_Smarty::PRINT_SNIPPET;
140 }
141 }
142
143 // if the request has a reset value, initialize the controller session
144 if (CRM_Utils_Array::value('reset', $_REQUEST)) {
145 $this->reset();
146 }
147 }
148
149 /**
150 * This function takes care of all the things common to all
151 * pages. This typically involves assigning the appropriate
152 * smarty variable :)
153 *
154 * @return string The content generated by running this page
155 */
156 function run() {
157 if ($this->_embedded) {
158 return;
159 }
160
161 self::$_template->assign('mode', $this->_mode);
162
163 $pageTemplateFile = $this->getHookedTemplateFileName();
164 self::$_template->assign('tplFile', $pageTemplateFile);
165
166 // invoke the pagRun hook, CRM-3906
167 CRM_Utils_Hook::pageRun($this);
168
169 if ($this->_print) {
170 if (in_array( $this->_print, array( CRM_Core_Smarty::PRINT_SNIPPET,
171 CRM_Core_Smarty::PRINT_PDF, CRM_Core_Smarty::PRINT_NOFORM, CRM_Core_Smarty::PRINT_JSON ))) {
172 $content = self::$_template->fetch('CRM/common/snippet.tpl');
173 }
174 else {
175 $content = self::$_template->fetch('CRM/common/print.tpl');
176 }
177
178 CRM_Utils_System::appendTPLFile($pageTemplateFile,
179 $content,
180 $this->overrideExtraTemplateFileName()
181 );
182
183 //its time to call the hook.
184 CRM_Utils_Hook::alterContent($content, 'page', $pageTemplateFile, $this);
185
186 if ($this->_print == CRM_Core_Smarty::PRINT_PDF) {
187 CRM_Utils_PDF_Utils::html2pdf($content, "{$this->_name}.pdf", FALSE,
188 array('paper_size' => 'a3', 'orientation' => 'landscape')
189 );
190 }
191 elseif ($this->_print == CRM_Core_Smarty::PRINT_JSON) {
192 CRM_Core_Page_AJAX::returnJsonResponse($content);
193 }
194 else {
195 echo $content;
196 }
197 CRM_Utils_System::civiExit();
198 }
199
200 $config = CRM_Core_Config::singleton();
201
202 // TODO: Is there a better way to ensure these actions don't happen during AJAX requests?
203 if (empty($_GET['snippet'])) {
204 // Version check and intermittent alert to admins
205 CRM_Utils_VersionCheck::singleton()->versionAlert();
206
207 // Debug msg once per hour
208 if ($config->debug && CRM_Core_Permission::check('administer CiviCRM') && CRM_Core_Session::singleton()->timer('debug_alert', 3600)) {
209 $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')));
210 CRM_Core_Session::setStatus($msg, ts('Debug Mode'));
211 }
212 }
213
214 $content = self::$_template->fetch('CRM/common/' . strtolower($config->userFramework) . '.tpl');
215
216 // Render page header
217 if (!defined('CIVICRM_UF_HEAD') && $region = CRM_Core_Region::instance('html-header', FALSE)) {
218 CRM_Utils_System::addHTMLHead($region->render(''));
219 }
220 CRM_Utils_System::appendTPLFile($pageTemplateFile, $content);
221
222 //its time to call the hook.
223 CRM_Utils_Hook::alterContent($content, 'page', $pageTemplateFile, $this);
224
225 echo CRM_Utils_System::theme($content, $this->_print);
226 return;
227 }
228
229 /**
230 * Store the variable with the value in the form scope
231 *
232 * @param string|array $name name of the variable or an assoc array of name/value pairs
233 * @param mixed $value value of the variable if string
234 *
235 * @access public
236 *
237 * @return void
238 *
239 */
240 function set($name, $value = NULL) {
241 self::$_session->set($name, $value, $this->_name);
242 }
243
244 /**
245 * Get the variable from the form scope
246 *
247 * @param string name : name of the variable
248 *
249 * @access public
250 *
251 * @return mixed
252 *
253 */
254 function get($name) {
255 return self::$_session->get($name, $this->_name);
256 }
257
258 /**
259 * assign value to name in template
260 *
261 * @param array|string $name name of variable
262 * @param mixed $value value of varaible
263 *
264 * @return void
265 * @access public
266 */
267 function assign($var, $value = NULL) {
268 self::$_template->assign($var, $value);
269 }
270
271 /**
272 * assign value to name in template by reference
273 *
274 * @param array|string $name name of variable
275 * @param mixed $value (reference) value of varaible
276 *
277 * @return void
278 * @access public
279 */
280 function assign_by_ref($var, &$value) {
281 self::$_template->assign_by_ref($var, $value);
282 }
283
284 /**
285 * appends values to template variables
286 *
287 * @param array|string $tpl_var the template variable name(s)
288 * @param mixed $value the value to append
289 * @param bool $merge
290 */
291 function append($tpl_var, $value=NULL, $merge=FALSE) {
292 self::$_template->append($tpl_var, $value, $merge);
293 }
294
295 /**
296 * Returns an array containing template variables
297 *
298 * @param string $name
299 * @param string $type
300 * @return array
301 */
302 function get_template_vars($name=null) {
303 return self::$_template->get_template_vars($name);
304 }
305
306 /**
307 * function to destroy all the session state of this page.
308 *
309 * @access public
310 *
311 * @return void
312 */
313 function reset() {
314 self::$_session->resetScope($this->_name);
315 }
316
317 /**
318 * Use the form name to create the tpl file name
319 *
320 * @return string
321 * @access public
322 */
323 function getTemplateFileName() {
324 return str_replace('_',
325 DIRECTORY_SEPARATOR,
326 CRM_Utils_System::getClassName($this)
327 ) . '.tpl';
328 }
329
330 /**
331 * A wrapper for getTemplateFileName that includes calling the hook to
332 * prevent us from having to copy & paste the logic of calling the hook
333 */
334 function getHookedTemplateFileName() {
335 $pageTemplateFile = $this->getTemplateFileName();
336 CRM_Utils_Hook::alterTemplateFile(get_class($this), $this, 'page', $pageTemplateFile);
337 return $pageTemplateFile;
338 }
339
340 /**
341 * Default extra tpl file basically just replaces .tpl with .extra.tpl
342 * i.e. we dont override
343 *
344 * @return string
345 * @access public
346 */
347 function overrideExtraTemplateFileName() {
348 return NULL;
349 }
350
351 /**
352 * setter for embedded
353 *
354 * @param boolean $embedded
355 *
356 * @return void
357 * @access public
358 */
359 function setEmbedded($embedded) {
360 $this->_embedded = $embedded;
361 }
362
363 /**
364 * getter for embedded
365 *
366 * @return boolean return the embedded value
367 * @access public
368 */
369 function getEmbedded() {
370 return $this->_embedded;
371 }
372
373 /**
374 * setter for print
375 *
376 * @param boolean $print
377 *
378 * @return void
379 * @access public
380 */
381 function setPrint($print) {
382 $this->_print = $print;
383 }
384
385 /**
386 * getter for print
387 *
388 * @return boolean return the print value
389 * @access public
390 */
391 function getPrint() {
392 return $this->_print;
393 }
394
395 static function &getTemplate() {
396 return self::$_template;
397 }
398
399 function getVar($name) {
400 return isset($this->$name) ? $this->$name : NULL;
401 }
402
403 function setVar($name, $value) {
404 $this->$name = $value;
405 }
406 }
407