PageRenderTime 107ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/joomla/database/table/content.php

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