/administrator/components/com_weblinks/models/weblink.php

https://bitbucket.org/eternaware/joomus · PHP · 236 lines · 119 code · 26 blank · 91 comment · 19 complexity · 057a2f8fd0fc4dcb61424726ef45c698 MD5 · raw file

  1. <?php
  2. /**
  3. * @package Joomla.Administrator
  4. * @subpackage com_weblinks
  5. *
  6. * @copyright Copyright (C) 2005 - 2012 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. /**
  11. * Weblinks model.
  12. *
  13. * @package Joomla.Administrator
  14. * @subpackage com_weblinks
  15. * @since 1.5
  16. */
  17. class WeblinksModelWeblink extends JModelAdmin
  18. {
  19. /**
  20. * @var string The prefix to use with controller messages.
  21. * @since 1.6
  22. */
  23. protected $text_prefix = 'COM_WEBLINKS';
  24. /**
  25. * Method to test whether a record can be deleted.
  26. *
  27. * @param object A record object.
  28. * @return boolean True if allowed to delete the record. Defaults to the permission set in the component.
  29. * @since 1.6
  30. */
  31. protected function canDelete($record)
  32. {
  33. if (!empty($record->id)) {
  34. if ($record->state != -2) {
  35. return;
  36. }
  37. $user = JFactory::getUser();
  38. if ($record->catid) {
  39. return $user->authorise('core.delete', 'com_weblinks.category.'.(int) $record->catid);
  40. }
  41. else {
  42. return parent::canDelete($record);
  43. }
  44. }
  45. }
  46. /**
  47. * Method to test whether a record can have its state changed.
  48. *
  49. * @param object A record object.
  50. * @return boolean True if allowed to change the state of the record. Defaults to the permission set in the component.
  51. * @since 1.6
  52. */
  53. protected function canEditState($record)
  54. {
  55. $user = JFactory::getUser();
  56. if (!empty($record->catid)) {
  57. return $user->authorise('core.edit.state', 'com_weblinks.category.'.(int) $record->catid);
  58. }
  59. else {
  60. return parent::canEditState($record);
  61. }
  62. }
  63. /**
  64. * Returns a reference to the a Table object, always creating it.
  65. *
  66. * @param type The table type to instantiate
  67. * @param string A prefix for the table class name. Optional.
  68. * @param array Configuration array for model. Optional.
  69. * @return JTable A database object
  70. * @since 1.6
  71. */
  72. public function getTable($type = 'Weblink', $prefix = 'WeblinksTable', $config = array())
  73. {
  74. return JTable::getInstance($type, $prefix, $config);
  75. }
  76. /**
  77. * Method to get the record form.
  78. *
  79. * @param array $data An optional array of data for the form to interogate.
  80. * @param boolean $loadData True if the form is to load its own data (default case), false if not.
  81. * @return JForm A JForm object on success, false on failure
  82. * @since 1.6
  83. */
  84. public function getForm($data = array(), $loadData = true)
  85. {
  86. $app = JFactory::getApplication();
  87. // Get the form.
  88. $form = $this->loadForm('com_weblinks.weblink', 'weblink', array('control' => 'jform', 'load_data' => $loadData));
  89. if (empty($form)) {
  90. return false;
  91. }
  92. // Determine correct permissions to check.
  93. if ($this->getState('weblink.id')) {
  94. // Existing record. Can only edit in selected categories.
  95. $form->setFieldAttribute('catid', 'action', 'core.edit');
  96. } else {
  97. // New record. Can only create in selected categories.
  98. $form->setFieldAttribute('catid', 'action', 'core.create');
  99. }
  100. // Modify the form based on access controls.
  101. if (!$this->canEditState((object) $data)) {
  102. // Disable fields for display.
  103. $form->setFieldAttribute('ordering', 'disabled', 'true');
  104. $form->setFieldAttribute('state', 'disabled', 'true');
  105. $form->setFieldAttribute('publish_up', 'disabled', 'true');
  106. $form->setFieldAttribute('publish_down', 'disabled', 'true');
  107. // Disable fields while saving.
  108. // The controller has already verified this is a record you can edit.
  109. $form->setFieldAttribute('ordering', 'filter', 'unset');
  110. $form->setFieldAttribute('state', 'filter', 'unset');
  111. $form->setFieldAttribute('publish_up', 'filter', 'unset');
  112. $form->setFieldAttribute('publish_down', 'filter', 'unset');
  113. }
  114. return $form;
  115. }
  116. /**
  117. * Method to get the data that should be injected in the form.
  118. *
  119. * @return mixed The data for the form.
  120. * @since 1.6
  121. */
  122. protected function loadFormData()
  123. {
  124. // Check the session for previously entered form data.
  125. $data = JFactory::getApplication()->getUserState('com_weblinks.edit.weblink.data', array());
  126. if (empty($data)) {
  127. $data = $this->getItem();
  128. // Prime some default values.
  129. if ($this->getState('weblink.id') == 0)
  130. {
  131. $app = JFactory::getApplication();
  132. $data->set('catid', $app->input->get('catid', $app->getUserState('com_weblinks.weblinks.filter.category_id'), 'int'));
  133. }
  134. }
  135. return $data;
  136. }
  137. /**
  138. * Method to get a single record.
  139. *
  140. * @param integer The id of the primary key.
  141. *
  142. * @return mixed Object on success, false on failure.
  143. * @since 1.6
  144. */
  145. public function getItem($pk = null)
  146. {
  147. if ($item = parent::getItem($pk))
  148. {
  149. // Convert the params field to an array.
  150. $registry = new JRegistry;
  151. $registry->loadString($item->metadata);
  152. $item->metadata = $registry->toArray();
  153. }
  154. if ($item = parent::getItem($pk))
  155. {
  156. // Convert the images field to an array.
  157. $registry = new JRegistry;
  158. $registry->loadString($item->images);
  159. $item->images = $registry->toArray();
  160. }
  161. return $item;
  162. }
  163. /**
  164. * Prepare and sanitise the table prior to saving.
  165. *
  166. * @since 1.6
  167. */
  168. protected function prepareTable($table)
  169. {
  170. $date = JFactory::getDate();
  171. $user = JFactory::getUser();
  172. $table->title = htmlspecialchars_decode($table->title, ENT_QUOTES);
  173. $table->alias = JApplication::stringURLSafe($table->alias);
  174. if (empty($table->alias)) {
  175. $table->alias = JApplication::stringURLSafe($table->title);
  176. }
  177. if (empty($table->id))
  178. {
  179. // Set the values
  180. // Set ordering to the last item if not set
  181. if (empty($table->ordering)) {
  182. $db = JFactory::getDbo();
  183. $db->setQuery('SELECT MAX(ordering) FROM #__weblinks');
  184. $max = $db->loadResult();
  185. $table->ordering = $max + 1;
  186. }
  187. else
  188. {
  189. // Set the values
  190. $table->modified = $date->toSql();
  191. $table->modified_by = $user->get('id');
  192. }
  193. // Increment the content version number.
  194. $table->version++;
  195. }
  196. }
  197. /**
  198. * A protected method to get a set of ordering conditions.
  199. *
  200. * @param object A record object.
  201. * @return array An array of conditions to add to add to ordering queries.
  202. * @since 1.6
  203. */
  204. protected function getReorderConditions($table)
  205. {
  206. $condition = array();
  207. $condition[] = 'catid = '.(int) $table->catid;
  208. return $condition;
  209. }
  210. }