PageRenderTime 39ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/legacy/table/content.php

https://github.com/andergmartins/joomla-platform
PHP | 364 lines | 263 code | 22 blank | 79 comment | 23 complexity | 1cb6d274f79968e1242075ec80c967cf MD5 | raw file
  1. <?php
  2. /**
  3. * @package Joomla.Legacy
  4. * @subpackage Table
  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
  8. */
  9. defined('JPATH_PLATFORM') or die;
  10. /**
  11. * Content table
  12. *
  13. * @package Joomla.Legacy
  14. * @subpackage Table
  15. * @since 11.1
  16. */
  17. class JTableContent extends JTable
  18. {
  19. /**
  20. * Constructor
  21. *
  22. * @param JDatabaseDriver $db A database connector object
  23. *
  24. * @since 11.1
  25. */
  26. public function __construct($db)
  27. {
  28. parent::__construct('#__content', 'id', $db);
  29. }
  30. /**
  31. * Method to compute the default name of the asset.
  32. * The default name is in the form table_name.id
  33. * where id is the value of the primary key of the table.
  34. *
  35. * @return string
  36. *
  37. * @since 11.1
  38. */
  39. protected function _getAssetName()
  40. {
  41. $k = $this->_tbl_key;
  42. return 'com_content.article.' . (int) $this->$k;
  43. }
  44. /**
  45. * Method to return the title to use for the asset table.
  46. *
  47. * @return string
  48. *
  49. * @since 11.1
  50. */
  51. protected function _getAssetTitle()
  52. {
  53. return $this->title;
  54. }
  55. /**
  56. * Method to get the parent asset id for the record
  57. *
  58. * @param JTable $table A JTable object (optional) for the asset parent
  59. * @param integer $id The id (optional) of the content.
  60. *
  61. * @return integer
  62. *
  63. * @since 11.1
  64. */
  65. protected function _getAssetParentId($table = null, $id = null)
  66. {
  67. $assetId = null;
  68. // This is a article under a category.
  69. if ($this->catid)
  70. {
  71. // Build the query to get the asset id for the parent category.
  72. $query = $this->_db->getQuery(true);
  73. $query->select($this->_db->quoteName('asset_id'));
  74. $query->from($this->_db->quoteName('#__categories'));
  75. $query->where($this->_db->quoteName('id') . ' = ' . (int) $this->catid);
  76. // Get the asset id from the database.
  77. $this->_db->setQuery($query);
  78. if ($result = $this->_db->loadResult())
  79. {
  80. $assetId = (int) $result;
  81. }
  82. }
  83. // Return the asset id.
  84. if ($assetId)
  85. {
  86. return $assetId;
  87. }
  88. else
  89. {
  90. return parent::_getAssetParentId($table, $id);
  91. }
  92. }
  93. /**
  94. * Overloaded bind function
  95. *
  96. * @param array $array Named array
  97. * @param mixed $ignore An optional array or space separated list of properties
  98. * to ignore while binding.
  99. *
  100. * @return mixed Null if operation was satisfactory, otherwise returns an error string
  101. *
  102. * @see JTable::bind
  103. * @since 11.1
  104. */
  105. public function bind($array, $ignore = '')
  106. {
  107. // Search for the {readmore} tag and split the text up accordingly.
  108. if (isset($array['articletext']))
  109. {
  110. $pattern = '#<hr\s+id=("|\')system-readmore("|\')\s*\/*>#i';
  111. $tagPos = preg_match($pattern, $array['articletext']);
  112. if ($tagPos == 0)
  113. {
  114. $this->introtext = $array['articletext'];
  115. $this->fulltext = '';
  116. }
  117. else
  118. {
  119. list ($this->introtext, $this->fulltext) = preg_split($pattern, $array['articletext'], 2);
  120. }
  121. }
  122. if (isset($array['attribs']) && is_array($array['attribs']))
  123. {
  124. $registry = new JRegistry;
  125. $registry->loadArray($array['attribs']);
  126. $array['attribs'] = (string) $registry;
  127. }
  128. if (isset($array['metadata']) && is_array($array['metadata']))
  129. {
  130. $registry = new JRegistry;
  131. $registry->loadArray($array['metadata']);
  132. $array['metadata'] = (string) $registry;
  133. }
  134. // Bind the rules.
  135. if (isset($array['rules']) && is_array($array['rules']))
  136. {
  137. $rules = new JAccessRules($array['rules']);
  138. $this->setRules($rules);
  139. }
  140. return parent::bind($array, $ignore);
  141. }
  142. /**
  143. * Overloaded check function
  144. *
  145. * @return boolean True on success, false on failure
  146. *
  147. * @see JTable::check
  148. * @since 11.1
  149. */
  150. public function check()
  151. {
  152. if (trim($this->title) == '')
  153. {
  154. $this->setError(JText::_('COM_CONTENT_WARNING_PROVIDE_VALID_NAME'));
  155. return false;
  156. }
  157. if (trim($this->alias) == '')
  158. {
  159. $this->alias = $this->title;
  160. }
  161. $this->alias = JApplication::stringURLSafe($this->alias);
  162. if (trim(str_replace('-', '', $this->alias)) == '')
  163. {
  164. $this->alias = JFactory::getDate()->format('Y-m-d-H-i-s');
  165. }
  166. if (trim(str_replace('&nbsp;', '', $this->fulltext)) == '')
  167. {
  168. $this->fulltext = '';
  169. }
  170. // Check the publish down date is not earlier than publish up.
  171. if ($this->publish_down > $this->_db->getNullDate() && $this->publish_down < $this->publish_up)
  172. {
  173. // Swap the dates.
  174. $temp = $this->publish_up;
  175. $this->publish_up = $this->publish_down;
  176. $this->publish_down = $temp;
  177. }
  178. // Clean up keywords -- eliminate extra spaces between phrases
  179. // and cr (\r) and lf (\n) characters from string
  180. if (!empty($this->metakey))
  181. {
  182. // Only process if not empty
  183. // Array of characters to remove
  184. $bad_characters = array("\n", "\r", "\"", "<", ">");
  185. // Remove bad characters
  186. $after_clean = JString::str_ireplace($bad_characters, "", $this->metakey);
  187. // Create array using commas as delimiter
  188. $keys = explode(',', $after_clean);
  189. $clean_keys = array();
  190. foreach ($keys as $key)
  191. {
  192. if (trim($key))
  193. {
  194. // Ignore blank keywords
  195. $clean_keys[] = trim($key);
  196. }
  197. }
  198. // Put array back together delimited by ", "
  199. $this->metakey = implode(", ", $clean_keys);
  200. }
  201. return true;
  202. }
  203. /**
  204. * Overrides JTable::store to set modified data and user id.
  205. *
  206. * @param boolean $updateNulls True to update fields even if they are null.
  207. *
  208. * @return boolean True on success.
  209. *
  210. * @since 11.1
  211. */
  212. public function store($updateNulls = false)
  213. {
  214. $date = JFactory::getDate();
  215. $user = JFactory::getUser();
  216. if ($this->id)
  217. {
  218. // Existing item
  219. $this->modified = $date->toSql();
  220. $this->modified_by = $user->get('id');
  221. }
  222. else
  223. {
  224. // New article. An article created and created_by field can be set by the user,
  225. // so we don't touch either of these if they are set.
  226. if (!(int) $this->created)
  227. {
  228. $this->created = $date->toSql();
  229. }
  230. if (empty($this->created_by))
  231. {
  232. $this->created_by = $user->get('id');
  233. }
  234. }
  235. // Verify that the alias is unique
  236. $table = JTable::getInstance('Content', 'JTable');
  237. if ($table->load(array('alias' => $this->alias, 'catid' => $this->catid)) && ($table->id != $this->id || $this->id == 0))
  238. {
  239. $this->setError(JText::_('JLIB_DATABASE_ERROR_ARTICLE_UNIQUE_ALIAS'));
  240. return false;
  241. }
  242. return parent::store($updateNulls);
  243. }
  244. /**
  245. * Method to set the publishing state for a row or list of rows in the database
  246. * table. The method respects checked out rows by other users and will attempt
  247. * to checkin rows that it can after adjustments are made.
  248. *
  249. * @param mixed $pks An optional array of primary key values to update. If not set the instance property value is used.
  250. * @param integer $state The publishing state. eg. [0 = unpublished, 1 = published]
  251. * @param integer $userId The user id of the user performing the operation.
  252. *
  253. * @return boolean True on success.
  254. *
  255. * @since 11.1
  256. */
  257. public function publish($pks = null, $state = 1, $userId = 0)
  258. {
  259. $k = $this->_tbl_key;
  260. // Sanitize input.
  261. JArrayHelper::toInteger($pks);
  262. $userId = (int) $userId;
  263. $state = (int) $state;
  264. // If there are no primary keys set check to see if the instance key is set.
  265. if (empty($pks))
  266. {
  267. if ($this->$k)
  268. {
  269. $pks = array($this->$k);
  270. }
  271. // Nothing to set publishing state on, return false.
  272. else
  273. {
  274. $this->setError(JText::_('JLIB_DATABASE_ERROR_NO_ROWS_SELECTED'));
  275. return false;
  276. }
  277. }
  278. // Build the WHERE clause for the primary keys.
  279. $where = $k . '=' . implode(' OR ' . $k . '=', $pks);
  280. // Determine if there is checkin support for the table.
  281. if (!property_exists($this, 'checked_out') || !property_exists($this, 'checked_out_time'))
  282. {
  283. $checkin = '';
  284. }
  285. else
  286. {
  287. $checkin = ' AND (checked_out = 0 OR checked_out = ' . (int) $userId . ')';
  288. }
  289. // Get the JDatabaseQuery object
  290. $query = $this->_db->getQuery(true);
  291. // Update the publishing state for rows with the given primary keys.
  292. $query->update($this->_db->quoteName($this->_tbl));
  293. $query->set($this->_db->quoteName('state') . ' = ' . (int) $state);
  294. $query->where('(' . $where . ')' . $checkin);
  295. $this->_db->setQuery($query);
  296. try
  297. {
  298. $this->_db->execute();
  299. }
  300. catch (RuntimeException $e)
  301. {
  302. $this->setError($e->getMessage());
  303. return false;
  304. }
  305. // If checkin is supported and all rows were adjusted, check them in.
  306. if ($checkin && (count($pks) == $this->_db->getAffectedRows()))
  307. {
  308. // Checkin the rows.
  309. foreach ($pks as $pk)
  310. {
  311. $this->checkin($pk);
  312. }
  313. }
  314. // If the JTable instance value is in the list of primary keys that were set, set the instance.
  315. if (in_array($this->$k, $pks))
  316. {
  317. $this->state = $state;
  318. }
  319. $this->setError('');
  320. return true;
  321. }
  322. }