Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
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 | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
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 | ||
fc05b8da CW |
107 | /** |
108 | * What to return to the client if in ajax mode (snippet=json) | |
109 | * | |
110 | * @var array | |
111 | */ | |
112 | public $ajaxResponse = array(); | |
113 | ||
7d93bcc4 CW |
114 | /** |
115 | * Url path used to reach this page | |
116 | * | |
117 | * @var array | |
118 | */ | |
119 | public $urlPath = array(); | |
120 | ||
96f50de2 CW |
121 | /** |
122 | * Should crm.livePage.js be added to the page? | |
123 | * @var bool | |
124 | */ | |
125 | public $useLivePageJS; | |
126 | ||
6a488035 TO |
127 | /** |
128 | * class constructor | |
129 | * | |
130 | * @param string $title title of the page | |
131 | * @param int $mode mode of the page | |
132 | * | |
133 | * @return CRM_Core_Page | |
134 | */ | |
135 | function __construct($title = NULL, $mode = NULL) { | |
136 | $this->_name = CRM_Utils_System::getClassName($this); | |
137 | $this->_title = $title; | |
138 | $this->_mode = $mode; | |
139 | ||
140 | // let the constructor initialize this, should happen only once | |
141 | if (!isset(self::$_template)) { | |
142 | self::$_template = CRM_Core_Smarty::singleton(); | |
143 | self::$_session = CRM_Core_Session::singleton(); | |
144 | } | |
145 | ||
0e017a41 CW |
146 | // FIXME - why are we messing with 'snippet'? Why not just pass it directly into $this->_print? |
147 | if (!empty($_REQUEST['snippet'])) { | |
03a7ec8f | 148 | if ($_REQUEST['snippet'] == CRM_Core_Smarty::PRINT_PDF) { |
6a488035 TO |
149 | $this->_print = CRM_Core_Smarty::PRINT_PDF; |
150 | } | |
03a7ec8f | 151 | // FIXME - why does this number not match the constant? |
0e017a41 | 152 | elseif ($_REQUEST['snippet'] == 5) { |
6a488035 TO |
153 | $this->_print = CRM_Core_Smarty::PRINT_NOFORM; |
154 | } | |
fc05b8da CW |
155 | // Support 'json' as well as legacy value '6' |
156 | elseif (in_array($_REQUEST['snippet'], array(CRM_Core_Smarty::PRINT_JSON, 6))) { | |
0e017a41 CW |
157 | $this->_print = CRM_Core_Smarty::PRINT_JSON; |
158 | } | |
6a488035 TO |
159 | else { |
160 | $this->_print = CRM_Core_Smarty::PRINT_SNIPPET; | |
161 | } | |
162 | } | |
163 | ||
164 | // if the request has a reset value, initialize the controller session | |
a7488080 | 165 | if (!empty($_REQUEST['reset'])) { |
6a488035 TO |
166 | $this->reset(); |
167 | } | |
168 | } | |
169 | ||
170 | /** | |
171 | * This function takes care of all the things common to all | |
172 | * pages. This typically involves assigning the appropriate | |
173 | * smarty variable :) | |
174 | * | |
175 | * @return string The content generated by running this page | |
176 | */ | |
177 | function run() { | |
178 | if ($this->_embedded) { | |
179 | return; | |
180 | } | |
181 | ||
182 | self::$_template->assign('mode', $this->_mode); | |
183 | ||
8aac22c8 | 184 | $pageTemplateFile = $this->getHookedTemplateFileName(); |
6a488035 TO |
185 | self::$_template->assign('tplFile', $pageTemplateFile); |
186 | ||
187 | // invoke the pagRun hook, CRM-3906 | |
188 | CRM_Utils_Hook::pageRun($this); | |
189 | ||
190 | if ($this->_print) { | |
191 | if (in_array( $this->_print, array( CRM_Core_Smarty::PRINT_SNIPPET, | |
0e017a41 | 192 | CRM_Core_Smarty::PRINT_PDF, CRM_Core_Smarty::PRINT_NOFORM, CRM_Core_Smarty::PRINT_JSON ))) { |
6a488035 TO |
193 | $content = self::$_template->fetch('CRM/common/snippet.tpl'); |
194 | } | |
195 | else { | |
196 | $content = self::$_template->fetch('CRM/common/print.tpl'); | |
197 | } | |
198 | ||
199 | CRM_Utils_System::appendTPLFile($pageTemplateFile, | |
200 | $content, | |
201 | $this->overrideExtraTemplateFileName() | |
202 | ); | |
203 | ||
204 | //its time to call the hook. | |
205 | CRM_Utils_Hook::alterContent($content, 'page', $pageTemplateFile, $this); | |
206 | ||
207 | if ($this->_print == CRM_Core_Smarty::PRINT_PDF) { | |
208 | CRM_Utils_PDF_Utils::html2pdf($content, "{$this->_name}.pdf", FALSE, | |
209 | array('paper_size' => 'a3', 'orientation' => 'landscape') | |
210 | ); | |
211 | } | |
0e017a41 | 212 | elseif ($this->_print == CRM_Core_Smarty::PRINT_JSON) { |
fc05b8da CW |
213 | $this->ajaxResponse['content'] = $content; |
214 | CRM_Core_Page_AJAX::returnJsonResponse($this->ajaxResponse); | |
0e017a41 | 215 | } |
6a488035 TO |
216 | else { |
217 | echo $content; | |
218 | } | |
219 | CRM_Utils_System::civiExit(); | |
220 | } | |
221 | ||
222 | $config = CRM_Core_Config::singleton(); | |
223 | ||
96f50de2 CW |
224 | // Version check and intermittent alert to admins |
225 | CRM_Utils_VersionCheck::singleton()->versionAlert(); | |
c90a093a | 226 | CRM_Utils_Check::singleton()->showPeriodicAlerts(); |
8ef12e64 | 227 | |
96f50de2 | 228 | // Debug msg once per hour |
570d99a2 | 229 | if ($config->debug && CRM_Core_Permission::check('administer CiviCRM') && CRM_Core_Session::singleton()->timer('debug_alert', 7200)) { |
96f50de2 CW |
230 | $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'))); |
231 | CRM_Core_Session::setStatus($msg, ts('Debug Mode')); | |
232 | } | |
233 | ||
1e012c2c | 234 | $mailingInfo = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME, 'mailing_backend'); |
235 | ||
ccd9f027 | 236 | // Outbound email msg once every 2 hours if outbound email is redirected or disabled |
1e012c2c | 237 | if (($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_REDIRECT_TO_DB |
ccd9f027 | 238 | || (defined('CIVICRM_MAIL_LOG') && CIVICRM_MAIL_LOG) |
1e012c2c | 239 | || $mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_DISABLED |
240 | || $mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_MOCK) | |
570d99a2 | 241 | && CRM_Core_Permission::check('administer CiviCRM') && CRM_Core_Session::singleton()->timer('outboundEmail_alert', 7200)) |
1e012c2c | 242 | { |
243 | $msg = ts('Warning: Outbound email is disabled in <a href="%1">system settings</a>. Proper settings should be enabled on production servers.', array(1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1'))); | |
244 | CRM_Core_Session::setStatus($msg, ts('Outbound Email Settings')); | |
245 | } | |
246 | ||
96f50de2 | 247 | if ($this->useLivePageJS && |
53f2643c | 248 | CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'ajaxPopupsEnabled', NULL, TRUE)) |
96f50de2 CW |
249 | { |
250 | CRM_Core_Resources::singleton()->addScriptFile('civicrm', 'js/crm.livePage.js'); | |
2e401199 | 251 | $this->assign('includeWysiwygEditor', TRUE); |
6a488035 TO |
252 | } |
253 | ||
254 | $content = self::$_template->fetch('CRM/common/' . strtolower($config->userFramework) . '.tpl'); | |
255 | ||
256 | // Render page header | |
9dc21423 | 257 | if (!defined('CIVICRM_UF_HEAD') && $region = CRM_Core_Region::instance('html-header', FALSE)) { |
6a488035 TO |
258 | CRM_Utils_System::addHTMLHead($region->render('')); |
259 | } | |
260 | CRM_Utils_System::appendTPLFile($pageTemplateFile, $content); | |
261 | ||
262 | //its time to call the hook. | |
263 | CRM_Utils_Hook::alterContent($content, 'page', $pageTemplateFile, $this); | |
264 | ||
265 | echo CRM_Utils_System::theme($content, $this->_print); | |
266 | return; | |
267 | } | |
268 | ||
269 | /** | |
270 | * Store the variable with the value in the form scope | |
271 | * | |
272 | * @param string|array $name name of the variable or an assoc array of name/value pairs | |
273 | * @param mixed $value value of the variable if string | |
274 | * | |
275 | * @access public | |
276 | * | |
277 | * @return void | |
278 | * | |
279 | */ | |
280 | function set($name, $value = NULL) { | |
281 | self::$_session->set($name, $value, $this->_name); | |
282 | } | |
283 | ||
284 | /** | |
285 | * Get the variable from the form scope | |
286 | * | |
287 | * @param string name : name of the variable | |
288 | * | |
289 | * @access public | |
290 | * | |
291 | * @return mixed | |
292 | * | |
293 | */ | |
294 | function get($name) { | |
295 | return self::$_session->get($name, $this->_name); | |
296 | } | |
297 | ||
298 | /** | |
299 | * assign value to name in template | |
300 | * | |
301 | * @param array|string $name name of variable | |
302 | * @param mixed $value value of varaible | |
303 | * | |
304 | * @return void | |
305 | * @access public | |
306 | */ | |
307 | function assign($var, $value = NULL) { | |
308 | self::$_template->assign($var, $value); | |
309 | } | |
310 | ||
311 | /** | |
312 | * assign value to name in template by reference | |
313 | * | |
314 | * @param array|string $name name of variable | |
315 | * @param mixed $value (reference) value of varaible | |
316 | * | |
317 | * @return void | |
318 | * @access public | |
319 | */ | |
320 | function assign_by_ref($var, &$value) { | |
321 | self::$_template->assign_by_ref($var, $value); | |
322 | } | |
323 | ||
4a9538ac CW |
324 | /** |
325 | * appends values to template variables | |
326 | * | |
327 | * @param array|string $tpl_var the template variable name(s) | |
328 | * @param mixed $value the value to append | |
329 | * @param bool $merge | |
330 | */ | |
331 | function append($tpl_var, $value=NULL, $merge=FALSE) { | |
332 | self::$_template->append($tpl_var, $value, $merge); | |
333 | } | |
334 | ||
335 | /** | |
336 | * Returns an array containing template variables | |
337 | * | |
338 | * @param string $name | |
339 | * @param string $type | |
340 | * @return array | |
341 | */ | |
342 | function get_template_vars($name=null) { | |
343 | return self::$_template->get_template_vars($name); | |
344 | } | |
345 | ||
6a488035 TO |
346 | /** |
347 | * function to destroy all the session state of this page. | |
348 | * | |
349 | * @access public | |
350 | * | |
351 | * @return void | |
352 | */ | |
353 | function reset() { | |
354 | self::$_session->resetScope($this->_name); | |
355 | } | |
356 | ||
357 | /** | |
358 | * Use the form name to create the tpl file name | |
359 | * | |
360 | * @return string | |
361 | * @access public | |
362 | */ | |
363 | function getTemplateFileName() { | |
364 | return str_replace('_', | |
365 | DIRECTORY_SEPARATOR, | |
366 | CRM_Utils_System::getClassName($this) | |
367 | ) . '.tpl'; | |
368 | } | |
369 | ||
8aac22c8 | 370 | /** |
371 | * A wrapper for getTemplateFileName that includes calling the hook to | |
372 | * prevent us from having to copy & paste the logic of calling the hook | |
373 | */ | |
374 | function getHookedTemplateFileName() { | |
375 | $pageTemplateFile = $this->getTemplateFileName(); | |
376 | CRM_Utils_Hook::alterTemplateFile(get_class($this), $this, 'page', $pageTemplateFile); | |
377 | return $pageTemplateFile; | |
378 | } | |
379 | ||
6a488035 TO |
380 | /** |
381 | * Default extra tpl file basically just replaces .tpl with .extra.tpl | |
382 | * i.e. we dont override | |
383 | * | |
384 | * @return string | |
385 | * @access public | |
386 | */ | |
387 | function overrideExtraTemplateFileName() { | |
388 | return NULL; | |
389 | } | |
390 | ||
391 | /** | |
392 | * setter for embedded | |
393 | * | |
394 | * @param boolean $embedded | |
395 | * | |
396 | * @return void | |
397 | * @access public | |
398 | */ | |
399 | function setEmbedded($embedded) { | |
400 | $this->_embedded = $embedded; | |
401 | } | |
402 | ||
403 | /** | |
404 | * getter for embedded | |
405 | * | |
406 | * @return boolean return the embedded value | |
407 | * @access public | |
408 | */ | |
409 | function getEmbedded() { | |
410 | return $this->_embedded; | |
411 | } | |
412 | ||
413 | /** | |
414 | * setter for print | |
415 | * | |
416 | * @param boolean $print | |
417 | * | |
418 | * @return void | |
419 | * @access public | |
420 | */ | |
421 | function setPrint($print) { | |
422 | $this->_print = $print; | |
423 | } | |
424 | ||
425 | /** | |
426 | * getter for print | |
427 | * | |
428 | * @return boolean return the print value | |
429 | * @access public | |
430 | */ | |
431 | function getPrint() { | |
432 | return $this->_print; | |
433 | } | |
434 | ||
435 | static function &getTemplate() { | |
436 | return self::$_template; | |
437 | } | |
438 | ||
439 | function getVar($name) { | |
440 | return isset($this->$name) ? $this->$name : NULL; | |
441 | } | |
442 | ||
443 | function setVar($name, $value) { | |
444 | $this->$name = $value; | |
445 | } | |
446 | } | |
447 |