1 <?php
2 /**
3 * @package Joomla.Platform
4 * @subpackage View
5 *
6 * @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
7 * @license GNU General Public License version 2 or later; see LICENSE
8 */
9
10 defined('JPATH_PLATFORM') or die;
11
12 jimport('joomla.filesystem.path');
13
14 /**
15 * Joomla Platform HTML View Class
16 *
17 * @since 12.1
18 */
19 abstract class JViewHtml extends JViewBase
20 {
21 /**
22 * The view layout.
23 *
24 * @var string
25 * @since 12.1
26 */
27 protected $layout = 'default';
28
29 /**
30 * The paths queue.
31 *
32 * @var SplPriorityQueue
33 * @since 12.1
34 */
35 protected $paths;
36
37 /**
38 * Method to instantiate the view.
39 *
40 * @param JModel $model The model object.
41 * @param SplPriorityQueue $paths The paths queue.
42 *
43 * @since 12.1
44 */
45 public function __construct(JModel $model, SplPriorityQueue $paths = null)
46 {
47 parent::__construct($model);
48
49 // Setup dependencies.
50 $this->paths = isset($paths) ? $paths : $this->loadPaths();
51 }
52
53 /**
54 * Magic toString method that is a proxy for the render method.
55 *
56 * @return string
57 *
58 * @since 12.1
59 */
60 public function __toString()
61 {
62 return $this->render();
63 }
64
65 /**
66 * Method to escape output.
67 *
68 * @param string $output The output to escape.
69 *
70 * @return string The escaped output.
71 *
72 * @see JView::escape()
73 * @since 12.1
74 */
75 public function escape($output)
76 {
77 // Escape the output.
78 return htmlspecialchars($output, ENT_COMPAT, 'UTF-8');
79 }
80
81 /**
82 * Method to get the view layout.
83 *
84 * @return string The layout name.
85 *
86 * @since 12.1
87 */
88 public function getLayout()
89 {
90 return $this->layout;
91 }
92
93 /**
94 * Method to get the layout path.
95 *
96 * @param string $layout The layout name.
97 *
98 * @return mixed The layout file name if found, false otherwise.
99 *
100 * @since 12.1
101 */
102 public function getPath($layout)
103 {
104 // Get the layout file name.
105 $file = JPath::clean($layout . '.php');
106
107 // Find the layout file path.
108 $path = JPath::find(clone $this->paths, $file);
109
110 return $path;
111 }
112
113 /**
114 * Method to get the view paths.
115 *
116 * @return SplPriorityQueue The paths queue.
117 *
118 * @since 12.1
119 */
120 public function getPaths()
121 {
122 return $this->paths;
123 }
124
125 /**
126 * Method to render the view.
127 *
128 * @return string The rendered view.
129 *
130 * @since 12.1
131 * @throws RuntimeException
132 */
133 public function render()
134 {
135 // Get the layout path.
136 $path = $this->getPath($this->getLayout());
137
138 // Check if the layout path was found.
139 if (!$path)
140 {
141 throw new RuntimeException('Layout Path Not Found');
142 }
143
144 // Start an output buffer.
145 ob_start();
146
147 // Load the layout.
148 include $path;
149
150 // Get the layout contents.
151 $output = ob_get_clean();
152
153 return $output;
154 }
155
156 /**
157 * Method to set the view layout.
158 *
159 * @param string $layout The layout name.
160 *
161 * @return JViewHtml Method supports chaining.
162 *
163 * @since 12.1
164 */
165 public function setLayout($layout)
166 {
167 $this->layout = $layout;
168
169 return $this;
170 }
171
172 /**
173 * Method to set the view paths.
174 *
175 * @param SplPriorityQueue $paths The paths queue.
176 *
177 * @return JViewHtml Method supports chaining.
178 *
179 * @since 12.1
180 */
181 public function setPaths(SplPriorityQueue $paths)
182 {
183 $this->paths = $paths;
184
185 return $this;
186 }
187
188 /**
189 * Method to load the paths queue.
190 *
191 * @return SplPriorityQueue The paths queue.
192 *
193 * @since 12.1
194 */
195 protected function loadPaths()
196 {
197 return new SplPriorityQueue;
198 }
199 }
200