PageRenderTime 176ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

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