1
|
|
|
<?php |
|
|
|
|
2
|
|
|
// |
3
|
|
|
// ------------------------------------------------------------------------ // |
4
|
|
|
// This program is free software; you can redistribute it and/or modify // |
5
|
|
|
// it under the terms of the GNU General Public License as published by // |
6
|
|
|
// the Free Software Foundation; either version 2 of the License, or // |
7
|
|
|
// (at your option) any later version. // |
8
|
|
|
// // |
9
|
|
|
// You may not change or alter any portion of this comment or credits // |
10
|
|
|
// of supporting developers from this source code or any supporting // |
11
|
|
|
// source code which is considered copyrighted (c) material of the // |
12
|
|
|
// original comment or credit authors. // |
13
|
|
|
// // |
14
|
|
|
// This program is distributed in the hope that it will be useful, // |
15
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of // |
16
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // |
17
|
|
|
// GNU General Public License for more details. // |
18
|
|
|
// // |
19
|
|
|
// You should have received a copy of the GNU General Public License // |
20
|
|
|
// along with this program; if not, write to the Free Software // |
21
|
|
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // |
22
|
|
|
// ------------------------------------------------------------------------ // |
23
|
|
|
// Author: phppp (D.J., [email protected]) // |
24
|
|
|
// URL: http://xoops.org // |
25
|
|
|
// Project: Article Project // |
26
|
|
|
// ------------------------------------------------------------------------ // |
27
|
|
|
|
28
|
|
|
// defined('XOOPS_ROOT_PATH') || exit('XOOPS root path not defined'); |
|
|
|
|
29
|
|
|
require_once $GLOBALS['xoops']->path('class/tree.php'); |
30
|
|
|
|
31
|
|
|
if (!class_exists('NewbbObjectTree')) { |
32
|
|
|
/** |
33
|
|
|
* Class NewbbObjectTree |
34
|
|
|
*/ |
35
|
|
|
class NewbbObjectTree extends XoopsObjectTree |
|
|
|
|
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* @param $objectArr |
39
|
|
|
* @param null $rootId |
40
|
|
|
*/ |
41
|
|
|
public function __construct(&$objectArr, $rootId = null) |
42
|
|
|
{ |
43
|
|
|
parent::__construct($objectArr, 'forum_id', 'parent_forum', $rootId); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Make options for a select box from |
48
|
|
|
* |
49
|
|
|
* @param int $key ID of the object to display as the root of select options |
50
|
|
|
* @param string $ret (reference to a string when called from outside) Result from previous recursions |
51
|
|
|
* @param string $prefix_orig String to indent items at deeper levels |
52
|
|
|
* @param string $prefix_curr String to indent the current item |
53
|
|
|
* @param null|array $tags |
54
|
|
|
* @internal param string $fieldName Name of the member variable from the |
55
|
|
|
* node objects that should be used as the title for the options. |
56
|
|
|
* @internal param string $selected Value to display as selected |
57
|
|
|
* @access private |
58
|
|
|
*/ |
59
|
|
|
protected function makeTreeItems($key, &$ret, $prefix_orig, $prefix_curr = '', $tags = null) |
60
|
|
|
{ |
61
|
|
|
if ($key > 0) { |
62
|
|
View Code Duplication |
if (count($tags) > 0) { |
|
|
|
|
63
|
|
|
foreach ($tags as $tag) { |
|
|
|
|
64
|
|
|
$ret[$key][$tag] = $this->tree[$key]['obj']->getVar($tag); |
65
|
|
|
} |
66
|
|
|
} else { |
67
|
|
|
$ret[$key]['forum_name'] = $this->tree[$key]['obj']->getVar('forum_name'); |
68
|
|
|
} |
69
|
|
|
$ret[$key]['prefix'] = $prefix_curr; |
70
|
|
|
$prefix_curr .= $prefix_orig; |
71
|
|
|
} |
72
|
|
|
if (isset($this->tree[$key]['child']) && !empty($this->tree[$key]['child'])) { |
73
|
|
|
foreach ($this->tree[$key]['child'] as $childkey) { |
74
|
|
|
$this->makeTreeItems($childkey, $ret, $prefix_orig, $prefix_curr, $tags); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Make a select box with options from the tree |
81
|
|
|
* |
82
|
|
|
* @param string $prefix String to indent deeper levels |
83
|
|
|
* @param integer $key ID of the object to display as the root of select options |
84
|
|
|
* @param null $tags |
85
|
|
|
* @return array|string HTML select box |
86
|
|
|
* @internal param string $name Name of the select box |
87
|
|
|
* @internal param string $fieldName Name of the member variable from the |
88
|
|
|
* node objects that should be used as the title for the options. |
89
|
|
|
* @internal param string $selected Value to display as selected |
90
|
|
|
* @internal param bool $addEmptyOption Set TRUE to add an empty option with value "0" at the top of the hierarchy |
91
|
|
|
*/ |
92
|
|
|
public function &makeTree($prefix = '-', $key = 0, $tags = null) |
93
|
|
|
{ |
94
|
|
|
$ret = []; |
95
|
|
|
$this->makeTreeItems($key, $ret, $prefix, '', $tags); |
96
|
|
|
|
97
|
|
|
return $ret; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Make a select box with options from the tree |
102
|
|
|
* |
103
|
|
|
* @param string $name Name of the select box |
104
|
|
|
* @param string $fieldName |
105
|
|
|
* @param string $prefix String to indent deeper levels |
106
|
|
|
* @param string $selected Value to display as selected |
107
|
|
|
* @param bool $addEmptyOption |
108
|
|
|
* @param integer $key ID of the object to display as the root of select options |
109
|
|
|
* @param string $extra |
110
|
|
|
* @return string HTML select box |
111
|
|
|
* @internal param bool $EmptyOption |
112
|
|
|
* @internal param string $fieldName Name of the member variable from the |
113
|
|
|
* node objects that should be used as the title for the options. |
114
|
|
|
* @internal param bool $addEmptyOption Set TRUE to add an empty option with value "0" at the top of the hierarchy |
115
|
|
|
*/ |
116
|
|
|
public function makeSelBox( |
117
|
|
|
$name, |
118
|
|
|
$fieldName, |
119
|
|
|
$prefix = '-', |
120
|
|
|
$selected = '', |
121
|
|
|
$addEmptyOption = false, |
122
|
|
|
$key = 0, |
123
|
|
|
$extra = '' |
124
|
|
|
) //makeSelBox($name, $prefix = '-', $selected = '', $EmptyOption = false, $key = 0) |
|
|
|
|
125
|
|
|
{ |
126
|
|
|
$ret = '<select name=' . $name . '>'; |
127
|
|
|
if (!empty($addEmptyOption)) { |
128
|
|
|
$ret .= '<option value="0">' . (is_string($EmptyOption) ? $EmptyOption : '') . '</option>'; |
|
|
|
|
129
|
|
|
} |
130
|
|
|
$this->_makeSelBoxOptions('forum_name', $selected, $key, $ret, $prefix); |
131
|
|
|
$ret .= '</select>'; |
132
|
|
|
|
133
|
|
|
return $ret; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Make a tree for the array of a given category |
138
|
|
|
* |
139
|
|
|
* @param string $key top key of the tree |
140
|
|
|
* @param array $ret the tree |
141
|
|
|
* @param integer $depth level of subcategories |
142
|
|
|
* @return array |
|
|
|
|
143
|
|
|
* @internal param array $tags fields to be used |
144
|
|
|
*/ |
145
|
|
|
public function getAllChildObject($key, &$ret, $depth = 0) |
146
|
|
|
{ |
147
|
|
|
if (--$depth == 0) { |
148
|
|
|
return; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
if (isset($this->tree[$key]['child'])) { |
152
|
|
|
foreach ($this->tree[$key]['child'] as $childkey) { |
153
|
|
|
if (isset($this->tree[$childkey]['obj'])) { |
154
|
|
|
$ret['child'][$childkey] = $this->tree[$childkey]['obj']; |
155
|
|
|
} |
156
|
|
|
$this->getAllChild_object($childkey, $ret['child'][$childkey], $depth); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Make a tree for the array |
163
|
|
|
* |
164
|
|
|
* @param int|string $key top key of the tree |
165
|
|
|
* @param integer $depth level of subcategories |
166
|
|
|
* @return array |
167
|
|
|
* @internal param array $tags fields to be used |
168
|
|
|
*/ |
169
|
|
|
public function &makeObjectTree($key = 0, $depth = 0) |
170
|
|
|
{ |
171
|
|
|
$ret = []; |
172
|
|
|
if ($depth > 0) { |
173
|
|
|
++$depth; |
174
|
|
|
} |
175
|
|
|
$this->getAllChild_object($key, $ret, $depth); |
176
|
|
|
|
177
|
|
|
return $ret; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Make a tree for the array of a given category |
182
|
|
|
* |
183
|
|
|
* @param string $key top key of the tree |
184
|
|
|
* @param array $ret the tree |
185
|
|
|
* @param array $tags fields to be used |
186
|
|
|
* @param integer $depth level of subcategories |
187
|
|
|
* @return array |
|
|
|
|
188
|
|
|
**/ |
189
|
|
|
public function getAllChildArray($key, &$ret, array $tags = [], $depth = 0) |
190
|
|
|
{ |
191
|
|
|
if (--$depth == 0) { |
192
|
|
|
return; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
if (isset($this->tree[$key]['child'])) { |
196
|
|
|
foreach ($this->tree[$key]['child'] as $childkey) { |
197
|
|
|
if (isset($this->tree[$childkey]['obj'])) { |
198
|
|
View Code Duplication |
if (count($tags) > 0) { |
|
|
|
|
199
|
|
|
foreach ($tags as $tag) { |
200
|
|
|
$ret['child'][$childkey][$tag] = $this->tree[$childkey]['obj']->getVar($tag); |
201
|
|
|
} |
202
|
|
|
} else { |
203
|
|
|
$ret['child'][$childkey]['forum_name'] = $this->tree[$childkey]['obj']->getVar('forum_name'); |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
$this->getAllChildArray($childkey, $ret['child'][$childkey], $tags, $depth); |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Make a tree for the array |
214
|
|
|
* |
215
|
|
|
* @param int|string $key top key of the tree |
216
|
|
|
* @param array $tags fields to be used |
|
|
|
|
217
|
|
|
* @param integer $depth level of subcategories |
218
|
|
|
* @return array |
219
|
|
|
*/ |
220
|
|
|
public function &makeArrayTree($key = 0, $tags = null, $depth = 0) |
221
|
|
|
{ |
222
|
|
|
$ret = []; |
223
|
|
|
if ($depth > 0) { |
224
|
|
|
++$depth; |
225
|
|
|
} |
226
|
|
|
$this->getAllChildArray($key, $ret, $tags, $depth); |
227
|
|
|
|
228
|
|
|
return $ret; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/**#@+ |
232
|
|
|
* get all parent forums |
233
|
|
|
* |
234
|
|
|
* @param string $key ID of the child object |
235
|
|
|
* @param array $ret (empty when called from outside) Result from previous recursions |
236
|
|
|
* @param int $uplevel (empty when called from outside) level of recursion |
237
|
|
|
* @return array Array of parent nodes. |
238
|
|
|
*/ |
239
|
|
|
public function &myGetParentForums($key, array $ret = [], $uplevel = 0) |
240
|
|
|
{ |
241
|
|
|
if (isset($this->tree[$key]['parent']) && isset($this->tree[$this->tree[$key]['parent']]['obj'])) { |
242
|
|
|
$ret[$uplevel] = $this->tree[$this->tree[$key]['parent']]['obj']; |
243
|
|
|
if ($this->tree[$key]['parent'] !== $key) { |
244
|
|
|
//$parents = $this->getParentForums($this->tree[$key]['parent'], $ret, $uplevel+1); |
|
|
|
|
245
|
|
|
$parents = $this->getParentForums($this->tree[$key]['parent']); |
246
|
|
|
foreach (array_keys($parents) as $newkey) { |
247
|
|
|
$ret[$newkey] = $parents[$newkey]; |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
return $ret; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* @param $key |
257
|
|
|
* @param bool $reverse |
258
|
|
|
* @return array |
259
|
|
|
*/ |
260
|
|
|
public function &getParentForums($key, $reverse = true) |
261
|
|
|
{ |
262
|
|
|
$ret = []; |
263
|
|
|
$pids = []; |
264
|
|
|
if (isset($this->tree[$key]['parent']) && isset($this->tree[$this->tree[$key]['parent']]['obj'])) { |
265
|
|
|
$pids[] = $this->tree[$this->tree[$key]['parent']]['obj']->getVar($this->myId); |
266
|
|
|
$parents = $this->myGetParentForums($this->tree[$key]['parent'], $ret); |
267
|
|
|
foreach (array_keys($parents) as $newkey) { |
268
|
|
|
if (!is_object($newkey)) { |
269
|
|
|
continue; |
270
|
|
|
} |
271
|
|
|
$ret[] = $parents[$newkey]->getVar($this->myId); |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
if ($reverse) { |
275
|
|
|
$pids = array_reverse($ret) + $pids; |
276
|
|
|
} else { |
277
|
|
|
$pids += $ret; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
return $pids; |
281
|
|
|
} |
282
|
|
|
/**#@-*/ |
283
|
|
|
} |
284
|
|
|
} |
285
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.