Merge pull request #16178 from seamuslee001/regen_dao
[civicrm-core.git] / CRM / Core / Page.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * A Page is basically data in a nice pretty format.
20 *
21 * Pages should not have any form actions / elements in them. If they
22 * do, make sure you use CRM_Core_Form and the related structures. You can
23 * embed simple forms in Page and do your own form handling.
24 *
25 */
26 class CRM_Core_Page {
27
28 /**
29 * The name of the page (auto generated from class name)
30 *
31 * @var string
32 */
33 protected $_name;
34
35 /**
36 * The title associated with this page.
37 *
38 * @var object
39 */
40 protected $_title;
41
42 /**
43 * A page can have multiple modes. (i.e. displays
44 * a different set of data based on the input
45 * @var int
46 */
47 protected $_mode;
48
49 /**
50 * Is this object being embedded in another object. If
51 * so the display routine needs to not do any work. (The
52 * parent object takes care of the display)
53 *
54 * @var bool
55 */
56 protected $_embedded = FALSE;
57
58 /**
59 * Are we in print mode? if so we need to modify the display
60 * functionality to do a minimal display :)
61 *
62 * @var bool
63 */
64 protected $_print = FALSE;
65
66 /**
67 * Cache the smarty template for efficiency reasons
68 *
69 * @var CRM_Core_Smarty
70 */
71 static protected $_template;
72
73 /**
74 * Cache the session for efficiency reasons
75 *
76 * @var CRM_Core_Session
77 */
78 static protected $_session;
79
80 /**
81 * What to return to the client if in ajax mode (snippet=json)
82 *
83 * @var array
84 */
85 public $ajaxResponse = [];
86
87 /**
88 * Url path used to reach this page
89 *
90 * @var array
91 */
92 public $urlPath = [];
93
94 /**
95 * Should crm.livePage.js be added to the page?
96 * @var bool
97 */
98 public $useLivePageJS;
99
100 /**
101 * Class constructor.
102 *
103 * @param string $title
104 * Title of the page.
105 * @param int $mode
106 * Mode of the page.
107 *
108 * @return CRM_Core_Page
109 */
110 public function __construct($title = NULL, $mode = NULL) {
111 $this->_name = CRM_Utils_System::getClassName($this);
112 $this->_title = $title;
113 $this->_mode = $mode;
114
115 // let the constructor initialize this, should happen only once
116 if (!isset(self::$_template)) {
117 self::$_template = CRM_Core_Smarty::singleton();
118 self::$_session = CRM_Core_Session::singleton();
119 }
120
121 // FIXME - why are we messing with 'snippet'? Why not just pass it directly into $this->_print?
122 if (!empty($_REQUEST['snippet'])) {
123 if ($_REQUEST['snippet'] == CRM_Core_Smarty::PRINT_PDF) {
124 $this->_print = CRM_Core_Smarty::PRINT_PDF;
125 }
126 // FIXME - why does this number not match the constant?
127 elseif ($_REQUEST['snippet'] == 5) {
128 $this->_print = CRM_Core_Smarty::PRINT_NOFORM;
129 }
130 // Support 'json' as well as legacy value '6'
131 elseif (in_array($_REQUEST['snippet'], [CRM_Core_Smarty::PRINT_JSON, 6])) {
132 $this->_print = CRM_Core_Smarty::PRINT_JSON;
133 }
134 else {
135 $this->_print = CRM_Core_Smarty::PRINT_SNIPPET;
136 }
137 }
138
139 // if the request has a reset value, initialize the controller session
140 if (!empty($_REQUEST['reset'])) {
141 $this->reset();
142 }
143 }
144
145 /**
146 * This function takes care of all the things common to all pages.
147 *
148 * This typically involves assigning the appropriate smarty variables :)
149 */
150 public function run() {
151 if ($this->_embedded) {
152 return NULL;
153 }
154
155 self::$_template->assign('mode', $this->_mode);
156
157 $pageTemplateFile = $this->getHookedTemplateFileName();
158 self::$_template->assign('tplFile', $pageTemplateFile);
159
160 // invoke the pagRun hook, CRM-3906
161 CRM_Utils_Hook::pageRun($this);
162
163 if ($this->_print) {
164 if (in_array($this->_print, [
165 CRM_Core_Smarty::PRINT_SNIPPET,
166 CRM_Core_Smarty::PRINT_PDF,
167 CRM_Core_Smarty::PRINT_NOFORM,
168 CRM_Core_Smarty::PRINT_JSON,
169 ])) {
170 $content = self::$_template->fetch('CRM/common/snippet.tpl');
171 }
172 else {
173 $content = self::$_template->fetch('CRM/common/print.tpl');
174 }
175
176 CRM_Utils_System::appendTPLFile($pageTemplateFile,
177 $content,
178 $this->overrideExtraTemplateFileName()
179 );
180
181 //its time to call the hook.
182 CRM_Utils_Hook::alterContent($content, 'page', $pageTemplateFile, $this);
183
184 if ($this->_print == CRM_Core_Smarty::PRINT_PDF) {
185 CRM_Utils_PDF_Utils::html2pdf($content, "{$this->_name}.pdf", FALSE,
186 ['paper_size' => 'a3', 'orientation' => 'landscape']
187 );
188 }
189 elseif ($this->_print == CRM_Core_Smarty::PRINT_JSON) {
190 $this->ajaxResponse['content'] = $content;
191 CRM_Core_Page_AJAX::returnJsonResponse($this->ajaxResponse);
192 }
193 else {
194 echo $content;
195 }
196 CRM_Utils_System::civiExit();
197 }
198
199 $config = CRM_Core_Config::singleton();
200
201 // @fixme this is probably the wrong place for this. It is required by jsortable.tpl which is inherited from many page templates.
202 // So we have to add it here to deprecate $config->defaultCurrencySymbol
203 $this->assign('defaultCurrencySymbol', CRM_Core_BAO_Country::defaultCurrencySymbol());
204
205 // Intermittent alert to admins
206 CRM_Utils_Check::singleton()->showPeriodicAlerts();
207
208 if ($this->useLivePageJS && Civi::settings()->get('ajaxPopupsEnabled')) {
209 CRM_Core_Resources::singleton()->addScriptFile('civicrm', 'js/crm.livePage.js', 1, 'html-header');
210 }
211
212 $content = self::$_template->fetch('CRM/common/' . strtolower($config->userFramework) . '.tpl');
213
214 // Render page header
215 if (!defined('CIVICRM_UF_HEAD') && $region = CRM_Core_Region::instance('html-header', FALSE)) {
216 CRM_Utils_System::addHTMLHead($region->render(''));
217 }
218 CRM_Utils_System::appendTPLFile($pageTemplateFile, $content);
219
220 //its time to call the hook.
221 CRM_Utils_Hook::alterContent($content, 'page', $pageTemplateFile, $this);
222
223 echo CRM_Utils_System::theme($content, $this->_print);
224 }
225
226 /**
227 * Store the variable with the value in the form scope.
228 *
229 * @param string|array $name name of the variable or an assoc array of name/value pairs
230 * @param mixed $value
231 * Value of the variable if string.
232 */
233 public function set($name, $value = NULL) {
234 self::$_session->set($name, $value, $this->_name);
235 }
236
237 /**
238 * Get the variable from the form scope.
239 *
240 * @param string $name name of the variable
241 *
242 * @return mixed
243 */
244 public function get($name) {
245 return self::$_session->get($name, $this->_name);
246 }
247
248 /**
249 * Assign value to name in template.
250 *
251 * @param string $var
252 * @param mixed $value
253 * Value of variable.
254 */
255 public function assign($var, $value = NULL) {
256 self::$_template->assign($var, $value);
257 }
258
259 /**
260 * Assign value to name in template by reference.
261 *
262 * @param string $var
263 * @param mixed $value
264 * (reference) value of variable.
265 */
266 public function assign_by_ref($var, &$value) {
267 self::$_template->assign_by_ref($var, $value);
268 }
269
270 /**
271 * Appends values to template variables.
272 *
273 * @param array|string $tpl_var the template variable name(s)
274 * @param mixed $value
275 * The value to append.
276 * @param bool $merge
277 */
278 public function append($tpl_var, $value = NULL, $merge = FALSE) {
279 self::$_template->append($tpl_var, $value, $merge);
280 }
281
282 /**
283 * Returns an array containing template variables.
284 *
285 * @param string $name
286 *
287 * @return array
288 */
289 public function get_template_vars($name = NULL) {
290 return self::$_template->get_template_vars($name);
291 }
292
293 /**
294 * Destroy all the session state of this page.
295 */
296 public function reset() {
297 self::$_session->resetScope($this->_name);
298 }
299
300 /**
301 * Use the form name to create the tpl file name.
302 *
303 * @return string
304 */
305 public function getTemplateFileName() {
306 return strtr(
307 CRM_Utils_System::getClassName($this),
308 [
309 '_' => DIRECTORY_SEPARATOR,
310 '\\' => DIRECTORY_SEPARATOR,
311 ]
312 ) . '.tpl';
313 }
314
315 /**
316 * A wrapper for getTemplateFileName that includes calling the hook to
317 * prevent us from having to copy & paste the logic of calling the hook
318 */
319 public function getHookedTemplateFileName() {
320 $pageTemplateFile = $this->getTemplateFileName();
321 CRM_Utils_Hook::alterTemplateFile(get_class($this), $this, 'page', $pageTemplateFile);
322 return $pageTemplateFile;
323 }
324
325 /**
326 * Default extra tpl file basically just replaces .tpl with .extra.tpl
327 * i.e. we dont override
328 *
329 * @return string
330 */
331 public function overrideExtraTemplateFileName() {
332 return NULL;
333 }
334
335 /**
336 * Setter for embedded.
337 *
338 * @param bool $embedded
339 */
340 public function setEmbedded($embedded) {
341 $this->_embedded = $embedded;
342 }
343
344 /**
345 * Getter for embedded.
346 *
347 * @return bool
348 * return the embedded value
349 */
350 public function getEmbedded() {
351 return $this->_embedded;
352 }
353
354 /**
355 * Setter for print.
356 *
357 * @param bool $print
358 */
359 public function setPrint($print) {
360 $this->_print = $print;
361 }
362
363 /**
364 * Getter for print.
365 *
366 * @return bool
367 * return the print value
368 */
369 public function getPrint() {
370 return $this->_print;
371 }
372
373 /**
374 * @return CRM_Core_Smarty
375 */
376 public static function &getTemplate() {
377 return self::$_template;
378 }
379
380 /**
381 * @param string $name
382 *
383 * @return null
384 */
385 public function getVar($name) {
386 return isset($this->$name) ? $this->$name : NULL;
387 }
388
389 /**
390 * @param string $name
391 * @param $value
392 */
393 public function setVar($name, $value) {
394 $this->$name = $value;
395 }
396
397 /**
398 * Assign metadata about fields to the template.
399 *
400 * In order to allow the template to format fields we assign information about them to the template.
401 *
402 * At this stage only date field metadata is assigned as that is the only use-case in play and
403 * we don't want to assign a lot of unneeded data.
404 *
405 * @param string $entity
406 * The entity being queried.
407 *
408 * @throws \CiviCRM_API3_Exception
409 */
410 protected function assignFieldMetadataToTemplate($entity) {
411 $fields = civicrm_api3($entity, 'getfields', ['action' => 'get']);
412 $dateFields = [];
413 foreach ($fields['values'] as $fieldName => $fieldMetaData) {
414 if (isset($fieldMetaData['html']) && CRM_Utils_Array::value('type', $fieldMetaData['html']) == 'Select Date') {
415 $dateFields[$fieldName] = CRM_Utils_Date::addDateMetadataToField($fieldMetaData, $fieldMetaData);
416 }
417 }
418 $this->assign('fields', $dateFields);
419 }
420
421 }