10 defined(
'JPATH_PLATFORM') or die;
28 protected $_name = null;
35 protected $_models = array();
42 protected $_basePath = null;
49 protected $_defaultModel = null;
56 protected $_layout =
'default';
63 protected $_layoutExt =
'php';
70 protected $_layoutTemplate =
'_';
77 protected $_path = array(
'template' => array(),
'helper' => array());
84 protected $_template = null;
91 protected $_output = null;
99 protected $_escape =
'htmlspecialchars';
106 protected $_charset =
'UTF-8';
122 public function __construct($config = array())
125 if (empty($this->_name))
127 if (array_key_exists(
'name', $config))
129 $this->_name = $config[
'name'];
133 $this->_name = $this->getName();
138 if (array_key_exists(
'charset', $config))
140 JLog::add(
'Setting a custom charset for escaping is deprecated. Override JViewLegacy::escape() instead.',
JLog::WARNING,
'deprecated');
141 $this->_charset = $config[
'charset'];
145 if (array_key_exists(
'escape', $config))
147 $this->setEscape($config[
'escape']);
151 if (array_key_exists(
'base_path', $config))
153 $this->_basePath = $config[
'base_path'];
157 $this->_basePath = JPATH_COMPONENT;
161 if (array_key_exists(
'template_path', $config))
164 $this->_setPath(
'template', $config[
'template_path']);
166 elseif (is_dir(JPATH_COMPONENT .
'/view'))
168 $this->_setPath(
'template', $this->_basePath .
'/view/' . $this->getName() .
'/tmpl');
172 $this->_setPath(
'template', $this->_basePath .
'/views/' . $this->getName() .
'/tmpl');
176 if (array_key_exists(
'helper_path', $config))
179 $this->_setPath(
'helper', $config[
'helper_path']);
183 $this->_setPath(
'helper', $this->_basePath .
'/helpers');
187 if (array_key_exists(
'layout', $config))
189 $this->setLayout($config[
'layout']);
193 $this->setLayout(
'default');
209 public function display($tpl = null)
211 $result = $this->loadTemplate($tpl);
212 if ($result instanceof Exception)
257 public function assign()
262 $arg0 = @func_get_arg(0);
263 $arg1 = @func_get_arg(1);
266 if (is_object($arg0))
269 foreach (get_object_vars($arg0) as $key => $val)
271 if (substr($key, 0, 1) !=
'_')
282 foreach ($arg0 as $key => $val)
284 if (substr($key, 0, 1) !=
'_')
296 if (is_string($arg0) && substr($arg0, 0, 1) !=
'_' && func_num_args() > 1)
298 $this->$arg0 = $arg1;
331 public function assignRef($key, &$val)
335 if (is_string($key) && substr($key, 0, 1) !=
'_')
356 public function escape($var)
358 if (in_array($this->_escape, array(
'htmlspecialchars',
'htmlentities')))
360 return call_user_func($this->_escape, $var, ENT_COMPAT, $this->_charset);
363 return call_user_func($this->_escape, $var);
376 public function get($property, $default = null)
379 if (is_null($default))
381 $model = $this->_defaultModel;
385 $model = strtolower($default);
389 if (isset($this->_models[$model]))
392 $method =
'get' . ucfirst($property);
395 if (method_exists($this->_models[$model], $method))
398 $result = $this->_models[$model]->$method();
405 $result = parent::get($property, $default);
419 public function getModel($name = null)
423 $name = $this->_defaultModel;
425 return $this->_models[strtolower($name)];
433 public function getLayout()
435 return $this->_layout;
443 public function getLayoutTemplate()
445 return $this->_layoutTemplate;
459 public function getName()
461 if (empty($this->_name))
463 $classname = get_class($this);
464 $viewpos = strpos($classname,
'View');
466 if ($viewpos ===
false)
468 throw new Exception(
JText::_(
'JLIB_APPLICATION_ERROR_VIEW_GET_NAME'), 500);
471 $this->_name = strtolower(substr($classname, $viewpos + 4));
491 public function setModel($model, $default =
false)
493 $name = strtolower($model->getName());
494 $this->_models[$name] = $model;
498 $this->_defaultModel = $name;
512 public function setLayout($layout)
514 $previous = $this->_layout;
515 if (strpos($layout,
':') ===
false)
517 $this->_layout = $layout;
522 $temp = explode(
':', $layout);
523 $this->_layout = $temp[1];
526 $this->_layoutTemplate = $temp[0];
541 public function setLayoutExt($value)
543 $previous = $this->_layoutExt;
544 if ($value = preg_replace(
'#[^A-Za-z0-9]#',
'', trim($value)))
546 $this->_layoutExt = $value;
562 public function setEscape($spec)
564 JLog::add(__METHOD__ .
' is deprecated. Override JViewLegacy::escape() instead.',
JLog::WARNING,
'deprecated');
566 $this->_escape = $spec;
578 public function addTemplatePath($path)
580 $this->_addPath(
'template', $path);
592 public function addHelperPath($path)
594 $this->_addPath(
'helper', $path);
607 public function loadTemplate($tpl = null)
610 $this->_output = null;
613 $layout = $this->getLayout();
614 $layoutTemplate = $this->getLayoutTemplate();
617 $file = isset($tpl) ? $layout .
'_' . $tpl : $layout;
620 $file = preg_replace(
'/[^A-Z0-9_\.-]/i',
'', $file);
621 $tpl = isset($tpl) ? preg_replace(
'/[^A-Z0-9_\.-]/i',
'', $tpl) : $tpl;
625 $lang->load(
'tpl_' . $template, JPATH_BASE, null,
false,
false)
626 || $lang->load(
'tpl_' . $template, JPATH_THEMES .
"/$template", null,
false,
false)
627 || $lang->load(
'tpl_' . $template, JPATH_BASE, $lang->getDefault(),
false,
false)
628 || $lang->load(
'tpl_' . $template, JPATH_THEMES .
"/$template", $lang->getDefault(),
false,
false);
631 if (isset($layoutTemplate) && $layoutTemplate !=
'_' && $layoutTemplate != $template)
633 $this->_path[
'template'] = str_replace($template, $layoutTemplate, $this->_path[
'template']);
637 jimport(
'joomla.filesystem.path');
638 $filetofind = $this->_createFileName(
'template', array(
'name' => $file));
639 $this->_template =
JPath::find($this->_path[
'template'], $filetofind);
642 if ($this->_template ==
false)
644 $filetofind = $this->_createFileName(
'', array(
'name' =>
'default' . (isset($tpl) ?
'_' . $tpl : $tpl)));
645 $this->_template =
JPath::find($this->_path[
'template'], $filetofind);
648 if ($this->_template !=
false)
655 if (isset($this->
this))
665 include $this->_template;
669 $this->_output = ob_get_contents();
672 return $this->_output;
676 throw new Exception(
JText::sprintf(
'JLIB_APPLICATION_ERROR_LAYOUTFILE_NOT_FOUND', $file), 500);
689 public function loadHelper($hlp = null)
692 $file = preg_replace(
'/[^A-Z0-9_\.-]/i',
'', $hlp);
695 jimport(
'joomla.filesystem.path');
696 $helper =
JPath::find($this->_path[
'helper'], $this->_createFileName(
'helper', array(
'name' => $file)));
698 if ($helper !=
false)
701 include_once $helper;
715 protected function _setPath($type, $path)
717 $component = JApplicationHelper::getComponentName();
721 $this->_path[$type] = array();
724 $this->_addPath($type, $path);
727 switch (strtolower($type))
733 $component = preg_replace(
'/[^A-Z0-9_\.-]/i',
'', $component);
734 $fallback = JPATH_THEMES .
'/' . $app->getTemplate() .
'/html/' . $component .
'/' . $this->getName();
735 $this->_addPath(
'template', $fallback);
751 protected function _addPath($type, $path)
754 settype($path,
'array');
757 foreach ($path as $dir)
763 if (substr($dir, -1) != DIRECTORY_SEPARATOR)
766 $dir .= DIRECTORY_SEPARATOR;
770 array_unshift($this->_path[$type], $dir);
784 protected function _createFileName($type, $parts = array())
789 $filename = strtolower($parts[
'name']) .
'.' . $this->_layoutExt;
793 $filename = strtolower($parts[
'name']) .
'.php';
806 public function getForm()
808 if (!is_object($this->form))
810 $this->form = $this->
get(
'Form');