[ Index ] |
PHP Cross Reference of Joomla 4.2.2 documentation |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * Joomla! Content Management System 5 * 6 * @copyright (C) 2006 Open Source Matters, Inc. <https://www.joomla.org> 7 * @license GNU General Public License version 2 or later; see LICENSE.txt 8 */ 9 10 namespace Joomla\CMS\MVC\View; 11 12 use Joomla\CMS\Application\ApplicationHelper; 13 use Joomla\CMS\Event\AbstractEvent; 14 use Joomla\CMS\Factory; 15 use Joomla\CMS\Filesystem\Path; 16 use Joomla\CMS\Language\Text; 17 use Joomla\CMS\Uri\Uri; 18 use Joomla\CMS\User\CurrentUserInterface; 19 use Joomla\CMS\User\CurrentUserTrait; 20 21 // phpcs:disable PSR1.Files.SideEffects 22 \defined('JPATH_PLATFORM') or die; 23 // phpcs:enable PSR1.Files.SideEffects 24 25 /** 26 * Base class for a Joomla Html View 27 * 28 * Class holding methods for displaying presentation data. 29 * 30 * @since 2.5.5 31 */ 32 class HtmlView extends AbstractView implements CurrentUserInterface 33 { 34 use CurrentUserTrait; 35 36 /** 37 * The base path of the view 38 * 39 * @var string 40 * @since 3.0 41 */ 42 protected $_basePath = null; 43 44 /** 45 * Layout name 46 * 47 * @var string 48 * @since 3.0 49 */ 50 protected $_layout = 'default'; 51 52 /** 53 * Layout extension 54 * 55 * @var string 56 * @since 3.0 57 */ 58 protected $_layoutExt = 'php'; 59 60 /** 61 * Layout template 62 * 63 * @var string 64 * @since 3.0 65 */ 66 protected $_layoutTemplate = '_'; 67 68 /** 69 * The set of search directories for resources (templates) 70 * 71 * @var array 72 * @since 3.0 73 */ 74 protected $_path = array('template' => array(), 'helper' => array()); 75 76 /** 77 * The name of the default template source file. 78 * 79 * @var string 80 * @since 3.0 81 */ 82 protected $_template = null; 83 84 /** 85 * The output of the template script. 86 * 87 * @var string 88 * @since 3.0 89 */ 90 protected $_output = null; 91 92 /** 93 * Charset to use in escaping mechanisms; defaults to urf8 (UTF-8) 94 * 95 * @var string 96 * @since 3.0 97 */ 98 protected $_charset = 'UTF-8'; 99 100 /** 101 * Constructor 102 * 103 * @param array $config A named configuration array for object construction. 104 * name: the name (optional) of the view (defaults to the view class name suffix). 105 * charset: the character set to use for display 106 * escape: the name (optional) of the function to use for escaping strings 107 * base_path: the parent path (optional) of the views directory (defaults to the component folder) 108 * template_plath: the path (optional) of the layout directory (defaults to base_path + /views/ + view name 109 * helper_path: the path (optional) of the helper files (defaults to base_path + /helpers/) 110 * layout: the layout (optional) to use to display the view 111 * 112 * @since 3.0 113 */ 114 public function __construct($config = array()) 115 { 116 parent::__construct($config); 117 118 // Set the charset (used by the variable escaping functions) 119 if (\array_key_exists('charset', $config)) { 120 @trigger_error( 121 'Setting a custom charset for escaping is deprecated. Override \JViewLegacy::escape() instead.', 122 E_USER_DEPRECATED 123 ); 124 $this->_charset = $config['charset']; 125 } 126 127 // Set a base path for use by the view 128 if (\array_key_exists('base_path', $config)) { 129 $this->_basePath = $config['base_path']; 130 } else { 131 $this->_basePath = JPATH_COMPONENT; 132 } 133 134 // Set the default template search path 135 if (\array_key_exists('template_path', $config)) { 136 // User-defined dirs 137 $this->_setPath('template', $config['template_path']); 138 } elseif (is_dir($this->_basePath . '/tmpl/' . $this->getName())) { 139 $this->_setPath('template', $this->_basePath . '/tmpl/' . $this->getName()); 140 } elseif (is_dir($this->_basePath . '/View/' . $this->getName() . '/tmpl')) { 141 $this->_setPath('template', $this->_basePath . '/View/' . $this->getName() . '/tmpl'); 142 } elseif (is_dir($this->_basePath . '/view/' . $this->getName() . '/tmpl')) { 143 $this->_setPath('template', $this->_basePath . '/view/' . $this->getName() . '/tmpl'); 144 } elseif (is_dir($this->_basePath . '/views/' . $this->getName() . '/tmpl')) { 145 $this->_setPath('template', $this->_basePath . '/views/' . $this->getName() . '/tmpl'); 146 } else { 147 $this->_setPath('template', $this->_basePath . '/views/' . $this->getName()); 148 } 149 150 // Set the default helper search path 151 if (\array_key_exists('helper_path', $config)) { 152 // User-defined dirs 153 $this->_setPath('helper', $config['helper_path']); 154 } else { 155 $this->_setPath('helper', $this->_basePath . '/helpers'); 156 } 157 158 // Set the layout 159 if (\array_key_exists('layout', $config)) { 160 $this->setLayout($config['layout']); 161 } else { 162 $this->setLayout('default'); 163 } 164 165 $this->baseurl = Uri::base(true); 166 } 167 168 /** 169 * Execute and display a template script. 170 * 171 * @param string $tpl The name of the template file to parse; automatically searches through the template paths. 172 * 173 * @return void 174 * 175 * @throws \Exception 176 * @see \JViewLegacy::loadTemplate() 177 * @since 3.0 178 */ 179 public function display($tpl = null) 180 { 181 $app = Factory::getApplication(); 182 183 if ($this->option) { 184 $component = $this->option; 185 } else { 186 $component = ApplicationHelper::getComponentName(); 187 } 188 189 $context = $component . '.' . $this->getName(); 190 191 $app->getDispatcher()->dispatch( 192 'onBeforeDisplay', 193 AbstractEvent::create( 194 'onBeforeDisplay', 195 [ 196 'eventClass' => 'Joomla\CMS\Event\View\DisplayEvent', 197 'subject' => $this, 198 'extension' => $context 199 ] 200 ) 201 ); 202 203 $result = $this->loadTemplate($tpl); 204 205 $eventResult = $app->getDispatcher()->dispatch( 206 'onAfterDisplay', 207 AbstractEvent::create( 208 'onAfterDisplay', 209 [ 210 'eventClass' => 'Joomla\CMS\Event\View\DisplayEvent', 211 'subject' => $this, 212 'extension' => $context, 213 'source' => $result 214 ] 215 ) 216 ); 217 218 $eventResult->getArgument('used', false); 219 220 echo $result; 221 } 222 223 /** 224 * Escapes a value for output in a view script. 225 * 226 * If escaping mechanism is htmlspecialchars, use 227 * {@link $_charset} setting. 228 * 229 * @param mixed $var The output to escape. 230 * 231 * @return mixed The escaped value. 232 * 233 * @note the ENT_COMPAT flag was replaced by ENT_QUOTES in Joomla 4.0 to also escape single quotes 234 * 235 * @since 3.0 236 */ 237 public function escape($var) 238 { 239 if ($var === null) { 240 return ''; 241 } 242 243 return htmlspecialchars($var, ENT_QUOTES, $this->_charset); 244 } 245 246 /** 247 * Get the layout. 248 * 249 * @return string The layout name 250 * 251 * @since 3.0 252 */ 253 public function getLayout() 254 { 255 return $this->_layout; 256 } 257 258 /** 259 * Get the layout template. 260 * 261 * @return string The layout template name 262 * 263 * @since 3.0 264 */ 265 public function getLayoutTemplate() 266 { 267 return $this->_layoutTemplate; 268 } 269 270 /** 271 * Sets the layout name to use 272 * 273 * @param string $layout The layout name or a string in format <template>:<layout file> 274 * 275 * @return string Previous value. 276 * 277 * @since 3.0 278 */ 279 public function setLayout($layout) 280 { 281 $previous = $this->_layout; 282 283 if (strpos($layout, ':') === false) { 284 $this->_layout = $layout; 285 } else { 286 // Convert parameter to array based on : 287 $temp = explode(':', $layout); 288 $this->_layout = $temp[1]; 289 290 // Set layout template 291 $this->_layoutTemplate = $temp[0]; 292 } 293 294 return $previous; 295 } 296 297 /** 298 * Allows a different extension for the layout files to be used 299 * 300 * @param string $value The extension. 301 * 302 * @return string Previous value 303 * 304 * @since 3.0 305 */ 306 public function setLayoutExt($value) 307 { 308 $previous = $this->_layoutExt; 309 310 if ($value = preg_replace('#[^A-Za-z0-9]#', '', trim($value))) { 311 $this->_layoutExt = $value; 312 } 313 314 return $previous; 315 } 316 317 /** 318 * Adds to the stack of view script paths in LIFO order. 319 * 320 * @param mixed $path A directory path or an array of paths. 321 * 322 * @return void 323 * 324 * @since 3.0 325 */ 326 public function addTemplatePath($path) 327 { 328 $this->_addPath('template', $path); 329 } 330 331 /** 332 * Adds to the stack of helper script paths in LIFO order. 333 * 334 * @param mixed $path A directory path or an array of paths. 335 * 336 * @return void 337 * 338 * @since 3.0 339 */ 340 public function addHelperPath($path) 341 { 342 $this->_addPath('helper', $path); 343 } 344 345 /** 346 * Load a template file -- first look in the templates folder for an override 347 * 348 * @param string $tpl The name of the template source file; automatically searches the template paths and compiles as needed. 349 * 350 * @return string The output of the the template script. 351 * 352 * @since 3.0 353 * @throws \Exception 354 */ 355 public function loadTemplate($tpl = null) 356 { 357 // Clear prior output 358 $this->_output = null; 359 360 $template = Factory::getApplication()->getTemplate(true); 361 $layout = $this->getLayout(); 362 $layoutTemplate = $this->getLayoutTemplate(); 363 364 // Create the template file name based on the layout 365 $file = isset($tpl) ? $layout . '_' . $tpl : $layout; 366 367 // Clean the file name 368 $file = preg_replace('/[^A-Z0-9_\.-]/i', '', $file); 369 $tpl = isset($tpl) ? preg_replace('/[^A-Z0-9_\.-]/i', '', $tpl) : $tpl; 370 371 // Load the language file for the template 372 $lang = Factory::getLanguage(); 373 $lang->load('tpl_' . $template->template, JPATH_BASE) 374 || $lang->load('tpl_' . $template->parent, JPATH_THEMES . '/' . $template->parent) 375 || $lang->load('tpl_' . $template->template, JPATH_THEMES . '/' . $template->template); 376 377 // Change the template folder if alternative layout is in different template 378 if (isset($layoutTemplate) && $layoutTemplate !== '_' && $layoutTemplate != $template->template) { 379 $this->_path['template'] = str_replace( 380 JPATH_THEMES . DIRECTORY_SEPARATOR . $template->template, 381 JPATH_THEMES . DIRECTORY_SEPARATOR . $layoutTemplate, 382 $this->_path['template'] 383 ); 384 } 385 386 // Load the template script 387 $filetofind = $this->_createFileName('template', array('name' => $file)); 388 $this->_template = Path::find($this->_path['template'], $filetofind); 389 390 // If alternate layout can't be found, fall back to default layout 391 if ($this->_template == false) { 392 $filetofind = $this->_createFileName('', array('name' => 'default' . (isset($tpl) ? '_' . $tpl : $tpl))); 393 $this->_template = Path::find($this->_path['template'], $filetofind); 394 } 395 396 if ($this->_template != false) { 397 // Unset so as not to introduce into template scope 398 unset($tpl, $file); 399 400 // Never allow a 'this' property 401 if (isset($this->this)) { 402 unset($this->this); 403 } 404 405 // Start capturing output into a buffer 406 ob_start(); 407 408 // Include the requested template filename in the local scope 409 // (this will execute the view logic). 410 include $this->_template; 411 412 // Done with the requested template; get the buffer and 413 // clear it. 414 $this->_output = ob_get_contents(); 415 ob_end_clean(); 416 417 return $this->_output; 418 } 419 420 throw new \Exception(Text::sprintf('JLIB_APPLICATION_ERROR_LAYOUTFILE_NOT_FOUND', $file), 500); 421 } 422 423 /** 424 * Load a helper file 425 * 426 * @param string $hlp The name of the helper source file automatically searches the helper paths and compiles as needed. 427 * 428 * @return void 429 * 430 * @since 3.0 431 */ 432 public function loadHelper($hlp = null) 433 { 434 // Clean the file name 435 $file = preg_replace('/[^A-Z0-9_\.-]/i', '', $hlp); 436 437 // Load the template script 438 $helper = Path::find($this->_path['helper'], $this->_createFileName('helper', array('name' => $file))); 439 440 if ($helper != false) { 441 // Include the requested template filename in the local scope 442 include_once $helper; 443 } 444 } 445 446 /** 447 * Sets an entire array of search paths for templates or resources. 448 * 449 * @param string $type The type of path to set, typically 'template'. 450 * @param mixed $path The new search path, or an array of search paths. If null or false, resets to the current directory only. 451 * 452 * @return void 453 * 454 * @since 3.0 455 */ 456 protected function _setPath($type, $path) 457 { 458 if ($this->option) { 459 $component = $this->option; 460 } else { 461 $component = ApplicationHelper::getComponentName(); 462 } 463 464 $app = Factory::getApplication(); 465 466 // Clear out the prior search dirs 467 $this->_path[$type] = array(); 468 469 // Actually add the user-specified directories 470 $this->_addPath($type, $path); 471 472 // Get the active template object 473 $template = $app->getTemplate(true); 474 475 // Always add the fallback directories as last resort 476 switch (strtolower($type)) { 477 case 'template': 478 // Set the alternative template search dir 479 if (isset($app)) { 480 if ($component) { 481 $component = preg_replace('/[^A-Z0-9_\.-]/i', '', $component); 482 } 483 484 $name = $this->getName(); 485 486 if (!empty($template->parent)) { 487 // Parent template's overrides 488 $this->_addPath('template', JPATH_THEMES . '/' . $template->parent . '/html/' . $component . '/' . $name); 489 490 // Child template's overrides 491 $this->_addPath('template', JPATH_THEMES . '/' . $template->template . '/html/' . $component . '/' . $name); 492 493 break; 494 } 495 496 $this->_addPath('template', JPATH_THEMES . '/' . $template->template . '/html/' . $component . '/' . $name); 497 } 498 break; 499 } 500 } 501 502 /** 503 * Adds to the search path for templates and resources. 504 * 505 * @param string $type The type of path to add. 506 * @param mixed $path The directory or stream, or an array of either, to search. 507 * 508 * @return void 509 * 510 * @since 3.0 511 */ 512 protected function _addPath($type, $path) 513 { 514 // Loop through the path directories 515 foreach ((array) $path as $dir) { 516 // Clean up the path 517 $dir = Path::clean($dir); 518 519 // Add trailing separators as needed 520 if (substr($dir, -1) !== DIRECTORY_SEPARATOR) { 521 // Directory 522 $dir .= DIRECTORY_SEPARATOR; 523 } 524 525 // Add to the top of the search dirs 526 array_unshift($this->_path[$type], $dir); 527 } 528 } 529 530 /** 531 * Create the filename for a resource 532 * 533 * @param string $type The resource type to create the filename for 534 * @param array $parts An associative array of filename information 535 * 536 * @return string The filename 537 * 538 * @since 3.0 539 */ 540 protected function _createFileName($type, $parts = array()) 541 { 542 switch ($type) { 543 case 'template': 544 $filename = strtolower($parts['name']) . '.' . $this->_layoutExt; 545 break; 546 547 default: 548 $filename = strtolower($parts['name']) . '.php'; 549 break; 550 } 551 552 return $filename; 553 } 554 555 /** 556 * Returns the form object 557 * 558 * @return mixed A \JForm object on success, false on failure 559 * 560 * @since 3.2 561 */ 562 public function getForm() 563 { 564 if (!\is_object($this->form)) { 565 $this->form = $this->get('Form'); 566 } 567 568 return $this->form; 569 } 570 571 /** 572 * Sets the document title according to Global Configuration options 573 * 574 * @param string $title The page title 575 * 576 * @return void 577 * 578 * @since 3.6 579 */ 580 public function setDocumentTitle($title) 581 { 582 $app = Factory::getApplication(); 583 584 // Check for empty title and add site name if param is set 585 if (empty($title)) { 586 $title = $app->get('sitename'); 587 } elseif ($app->get('sitename_pagetitles', 0) == 1) { 588 $title = Text::sprintf('JPAGETITLE', $app->get('sitename'), $title); 589 } elseif ($app->get('sitename_pagetitles', 0) == 2) { 590 $title = Text::sprintf('JPAGETITLE', $title, $app->get('sitename')); 591 } 592 593 $this->document->setTitle($title); 594 } 595 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Sep 7 05:41:13 2022 | Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer |