PageRenderTime 54ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/src/administrator/components/com_tags/models/tag.php

https://bitbucket.org/ke2083/transfans.co.uk-website
PHP | 444 lines | 215 code | 70 blank | 159 comment | 32 complexity | 1f8e0ea1a47d20fc0d7df84079a757d7 MD5 | raw file
  1. <?php
  2. /**
  3. * @package Joomla.Administrator
  4. * @subpackage com_tags
  5. *
  6. * @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE.txt
  8. */
  9. defined('_JEXEC') or die;
  10. use Joomla\Registry\Registry;
  11. use Joomla\String\StringHelper;
  12. /**
  13. * Tags Component Tag Model
  14. *
  15. * @since 3.1
  16. */
  17. class TagsModelTag extends JModelAdmin
  18. {
  19. /**
  20. * @var string The prefix to use with controller messages.
  21. * @since 3.1
  22. */
  23. protected $text_prefix = 'COM_TAGS';
  24. /**
  25. * @var string The type alias for this content type.
  26. * @since 3.2
  27. */
  28. public $typeAlias = 'com_tags.tag';
  29. /**
  30. * Allowed batch commands
  31. *
  32. * @var array
  33. * @since 3.7.0
  34. */
  35. protected $batch_commands = array(
  36. 'assetgroup_id' => 'batchAccess',
  37. 'language_id' => 'batchLanguage',
  38. );
  39. /**
  40. * Method to test whether a record can be deleted.
  41. *
  42. * @param object $record A record object.
  43. *
  44. * @return boolean True if allowed to delete the record. Defaults to the permission set in the component.
  45. *
  46. * @since 3.1
  47. */
  48. protected function canDelete($record)
  49. {
  50. if (!empty($record->id))
  51. {
  52. if ($record->published != -2)
  53. {
  54. return false;
  55. }
  56. return parent::canDelete($record);
  57. }
  58. }
  59. /**
  60. * Method to test whether a record can have its state changed.
  61. *
  62. * @param object $record A record object.
  63. *
  64. * @return boolean True if allowed to change the state of the record. Defaults to the permission set in the component.
  65. *
  66. * @since 3.1
  67. */
  68. protected function canEditState($record)
  69. {
  70. return parent::canEditState($record);
  71. }
  72. /**
  73. * Method to get a table object, load it if necessary.
  74. *
  75. * @param string $type The table name. Optional.
  76. * @param string $prefix The class prefix. Optional.
  77. * @param array $config Configuration array for model. Optional.
  78. *
  79. * @return JTable A JTable object
  80. *
  81. * @since 3.1
  82. */
  83. public function getTable($type = 'Tag', $prefix = 'TagsTable', $config = array())
  84. {
  85. return JTable::getInstance($type, $prefix, $config);
  86. }
  87. /**
  88. * Auto-populate the model state.
  89. *
  90. * @note Calling getState in this method will result in recursion.
  91. *
  92. * @return void
  93. *
  94. * @since 3.1
  95. */
  96. protected function populateState()
  97. {
  98. $app = JFactory::getApplication('administrator');
  99. $parentId = $app->input->getInt('parent_id');
  100. $this->setState('tag.parent_id', $parentId);
  101. // Load the User state.
  102. $pk = $app->input->getInt('id');
  103. $this->setState($this->getName() . '.id', $pk);
  104. // Load the parameters.
  105. $params = JComponentHelper::getParams('com_tags');
  106. $this->setState('params', $params);
  107. }
  108. /**
  109. * Method to get a tag.
  110. *
  111. * @param integer $pk An optional id of the object to get, otherwise the id from the model state is used.
  112. *
  113. * @return mixed Tag data object on success, false on failure.
  114. *
  115. * @since 3.1
  116. */
  117. public function getItem($pk = null)
  118. {
  119. if ($result = parent::getItem($pk))
  120. {
  121. // Prime required properties.
  122. if (empty($result->id))
  123. {
  124. $result->parent_id = $this->getState('tag.parent_id');
  125. }
  126. // Convert the metadata field to an array.
  127. $registry = new Registry($result->metadata);
  128. $result->metadata = $registry->toArray();
  129. // Convert the images field to an array.
  130. $registry = new Registry($result->images);
  131. $result->images = $registry->toArray();
  132. // Convert the urls field to an array.
  133. $registry = new Registry($result->urls);
  134. $result->urls = $registry->toArray();
  135. // Convert the created and modified dates to local user time for display in the form.
  136. $tz = new DateTimeZone(JFactory::getApplication()->get('offset'));
  137. if ((int) $result->created_time)
  138. {
  139. $date = new JDate($result->created_time);
  140. $date->setTimezone($tz);
  141. $result->created_time = $date->toSql(true);
  142. }
  143. else
  144. {
  145. $result->created_time = null;
  146. }
  147. if ((int) $result->modified_time)
  148. {
  149. $date = new JDate($result->modified_time);
  150. $date->setTimezone($tz);
  151. $result->modified_time = $date->toSql(true);
  152. }
  153. else
  154. {
  155. $result->modified_time = null;
  156. }
  157. }
  158. return $result;
  159. }
  160. /**
  161. * Method to get the row form.
  162. *
  163. * @param array $data Data for the form.
  164. * @param boolean $loadData True if the form is to load its own data (default case), false if not.
  165. *
  166. * @return mixed A JForm object on success, false on failure
  167. *
  168. * @since 3.1
  169. */
  170. public function getForm($data = array(), $loadData = true)
  171. {
  172. $jinput = JFactory::getApplication()->input;
  173. // Get the form.
  174. $form = $this->loadForm('com_tags.tag', 'tag', array('control' => 'jform', 'load_data' => $loadData));
  175. if (empty($form))
  176. {
  177. return false;
  178. }
  179. $user = JFactory::getUser();
  180. if (!$user->authorise('core.edit.state', 'com_tags' . $jinput->get('id')))
  181. {
  182. // Disable fields for display.
  183. $form->setFieldAttribute('ordering', 'disabled', 'true');
  184. $form->setFieldAttribute('published', 'disabled', 'true');
  185. // Disable fields while saving.
  186. // The controller has already verified this is a record you can edit.
  187. $form->setFieldAttribute('ordering', 'filter', 'unset');
  188. $form->setFieldAttribute('published', 'filter', 'unset');
  189. }
  190. return $form;
  191. }
  192. /**
  193. * Method to get the data that should be injected in the form.
  194. *
  195. * @return mixed The data for the form.
  196. *
  197. * @since 3.1
  198. */
  199. protected function loadFormData()
  200. {
  201. // Check the session for previously entered form data.
  202. $data = JFactory::getApplication()->getUserState('com_tags.edit.tag.data', array());
  203. if (empty($data))
  204. {
  205. $data = $this->getItem();
  206. }
  207. $this->preprocessData('com_tags.tag', $data);
  208. return $data;
  209. }
  210. /**
  211. * Method to save the form data.
  212. *
  213. * @param array $data The form data.
  214. *
  215. * @return boolean True on success.
  216. *
  217. * @since 3.1
  218. */
  219. public function save($data)
  220. {
  221. $dispatcher = JEventDispatcher::getInstance();
  222. $table = $this->getTable();
  223. $input = JFactory::getApplication()->input;
  224. $pk = (!empty($data['id'])) ? $data['id'] : (int) $this->getState($this->getName() . '.id');
  225. $isNew = true;
  226. $context = $this->option . '.' . $this->name;
  227. // Include the plugins for the save events.
  228. JPluginHelper::importPlugin($this->events_map['save']);
  229. // Load the row if saving an existing tag.
  230. if ($pk > 0)
  231. {
  232. $table->load($pk);
  233. $isNew = false;
  234. }
  235. // Set the new parent id if parent id not matched OR while New/Save as Copy .
  236. if ($table->parent_id != $data['parent_id'] || $data['id'] == 0)
  237. {
  238. $table->setLocation($data['parent_id'], 'last-child');
  239. }
  240. if (isset($data['images']) && is_array($data['images']))
  241. {
  242. $registry = new Registry($data['images']);
  243. $data['images'] = (string) $registry;
  244. }
  245. if (isset($data['urls']) && is_array($data['urls']))
  246. {
  247. $registry = new Registry($data['urls']);
  248. $data['urls'] = (string) $registry;
  249. }
  250. // Alter the title for save as copy
  251. if ($input->get('task') == 'save2copy')
  252. {
  253. list($title, $alias) = $this->generateNewTitle($data['parent_id'], $data['alias'], $data['title']);
  254. $data['title'] = $title;
  255. $data['alias'] = $alias;
  256. }
  257. // Bind the data.
  258. if (!$table->bind($data))
  259. {
  260. $this->setError($table->getError());
  261. return false;
  262. }
  263. // Bind the rules.
  264. if (isset($data['rules']))
  265. {
  266. $rules = new JAccessRules($data['rules']);
  267. $table->setRules($rules);
  268. }
  269. // Check the data.
  270. if (!$table->check())
  271. {
  272. $this->setError($table->getError());
  273. return false;
  274. }
  275. // Trigger the before save event.
  276. $result = $dispatcher->trigger($this->event_before_save, array($context, &$table, $isNew));
  277. if (in_array(false, $result, true))
  278. {
  279. $this->setError($table->getError());
  280. return false;
  281. }
  282. // Store the data.
  283. if (!$table->store())
  284. {
  285. $this->setError($table->getError());
  286. return false;
  287. }
  288. // Trigger the after save event.
  289. $dispatcher->trigger($this->event_after_save, array($context, &$table, $isNew));
  290. // Rebuild the path for the tag:
  291. if (!$table->rebuildPath($table->id))
  292. {
  293. $this->setError($table->getError());
  294. return false;
  295. }
  296. // Rebuild the paths of the tag's children:
  297. if (!$table->rebuild($table->id, $table->lft, $table->level, $table->path))
  298. {
  299. $this->setError($table->getError());
  300. return false;
  301. }
  302. $this->setState($this->getName() . '.id', $table->id);
  303. // Clear the cache
  304. $this->cleanCache();
  305. return true;
  306. }
  307. /**
  308. * Method rebuild the entire nested set tree.
  309. *
  310. * @return boolean False on failure or error, true otherwise.
  311. *
  312. * @since 3.1
  313. */
  314. public function rebuild()
  315. {
  316. // Get an instance of the table object.
  317. $table = $this->getTable();
  318. if (!$table->rebuild())
  319. {
  320. $this->setError($table->getError());
  321. return false;
  322. }
  323. // Clear the cache
  324. $this->cleanCache();
  325. return true;
  326. }
  327. /**
  328. * Method to save the reordered nested set tree.
  329. * First we save the new order values in the lft values of the changed ids.
  330. * Then we invoke the table rebuild to implement the new ordering.
  331. *
  332. * @param array $idArray An array of primary key ids.
  333. * @param integer $lft_array The lft value
  334. *
  335. * @return boolean False on failure or error, True otherwise
  336. *
  337. * @since 3.1
  338. */
  339. public function saveorder($idArray = null, $lft_array = null)
  340. {
  341. // Get an instance of the table object.
  342. $table = $this->getTable();
  343. if (!$table->saveorder($idArray, $lft_array))
  344. {
  345. $this->setError($table->getError());
  346. return false;
  347. }
  348. // Clear the cache
  349. $this->cleanCache();
  350. return true;
  351. }
  352. /**
  353. * Method to change the title & alias.
  354. *
  355. * @param integer $parent_id The id of the parent.
  356. * @param string $alias The alias.
  357. * @param string $title The title.
  358. *
  359. * @return array Contains the modified title and alias.
  360. *
  361. * @since 3.1
  362. */
  363. protected function generateNewTitle($parent_id, $alias, $title)
  364. {
  365. // Alter the title & alias
  366. $table = $this->getTable();
  367. while ($table->load(array('alias' => $alias, 'parent_id' => $parent_id)))
  368. {
  369. $title = ($table->title != $title) ? $title : StringHelper::increment($title);
  370. $alias = StringHelper::increment($alias, 'dash');
  371. }
  372. return array($title, $alias);
  373. }
  374. }