PageRenderTime 37ms CodeModel.GetById 8ms RepoModel.GetById 1ms app.codeStats 0ms

/administrator/components/com_content/models/article.php

https://github.com/rietn/minima
PHP | 353 lines | 183 code | 41 blank | 129 comment | 32 complexity | ea0181b314871ceb31d29dc742da280a MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: article.php 21148 2011-04-14 17:30:08Z ian $
  4. * @package Joomla.Administrator
  5. * @subpackage com_content
  6. * @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE.txt
  8. */
  9. // No direct access
  10. defined('_JEXEC') or die;
  11. jimport('joomla.application.component.modeladmin');
  12. require_once JPATH_COMPONENT_ADMINISTRATOR.'/helpers/content.php';
  13. /**
  14. * Item Model for an Article.
  15. *
  16. * @package Joomla.Administrator
  17. * @subpackage com_content
  18. * @since 1.6
  19. */
  20. class ContentModelArticle extends JModelAdmin
  21. {
  22. /**
  23. * @var string The prefix to use with controller messages.
  24. * @since 1.6
  25. */
  26. protected $text_prefix = 'COM_CONTENT';
  27. /**
  28. * Method to test whether a record can be deleted.
  29. *
  30. * @param object $record A record object.
  31. *
  32. * @return boolean True if allowed to delete the record. Defaults to the permission set in the component.
  33. * @since 1.6
  34. */
  35. protected function canDelete($record)
  36. {
  37. if (!empty($record->id)) {
  38. if ($record->state != -2) {
  39. return ;
  40. }
  41. $user = JFactory::getUser();
  42. return $user->authorise('core.delete', 'com_content.article.'.(int) $record->id);
  43. }
  44. }
  45. /**
  46. * Method to test whether a record can have its state edited.
  47. *
  48. * @param object $record A record object.
  49. *
  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. // Check for existing article.
  57. if (!empty($record->id)) {
  58. return $user->authorise('core.edit.state', 'com_content.article.'.(int) $record->id);
  59. }
  60. // New article, so check against the category.
  61. else if (!empty($record->catid)) {
  62. return $user->authorise('core.edit.state', 'com_content.category.'.(int) $record->catid);
  63. }
  64. // Default to component settings if neither article nor category known.
  65. else {
  66. return parent::canEditState($record);
  67. }
  68. }
  69. /**
  70. * Prepare and sanitise the table data prior to saving.
  71. *
  72. * @param JTable A JTable object.
  73. *
  74. * @return void
  75. * @since 1.6
  76. */
  77. protected function prepareTable(&$table)
  78. {
  79. // Set the publish date to now
  80. if($table->state == 1 && intval($table->publish_up) == 0) {
  81. $table->publish_up = JFactory::getDate()->toMySQL();
  82. }
  83. // Increment the content version number.
  84. $table->version++;
  85. // Reorder the articles within the category so the new article is first
  86. if (empty($table->id)) {
  87. $table->reorder('catid = '.(int) $table->catid.' AND state >= 0');
  88. }
  89. }
  90. /**
  91. * Returns a Table object, always creating it.
  92. *
  93. * @param type The table type to instantiate
  94. * @param string A prefix for the table class name. Optional.
  95. * @param array Configuration array for model. Optional.
  96. *
  97. * @return JTable A database object
  98. */
  99. public function getTable($type = 'Content', $prefix = 'JTable', $config = array())
  100. {
  101. return JTable::getInstance($type, $prefix, $config);
  102. }
  103. /**
  104. * Method to get a single record.
  105. *
  106. * @param integer The id of the primary key.
  107. *
  108. * @return mixed Object on success, false on failure.
  109. */
  110. public function getItem($pk = null)
  111. {
  112. if ($item = parent::getItem($pk)) {
  113. // Convert the params field to an array.
  114. $registry = new JRegistry;
  115. $registry->loadJSON($item->attribs);
  116. $item->attribs = $registry->toArray();
  117. // Convert the params field to an array.
  118. $registry = new JRegistry;
  119. $registry->loadJSON($item->metadata);
  120. $item->metadata = $registry->toArray();
  121. $item->articletext = trim($item->fulltext) != '' ? $item->introtext . "<hr id=\"system-readmore\" />" . $item->fulltext : $item->introtext;
  122. }
  123. return $item;
  124. }
  125. /**
  126. * Method to get the record form.
  127. *
  128. * @param array $data Data for the form.
  129. * @param boolean $loadData True if the form is to load its own data (default case), false if not.
  130. *
  131. * @return mixed A JForm object on success, false on failure
  132. * @since 1.6
  133. */
  134. public function getForm($data = array(), $loadData = true)
  135. {
  136. // Get the form.
  137. $form = $this->loadForm('com_content.article', 'article', array('control' => 'jform', 'load_data' => $loadData));
  138. if (empty($form)) {
  139. return false;
  140. }
  141. // Determine correct permissions to check.
  142. if ($id = (int) $this->getState('article.id')) {
  143. // Existing record. Can only edit in selected categories.
  144. $form->setFieldAttribute('catid', 'action', 'core.edit');
  145. // Existing record. Can only edit own articles in selected categories.
  146. $form->setFieldAttribute('catid', 'action', 'core.edit.own');
  147. }
  148. else {
  149. // New record. Can only create in selected categories.
  150. $form->setFieldAttribute('catid', 'action', 'core.create');
  151. }
  152. // Modify the form based on Edit State access controls.
  153. if (!$this->canEditState((object) $data)) {
  154. // Disable fields for display.
  155. $form->setFieldAttribute('featured', 'disabled', 'true');
  156. $form->setFieldAttribute('ordering', 'disabled', 'true');
  157. $form->setFieldAttribute('publish_up', 'disabled', 'true');
  158. $form->setFieldAttribute('publish_down', 'disabled', 'true');
  159. $form->setFieldAttribute('state', 'disabled', 'true');
  160. // Disable fields while saving.
  161. // The controller has already verified this is an article you can edit.
  162. $form->setFieldAttribute('featured', 'filter', 'unset');
  163. $form->setFieldAttribute('ordering', 'filter', 'unset');
  164. $form->setFieldAttribute('publish_up', 'filter', 'unset');
  165. $form->setFieldAttribute('publish_down', 'filter', 'unset');
  166. $form->setFieldAttribute('state', 'filter', 'unset');
  167. }
  168. return $form;
  169. }
  170. /**
  171. * Method to get the data that should be injected in the form.
  172. *
  173. * @return mixed The data for the form.
  174. * @since 1.6
  175. */
  176. protected function loadFormData()
  177. {
  178. // Check the session for previously entered form data.
  179. $data = JFactory::getApplication()->getUserState('com_content.edit.article.data', array());
  180. if (empty($data)) {
  181. $data = $this->getItem();
  182. // Prime some default values.
  183. if ($this->getState('article.id') == 0) {
  184. $app = JFactory::getApplication();
  185. $data->set('catid', JRequest::getInt('catid', $app->getUserState('com_content.articles.filter.category_id')));
  186. }
  187. }
  188. return $data;
  189. }
  190. /**
  191. * Method to save the form data.
  192. *
  193. * @param array The form data.
  194. *
  195. * @return boolean True on success.
  196. * @since 1.6
  197. */
  198. public function save($data)
  199. {
  200. if (parent::save($data)) {
  201. if (isset($data['featured'])) {
  202. $this->featured($this->getState($this->getName().'.id'), $data['featured']);
  203. }
  204. return true;
  205. }
  206. return false;
  207. }
  208. /**
  209. * Method to toggle the featured setting of articles.
  210. *
  211. * @param array The ids of the items to toggle.
  212. * @param int The value to toggle to.
  213. *
  214. * @return boolean True on success.
  215. */
  216. public function featured($pks, $value = 0)
  217. {
  218. // Sanitize the ids.
  219. $pks = (array) $pks;
  220. JArrayHelper::toInteger($pks);
  221. if (empty($pks)) {
  222. $this->setError(JText::_('COM_CONTENT_NO_ITEM_SELECTED'));
  223. return false;
  224. }
  225. $table = $this->getTable('Featured', 'ContentTable');
  226. try {
  227. $db = $this->getDbo();
  228. $db->setQuery(
  229. 'UPDATE #__content AS a' .
  230. ' SET a.featured = '.(int) $value.
  231. ' WHERE a.id IN ('.implode(',', $pks).')'
  232. );
  233. if (!$db->query()) {
  234. throw new Exception($db->getErrorMsg());
  235. }
  236. if ((int)$value == 0) {
  237. // Adjust the mapping table.
  238. // Clear the existing features settings.
  239. $db->setQuery(
  240. 'DELETE FROM #__content_frontpage' .
  241. ' WHERE content_id IN ('.implode(',', $pks).')'
  242. );
  243. if (!$db->query()) {
  244. throw new Exception($db->getErrorMsg());
  245. }
  246. } else {
  247. // first, we find out which of our new featured articles are already featured.
  248. $query = $db->getQuery(true);
  249. $query->select('f.content_id');
  250. $query->from('#__content_frontpage AS f');
  251. $query->where('content_id IN ('.implode(',', $pks).')');
  252. //echo $query;
  253. $db->setQuery($query);
  254. if (!is_array($old_featured = $db->loadResultArray())) {
  255. throw new Exception($db->getErrorMsg());
  256. }
  257. // we diff the arrays to get a list of the articles that are newly featured
  258. $new_featured = array_diff($pks, $old_featured);
  259. // Featuring.
  260. $tuples = array();
  261. foreach ($new_featured as $pk) {
  262. $tuples[] = '('.$pk.', 0)';
  263. }
  264. if (count($tuples)) {
  265. $db->setQuery(
  266. 'INSERT INTO #__content_frontpage (`content_id`, `ordering`)' .
  267. ' VALUES '.implode(',', $tuples)
  268. );
  269. if (!$db->query()) {
  270. $this->setError($db->getErrorMsg());
  271. return false;
  272. }
  273. }
  274. }
  275. } catch (Exception $e) {
  276. $this->setError($e->getMessage());
  277. return false;
  278. }
  279. $table->reorder();
  280. $this->cleanCache();
  281. return true;
  282. }
  283. /**
  284. * A protected method to get a set of ordering conditions.
  285. *
  286. * @param object A record object.
  287. *
  288. * @return array An array of conditions to add to add to ordering queries.
  289. * @since 1.6
  290. */
  291. protected function getReorderConditions($table)
  292. {
  293. $condition = array();
  294. $condition[] = 'catid = '.(int) $table->catid;
  295. return $condition;
  296. }
  297. /**
  298. * Custom clean the cache of com_content and content modules
  299. *
  300. * @since 1.6
  301. */
  302. protected function cleanCache()
  303. {
  304. parent::cleanCache('com_content');
  305. parent::cleanCache('mod_articles_archive');
  306. parent::cleanCache('mod_articles_categories');
  307. parent::cleanCache('mod_articles_category');
  308. parent::cleanCache('mod_articles_latest');
  309. parent::cleanCache('mod_articles_news');
  310. parent::cleanCache('mod_articles_popular');
  311. }
  312. }