Merge remote-tracking branch 'upstream/4.6' into 4.6-master-2015-09-05-17-42-45
[civicrm-core.git] / CRM / Core / Page.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
32 */
33
34 /**
35 * A Page is basically data in a nice pretty format.
36 *
37 * Pages should not have any form actions / elements in them. If they
38 * do, make sure you use CRM_Core_Form and the related structures. You can
39 * embed simple forms in Page and do your own form handling.
40 *
41 */
42 class CRM_Core_Page {
43
44 /**
45 * The name of the page (auto generated from class name)
46 *
47 * @var string
48 */
49 protected $_name;
50
51 /**
52 * The title associated with this page.
53 *
54 * @var object
55 */
56 protected $_title;
57
58 /**
59 * A page can have multiple modes. (i.e. displays
60 * a different set of data based on the input
61 * @var int
62 */
63 protected $_mode;
64
65 /**
66 * Is this object being embedded in another object. If
67 * so the display routine needs to not do any work. (The
68 * parent object takes care of the display)
69 *
70 * @var boolean
71 */
72 protected $_embedded = FALSE;
73
74 /**
75 * Are we in print mode? if so we need to modify the display
76 * functionality to do a minimal display :)
77 *
78 * @var boolean
79 */
80 protected $_print = FALSE;
81
82 /**
83 * Cache the smarty template for efficiency reasons
84 *
85 * @var CRM_Core_Smarty
86 */
87 static protected $_template;
88
89 /**
90 * Cache the session for efficiency reasons
91 *
92 * @var CRM_Core_Session
93 */
94 static protected $_session;
95
96 /**
97 * What to return to the client if in ajax mode (snippet=json)
98 *
99 * @var array
100 */
101 public $ajaxResponse = array();
102
103 /**
104 * Url path used to reach this page
105 *
106 * @var array
107 */
108 public $urlPath = array();
109
110 /**
111 * Should crm.livePage.js be added to the page?
112 * @var bool
113 */
114 public $useLivePageJS;
115
116 /**
117 * Class constructor.
118 *
119 * @param string $title
120 * Title of the page.
121 * @param int $mode
122 * Mode of the page.
123 *
124 * @return CRM_Core_Page
125 */
126 public function __construct($title = NULL, $mode = NULL) {
127 $this->_name = CRM_Utils_System::getClassName($this);
128 $this->_title = $title;
129 $this->_mode = $mode;
130
131 // let the constructor initialize this, should happen only once
132 if (!isset(self::$_template)) {
133 self::$_template = CRM_Core_Smarty::singleton();
134 self::$_session = CRM_Core_Session::singleton();
135 }
136
137 // FIXME - why are we messing with 'snippet'? Why not just pass it directly into $this->_print?
138 if (!empty($_REQUEST['snippet'])) {
139 if ($_REQUEST['snippet'] == CRM_Core_Smarty::PRINT_PDF) {
140 $this->_print = CRM_Core_Smarty::PRINT_PDF;
141 }
142 // FIXME - why does this number not match the constant?
143 elseif ($_REQUEST['snippet'] == 5) {
144 $this->_print = CRM_Core_Smarty::PRINT_NOFORM;
145 }
146 // Support 'json' as well as legacy value '6'
147 elseif (in_array($_REQUEST['snippet'], array(CRM_Core_Smarty::PRINT_JSON, 6))) {
148 $this->_print = CRM_Core_Smarty::PRINT_JSON;
149 }
150 else {
151 $this->_print = CRM_Core_Smarty::PRINT_SNIPPET;
152 }
153 }
154
155 // if the request has a reset value, initialize the controller session
156 if (!empty($_REQUEST['reset'])) {
157 $this->reset();
158 }
159 }
160
161 /**
162 * This function takes care of all the things common to all pages.
163 *
164 * This typically involves assigning the appropriate smarty variables :)
165 */
166 public function run() {
167 if ($this->_embedded) {
168 return NULL;
169 }
170
171 self::$_template->assign('mode', $this->_mode);
172
173 $pageTemplateFile = $this->getHookedTemplateFileName();
174 self::$_template->assign('tplFile', $pageTemplateFile);
175
176 // invoke the pagRun hook, CRM-3906
177 CRM_Utils_Hook::pageRun($this);
178
179 if ($this->_print) {
180 if (in_array($this->_print, array(
181 CRM_Core_Smarty::PRINT_SNIPPET,
182 CRM_Core_Smarty::PRINT_PDF,
183 CRM_Core_Smarty::PRINT_NOFORM,
184 CRM_Core_Smarty::PRINT_JSON,
185 ))) {
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 // Intermittent alert to admins
218 CRM_Utils_Check::singleton()->showPeriodicAlerts();
219
220 if ($this->useLivePageJS &&
221 CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'ajaxPopupsEnabled', NULL, TRUE)
222 ) {
223 CRM_Core_Resources::singleton()->addScriptFile('civicrm', 'js/crm.livePage.js', 1, 'html-header');
224 }
225
226 $content = self::$_template->fetch('CRM/common/' . strtolower($config->userFramework) . '.tpl');
227
228 // Render page header
229 if (!defined('CIVICRM_UF_HEAD') && $region = CRM_Core_Region::instance('html-header', FALSE)) {
230 CRM_Utils_System::addHTMLHead($region->render(''));
231 }
232 CRM_Utils_System::appendTPLFile($pageTemplateFile, $content);
233
234 //its time to call the hook.
235 CRM_Utils_Hook::alterContent($content, 'page', $pageTemplateFile, $this);
236
237 echo CRM_Utils_System::theme($content, $this->_print);
238 }
239
240 /**
241 * Store the variable with the value in the form scope.
242 *
243 * @param string|array $name name of the variable or an assoc array of name/value pairs
244 * @param mixed $value
245 * Value of the variable if string.
246 */
247 public function set($name, $value = NULL) {
248 self::$_session->set($name, $value, $this->_name);
249 }
250
251 /**
252 * Get the variable from the form scope.
253 *
254 * @param string $name name of the variable
255 *
256 * @return mixed
257 */
258 public function get($name) {
259 return self::$_session->get($name, $this->_name);
260 }
261
262 /**
263 * Assign value to name in template.
264 *
265 * @param string $var
266 * @param mixed $value
267 * Value of variable.
268 */
269 public function assign($var, $value = NULL) {
270 self::$_template->assign($var, $value);
271 }
272
273 /**
274 * Assign value to name in template by reference.
275 *
276 * @param string $var
277 * @param mixed $value
278 * (reference) value of variable.
279 */
280 public 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
289 * The value to append.
290 * @param bool $merge
291 */
292 public function append($tpl_var, $value = NULL, $merge = FALSE) {
293 self::$_template->append($tpl_var, $value, $merge);
294 }
295
296 /**
297 * Returns an array containing template variables.
298 *
299 * @param string $name
300 *
301 * @return array
302 */
303 public function get_template_vars($name = NULL) {
304 return self::$_template->get_template_vars($name);
305 }
306
307 /**
308 * Destroy all the session state of this page.
309 */
310 public function reset() {
311 self::$_session->resetScope($this->_name);
312 }
313
314 /**
315 * Use the form name to create the tpl file name.
316 *
317 * @return string
318 */
319 public function getTemplateFileName() {
320 return strtr(
321 CRM_Utils_System::getClassName($this),
322 array(
323 '_' => DIRECTORY_SEPARATOR,
324 '\\' => DIRECTORY_SEPARATOR,
325 )
326 ) . '.tpl';
327 }
328
329 /**
330 * A wrapper for getTemplateFileName that includes calling the hook to
331 * prevent us from having to copy & paste the logic of calling the hook
332 */
333 public function getHookedTemplateFileName() {
334 $pageTemplateFile = $this->getTemplateFileName();
335 CRM_Utils_Hook::alterTemplateFile(get_class($this), $this, 'page', $pageTemplateFile);
336 return $pageTemplateFile;
337 }
338
339 /**
340 * Default extra tpl file basically just replaces .tpl with .extra.tpl
341 * i.e. we dont override
342 *
343 * @return string
344 */
345 public function overrideExtraTemplateFileName() {
346 return NULL;
347 }
348
349 /**
350 * Setter for embedded.
351 *
352 * @param bool $embedded
353 */
354 public function setEmbedded($embedded) {
355 $this->_embedded = $embedded;
356 }
357
358 /**
359 * Getter for embedded.
360 *
361 * @return bool
362 * return the embedded value
363 */
364 public function getEmbedded() {
365 return $this->_embedded;
366 }
367
368 /**
369 * Setter for print.
370 *
371 * @param bool $print
372 */
373 public function setPrint($print) {
374 $this->_print = $print;
375 }
376
377 /**
378 * Getter for print.
379 *
380 * @return bool
381 * return the print value
382 */
383 public function getPrint() {
384 return $this->_print;
385 }
386
387 /**
388 * @return CRM_Core_Smarty
389 */
390 public static function &getTemplate() {
391 return self::$_template;
392 }
393
394 /**
395 * @param string $name
396 *
397 * @return null
398 */
399 public function getVar($name) {
400 return isset($this->$name) ? $this->$name : NULL;
401 }
402
403 /**
404 * @param string $name
405 * @param $value
406 */
407 public function setVar($name, $value) {
408 $this->$name = $value;
409 }
410
411 }