PageRenderTime 3314ms CodeModel.GetById 38ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/chola/database/table/content.php

https://github.com/cholalabs/CholaApps2.0
PHP | 341 lines | 253 code | 23 blank | 65 comment | 29 complexity | b3d722ada93ebda0cc472c2e9aed472b MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: content.php 20228 2011-01-10 00:52:54Z eddieajau $
  4. * @copyright Copyright (C) 2005 - 2011 Cholalabs Software LLP. All rights reserved.
  5. * @license GNU General Public License version 2 or later; see LICENSE.txt
  6. */
  7. // No direct access
  8. defined('JPATH_BASE') or die;
  9. jimport('chola.database.tableasset');
  10. /**
  11. * Content table
  12. *
  13. * @package Chola.Framework
  14. * @subpackage Table
  15. * @since 1.0
  16. */
  17. class JTableContent extends JTable
  18. {
  19. /**
  20. * @param database A database connector object
  21. * @since 1.0
  22. */
  23. function __construct(&$db)
  24. {
  25. parent::__construct('#__content', 'id', $db);
  26. }
  27. /**
  28. * Method to compute the default name of the asset.
  29. * The default name is in the form `table_name.id`
  30. * where id is the value of the primary key of the table.
  31. *
  32. * @return string
  33. * @since 1.6
  34. */
  35. protected function _getAssetName()
  36. {
  37. $k = $this->_tbl_key;
  38. return 'com_content.article.'.(int) $this->$k;
  39. }
  40. /**
  41. * Method to return the title to use for the asset table.
  42. *
  43. * @return string
  44. * @since 1.6
  45. */
  46. protected function _getAssetTitle()
  47. {
  48. return $this->title;
  49. }
  50. /**
  51. * Get the parent asset id for the record
  52. *
  53. * @return int
  54. * @since 1.6
  55. */
  56. protected function _getAssetParentId($table = null, $id = null)
  57. {
  58. // Initialise variables.
  59. $assetId = null;
  60. $db = $this->getDbo();
  61. // This is a article under a category.
  62. if ($this->catid) {
  63. // Build the query to get the asset id for the parent category.
  64. $query = $db->getQuery(true);
  65. $query->select('asset_id');
  66. $query->from('#__categories');
  67. $query->where('id = '.(int) $this->catid);
  68. // Get the asset id from the database.
  69. $this->_db->setQuery($query);
  70. if ($result = $this->_db->loadResult()) {
  71. $assetId = (int) $result;
  72. }
  73. }
  74. // Return the asset id.
  75. if ($assetId) {
  76. return $assetId;
  77. } else {
  78. return parent::_getAssetParentId($table, $id);
  79. }
  80. }
  81. /**
  82. * Overloaded bind function
  83. *
  84. * @param array $hash named array
  85. *
  86. * @return null|string null is operation was satisfactory, otherwise returns an error
  87. * @see JTable:bind
  88. * @since 1.5
  89. */
  90. public function bind($array, $ignore = '')
  91. {
  92. // Search for the {readmore} tag and split the text up accordingly.
  93. if (isset($array['articletext'])) {
  94. $pattern = '#<hr\s+id=("|\')system-readmore("|\')\s*\/*>#i';
  95. $tagPos = preg_match($pattern, $array['articletext']);
  96. if ($tagPos == 0) {
  97. $this->introtext = $array['articletext'];
  98. $this->fulltext = '';
  99. } else {
  100. list($this->introtext, $this->fulltext) = preg_split($pattern, $array['articletext'], 2);
  101. }
  102. }
  103. if (isset($array['attribs']) && is_array($array['attribs'])) {
  104. $registry = new JRegistry();
  105. $registry->loadArray($array['attribs']);
  106. $array['attribs'] = (string)$registry;
  107. }
  108. if (isset($array['metadata']) && is_array($array['metadata'])) {
  109. $registry = new JRegistry();
  110. $registry->loadArray($array['metadata']);
  111. $array['metadata'] = (string)$registry;
  112. }
  113. // Bind the rules.
  114. if (isset($array['rules']) && is_array($array['rules'])) {
  115. $rules = new JRules($array['rules']);
  116. $this->setRules($rules);
  117. }
  118. return parent::bind($array, $ignore);
  119. }
  120. /**
  121. * Overloaded check function
  122. *
  123. * @return boolean
  124. * @see JTable::check
  125. * @since 1.5
  126. */
  127. public function check()
  128. {
  129. if (trim($this->title) == '') {
  130. $this->setError(JText::_('COM_CONTENT_WARNING_PROVIDE_VALID_NAME'));
  131. return false;
  132. }
  133. if (trim($this->alias) == '') {
  134. $this->alias = $this->title;
  135. }
  136. $this->alias = JApplication::stringURLSafe($this->alias);
  137. if (trim(str_replace('-','',$this->alias)) == '') {
  138. $this->alias = JFactory::getDate()->format('Y-m-d-H-i-s');
  139. }
  140. if (trim(str_replace('&nbsp;', '', $this->fulltext)) == '') {
  141. $this->fulltext = '';
  142. }
  143. if (trim($this->introtext) == '' && trim($this->fulltext) == '') {
  144. $this->setError(JText::_('JGLOBAL_ARTICLE_MUST_HAVE_TEXT'));
  145. return false;
  146. }
  147. // Check the publish down date is not earlier than publish up.
  148. if (intval($this->publish_down) > 0 && $this->publish_down < $this->publish_up) {
  149. // Swap the dates.
  150. $temp = $this->publish_up;
  151. $this->publish_up = $this->publish_down;
  152. $this->publish_down = $temp;
  153. }
  154. // clean up keywords -- eliminate extra spaces between phrases
  155. // and cr (\r) and lf (\n) characters from string
  156. if (!empty($this->metakey)) {
  157. // only process if not empty
  158. $bad_characters = array("\n", "\r", "\"", "<", ">"); // array of characters to remove
  159. $after_clean = JString::str_ireplace($bad_characters, "", $this->metakey); // remove bad characters
  160. $keys = explode(',', $after_clean); // create array using commas as delimiter
  161. $clean_keys = array();
  162. foreach($keys as $key) {
  163. if (trim($key)) { // ignore blank keywords
  164. $clean_keys[] = trim($key);
  165. }
  166. }
  167. $this->metakey = implode(", ", $clean_keys); // put array back together delimited by ", "
  168. }
  169. return true;
  170. }
  171. /**
  172. * Overriden JTable::store to set modified data and user id.
  173. *
  174. * @param boolean True to update fields even if they are null.
  175. *
  176. * @return boolean True on success.
  177. * @since 1.6
  178. */
  179. public function store($updateNulls = false)
  180. {
  181. $date = JFactory::getDate();
  182. $user = JFactory::getUser();
  183. if ($this->id) {
  184. // Existing item
  185. $this->modified = $date->toMySQL();
  186. $this->modified_by = $user->get('id');
  187. } else {
  188. // New article. An article created and created_by field can be set by the user,
  189. // so we don't touch either of these if they are set.
  190. if (!intval($this->created)) {
  191. $this->created = $date->toMySQL();
  192. }
  193. if (empty($this->created_by)) {
  194. $this->created_by = $user->get('id');
  195. }
  196. }
  197. // Verify that the alias is unique
  198. $table = JTable::getInstance('Content','JTable');
  199. if ($table->load(array('alias'=>$this->alias,'catid'=>$this->catid)) && ($table->id != $this->id || $this->id==0)) {
  200. $this->setError(JText::_('JLIB_DATABASE_ERROR_ARTICLE_UNIQUE_ALIAS'));
  201. return false;
  202. }
  203. return parent::store($updateNulls);
  204. }
  205. /**
  206. * Method to set the publishing state for a row or list of rows in the database
  207. * table. The method respects checked out rows by other users and will attempt
  208. * to checkin rows that it can after adjustments are made.
  209. *
  210. * @param mixed An optional array of primary key values to update. If not
  211. * set the instance property value is used.
  212. * @param integer The publishing state. eg. [0 = unpublished, 1 = published]
  213. * @param integer The user id of the user performing the operation.
  214. *
  215. * @return boolean True on success.
  216. * @since 1.0.4
  217. */
  218. public function publish($pks = null, $state = 1, $userId = 0)
  219. {
  220. // Initialise variables.
  221. $k = $this->_tbl_key;
  222. // Sanitize input.
  223. JArrayHelper::toInteger($pks);
  224. $userId = (int) $userId;
  225. $state = (int) $state;
  226. // If there are no primary keys set check to see if the instance key is set.
  227. if (empty($pks)) {
  228. if ($this->$k) {
  229. $pks = array($this->$k);
  230. }
  231. // Nothing to set publishing state on, return false.
  232. else {
  233. $this->setError(JText::_('JLIB_DATABASE_ERROR_NO_ROWS_SELECTED'));
  234. return false;
  235. }
  236. }
  237. // Build the WHERE clause for the primary keys.
  238. $where = $k.'='.implode(' OR '.$k.'=', $pks);
  239. // Determine if there is checkin support for the table.
  240. if (!property_exists($this, 'checked_out') || !property_exists($this, 'checked_out_time')) {
  241. $checkin = '';
  242. } else {
  243. $checkin = ' AND (checked_out = 0 OR checked_out = '.(int) $userId.')';
  244. }
  245. // Update the publishing state for rows with the given primary keys.
  246. $this->_db->setQuery(
  247. 'UPDATE `'.$this->_tbl.'`' .
  248. ' SET `state` = '.(int) $state .
  249. ' WHERE ('.$where.')' .
  250. $checkin
  251. );
  252. $this->_db->query();
  253. // Check for a database error.
  254. if ($this->_db->getErrorNum()) {
  255. $this->setError($this->_db->getErrorMsg());
  256. return false;
  257. }
  258. // If checkin is supported and all rows were adjusted, check them in.
  259. if ($checkin && (count($pks) == $this->_db->getAffectedRows())) {
  260. // Checkin the rows.
  261. foreach($pks as $pk) {
  262. $this->checkin($pk);
  263. }
  264. }
  265. // If the JTable instance value is in the list of primary keys that were set, set the instance.
  266. if (in_array($this->$k, $pks)) {
  267. $this->state = $state;
  268. }
  269. $this->setError('');
  270. return true;
  271. }
  272. /**
  273. * Converts record to XML
  274. *
  275. * @param boolean Map foreign keys to text values
  276. * @since 1.5
  277. */
  278. function toXML($mapKeysToText=false)
  279. {
  280. $db = JFactory::getDbo();
  281. if ($mapKeysToText) {
  282. $query = 'SELECT name'
  283. . ' FROM #__categories'
  284. . ' WHERE id = '. (int) $this->catid
  285. ;
  286. $db->setQuery($query);
  287. $this->catid = $db->loadResult();
  288. $query = 'SELECT name'
  289. . ' FROM #__users'
  290. . ' WHERE id = ' . (int) $this->created_by
  291. ;
  292. $db->setQuery($query);
  293. $this->created_by = $db->loadResult();
  294. }
  295. return parent::toXML($mapKeysToText);
  296. }
  297. }