PageRenderTime 52ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/joomla/libraries/legacy/table/content.php

https://gitlab.com/ricardosanchez/prueba
PHP | 348 lines | 227 code | 33 blank | 88 comment | 29 complexity | e938e15f4b5857ff7102c6bc1fcb269e MD5 | raw file
  1. <?php
  2. /**
  3. * @package Joomla.Legacy
  4. * @subpackage Table
  5. *
  6. * @copyright Copyright (C) 2005 - 2015 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. use Joomla\Registry\Registry;
  11. /**
  12. * Content table
  13. *
  14. * @since 11.1
  15. * @deprecated Class will be removed upon completion of transition to UCM
  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(JDatabaseDriver $db)
  27. {
  28. parent::__construct('#__content', 'id', $db);
  29. JTableObserverTags::createObserver($this, array('typeAlias' => 'com_content.article'));
  30. JTableObserverContenthistory::createObserver($this, array('typeAlias' => 'com_content.article'));
  31. // Set the alias since the column is called state
  32. $this->setColumnAlias('published', 'state');
  33. }
  34. /**
  35. * Method to compute the default name of the asset.
  36. * The default name is in the form table_name.id
  37. * where id is the value of the primary key of the table.
  38. *
  39. * @return string
  40. *
  41. * @since 11.1
  42. */
  43. protected function _getAssetName()
  44. {
  45. $k = $this->_tbl_key;
  46. return 'com_content.article.' . (int) $this->$k;
  47. }
  48. /**
  49. * Method to return the title to use for the asset table.
  50. *
  51. * @return string
  52. *
  53. * @since 11.1
  54. */
  55. protected function _getAssetTitle()
  56. {
  57. return $this->title;
  58. }
  59. /**
  60. * Method to get the parent asset id for the record
  61. *
  62. * @param JTable $table A JTable object (optional) for the asset parent
  63. * @param integer $id The id (optional) of the content.
  64. *
  65. * @return integer
  66. *
  67. * @since 11.1
  68. */
  69. protected function _getAssetParentId(JTable $table = null, $id = null)
  70. {
  71. $assetId = null;
  72. // This is a article under a category.
  73. if ($this->catid)
  74. {
  75. // Build the query to get the asset id for the parent category.
  76. $query = $this->_db->getQuery(true)
  77. ->select($this->_db->quoteName('asset_id'))
  78. ->from($this->_db->quoteName('#__categories'))
  79. ->where($this->_db->quoteName('id') . ' = ' . (int) $this->catid);
  80. // Get the asset id from the database.
  81. $this->_db->setQuery($query);
  82. if ($result = $this->_db->loadResult())
  83. {
  84. $assetId = (int) $result;
  85. }
  86. }
  87. // Return the asset id.
  88. if ($assetId)
  89. {
  90. return $assetId;
  91. }
  92. else
  93. {
  94. return parent::_getAssetParentId($table, $id);
  95. }
  96. }
  97. /**
  98. * Overloaded bind function
  99. *
  100. * @param array $array Named array
  101. * @param mixed $ignore An optional array or space separated list of properties
  102. * to ignore while binding.
  103. *
  104. * @return mixed Null if operation was satisfactory, otherwise returns an error string
  105. *
  106. * @see JTable::bind()
  107. * @since 11.1
  108. */
  109. public function bind($array, $ignore = '')
  110. {
  111. // Search for the {readmore} tag and split the text up accordingly.
  112. if (isset($array['articletext']))
  113. {
  114. $pattern = '#<hr\s+id=("|\')system-readmore("|\')\s*\/*>#i';
  115. $tagPos = preg_match($pattern, $array['articletext']);
  116. if ($tagPos == 0)
  117. {
  118. $this->introtext = $array['articletext'];
  119. $this->fulltext = '';
  120. }
  121. else
  122. {
  123. list ($this->introtext, $this->fulltext) = preg_split($pattern, $array['articletext'], 2);
  124. }
  125. }
  126. if (isset($array['attribs']) && is_array($array['attribs']))
  127. {
  128. $registry = new Registry;
  129. $registry->loadArray($array['attribs']);
  130. $array['attribs'] = (string) $registry;
  131. }
  132. if (isset($array['metadata']) && is_array($array['metadata']))
  133. {
  134. $registry = new Registry;
  135. $registry->loadArray($array['metadata']);
  136. $array['metadata'] = (string) $registry;
  137. }
  138. // Bind the rules.
  139. if (isset($array['rules']) && is_array($array['rules']))
  140. {
  141. $rules = new JAccessRules($array['rules']);
  142. $this->setRules($rules);
  143. }
  144. return parent::bind($array, $ignore);
  145. }
  146. /**
  147. * Overloaded check function
  148. *
  149. * @return boolean True on success, false on failure
  150. *
  151. * @see JTable::check()
  152. * @since 11.1
  153. */
  154. public function check()
  155. {
  156. if (trim($this->title) == '')
  157. {
  158. $this->setError(JText::_('COM_CONTENT_WARNING_PROVIDE_VALID_NAME'));
  159. return false;
  160. }
  161. if (trim($this->alias) == '')
  162. {
  163. $this->alias = $this->title;
  164. }
  165. $this->alias = JApplicationHelper::stringURLSafe($this->alias);
  166. if (trim(str_replace('-', '', $this->alias)) == '')
  167. {
  168. $this->alias = JFactory::getDate()->format('Y-m-d-H-i-s');
  169. }
  170. if (trim(str_replace('&nbsp;', '', $this->fulltext)) == '')
  171. {
  172. $this->fulltext = '';
  173. }
  174. /**
  175. * Ensure any new items have compulsory fields set. This is needed for things like
  176. * frontend editing where we don't show all the fields or using some kind of API
  177. */
  178. if (!$this->id)
  179. {
  180. // Images can be an empty json string
  181. if (!isset($this->images))
  182. {
  183. $this->images = '{}';
  184. }
  185. // URLs can be an empty json string
  186. if (!isset($this->urls))
  187. {
  188. $this->urls = '{}';
  189. }
  190. // Attributes (article params) can be an empty json string
  191. if (!isset($this->attribs))
  192. {
  193. $this->attribs = '{}';
  194. }
  195. // Metadata can be an empty json string
  196. if (!isset($this->metadata))
  197. {
  198. $this->metadata = '{}';
  199. }
  200. // If we don't have any access rules set at this point just use an empty JAccessRules class
  201. if (!$this->getRules())
  202. {
  203. $rules = $this->getDefaultAssetValues('com_content');
  204. $this->setRules($rules);
  205. }
  206. }
  207. // Check the publish down date is not earlier than publish up.
  208. if ($this->publish_down > $this->_db->getNullDate() && $this->publish_down < $this->publish_up)
  209. {
  210. // Swap the dates.
  211. $temp = $this->publish_up;
  212. $this->publish_up = $this->publish_down;
  213. $this->publish_down = $temp;
  214. }
  215. // Clean up keywords -- eliminate extra spaces between phrases
  216. // and cr (\r) and lf (\n) characters from string
  217. if (!empty($this->metakey))
  218. {
  219. // Only process if not empty
  220. // Array of characters to remove
  221. $bad_characters = array("\n", "\r", "\"", "<", ">");
  222. // Remove bad characters
  223. $after_clean = JString::str_ireplace($bad_characters, "", $this->metakey);
  224. // Create array using commas as delimiter
  225. $keys = explode(',', $after_clean);
  226. $clean_keys = array();
  227. foreach ($keys as $key)
  228. {
  229. if (trim($key))
  230. {
  231. // Ignore blank keywords
  232. $clean_keys[] = trim($key);
  233. }
  234. }
  235. // Put array back together delimited by ", "
  236. $this->metakey = implode(", ", $clean_keys);
  237. }
  238. return true;
  239. }
  240. /**
  241. * Gets the default asset values for a component.
  242. *
  243. * @param $string $component The component asset name to search for
  244. *
  245. * @return JAccessRules The JAccessRules object for the asset
  246. */
  247. protected function getDefaultAssetValues($component)
  248. {
  249. // Need to find the asset id by the name of the component.
  250. $db = JFactory::getDbo();
  251. $query = $db->getQuery(true)
  252. ->select($db->quoteName('id'))
  253. ->from($db->quoteName('#__assets'))
  254. ->where($db->quoteName('name') . ' = ' . $db->quote($component));
  255. $db->setQuery($query);
  256. $assetId = (int) $db->loadResult();
  257. return JAccess::getAssetRules($assetId);
  258. }
  259. /**
  260. * Overrides JTable::store to set modified data and user id.
  261. *
  262. * @param boolean $updateNulls True to update fields even if they are null.
  263. *
  264. * @return boolean True on success.
  265. *
  266. * @since 11.1
  267. */
  268. public function store($updateNulls = false)
  269. {
  270. $date = JFactory::getDate();
  271. $user = JFactory::getUser();
  272. $this->modified = $date->toSql();
  273. if ($this->id)
  274. {
  275. // Existing item
  276. $this->modified_by = $user->get('id');
  277. }
  278. else
  279. {
  280. // New article. An article created and created_by field can be set by the user,
  281. // so we don't touch either of these if they are set.
  282. if (!(int) $this->created)
  283. {
  284. $this->created = $date->toSql();
  285. }
  286. if (empty($this->created_by))
  287. {
  288. $this->created_by = $user->get('id');
  289. }
  290. }
  291. // Verify that the alias is unique
  292. $table = JTable::getInstance('Content', 'JTable', array('dbo', $this->getDbo()));
  293. if ($table->load(array('alias' => $this->alias, 'catid' => $this->catid)) && ($table->id != $this->id || $this->id == 0))
  294. {
  295. $this->setError(JText::_('JLIB_DATABASE_ERROR_ARTICLE_UNIQUE_ALIAS'));
  296. return false;
  297. }
  298. return parent::store($updateNulls);
  299. }
  300. }