PageRenderTime 55ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

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

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