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