/administrator/components/com_weblinks/tables/weblink.php

https://github.com/joebushi/joomla · PHP · 269 lines · 116 code · 39 blank · 114 comment · 24 complexity · c93c902d9df858cf78c45c24a5fc9fcc MD5 · raw file

  1. <?php
  2. /**
  3. * @version $Id$
  4. * @copyright Copyright (C) 2005 - 2010 Open Source Matters, Inc. All rights reserved.
  5. * @license GNU General Public License version 2 or later; see LICENSE.txt
  6. */
  7. // No direct access
  8. defined('_JEXEC') or die;
  9. /**
  10. * Weblink Table class
  11. *
  12. * @package Joomla.Administrator
  13. * @subpackage com_weblinks
  14. * @since 1.5
  15. */
  16. class WeblinksTableWeblink extends JTable
  17. {
  18. /**
  19. * Primary Key
  20. *
  21. * @var int
  22. */
  23. public $id = null;
  24. /**
  25. * @var int
  26. */
  27. public $catid = null;
  28. /**
  29. * @var int
  30. */
  31. public $sid = null;
  32. /**
  33. * @var string
  34. */
  35. public $title = null;
  36. /**
  37. * @var string
  38. */
  39. public $alias = null;
  40. /**
  41. * @var string
  42. */
  43. public $url = null;
  44. /**
  45. * @var string
  46. */
  47. public $description = null;
  48. /**
  49. * @var datetime
  50. */
  51. public $date = null;
  52. /**
  53. * @var int
  54. */
  55. public $hits = null;
  56. /**
  57. * @var int
  58. */
  59. public $state = null;
  60. /**
  61. * @var boolean
  62. */
  63. public $checked_out = 0;
  64. /**
  65. * @var time
  66. */
  67. public $checked_out_time = 0;
  68. /**
  69. * @var int
  70. */
  71. public $ordering = null;
  72. /**
  73. * @var int
  74. */
  75. public $archived = null;
  76. /**
  77. * @var int
  78. */
  79. public $approved = null;
  80. /**
  81. * @var int
  82. */
  83. public $access = null;
  84. /**
  85. * @var string
  86. */
  87. public $params = null;
  88. /**
  89. * @var string
  90. */
  91. public $language = null;
  92. /**
  93. * Constructor
  94. *
  95. * @param JDatabase A database connector object
  96. */
  97. public function __construct(&$db)
  98. {
  99. parent::__construct('#__weblinks', 'id', $db);
  100. }
  101. /**
  102. * Overload the store method for the Weblinks table.
  103. *
  104. * @param boolean $updateNulls Toggle whether null values should be updated.
  105. * @return boolean True on success, false on failure.
  106. * @since 1.6
  107. */
  108. public function store($updateNulls = false)
  109. {
  110. // Transform the params field
  111. if (is_array($this->params))
  112. {
  113. $registry = new JRegistry();
  114. $registry->loadArray($this->params);
  115. $this->params = $registry->toString();
  116. }
  117. // Attempt to store the user data.
  118. return parent::store($updateNulls);
  119. }
  120. /**
  121. * Overloaded check method to ensure data integrity.
  122. *
  123. * @return boolean True on success.
  124. */
  125. public function check()
  126. {
  127. if (JFilterInput::checkAttribute(array ('href', $this->url))) {
  128. $this->setError(JText::_('Please provide a valid URL'));
  129. return false;
  130. }
  131. /** check for valid name */
  132. if (trim($this->title) == '') {
  133. $this->setError(JText::_('Your Weblink must contain a title.'));
  134. return false;
  135. }
  136. // check for http, https, ftp on webpage
  137. if ((stripos($this->url, 'http://') === false)
  138. && (stripos($this->url, 'https://') === false)
  139. && (stripos($this->url, 'ftp://') === false))
  140. {
  141. $this->url = 'http://'.$this->url;
  142. }
  143. /** check for existing name */
  144. $query = 'SELECT id FROM #__weblinks WHERE title = '.$this->_db->Quote($this->title).' AND catid = '.(int) $this->catid;
  145. $this->_db->setQuery($query);
  146. $xid = intval($this->_db->loadResult());
  147. if ($xid && $xid != intval($this->id)) {
  148. $this->setError(JText::sprintf('WARNNAMETRYAGAIN', JText::_('Web Link')));
  149. return false;
  150. }
  151. if (empty($this->alias)) {
  152. $this->alias = $this->title;
  153. }
  154. $this->alias = JApplication::stringURLSafe($this->alias);
  155. if (trim(str_replace('-','',$this->alias)) == '') {
  156. $this->alias = JFactory::getDate()->toFormat("%Y-%m-%d-%H-%M-%S");
  157. }
  158. return true;
  159. }
  160. /**
  161. * Method to set the publishing state for a row or list of rows in the database
  162. * table. The method respects checked out rows by other users and will attempt
  163. * to checkin rows that it can after adjustments are made.
  164. *
  165. * @param mixed An optional array of primary key values to update. If not
  166. * set the instance property value is used.
  167. * @param integer The publishing state. eg. [0 = unpublished, 1 = published]
  168. * @param integer The user id of the user performing the operation.
  169. * @return boolean True on success.
  170. * @since 1.0.4
  171. */
  172. public function publish($pks = null, $state = 1, $userId = 0)
  173. {
  174. // Initialise variables.
  175. $k = $this->_tbl_key;
  176. // Sanitize input.
  177. JArrayHelper::toInteger($pks);
  178. $userId = (int) $userId;
  179. $state = (int) $state;
  180. // If there are no primary keys set check to see if the instance key is set.
  181. if (empty($pks))
  182. {
  183. if ($this->$k) {
  184. $pks = array($this->$k);
  185. }
  186. // Nothing to set publishing state on, return false.
  187. else {
  188. $this->setError(JText::_('No_Rows_Selected'));
  189. return false;
  190. }
  191. }
  192. // Build the WHERE clause for the primary keys.
  193. $where = $k.'='.implode(' OR '.$k.'=', $pks);
  194. // Determine if there is checkin support for the table.
  195. if (!property_exists($this, 'checked_out') || !property_exists($this, 'checked_out_time')) {
  196. $checkin = '';
  197. }
  198. else {
  199. $checkin = ' AND (checked_out = 0 OR checked_out = '.(int) $userId.')';
  200. }
  201. // Update the publishing state for rows with the given primary keys.
  202. $this->_db->setQuery(
  203. 'UPDATE `'.$this->_tbl.'`' .
  204. ' SET `state` = '.(int) $state .
  205. ' WHERE ('.$where.')' .
  206. $checkin
  207. );
  208. $this->_db->query();
  209. // Check for a database error.
  210. if ($this->_db->getErrorNum()) {
  211. $this->setError($this->_db->getErrorMsg());
  212. return false;
  213. }
  214. // If checkin is supported and all rows were adjusted, check them in.
  215. if ($checkin && (count($pks) == $this->_db->getAffectedRows()))
  216. {
  217. // Checkin the rows.
  218. foreach($pks as $pk)
  219. {
  220. $this->checkin($pk);
  221. }
  222. }
  223. // If the JTable instance value is in the list of primary keys that were set, set the instance.
  224. if (in_array($this->$k, $pks)) {
  225. $this->state = $state;
  226. }
  227. $this->setError('');
  228. return true;
  229. }
  230. }