PageRenderTime 503ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/src/libraries/src/Table/Content.php

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