-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcategoryext.php
More file actions
418 lines (353 loc) · 11.1 KB
/
Copy pathcategoryext.php
File metadata and controls
418 lines (353 loc) · 11.1 KB
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
<?php
/**
* @package Joomla.Legacy
* @subpackage Form
*
* @copyright Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
defined('JPATH_PLATFORM') or die;
JFormHelper::loadFieldClass('list');
use Joomla\CMS\Factory;
/**
* Form Field class for the Joomla Platform.
* Supports an HTML select list of categories
*
* @since 11.1
*/
class GJFieldsFormFieldCategoryext extends JFormFieldCategory
{
/**
* The form field type.
*
* @var string
* @since 11.1
*/
public $type = 'Categoryext';
/**
* Method to get the field options for category
* Use the extension attribute in a form to specify the.specific extension for
* which categories should be displayed.
* Use the show_root attribute to specify whether to show the global category root in the list.
*
* @return array The field option objects.
*
* @since 11.1
*/
public function getOptions($flag = false)
{
// Due to inheritance the function can be called twice. To avoid we use the $flag parameter and static $options
static $options = false;
if (!$flag)
{
return $options;
}
$options = array();
$context_or_contenttype = $this->element['context_or_contenttype']
? (string) $this->element['context_or_contenttype'] : (string) $this->element['scope'];
$extension = $this->element[$context_or_contenttype] ? (string) $this->element[$context_or_contenttype] : (string) $this->element['scope'];
$extension = explode(PHP_EOL, $extension);
$extension = trim($extension[0]);
switch ($context_or_contenttype)
{
case 'context':
break;
case 'content_type':
default :
$category = JTable::getInstance('contenttype');
$category->load($extension);
$extension = $category->type_alias;
break;
}
$extension = explode('.', $extension);
$extension = $extension[0];
$published = (string) $this->element['published'];
// Load the category options for a given extension.
if (!empty($extension))
{
switch ($extension)
{
case 'com_k2':
try
{
$db = Factory::getDBO();
$query = 'SELECT m.* FROM #__k2_categories m WHERE trash = 0 ORDER BY parent, ordering';
$db->setQuery($query);
$mitems = $db->loadObjectList();
$children = array();
if ($mitems)
{
foreach ($mitems as $v)
{
if (K2_JVERSION != '15')
{
$v->title = $v->name;
$v->parent_id = $v->parent;
}
$pt = $v->parent;
$list = @$children[$pt] ? $children[$pt] : array();
array_push($list, $v);
$children[$pt] = $list;
}
}
$list = JHTML::_('menu.treerecurse', 0, '', array(), $children, 9999, 0, 0);
$mitems = array();
foreach ($list as $item)
{
$item->treename = JString::str_ireplace(' ', ' -', $item->treename);
$mitems[] = JHTML::_('select.option', $item->id, $item->treename);
}
$options = $mitems;
}
catch (Exception $e)
{
Factory::getApplication()->enqueueMessage(
JText::sprintf('LIB_GJFIELDS_NOT_INSTALLED', $extension) . '<br><pre>' . $e->getMessage() . '</pre>',
'error'
);
}
break;
case 'com_phocadownload':
case 'com_phocagallery':
switch ($extension) {
case 'com_phocadownload':
$tblName = '#__phocadownload_categories';
if (! class_exists('\PhocaDownloadCategory'))
{
require JPATH_ADMINISTRATOR . '/components/com_phocadownload/libraries/phocadownload/category/category.php';
}
break;
case 'com_phocagallery':
$tblName = '#__phocagallery_categories';
if (! class_exists('\PhocaGalleryCategory'))
{
require JPATH_ADMINISTRATOR . '/components/com_phocagallery/libraries/phocagallery/html/category.php';
}
break;
}
$db = \Factory::getDBO();
// Build the list of categories
$query = 'SELECT a.title AS text, a.id AS value, a.parent_id as parentid'
. ' FROM ' . $tblName . ' AS a'
. ' WHERE a.published = 1'
. ' ORDER BY a.ordering';
$db->setQuery($query);
$data = $db->loadObjectList();
$catId = -1;
$tree = array();
$text = '';
switch ($extension) {
case 'com_phocadownload':
$tree = \PhocaDownloadCategory::CategoryTreeOption($data, $tree, 0, $text, $catId);
break;
case 'com_phocagallery':
$tree = \PhocaGalleryCategory::CategoryTreeOption($data, $tree, 0, $text, $catId);
break;
}
$options = $tree;
break;
case 'com_jdownloads':
$file = JPATH_ADMINISTRATOR . '/components/' . $extension . '/models/fields/jdcategoryselect.php';
if (!file_exists($file))
{
Factory::getApplication()->enqueueMessage(
JText::sprintf('LIB_GJFIELDS_NOT_INSTALLED', $extension) . '<br>' . JText::_('LIB_GJFIELDS_FILE_NOT_EXISTS') . '<pre>' . $file . '</pre>',
'error'
);
break;
}
JLoader::register('JFormFieldjdCategorySelect', $file);
$formfield = JFormHelper::loadFieldType('jdcategoryselect');
$formfield->setup($this->element, '');
$options = $formfield->getOptions();
if (empty($options) && Factory::getApplication()->isSite()) {
$options = $this->getJDownloadsCategories();
}
break;
default :
if (strpos($extension, 'com_') !== 0)
{
$extension = 'com_' . $extension;
}
// Filter over published state or not depending upon if it is present.
if ($published)
{
$options = JHtml::_('category.options', $extension, array('filter.published' => explode(',', $published)));
}
else
{
$options = JHtml::_('category.options', $extension);
}
// Verify permissions. If the action attribute is set, then we scan the options.
if ((string) $this->element['action'])
{
// Get the current user object.
$user = Factory::getUser();
foreach ($options as $i => $option)
{
/*
* To take save or create in a category you need to have create rights for that category
* unless the item is already in that category.
* Unset the option if the user isn't authorised for it. In this field assets are always categories.
*/
if ($user->authorise('core.create', $extension . '.category.' . $option->value) != true)
{
unset($options[$i]);
}
}
}
break;
}
if (isset($this->element['show_root']))
{
array_unshift($options, JHtml::_('select.option', '0', JText::_('JGLOBAL_ROOT')));
}
}
else
{
JLog::add(
'GJFields: ' . $this->getAttribute('name') . ' ' . JText::_('JLIB_FORM_ERROR_FIELDS_CATEGORY_ERROR_EXTENSION_EMPTY'),
JLog::WARNING,
'jerror'
);
}
// Merge any additional options in the XML definition.
/*##mygruz20160213194844 {
$options = array_merge(parent::getOptions(), $options);
It was:
It became:*/
/*##mygruz20160213194844 } */
return $options;
}
/**
* HTML output
*
* @return string
*/
protected function getInput()
{
$options = (array) $this->getOptions(true);
if (empty($options))
{
$formfield = JFormHelper::loadFieldType('text');
$formfield->setup($this->element, '');
if (is_array($this->value))
{
$formfield->value = implode(',', $this->value);
}
else
{
$formfield->value = $this->value;
}
$formfield->hint = JText::_((string) $this->hint);
return $formfield->getInput() . PHP_EOL;
}
return parent::getInput();
}
/**
* This is a copy of file administrator/components/com_jdownloads/models/fields/jdcategoryselect.php:getOptions()
*
* Original code doesn't allow to load categroies on Frontend. So must copy it here.
*
*/
private function getJDownloadsCategories() {
// Include helpers
require_once JPATH_SITE.'/administrator/components/com_jdownloads/helpers/jdownloadshelper.php';
$jd_config = JDownloadsHelper::buildjlistConfig();
// Initialise variables.
$options = array();
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('a.id AS value, a.title AS text, a.level');
$query->from('#__jdownloads_categories AS a');
$query->join('LEFT', '`#__jdownloads_categories` AS b ON a.lft > b.lft AND a.rgt < b.rgt');
// Prevent parenting to children of this item.
/* if ($id = $this->form->getValue('id')) {
$query->join('LEFT', '`#__jdownloads_categories` AS p ON p.id = '.(int) $id);
$query->where('NOT(a.lft >= p.lft AND a.rgt <= p.rgt)');
$rowQuery = $db->getQuery(true);
$rowQuery->select('a.id AS value, a.title AS text, a.level, a.parent_id');
$rowQuery->from('#__jdownloads_categories AS a');
$rowQuery->where('a.id = ' . (int) $id);
$db->setQuery($rowQuery);
$row = $db->loadObject();
} */
$query->where('a.published IN (0,1)');
$query->group('a.id');
$query->order('a.lft ASC');
// Get the options.
$db->setQuery($query);
$options = $db->loadObjectList();
// Check for a database error.
if ($db->getErrorNum()) {
JError::raiseWarning(500, $db->getErrorMsg());
}
// Pad the option text with spaces using depth level as a multiplier.
for ($i = 0, $n = count($options); $i < $n; $i++)
{
// Translate ROOT
if ($options[$i]->level == 0) {
if ($jd_config['show.header.catlist.uncategorised'] == 1){
$options[$i]->text = JText::_('COM_JDOWNLOADS_SELECT_UNCATEGORISED');
} else {
$root = array_shift($options);
$i++;
$n--;
}
}
if ($options[$i]->level > 1){
$options[$i]->text = str_repeat('- ',($options[$i]->level -1)).$options[$i]->text;
}
}
// Initialise variables.
/** ##mygruz20200718174443 {
It was:
$user = JFactory::getUser();
if (empty($id)) {
// New item, only have to check core.create.
foreach ($options as $i => $option)
{
// Unset the option if the user isn't authorised for it.
if (!$user->authorise('core.create', 'com_jdownloads.category.'.$option->value)) {
unset($options[$i]);
}
}
}
else {
// Existing item is a bit more complex. Need to account for core.edit and core.edit.own.
foreach ($options as $i => $option)
{
// Unset the option if the user isn't authorised for it.
if (!$user->authorise('core.edit', 'com_jdownloads.category.'.$option->value)) {
// As a backup, check core.edit.own
if (!$user->authorise('core.edit.own', 'com_jdownloads.category.'.$option->value)) {
// No core.edit nor core.edit.own - bounce this one
unset($options[$i]);
}
else {
// TODO I've got a funny feeling we need to check core.create here.
// Maybe you can only get the list of categories you are allowed to create in?
// Need to think about that. If so, this is the place to do the check.
}
}
}
}
It became: */
/** ##mygruz20200718174443 } */
// Merge any additional options in the XML definition.
$options = array_merge(parent::getOptions(), $options);
return $options;
}
}
// Preserve compatibility
if (!class_exists('JFormFieldCategoryext'))
{
/**
* Old-fashioned field name
*
* @since 1.2.0
*/
class JFormFieldCategoryext extends GJFieldsFormFieldCategoryext
{
}
}