1 <?php
2 3 4 5 6 7 8
9
10 defined('JPATH_PLATFORM') or die;
11
12 use Joomla\Utilities\ArrayHelper;
13
14 15 16 17 18
19 abstract class JHtmlCategory
20 {
21 22 23 24 25 26
27 protected static $items = array();
28
29 30 31 32 33 34 35 36 37 38 39
40 public static function options($extension, $config = array('filter.published' => array(0, 1)))
41 {
42 $hash = md5($extension . '.' . serialize($config));
43
44 if (!isset(static::$items[$hash]))
45 {
46 $config = (array) $config;
47 $db = JFactory::getDbo();
48 $user = JFactory::getUser();
49 $groups = implode(',', $user->getAuthorisedViewLevels());
50
51 $query = $db->getQuery(true)
52 ->select('a.id, a.title, a.level, a.language')
53 ->from('#__categories AS a')
54 ->where('a.parent_id > 0');
55
56
57 $query->where('extension = ' . $db->quote($extension));
58
59
60 $query->where('a.access IN (' . $groups . ')');
61
62
63 if (isset($config['filter.published']))
64 {
65 if (is_numeric($config['filter.published']))
66 {
67 $query->where('a.published = ' . (int) $config['filter.published']);
68 }
69 elseif (is_array($config['filter.published']))
70 {
71 $config['filter.published'] = ArrayHelper::toInteger($config['filter.published']);
72 $query->where('a.published IN (' . implode(',', $config['filter.published']) . ')');
73 }
74 }
75
76
77 if (isset($config['filter.language']))
78 {
79 if (is_string($config['filter.language']))
80 {
81 $query->where('a.language = ' . $db->quote($config['filter.language']));
82 }
83 elseif (is_array($config['filter.language']))
84 {
85 foreach ($config['filter.language'] as &$language)
86 {
87 $language = $db->quote($language);
88 }
89
90 $query->where('a.language IN (' . implode(',', $config['filter.language']) . ')');
91 }
92 }
93
94
95 if (isset($config['filter.access']))
96 {
97 if (is_string($config['filter.access']))
98 {
99 $query->where('a.access = ' . $db->quote($config['filter.access']));
100 }
101 elseif (is_array($config['filter.access']))
102 {
103 foreach ($config['filter.access'] as &$access)
104 {
105 $access = $db->quote($access);
106 }
107
108 $query->where('a.access IN (' . implode(',', $config['filter.access']) . ')');
109 }
110 }
111
112 $query->order('a.lft');
113
114 $db->setQuery($query);
115 $items = $db->loadObjectList();
116
117
118 static::$items[$hash] = array();
119
120 foreach ($items as &$item)
121 {
122 $repeat = ($item->level - 1 >= 0) ? $item->level - 1 : 0;
123 $item->title = str_repeat('- ', $repeat) . $item->title;
124
125 if ($item->language !== '*')
126 {
127 $item->title .= ' (' . $item->language . ')';
128 }
129
130 static::$items[$hash][] = JHtml::_('select.option', $item->id, $item->title);
131 }
132 }
133
134 return static::$items[$hash];
135 }
136
137 138 139 140 141 142 143 144 145 146
147 public static function categories($extension, $config = array('filter.published' => array(0, 1)))
148 {
149 $hash = md5($extension . '.' . serialize($config));
150
151 if (!isset(static::$items[$hash]))
152 {
153 $config = (array) $config;
154 $user = JFactory::getUser();
155 $db = JFactory::getDbo();
156 $query = $db->getQuery(true)
157 ->select('a.id, a.title, a.level, a.parent_id')
158 ->from('#__categories AS a')
159 ->where('a.parent_id > 0');
160
161
162 $query->where('extension = ' . $db->quote($extension));
163
164
165 $groups = implode(',', $user->getAuthorisedViewLevels());
166 $query->where('a.access IN (' . $groups . ')');
167
168
169 if (isset($config['filter.published']))
170 {
171 if (is_numeric($config['filter.published']))
172 {
173 $query->where('a.published = ' . (int) $config['filter.published']);
174 }
175 elseif (is_array($config['filter.published']))
176 {
177 $config['filter.published'] = ArrayHelper::toInteger($config['filter.published']);
178 $query->where('a.published IN (' . implode(',', $config['filter.published']) . ')');
179 }
180 }
181
182 $query->order('a.lft');
183
184 $db->setQuery($query);
185 $items = $db->loadObjectList();
186
187
188 static::$items[$hash] = array();
189
190 foreach ($items as &$item)
191 {
192 $repeat = ($item->level - 1 >= 0) ? $item->level - 1 : 0;
193 $item->title = str_repeat('- ', $repeat) . $item->title;
194 static::$items[$hash][] = JHtml::_('select.option', $item->id, $item->title);
195 }
196
197 static::$items[$hash][] = JHtml::_('select.option', '1', JText::_('JLIB_HTML_ADD_TO_ROOT'));
198 }
199
200 return static::$items[$hash];
201 }
202 }
203