10 defined(
'JPATH_PLATFORM') or die;
27 protected $text_prefix = null;
35 protected $event_after_delete = null;
43 protected $event_after_save = null;
51 protected $event_before_delete = null;
59 protected $event_before_save = null;
67 protected $event_change_state = null;
77 public function __construct($config = array())
79 parent::__construct($config);
81 if (isset($config[
'event_after_delete']))
83 $this->event_after_delete = $config[
'event_after_delete'];
85 elseif (empty($this->event_after_delete))
87 $this->event_after_delete =
'onContentAfterDelete';
90 if (isset($config[
'event_after_save']))
92 $this->event_after_save = $config[
'event_after_save'];
94 elseif (empty($this->event_after_save))
96 $this->event_after_save =
'onContentAfterSave';
99 if (isset($config[
'event_before_delete']))
101 $this->event_before_delete = $config[
'event_before_delete'];
103 elseif (empty($this->event_before_delete))
105 $this->event_before_delete =
'onContentBeforeDelete';
108 if (isset($config[
'event_before_save']))
110 $this->event_before_save = $config[
'event_before_save'];
112 elseif (empty($this->event_before_save))
114 $this->event_before_save =
'onContentBeforeSave';
117 if (isset($config[
'event_change_state']))
119 $this->event_change_state = $config[
'event_change_state'];
121 elseif (empty($this->event_change_state))
123 $this->event_change_state =
'onContentChangeState';
127 if (isset($config[
'text_prefix']))
129 $this->text_prefix = strtoupper($config[
'text_prefix']);
131 elseif (empty($this->text_prefix))
133 $this->text_prefix = strtoupper($this->option);
148 public function batch($commands, $pks, $contexts)
151 $pks = array_unique($pks);
155 if (array_search(0, $pks,
true))
157 unset($pks[array_search(0, $pks,
true)]);
162 $this->setError(
JText::_(
'JGLOBAL_NO_ITEM_SELECTED'));
171 $this->table = $this->getTable();
172 $this->tableClassName = get_class($this->table);
173 $this->contentType =
new JUcmType;
174 $this->type = $this->contentType->getTypeByTable($this->tableClassName);
175 $this->batchSet =
true;
177 if ($this->type ==
false)
179 $type =
new JUcmType;
180 $this->type = $type->getTypeByAlias($this->typeAlias);
183 if ($this->type ===
false)
185 $type =
new JUcmType;
186 $this->type = $type->getTypeByAlias($this->typeAlias);
187 $typeAlias = $this->type->type_alias;
191 $typeAlias = $this->type->type_alias;
193 $this->tagsObserver = $this->table->getObserverOfClass(
'JTableObserverTags');
195 if (!empty($commands[
'category_id']))
201 $result = $this->batchCopy($commands[
'category_id'], $pks, $contexts);
203 if (is_array($result))
212 elseif ($cmd ==
'm' && !$this->batchMove($commands[
'category_id'], $pks, $contexts))
220 if (!empty($commands[
'assetgroup_id']))
222 if (!$this->batchAccess($commands[
'assetgroup_id'], $pks, $contexts))
230 if (!empty($commands[
'language_id']))
232 if (!$this->batchLanguage($commands[
'language_id'], $pks, $contexts))
240 if (!empty($commands[
'tag']))
242 if (!$this->batchTag($commands[
'tag'], $pks, $contexts))
252 $this->setError(
JText::_(
'JLIB_APPLICATION_ERROR_INSUFFICIENT_BATCH_INFORMATION'));
273 protected function batchAccess($value, $pks, $contexts)
275 if (!$this->batchSet)
279 $this->table = $this->getTable();
280 $this->tableClassName = get_class($this->table);
281 $this->contentType =
new JUcmType;
282 $this->type = $this->contentType->getTypeByTable($this->tableClassName);
285 foreach ($pks as $pk)
287 if ($this->user->authorise(
'core.edit', $contexts[$pk]))
289 $this->table->reset();
290 $this->table->load($pk);
291 $this->table->access = (int) $value;
293 static::createTagsHelper($this->tagsObserver, $this->type, $pk, $this->typeAlias, $this->table);
295 if (!$this->table->store())
297 $this->setError($table->getError());
304 $this->setError(
JText::_(
'JLIB_APPLICATION_ERROR_BATCH_CANNOT_EDIT'));
327 protected function batchCopy($value, $pks, $contexts)
329 if (!$this->batchSet)
333 $this->table = $this->getTable();
334 $this->tableClassName = get_class($this->table);
335 $this->contentType =
new JUcmType;
336 $this->type = $this->contentType->getTypeByTable($this->tableClassName);
341 $categoryId = $value;
343 if (!static::checkCategoryId($categoryId))
352 $pk = array_shift($pks);
354 $this->table->reset();
357 if (!$this->table->load($pk))
359 if ($error = $this->table->getError())
362 $this->setError($error);
369 $this->setError(
JText::sprintf(
'JLIB_APPLICATION_ERROR_BATCH_MOVE_ROW_NOT_FOUND', $pk));
374 static::generateTitle($categoryId, $this->table);
377 $this->table->id = 0;
380 $this->table->catid = $categoryId;
386 if (!$this->table->check())
388 $this->setError($this->table->getError());
393 static::createTagsHelper($this->tagsObserver, $this->type, $pk, $this->typeAlias, $this->table);
396 if (!$this->table->store())
398 $this->setError($table->getError());
404 $newId = $this->table->get(
'id');
407 $newIds[$i] = $newId;
428 protected function batchLanguage($value, $pks, $contexts)
430 if (!$this->batchSet)
434 $this->table = $this->getTable();
435 $this->tableClassName = get_class($this->table);
436 $this->contentType =
new JUcmType;
437 $this->type = $this->contentType->getTypeByTable($this->tableClassName);
440 foreach ($pks as $pk)
442 if ($this->user->authorise(
'core.edit', $contexts[$pk]))
444 $this->table->reset();
445 $this->table->load($pk);
446 $this->table->language = $value;
448 static::createTagsHelper($this->tagsObserver, $this->type, $pk, $this->typeAlias, $this->table);
450 if (!$this->table->store())
452 $this->setError($this->table->getError());
459 $this->setError(
JText::_(
'JLIB_APPLICATION_ERROR_BATCH_CANNOT_EDIT'));
482 protected function batchMove($value, $pks, $contexts)
484 if (!$this->batchSet)
488 $this->table = $this->getTable();
489 $this->tableClassName = get_class($this->table);
490 $this->contentType =
new JUcmType;
491 $this->type = $this->contentType->getTypeByTable($this->tableClassName);
494 $categoryId = (int) $value;
496 if (!static::checkCategoryId($categoryId))
502 foreach ($pks as $pk)
504 if (!$this->user->authorise(
'core.edit', $contexts[$pk]))
506 $this->setError(
JText::_(
'JLIB_APPLICATION_ERROR_BATCH_CANNOT_EDIT'));
512 if (!$this->table->load($pk))
514 if ($error = $this->table->getError())
517 $this->setError($error);
524 $this->setError(
JText::sprintf(
'JLIB_APPLICATION_ERROR_BATCH_MOVE_ROW_NOT_FOUND', $pk));
530 $this->table->catid = $categoryId;
533 if (!$this->table->check())
535 $this->setError($this->table->getError());
540 static::createTagsHelper($this->tagsObserver, $this->type, $pk, $this->typeAlias, $this->table);
543 if (!$this->table->store())
545 $this->setError($this->table->getError());
568 protected function batchTag($value, $pks, $contexts)
572 $table = $this->getTable();
574 foreach ($pks as $pk)
576 if ($user->authorise(
'core.edit', $contexts[$pk]))
580 $tags = array($value);
585 $tagsObserver = $table->getObserverOfClass(
'JTableObserverTags');
586 $result = $tagsObserver->setNewTags($tags,
false);
590 $this->setError($table->getError());
597 $this->setError(
JText::_(
'JLIB_APPLICATION_ERROR_BATCH_CANNOT_EDIT'));
618 protected function canDelete($record)
622 return $user->authorise(
'core.delete', $this->option);
634 protected function canEditState($record)
638 return $user->authorise(
'core.edit.state', $this->option);
650 public function checkin($pks = array())
653 $table = $this->getTable();
658 $pks = array((
int) $this->getState($this->getName() .
'.id'));
662 foreach ($pks as $pk)
664 if ($table->load($pk))
667 if ($table->checked_out > 0)
669 if (!parent::checkin($pk))
678 $this->setError($table->getError());
696 public function checkout($pk = null)
698 $pk = (!empty($pk)) ? $pk : (
int) $this->getState($this->getName() .
'.id');
700 return parent::checkout($pk);
712 public function delete(&$pks)
716 $table = $this->getTable();
719 JPluginHelper::importPlugin(
'content');
722 foreach ($pks as $i => $pk)
725 if ($table->load($pk))
728 if ($this->canDelete($table))
731 $context = $this->option .
'.' . $this->name;
734 $result = $dispatcher->trigger($this->event_before_delete, array($context, $table));
736 if (in_array(
false, $result,
true))
738 $this->setError($table->getError());
742 if (!$table->delete($pk))
744 $this->setError($table->getError());
749 $dispatcher->trigger($this->event_after_delete, array($context, $table));
757 $error = $this->getError();
773 $this->setError($table->getError());
795 protected function generateNewTitle($category_id, $alias, $title)
798 $table = $this->getTable();
799 while ($table->load(array(
'alias' => $alias,
'catid' => $category_id)))
805 return array($title, $alias);
817 public function getItem($pk = null)
819 $pk = (!empty($pk)) ? $pk : (
int) $this->getState($this->getName() .
'.id');
820 $table = $this->getTable();
825 $return = $table->load($pk);
828 if ($return ===
false && $table->getError())
830 $this->setError($table->getError());
836 $properties = $table->getProperties(1);
839 if (property_exists($item,
'params'))
843 $item->params = $registry->toArray();
858 protected function getReorderConditions($table)
870 protected function populateState()
872 $table = $this->getTable();
873 $key = $table->getKeyName();
877 $this->setState($this->getName() .
'.id', $pk);
880 $value = JComponentHelper::getParams($this->option);
881 $this->setState(
'params', $value);
893 protected function prepareTable($table)
908 public function publish(&$pks, $value = 1)
912 $table = $this->getTable();
916 JPluginHelper::importPlugin(
'content');
919 foreach ($pks as $i => $pk)
923 if ($table->load($pk))
925 if (!$this->canEditState($table))
937 if (!$table->publish($pks, $value, $user->get(
'id')))
939 $this->setError($table->getError());
944 $context = $this->option .
'.' . $this->name;
947 $result = $dispatcher->trigger($this->event_change_state, array($context, $pks, $value));
949 if (in_array(
false, $result,
true))
951 $this->setError($table->getError());
975 public function reorder($pks, $delta = 0)
977 $table = $this->getTable();
983 foreach ($pks as $i => $pk)
987 if ($table->load($pk) && $this->checkout($pk))
990 if (!$this->canEditState($table))
1000 $where = $this->getReorderConditions($table);
1002 if (!$table->move($delta, $where))
1004 $this->setError($table->getError());
1009 $this->checkin($pk);
1013 $this->setError($table->getError());
1019 if ($allowed ===
false && empty($pks))
1025 if ($result ==
true)
1027 $this->cleanCache();
1042 public function save($data)
1045 $table = $this->getTable();
1047 if ((!empty($data[
'tags']) && $data[
'tags'][0] !=
''))
1049 $table->newTags = $data[
'tags'];
1052 $key = $table->getKeyName();
1053 $pk = (!empty($data[$key])) ? $data[$key] : (
int) $this->getState($this->getName() .
'.id');
1057 JPluginHelper::importPlugin(
'content');
1070 if (!$table->bind($data))
1072 $this->setError($table->getError());
1078 $this->prepareTable($table);
1081 if (!$table->check())
1083 $this->setError($table->getError());
1088 $result = $dispatcher->trigger($this->event_before_save, array($this->option .
'.' . $this->name, $table, $isNew));
1090 if (in_array(
false, $result,
true))
1092 $this->setError($table->getError());
1097 if (!$table->store())
1099 $this->setError($table->getError());
1104 $this->cleanCache();
1107 $dispatcher->trigger($this->event_after_save, array($this->option .
'.' . $this->name, $table, $isNew));
1109 catch (Exception $e)
1111 $this->setError($e->getMessage());
1116 $pkName = $table->getKeyName();
1118 if (isset($table->$pkName))
1120 $this->setState($this->getName() .
'.id', $table->$pkName);
1122 $this->setState($this->getName() .
'.new', $isNew);
1137 public function saveorder($pks = null, $order = null)
1139 $table = $this->getTable();
1140 $tableClassName = get_class($table);
1141 $contentType =
new JUcmType;
1142 $type = $contentType->getTypeByTable($tableClassName);
1143 $typeAlias = $type->type_alias;
1144 $tagsObserver = $table->getObserverOfClass(
'JTableObserverTags');
1145 $conditions = array();
1153 foreach ($pks as $i => $pk)
1155 $table->load((
int) $pk);
1158 if (!$this->canEditState($table))
1164 elseif ($table->ordering != $order[$i])
1166 $table->ordering = $order[$i];
1168 $this->createTagsHelper($tagsObserver, $type, $pk, $typeAlias, $table);
1170 if (!$table->store())
1172 $this->setError($table->getError());
1177 $condition = $this->getReorderConditions($table);
1180 foreach ($conditions as $cond)
1182 if ($cond[1] == $condition)
1191 $key = $table->getKeyName();
1192 $conditions[] = array($table->$key, $condition);
1198 foreach ($conditions as $cond)
1200 $table->load($cond[0]);
1201 $table->reorder($cond[1]);
1205 $this->cleanCache();
1223 public function createTagsHelper($tagsObserver, $type, $pk, $typeAlias, $table)
1225 if (!empty($tagsObserver) && !empty($type))
1227 $table->tagsHelper =
new JHelperTags();
1228 $table->tagsHelper->typeAlias = $typeAlias;
1229 $table->tagsHelper->tags = explode(
',', $table->tagsHelper->getTagIds($pk, $typeAlias));
1242 protected function checkCategoryId($categoryId)
1249 if (!$categoryTable->load($categoryId))
1251 if ($error = $categoryTable->getError())
1254 $this->setError($error);
1259 $this->setError(
JText::_(
'JLIB_APPLICATION_ERROR_BATCH_MOVE_CATEGORY_NOT_FOUND'));
1266 if (empty($categoryId))
1268 $this->setError(
JText::_(
'JLIB_APPLICATION_ERROR_BATCH_MOVE_CATEGORY_NOT_FOUND'));
1275 if (!$this->user->authorise(
'core.create', $extension .
'.category.' . $categoryId))
1277 $this->setError(
JText::_(
'JLIB_APPLICATION_ERROR_BATCH_CANNOT_CREATE'));
1296 public function generateTitle($categoryId, $table)
1299 $data = $this->generateNewTitle($categoryId, $table->alias, $table->title);
1300 $table->title = $data[
'0'];
1301 $table->alias = $data[
'1'];