Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
6a488035 TO |
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 | |
6a488035 TO |
48 | */ |
49 | protected $_name; | |
50 | ||
51 | /** | |
fe482240 | 52 | * The title associated with this page. |
6a488035 TO |
53 | * |
54 | * @var object | |
6a488035 TO |
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 | |
6a488035 TO |
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 | * | |
b67daa72 | 70 | * @var bool |
6a488035 TO |
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 | * | |
b67daa72 | 78 | * @var bool |
6a488035 TO |
79 | */ |
80 | protected $_print = FALSE; | |
81 | ||
82 | /** | |
100fef9d | 83 | * Cache the smarty template for efficiency reasons |
6a488035 TO |
84 | * |
85 | * @var CRM_Core_Smarty | |
6a488035 TO |
86 | */ |
87 | static protected $_template; | |
88 | ||
89 | /** | |
100fef9d | 90 | * Cache the session for efficiency reasons |
6a488035 TO |
91 | * |
92 | * @var CRM_Core_Session | |
6a488035 TO |
93 | */ |
94 | static protected $_session; | |
95 | ||
fc05b8da CW |
96 | /** |
97 | * What to return to the client if in ajax mode (snippet=json) | |
98 | * | |
99 | * @var array | |
100 | */ | |
be2fb01f | 101 | public $ajaxResponse = []; |
fc05b8da | 102 | |
7d93bcc4 CW |
103 | /** |
104 | * Url path used to reach this page | |
105 | * | |
106 | * @var array | |
107 | */ | |
be2fb01f | 108 | public $urlPath = []; |
7d93bcc4 | 109 | |
96f50de2 CW |
110 | /** |
111 | * Should crm.livePage.js be added to the page? | |
112 | * @var bool | |
113 | */ | |
114 | public $useLivePageJS; | |
115 | ||
6a488035 | 116 | /** |
fe482240 | 117 | * Class constructor. |
6a488035 | 118 | * |
6a0b768e TO |
119 | * @param string $title |
120 | * Title of the page. | |
121 | * @param int $mode | |
122 | * Mode of the page. | |
6a488035 TO |
123 | * |
124 | * @return CRM_Core_Page | |
125 | */ | |
00be9182 | 126 | public function __construct($title = NULL, $mode = NULL) { |
353ffa53 | 127 | $this->_name = CRM_Utils_System::getClassName($this); |
6a488035 | 128 | $this->_title = $title; |
353ffa53 | 129 | $this->_mode = $mode; |
6a488035 TO |
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 | ||
0e017a41 CW |
137 | // FIXME - why are we messing with 'snippet'? Why not just pass it directly into $this->_print? |
138 | if (!empty($_REQUEST['snippet'])) { | |
03a7ec8f | 139 | if ($_REQUEST['snippet'] == CRM_Core_Smarty::PRINT_PDF) { |
6a488035 TO |
140 | $this->_print = CRM_Core_Smarty::PRINT_PDF; |
141 | } | |
03a7ec8f | 142 | // FIXME - why does this number not match the constant? |
0e017a41 | 143 | elseif ($_REQUEST['snippet'] == 5) { |
6a488035 TO |
144 | $this->_print = CRM_Core_Smarty::PRINT_NOFORM; |
145 | } | |
fc05b8da | 146 | // Support 'json' as well as legacy value '6' |
be2fb01f | 147 | elseif (in_array($_REQUEST['snippet'], [CRM_Core_Smarty::PRINT_JSON, 6])) { |
0e017a41 CW |
148 | $this->_print = CRM_Core_Smarty::PRINT_JSON; |
149 | } | |
6a488035 TO |
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 | |
a7488080 | 156 | if (!empty($_REQUEST['reset'])) { |
6a488035 TO |
157 | $this->reset(); |
158 | } | |
159 | } | |
160 | ||
161 | /** | |
8eedd10a | 162 | * This function takes care of all the things common to all pages. |
6a488035 | 163 | * |
8eedd10a | 164 | * This typically involves assigning the appropriate smarty variables :) |
6a488035 | 165 | */ |
00be9182 | 166 | public function run() { |
6a488035 | 167 | if ($this->_embedded) { |
8d7a9d07 | 168 | return NULL; |
6a488035 TO |
169 | } |
170 | ||
171 | self::$_template->assign('mode', $this->_mode); | |
172 | ||
8aac22c8 | 173 | $pageTemplateFile = $this->getHookedTemplateFileName(); |
6a488035 TO |
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) { | |
be2fb01f | 180 | if (in_array($this->_print, [ |
353ffa53 TO |
181 | CRM_Core_Smarty::PRINT_SNIPPET, |
182 | CRM_Core_Smarty::PRINT_PDF, | |
183 | CRM_Core_Smarty::PRINT_NOFORM, | |
8d7a9d07 | 184 | CRM_Core_Smarty::PRINT_JSON, |
be2fb01f | 185 | ])) { |
6a488035 TO |
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, | |
be2fb01f | 202 | ['paper_size' => 'a3', 'orientation' => 'landscape'] |
6a488035 TO |
203 | ); |
204 | } | |
0e017a41 | 205 | elseif ($this->_print == CRM_Core_Smarty::PRINT_JSON) { |
fc05b8da CW |
206 | $this->ajaxResponse['content'] = $content; |
207 | CRM_Core_Page_AJAX::returnJsonResponse($this->ajaxResponse); | |
0e017a41 | 208 | } |
6a488035 TO |
209 | else { |
210 | echo $content; | |
211 | } | |
212 | CRM_Utils_System::civiExit(); | |
213 | } | |
214 | ||
215 | $config = CRM_Core_Config::singleton(); | |
216 | ||
a5dfa653 MWMC |
217 | // @fixme this is probably the wrong place for this. It is required by jsortable.tpl which is inherited from many page templates. |
218 | // So we have to add it here to deprecate $config->defaultCurrencySymbol | |
219 | $this->assign('defaultCurrencySymbol', CRM_Core_BAO_Country::defaultCurrencySymbol()); | |
220 | ||
5c33ad28 | 221 | // Intermittent alert to admins |
c90a093a | 222 | CRM_Utils_Check::singleton()->showPeriodicAlerts(); |
8ef12e64 | 223 | |
84fb7424 | 224 | if ($this->useLivePageJS && Civi::settings()->get('ajaxPopupsEnabled')) { |
96ed17aa | 225 | CRM_Core_Resources::singleton()->addScriptFile('civicrm', 'js/crm.livePage.js', 1, 'html-header'); |
6a488035 TO |
226 | } |
227 | ||
228 | $content = self::$_template->fetch('CRM/common/' . strtolower($config->userFramework) . '.tpl'); | |
229 | ||
230 | // Render page header | |
9dc21423 | 231 | if (!defined('CIVICRM_UF_HEAD') && $region = CRM_Core_Region::instance('html-header', FALSE)) { |
6a488035 TO |
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); | |
6a488035 TO |
240 | } |
241 | ||
242 | /** | |
fe482240 | 243 | * Store the variable with the value in the form scope. |
6a488035 | 244 | * |
6a0b768e TO |
245 | * @param string|array $name name of the variable or an assoc array of name/value pairs |
246 | * @param mixed $value | |
247 | * Value of the variable if string. | |
6a488035 | 248 | */ |
00be9182 | 249 | public function set($name, $value = NULL) { |
6a488035 TO |
250 | self::$_session->set($name, $value, $this->_name); |
251 | } | |
252 | ||
253 | /** | |
fe482240 | 254 | * Get the variable from the form scope. |
6a488035 | 255 | * |
8d7a9d07 | 256 | * @param string $name name of the variable |
6a488035 TO |
257 | * |
258 | * @return mixed | |
6a488035 | 259 | */ |
00be9182 | 260 | public function get($name) { |
6a488035 TO |
261 | return self::$_session->get($name, $this->_name); |
262 | } | |
263 | ||
264 | /** | |
fe482240 | 265 | * Assign value to name in template. |
6a488035 | 266 | * |
c490a46a | 267 | * @param string $var |
6a0b768e | 268 | * @param mixed $value |
8eedd10a | 269 | * Value of variable. |
6a488035 | 270 | */ |
00be9182 | 271 | public function assign($var, $value = NULL) { |
6a488035 TO |
272 | self::$_template->assign($var, $value); |
273 | } | |
274 | ||
275 | /** | |
fe482240 | 276 | * Assign value to name in template by reference. |
6a488035 | 277 | * |
c490a46a | 278 | * @param string $var |
6a0b768e | 279 | * @param mixed $value |
8eedd10a | 280 | * (reference) value of variable. |
6a488035 | 281 | */ |
00be9182 | 282 | public function assign_by_ref($var, &$value) { |
6a488035 TO |
283 | self::$_template->assign_by_ref($var, $value); |
284 | } | |
285 | ||
4a9538ac | 286 | /** |
fe482240 | 287 | * Appends values to template variables. |
4a9538ac CW |
288 | * |
289 | * @param array|string $tpl_var the template variable name(s) | |
6a0b768e TO |
290 | * @param mixed $value |
291 | * The value to append. | |
4a9538ac CW |
292 | * @param bool $merge |
293 | */ | |
2aa397bc | 294 | public function append($tpl_var, $value = NULL, $merge = FALSE) { |
4a9538ac CW |
295 | self::$_template->append($tpl_var, $value, $merge); |
296 | } | |
297 | ||
298 | /** | |
fe482240 | 299 | * Returns an array containing template variables. |
4a9538ac CW |
300 | * |
301 | * @param string $name | |
fd31fa4c | 302 | * |
4a9538ac CW |
303 | * @return array |
304 | */ | |
2aa397bc | 305 | public function get_template_vars($name = NULL) { |
4a9538ac CW |
306 | return self::$_template->get_template_vars($name); |
307 | } | |
308 | ||
6a488035 | 309 | /** |
100fef9d | 310 | * Destroy all the session state of this page. |
6a488035 | 311 | */ |
00be9182 | 312 | public function reset() { |
6a488035 TO |
313 | self::$_session->resetScope($this->_name); |
314 | } | |
315 | ||
316 | /** | |
fe482240 | 317 | * Use the form name to create the tpl file name. |
6a488035 TO |
318 | * |
319 | * @return string | |
6a488035 | 320 | */ |
00be9182 | 321 | public function getTemplateFileName() { |
9b591d79 TO |
322 | return strtr( |
323 | CRM_Utils_System::getClassName($this), | |
be2fb01f | 324 | [ |
9b591d79 TO |
325 | '_' => DIRECTORY_SEPARATOR, |
326 | '\\' => DIRECTORY_SEPARATOR, | |
be2fb01f | 327 | ] |
6a488035 TO |
328 | ) . '.tpl'; |
329 | } | |
330 | ||
8aac22c8 | 331 | /** |
332 | * A wrapper for getTemplateFileName that includes calling the hook to | |
333 | * prevent us from having to copy & paste the logic of calling the hook | |
334 | */ | |
00be9182 | 335 | public function getHookedTemplateFileName() { |
8aac22c8 | 336 | $pageTemplateFile = $this->getTemplateFileName(); |
337 | CRM_Utils_Hook::alterTemplateFile(get_class($this), $this, 'page', $pageTemplateFile); | |
338 | return $pageTemplateFile; | |
339 | } | |
340 | ||
6a488035 TO |
341 | /** |
342 | * Default extra tpl file basically just replaces .tpl with .extra.tpl | |
343 | * i.e. we dont override | |
344 | * | |
345 | * @return string | |
6a488035 | 346 | */ |
00be9182 | 347 | public function overrideExtraTemplateFileName() { |
6a488035 TO |
348 | return NULL; |
349 | } | |
350 | ||
351 | /** | |
fe482240 | 352 | * Setter for embedded. |
6a488035 | 353 | * |
6a0b768e | 354 | * @param bool $embedded |
6a488035 | 355 | */ |
00be9182 | 356 | public function setEmbedded($embedded) { |
6a488035 TO |
357 | $this->_embedded = $embedded; |
358 | } | |
359 | ||
360 | /** | |
fe482240 | 361 | * Getter for embedded. |
6a488035 | 362 | * |
8d7a9d07 | 363 | * @return bool |
a6c01b45 | 364 | * return the embedded value |
6a488035 | 365 | */ |
00be9182 | 366 | public function getEmbedded() { |
6a488035 TO |
367 | return $this->_embedded; |
368 | } | |
369 | ||
370 | /** | |
fe482240 | 371 | * Setter for print. |
6a488035 | 372 | * |
6a0b768e | 373 | * @param bool $print |
6a488035 | 374 | */ |
00be9182 | 375 | public function setPrint($print) { |
6a488035 TO |
376 | $this->_print = $print; |
377 | } | |
378 | ||
379 | /** | |
fe482240 | 380 | * Getter for print. |
6a488035 | 381 | * |
8d7a9d07 | 382 | * @return bool |
a6c01b45 | 383 | * return the print value |
6a488035 | 384 | */ |
00be9182 | 385 | public function getPrint() { |
6a488035 TO |
386 | return $this->_print; |
387 | } | |
388 | ||
a0ee3941 EM |
389 | /** |
390 | * @return CRM_Core_Smarty | |
391 | */ | |
00be9182 | 392 | public static function &getTemplate() { |
6a488035 TO |
393 | return self::$_template; |
394 | } | |
395 | ||
a0ee3941 | 396 | /** |
100fef9d | 397 | * @param string $name |
a0ee3941 EM |
398 | * |
399 | * @return null | |
400 | */ | |
00be9182 | 401 | public function getVar($name) { |
6a488035 TO |
402 | return isset($this->$name) ? $this->$name : NULL; |
403 | } | |
404 | ||
a0ee3941 | 405 | /** |
100fef9d | 406 | * @param string $name |
a0ee3941 EM |
407 | * @param $value |
408 | */ | |
00be9182 | 409 | public function setVar($name, $value) { |
6a488035 TO |
410 | $this->$name = $value; |
411 | } | |
96025800 | 412 | |
ed0ca248 | 413 | /** |
414 | * Assign metadata about fields to the template. | |
415 | * | |
416 | * In order to allow the template to format fields we assign information about them to the template. | |
417 | * | |
418 | * At this stage only date field metadata is assigned as that is the only use-case in play and | |
419 | * we don't want to assign a lot of unneeded data. | |
420 | * | |
421 | * @param string $entity | |
422 | * The entity being queried. | |
423 | * | |
424 | * @throws \CiviCRM_API3_Exception | |
425 | */ | |
426 | protected function assignFieldMetadataToTemplate($entity) { | |
be2fb01f CW |
427 | $fields = civicrm_api3($entity, 'getfields', ['action' => 'get']); |
428 | $dateFields = []; | |
ed0ca248 | 429 | foreach ($fields['values'] as $fieldName => $fieldMetaData) { |
430 | if (isset($fieldMetaData['html']) && CRM_Utils_Array::value('type', $fieldMetaData['html']) == 'Select Date') { | |
431 | $dateFields[$fieldName] = CRM_Utils_Date::addDateMetadataToField($fieldMetaData, $fieldMetaData); | |
432 | } | |
433 | } | |
434 | $this->assign('fields', $dateFields); | |
435 | } | |
436 | ||
6a488035 | 437 | } |