-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
tag.php
427 lines (358 loc) · 9.71 KB
/
tag.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
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
419
420
421
422
423
424
425
426
427
<?php
/**
* @package Joomla.Administrator
* @subpackage com_tags
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
defined('_JEXEC') or die;
use Joomla\Registry\Registry;
use Joomla\String\StringHelper;
/**
* Tags Component Tag Model
*
* @since 3.1
*/
class TagsModelTag extends JModelAdmin
{
/**
* @var string The prefix to use with controller messages.
* @since 3.1
*/
protected $text_prefix = 'COM_TAGS';
/**
* @var string The type alias for this content type.
* @since 3.2
*/
public $typeAlias = 'com_tags.tag';
/**
* Allowed batch commands
*
* @var array
* @since 3.7.0
*/
protected $batch_commands = array(
'assetgroup_id' => 'batchAccess',
'language_id' => 'batchLanguage',
);
/**
* Method to test whether a record can be deleted.
*
* @param object $record A record object.
*
* @return boolean True if allowed to delete the record. Defaults to the permission set in the component.
*
* @since 3.1
*/
protected function canDelete($record)
{
if (empty($record->id) || $record->published != -2)
{
return false;
}
return parent::canDelete($record);
}
/**
* Method to get a table object, load it if necessary.
*
* @param string $type The table name. Optional.
* @param string $prefix The class prefix. Optional.
* @param array $config Configuration array for model. Optional.
*
* @return JTable A JTable object
*
* @since 3.1
*/
public function getTable($type = 'Tag', $prefix = 'TagsTable', $config = array())
{
return JTable::getInstance($type, $prefix, $config);
}
/**
* Auto-populate the model state.
*
* @note Calling getState in this method will result in recursion.
*
* @return void
*
* @since 3.1
*/
protected function populateState()
{
$app = JFactory::getApplication('administrator');
$parentId = $app->input->getInt('parent_id');
$this->setState('tag.parent_id', $parentId);
// Load the User state.
$pk = $app->input->getInt('id');
$this->setState($this->getName() . '.id', $pk);
// Load the parameters.
$params = JComponentHelper::getParams('com_tags');
$this->setState('params', $params);
}
/**
* Method to get a tag.
*
* @param integer $pk An optional id of the object to get, otherwise the id from the model state is used.
*
* @return mixed Tag data object on success, false on failure.
*
* @since 3.1
*/
public function getItem($pk = null)
{
if ($result = parent::getItem($pk))
{
// Prime required properties.
if (empty($result->id))
{
$result->parent_id = $this->getState('tag.parent_id');
}
// Convert the metadata field to an array.
$registry = new Registry($result->metadata);
$result->metadata = $registry->toArray();
// Convert the images field to an array.
$registry = new Registry($result->images);
$result->images = $registry->toArray();
// Convert the urls field to an array.
$registry = new Registry($result->urls);
$result->urls = $registry->toArray();
// Convert the created and modified dates to local user time for display in the form.
$tz = new DateTimeZone(JFactory::getApplication()->get('offset'));
if ((int) $result->created_time)
{
$date = new JDate($result->created_time);
$date->setTimezone($tz);
$result->created_time = $date->toSql(true);
}
else
{
$result->created_time = null;
}
if ((int) $result->modified_time)
{
$date = new JDate($result->modified_time);
$date->setTimezone($tz);
$result->modified_time = $date->toSql(true);
}
else
{
$result->modified_time = null;
}
}
return $result;
}
/**
* Method to get the row form.
*
* @param array $data Data for the form.
* @param boolean $loadData True if the form is to load its own data (default case), false if not.
*
* @return mixed A JForm object on success, false on failure
*
* @since 3.1
*/
public function getForm($data = array(), $loadData = true)
{
$jinput = JFactory::getApplication()->input;
// Get the form.
$form = $this->loadForm('com_tags.tag', 'tag', array('control' => 'jform', 'load_data' => $loadData));
if (empty($form))
{
return false;
}
$user = JFactory::getUser();
if (!$user->authorise('core.edit.state', 'com_tags' . $jinput->get('id')))
{
// Disable fields for display.
$form->setFieldAttribute('ordering', 'disabled', 'true');
$form->setFieldAttribute('published', 'disabled', 'true');
// Disable fields while saving.
// The controller has already verified this is a record you can edit.
$form->setFieldAttribute('ordering', 'filter', 'unset');
$form->setFieldAttribute('published', 'filter', 'unset');
}
return $form;
}
/**
* Method to get the data that should be injected in the form.
*
* @return mixed The data for the form.
*
* @since 3.1
*/
protected function loadFormData()
{
// Check the session for previously entered form data.
$data = JFactory::getApplication()->getUserState('com_tags.edit.tag.data', array());
if (empty($data))
{
$data = $this->getItem();
}
$this->preprocessData('com_tags.tag', $data);
return $data;
}
/**
* Method to save the form data.
*
* @param array $data The form data.
*
* @return boolean True on success.
*
* @since 3.1
*/
public function save($data)
{
$dispatcher = JEventDispatcher::getInstance();
$table = $this->getTable();
$input = JFactory::getApplication()->input;
$pk = (!empty($data['id'])) ? $data['id'] : (int) $this->getState($this->getName() . '.id');
$isNew = true;
$context = $this->option . '.' . $this->name;
// Include the plugins for the save events.
JPluginHelper::importPlugin($this->events_map['save']);
// Load the row if saving an existing tag.
if ($pk > 0)
{
$table->load($pk);
$isNew = false;
}
// Set the new parent id if parent id not matched OR while New/Save as Copy .
if ($table->parent_id != $data['parent_id'] || $data['id'] == 0)
{
$table->setLocation($data['parent_id'], 'last-child');
}
if (isset($data['images']) && is_array($data['images']))
{
$registry = new Registry($data['images']);
$data['images'] = (string) $registry;
}
if (isset($data['urls']) && is_array($data['urls']))
{
$registry = new Registry($data['urls']);
$data['urls'] = (string) $registry;
}
// Alter the title for save as copy
if ($input->get('task') == 'save2copy')
{
list($title, $alias) = $this->generateNewTitle($data['parent_id'], $data['alias'], $data['title']);
$data['title'] = $title;
$data['alias'] = $alias;
}
// Bind the data.
if (!$table->bind($data))
{
$this->setError($table->getError());
return false;
}
// Bind the rules.
if (isset($data['rules']))
{
$rules = new JAccessRules($data['rules']);
$table->setRules($rules);
}
// Check the data.
if (!$table->check())
{
$this->setError($table->getError());
return false;
}
// Trigger the before save event.
$result = $dispatcher->trigger($this->event_before_save, array($context, &$table, $isNew));
if (in_array(false, $result, true))
{
$this->setError($table->getError());
return false;
}
// Store the data.
if (!$table->store())
{
$this->setError($table->getError());
return false;
}
// Trigger the after save event.
$dispatcher->trigger($this->event_after_save, array($context, &$table, $isNew));
// Rebuild the path for the tag:
if (!$table->rebuildPath($table->id))
{
$this->setError($table->getError());
return false;
}
// Rebuild the paths of the tag's children:
if (!$table->rebuild($table->id, $table->lft, $table->level, $table->path))
{
$this->setError($table->getError());
return false;
}
$this->setState($this->getName() . '.id', $table->id);
// Clear the cache
$this->cleanCache();
return true;
}
/**
* Method rebuild the entire nested set tree.
*
* @return boolean False on failure or error, true otherwise.
*
* @since 3.1
*/
public function rebuild()
{
// Get an instance of the table object.
$table = $this->getTable();
if (!$table->rebuild())
{
$this->setError($table->getError());
return false;
}
// Clear the cache
$this->cleanCache();
return true;
}
/**
* Method to save the reordered nested set tree.
* First we save the new order values in the lft values of the changed ids.
* Then we invoke the table rebuild to implement the new ordering.
*
* @param array $idArray An array of primary key ids.
* @param integer $lft_array The lft value
*
* @return boolean False on failure or error, True otherwise
*
* @since 3.1
*/
public function saveorder($idArray = null, $lft_array = null)
{
// Get an instance of the table object.
$table = $this->getTable();
if (!$table->saveorder($idArray, $lft_array))
{
$this->setError($table->getError());
return false;
}
// Clear the cache
$this->cleanCache();
return true;
}
/**
* Method to change the title & alias.
*
* @param integer $parent_id The id of the parent.
* @param string $alias The alias.
* @param string $title The title.
*
* @return array Contains the modified title and alias.
*
* @since 3.1
*/
protected function generateNewTitle($parent_id, $alias, $title)
{
// Alter the title & alias
$table = $this->getTable();
while ($table->load(array('alias' => $alias, 'parent_id' => $parent_id)))
{
$title = ($table->title != $title) ? $title : StringHelper::increment($title);
$alias = StringHelper::increment($alias, 'dash');
}
return array($title, $alias);
}
}