PageRenderTime 54ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/legacy/table/content.php

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