-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
categoryedit.php
247 lines (217 loc) · 7.52 KB
/
categoryedit.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
<?php
/**
* @package Joomla.Administrator
* @subpackage com_categories
*
* @copyright Copyright (C) 2005 - 2016 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
defined('JPATH_BASE') or die;
use Joomla\Utilities\ArrayHelper;
JFormHelper::loadFieldClass('list');
/**
* Form Field class for the Joomla Framework.
*
* @since 1.6
*/
class JFormFieldCategoryEdit extends JFormFieldList
{
/**
* A flexible category list that respects access controls
*
* @var string
* @since 1.6
*/
public $type = 'CategoryEdit';
/**
* Method to get a list of categories that respects access controls and can be used for
* either category assignment or parent category assignment in edit screens.
* Use the parent element to indicate that the field will be used for assigning parent categories.
*
* @return array The field option objects.
*
* @since 1.6
*/
protected function getOptions()
{
$options = array();
$published = $this->element['published'] ? $this->element['published'] : array(0, 1);
$name = (string) $this->element['name'];
// Let's get the id for the current item, either category or content item.
$jinput = JFactory::getApplication()->input;
// Load the category options for a given extension.
// For categories the old category is the category id or 0 for new category.
if ($this->element['parent'] || $jinput->get('option') == 'com_categories')
{
$oldCat = $jinput->get('id', 0);
$oldParent = $this->form->getValue($name, 0);
$extension = $this->element['extension'] ? (string) $this->element['extension'] : (string) $jinput->get('extension', 'com_content');
}
else
// For items the old category is the category they are in when opened or 0 if new.
{
$oldCat = $this->form->getValue($name, 0);
$extension = $this->element['extension'] ? (string) $this->element['extension'] : (string) $jinput->get('option', 'com_content');
}
$db = JFactory::getDbo();
$query = $db->getQuery(true)
->select('DISTINCT a.id AS value, a.title AS text, a.level, a.published, a.lft');
$subQuery = $db->getQuery(true)
->select('id,title,level,published,parent_id,extension,lft,rgt')
->from('#__categories');
// Filter by the extension type
if ($this->element['parent'] == true || $jinput->get('option') == 'com_categories')
{
$subQuery->where('(extension = ' . $db->quote($extension) . ' OR parent_id = 0)');
}
else
{
$subQuery->where('(extension = ' . $db->quote($extension) . ')');
}
// Filter language
if (!empty($this->element['language']))
{
$subQuery->where('language = ' . $db->quote($this->element['language']));
}
// Filter on the published state
if (is_numeric($published))
{
$subQuery->where('published = ' . (int) $published);
}
elseif (is_array($published))
{
$subQuery->where('published IN (' . implode(',', ArrayHelper::toInteger($published)) . ')');
}
$query->from('(' . (string) $subQuery . ') AS a')
->join('LEFT', $db->quoteName('#__categories') . ' AS b ON a.lft > b.lft AND a.rgt < b.rgt');
$query->order('a.lft ASC');
// If parent isn't explicitly stated but we are in com_categories assume we want parents
if ($oldCat != 0 && ($this->element['parent'] == true || $jinput->get('option') == 'com_categories'))
{
// Prevent parenting to children of this item.
// To rearrange parents and children move the children up, not the parents down.
$query->join('LEFT', $db->quoteName('#__categories') . ' AS p ON p.id = ' . (int) $oldCat)
->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')
->from('#__categories AS a')
->where('a.id = ' . (int) $oldCat);
$db->setQuery($rowQuery);
$row = $db->loadObject();
}
// Get the options.
$db->setQuery($query);
try
{
$options = $db->loadObjectList();
}
catch (RuntimeException $e)
{
JError::raiseWarning(500, $e->getMessage());
}
// Pad the option text with spaces using depth level as a multiplier.
for ($i = 0, $n = count($options); $i < $n; $i++)
{
// Translate ROOT
if ($this->element['parent'] == true || $jinput->get('option') == 'com_categories')
{
if ($options[$i]->level == 0)
{
$options[$i]->text = JText::_('JGLOBAL_ROOT_PARENT');
}
}
// Displays language code if not set to All
$db = JFactory::getDbo();
$query = $db->getQuery(true)
->select($db->quoteName('language'))
->where($db->quoteName('id') . '=' . (int) $options[$i]->value)
->from($db->quoteName('#__categories'));
$db->setQuery($query);
$language = $db->loadResult();
if ($options[$i]->published == 1)
{
$options[$i]->text = str_repeat('- ', $options[$i]->level) . $options[$i]->text;
}
else
{
$options[$i]->text = str_repeat('- ', $options[$i]->level) . '[' . $options[$i]->text . ']';
}
if ($language !== '*')
{
$options[$i]->text = $options[$i]->text . ' (' . $language . ')';
}
}
// Get the current user object.
$user = JFactory::getUser();
// For new items we want a list of categories you are allowed to create in.
if ($oldCat == 0)
{
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 && $option->level != 0)
{
unset($options[$i]);
}
}
}
// If you have an existing category id things are more complex.
else
{
/* If you are only allowed to edit in this category but not edit.state, you should not get any
* option to change the category parent for a category or the category for a content item,
* but you should be able to save in that category.
*/
foreach ($options as $i => $option)
{
if ($user->authorise('core.edit.state', $extension . '.category.' . $oldCat) != true && !isset($oldParent))
{
if ($option->value != $oldCat)
{
unset($options[$i]);
}
}
if ($user->authorise('core.edit.state', $extension . '.category.' . $oldCat) != true
&& (isset($oldParent))
&& $option->value != $oldParent)
{
unset($options[$i]);
}
// However, if you can edit.state you can also move this to another category for which you have
// create permission and you should also still be able to save in the current category.
if (($user->authorise('core.create', $extension . '.category.' . $option->value) != true)
&& ($option->value != $oldCat && !isset($oldParent)))
{
{
unset($options[$i]);
}
}
if (($user->authorise('core.create', $extension . '.category.' . $option->value) != true)
&& (isset($oldParent))
&& $option->value != $oldParent)
{
{
unset($options[$i]);
}
}
}
}
if (($this->element['parent'] == true || $jinput->get('option') == 'com_categories')
&& (isset($row) && !isset($options[0]))
&& isset($this->element['show_root']))
{
if ($row->parent_id == '1')
{
$parent = new stdClass;
$parent->text = JText::_('JGLOBAL_ROOT_PARENT');
array_unshift($options, $parent);
}
array_unshift($options, JHtml::_('select.option', '0', JText::_('JGLOBAL_ROOT')));
}
// Merge any additional options in the XML definition.
return array_merge(parent::getOptions(), $options);
}
}