PageRenderTime 70ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/legacy/table/content.php

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