PageRenderTime 56ms CodeModel.GetById 8ms RepoModel.GetById 1ms app.codeStats 0ms

/components/com_weblinks/models/weblink.php

https://github.com/ponlue/abktours
PHP | 293 lines | 155 code | 26 blank | 112 comment | 22 complexity | 36cad1e10e0c5b7a599d23902bb59d14 MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: weblink.php 14401 2010-01-26 14:10:00Z louis $
  4. * @package Joomla
  5. * @subpackage Content
  6. * @copyright Copyright (C) 2005 - 2010 Open Source Matters. All rights reserved.
  7. * @license GNU/GPL, see LICENSE.php
  8. * Joomla! is free software. This version may have been modified pursuant to the
  9. * GNU General Public License, and as distributed it includes or is derivative
  10. * of works licensed under the GNU General Public License or other free or open
  11. * source software licenses. See COPYRIGHT.php for copyright notices and
  12. * details.
  13. */
  14. // Check to ensure this file is included in Joomla!
  15. defined('_JEXEC') or die( 'Restricted access' );
  16. jimport('joomla.application.component.model');
  17. /**
  18. * Weblinks Component Weblink Model
  19. *
  20. * @package Joomla
  21. * @subpackage Weblinks
  22. * @since 1.5
  23. */
  24. class WeblinksModelWeblink extends JModel
  25. {
  26. /**
  27. * Weblink id
  28. *
  29. * @var int
  30. */
  31. var $_id = null;
  32. /**
  33. * Weblink data
  34. *
  35. * @var array
  36. */
  37. var $_data = null;
  38. /**
  39. * Constructor
  40. *
  41. * @since 1.5
  42. */
  43. function __construct()
  44. {
  45. parent::__construct();
  46. $id = JRequest::getVar('id', 0, '', 'int');
  47. $this->setId((int)$id);
  48. }
  49. /**
  50. * Method to set the weblink identifier
  51. *
  52. * @access public
  53. * @param int Weblink identifier
  54. */
  55. function setId($id)
  56. {
  57. // Set weblink id and wipe data
  58. $this->_id = $id;
  59. $this->_data = null;
  60. }
  61. /**
  62. * Method to get a weblink
  63. *
  64. * @since 1.5
  65. */
  66. function &getData()
  67. {
  68. // Load the weblink data
  69. if ($this->_loadData())
  70. {
  71. // Initialize some variables
  72. $user = &JFactory::getUser();
  73. // Make sure the weblink is published
  74. if (!$this->_data->published) {
  75. JError::raiseError(404, JText::_("Resource Not Found"));
  76. return false;
  77. }
  78. // Check to see if the category is published
  79. if (!$this->_data->cat_pub) {
  80. JError::raiseError( 404, JText::_("Resource Not Found") );
  81. return;
  82. }
  83. // Check whether category access level allows access
  84. if ($this->_data->cat_access > $user->get('aid', 0)) {
  85. JError::raiseError( 403, JText::_('ALERTNOTAUTH') );
  86. return;
  87. }
  88. }
  89. else $this->_initData();
  90. return $this->_data;
  91. }
  92. /**
  93. * Method to increment the hit counter for the weblink
  94. *
  95. * @access public
  96. * @return boolean True on success
  97. * @since 1.5
  98. */
  99. function hit()
  100. {
  101. global $mainframe;
  102. if ($this->_id)
  103. {
  104. $weblink = & $this->getTable();
  105. $weblink->hit($this->_id);
  106. return true;
  107. }
  108. return false;
  109. }
  110. /**
  111. * Tests if weblink is checked out
  112. *
  113. * @access public
  114. * @param int A user id
  115. * @return boolean True if checked out
  116. * @since 1.5
  117. */
  118. function isCheckedOut( $uid=0 )
  119. {
  120. if ($this->_loadData())
  121. {
  122. if ($uid) {
  123. return ($this->_data->checked_out && $this->_data->checked_out != $uid);
  124. } else {
  125. return $this->_data->checked_out;
  126. }
  127. }
  128. }
  129. /**
  130. * Method to checkin/unlock the weblink
  131. *
  132. * @access public
  133. * @return boolean True on success
  134. * @since 1.5
  135. */
  136. function checkin()
  137. {
  138. if ($this->_id)
  139. {
  140. $weblink = & $this->getTable();
  141. if(! $weblink->checkin($this->_id)) {
  142. $this->setError($this->_db->getErrorMsg());
  143. return false;
  144. }
  145. return true;
  146. }
  147. return false;
  148. }
  149. /**
  150. * Method to checkout/lock the weblink
  151. *
  152. * @access public
  153. * @param int $uid User ID of the user checking the article out
  154. * @return boolean True on success
  155. * @since 1.5
  156. */
  157. function checkout($uid = null)
  158. {
  159. if ($this->_id)
  160. {
  161. // Make sure we have a user id to checkout the article with
  162. if (is_null($uid)) {
  163. $user =& JFactory::getUser();
  164. $uid = $user->get('id');
  165. }
  166. // Lets get to it and checkout the thing...
  167. $weblink = & $this->getTable();
  168. if(!$weblink->checkout($uid, $this->_id)) {
  169. $this->setError($this->_db->getErrorMsg());
  170. return false;
  171. }
  172. return true;
  173. }
  174. return false;
  175. }
  176. /**
  177. * Method to store the weblink
  178. *
  179. * @access public
  180. * @return boolean True on success
  181. * @since 1.5
  182. */
  183. function store($data)
  184. {
  185. $row =& $this->getTable();
  186. // Bind the form fields to the web link table
  187. if (!$row->bind($data)) {
  188. $this->setError($this->_db->getErrorMsg());
  189. return false;
  190. }
  191. // Create the timestamp for the date
  192. $row->date = gmdate('Y-m-d H:i:s');
  193. // if new item, order last in appropriate group
  194. if (!$row->id) {
  195. $where = 'catid = ' . (int) $row->catid ;
  196. $row->ordering = $row->getNextOrder( $where );
  197. }
  198. // Make sure the web link table is valid
  199. if (!$row->check()) {
  200. $this->setError($this->_db->getErrorMsg());
  201. return false;
  202. }
  203. // Store the web link table to the database
  204. if (!$row->store()) {
  205. $this->setError($this->_db->getErrorMsg());
  206. return false;
  207. }
  208. return true;
  209. }
  210. /**
  211. * Method to load content weblink data
  212. *
  213. * @access private
  214. * @return boolean True on success
  215. * @since 1.5
  216. */
  217. function _loadData()
  218. {
  219. // Lets load the content if it doesn't already exist
  220. if (empty($this->_data))
  221. {
  222. $query = 'SELECT w.*, cc.title AS category,' .
  223. ' cc.published AS cat_pub, cc.access AS cat_access'.
  224. ' FROM #__weblinks AS w' .
  225. ' LEFT JOIN #__categories AS cc ON cc.id = w.catid' .
  226. ' WHERE w.id = '. (int) $this->_id;
  227. $this->_db->setQuery($query);
  228. $this->_data = $this->_db->loadObject();
  229. return (boolean) $this->_data;
  230. }
  231. return true;
  232. }
  233. /**
  234. * Method to initialise the weblink data
  235. *
  236. * @access private
  237. * @return boolean True on success
  238. * @since 1.5
  239. */
  240. function _initData()
  241. {
  242. // Lets load the content if it doesn't already exist
  243. if (empty($this->_data))
  244. {
  245. $weblink = new stdClass();
  246. $weblink->id = 0;
  247. $weblink->catid = 0;
  248. $weblink->sid = 0;
  249. $weblink->title = null;
  250. $weblink->url = null;
  251. $weblink->description = null;
  252. $weblink->date = null;
  253. $weblink->hits = 0;
  254. $weblink->published = 0;
  255. $weblink->checked_out = 0;
  256. $weblink->checked_out_time = 0;
  257. $weblink->ordering = 0;
  258. $weblink->archived = 0;
  259. $weblink->approved = 0;
  260. $weblink->params = null;
  261. $weblink->category = null;
  262. $this->_data = $weblink;
  263. return (boolean) $this->_data;
  264. }
  265. return true;
  266. }
  267. }