forked from Kunena/Kunena-Forum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
display.php
286 lines (247 loc) · 6.01 KB
/
display.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<?php
/**
* Kunena Component
* @package Kunena.Framework
* @subpackage Controller
*
* @copyright (C) 2008 - 2015 Kunena Team. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
* @link http://www.kunena.org
**/
defined ( '_JEXEC' ) or die ();
abstract class KunenaControllerDisplay extends KunenaControllerBase
{
protected $name = 'Empty';
public $output = null;
public $layout = 'default';
public $config;
protected $primary = false;
/**
* @see KunenaControllerBase::execute()
*/
public function execute()
{
KUNENA_PROFILER ? KunenaProfiler::instance()->start('function '.get_class($this).'::'.__FUNCTION__.'()') : null;
try
{
// Run before executing action.
$result = $this->before();
if ($result === false)
{
KUNENA_PROFILER ? KunenaProfiler::instance()->stop('function '.get_class($this).'::'.__FUNCTION__.'()') : null;
return KunenaLayout::factory('Empty')->setOptions($this->getOptions());
}
// Display layout with given parameters.
$this->output = $this->display();
// Run after executing action.
$this->after();
}
catch (KunenaExceptionAuthorise $e)
{
if ($this->primary)
{
KUNENA_PROFILER ? KunenaProfiler::instance()->stop('function '.get_class($this).'::'.__FUNCTION__.'()') : null;
throw $e;
}
else
{
$this->output = KunenaLayout::factory('Empty')->setOptions($this->getOptions());
}
}
KUNENA_PROFILER ? KunenaProfiler::instance()->stop('function '.get_class($this).'::'.__FUNCTION__.'()') : null;
return $this->output;
}
/**
* Initialize and display the layout.
*
* @return KunenaLayout
*/
protected function display()
{
// Display layout with given parameters.
$content = KunenaLayout::factory($this->name)
->setProperties($this->getProperties())
->setOptions($this->getOptions());
return $content;
}
/**
* @internal
*/
public function setPrimary()
{
$this->primary = true;
return $this;
}
/**
* Executed before display.
*/
protected function before()
{
$this->layout = $this->input->getCmd('layout', 'default');
$this->config = KunenaConfig::getInstance();
if ($this->primary)
{
$this->document = JFactory::getDocument();
}
}
/**
* Executed after display.
*/
protected function after()
{
if ($this->primary) $this->prepareDocument();
}
/**
* Prepare title, description, keywords, breadcrumb etc.
*/
protected function prepareDocument()
{
}
/**
* Return view as a string.
*
* @return string
*/
public function __toString()
{
try
{
$output = $this->execute();
}
catch (KunenaExceptionAuthorise $e)
{
if (!$this->primary)
{
return (string) KunenaLayout::factory('Empty');
}
$document = JFactory::getDocument();
$document->setTitle($e->getResponseStatus());
JResponse::setHeader('Status', $e->getResponseStatus(), true);
$output = KunenaLayout::factory('Misc/Default', 'pages')
->set('header', $e->getResponseStatus())
->set('body', $e->getMessage());
}
catch (Exception $e)
{
// TODO: error message?
if (!$this->primary) {
return "<b>Exception</b> in layout <b>{$this->name}!</b>" . (!JDEBUG ? $e->getMessage() : '');
}
$title = '500 Internal Server Error';
$document = JFactory::getDocument();
$document->setTitle($title);
JResponse::setHeader('Status', $title, true);
$output = KunenaLayout::factory('Misc/Default', 'pages')
->set('header', $title)
->set('body', $e->getMessage());
}
return (string) $output;
}
/**
* Method to get the view layout.
*
* @return string The layout name.
*/
public function getLayout()
{
$layout = preg_replace('/[^a-z0-9_]/', '', strtolower($this->layout));
return $layout ? $layout : 'default';
}
/**
* Method to set the view layout.
*
* @param string $layout The layout name.
*
* @return KunenaLayout Method supports chaining.
*/
public function setLayout($layout)
{
if (!$layout) $layout = 'default';
$this->layout = $layout;
return $this;
}
/**
* Returns an associative array of public object properties.
*
* @return array
*/
public function getProperties()
{
$properties = (array) $this;
$list = array();
foreach ($properties as $property=>$value)
{
if ($property[0] != "\0") $list[$property] = $value;
}
return $list;
}
/**
* Set the object properties based on a named array/hash.
*
* @param mixed $properties Either an associative array or another object.
*
* @return KunenaControllerDisplay Method supports chaining.
*
* @see set()
* @throws \InvalidArgumentException
*/
public function setProperties($properties)
{
if (!is_array($properties) && !is_object($properties))
{
throw new \InvalidArgumentException('Parameter should be either array or an object.');
}
foreach ((array) $properties as $k => $v) {
// Use the set function which might be overridden.
if ($k[0] != "\0")
{
$this->$k = $v;
}
}
return $this;
}
/**
* Shortcut for $this->input->set()
*
* @param $key
* @param $value
* @return $this
*/
public function set($key, $value)
{
$this->input->set($key, $value);
return $this;
}
protected function setTitle($title, $replace = false)
{
if (!$replace)
{
// Obey Joomla configuration.
if ($this->app->getCfg('sitename_pagetitles', 0) == 1)
{
$title = JText::sprintf('JPAGETITLE', $this->app->getCfg('sitename'), $this->config->board_title .' - '. $title);
}
elseif ($this->app->getCfg('sitename_pagetitles', 0) == 2)
{
$title = JText::sprintf('JPAGETITLE', $title .' - '. $this->config->board_title, $this->app->getCfg('sitename'));
}
else
{
$title = KunenaFactory::getConfig()->board_title .' - '. $title;
}
}
$title = strip_tags($title);
$this->document->setTitle($title);
}
protected function setKeywords($keywords)
{
if (!empty($keywords))
{
$this->document->setMetadata('keywords', $keywords);
}
}
protected function setDescription($description)
{
$this->document->setMetadata('description', $description);
}
}