[ Index ]

PHP Cross Reference of Joomla 1.5.26 DE

title

Body

[close]

/administrator/components/com_weblinks/models/ -> weblink.php (source)

   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  
  15  // Check to ensure this file is included in Joomla!
  16  defined('_JEXEC') or die( 'Restricted access' );
  17  
  18  jimport('joomla.application.component.model');
  19  
  20  /**
  21   * Weblinks Component Weblink Model
  22   *
  23   * @package        Joomla
  24   * @subpackage    Weblinks
  25   * @since 1.5
  26   */
  27  class WeblinksModelWeblink extends JModel
  28  {
  29      /**
  30       * Weblink id
  31       *
  32       * @var int
  33       */
  34      var $_id = null;
  35  
  36      /**
  37       * Weblink data
  38       *
  39       * @var array
  40       */
  41      var $_data = null;
  42  
  43      /**
  44       * Constructor
  45       *
  46       * @since 1.5
  47       */
  48  	function __construct()
  49      {
  50          parent::__construct();
  51  
  52          $array = JRequest::getVar('cid', array(0), '', 'array');
  53          $edit    = JRequest::getVar('edit',true);
  54          if($edit)
  55              $this->setId((int)$array[0]);
  56      }
  57  
  58      /**
  59       * Method to set the weblink identifier
  60       *
  61       * @access    public
  62       * @param    int Weblink identifier
  63       */
  64  	function setId($id)
  65      {
  66          // Set weblink id and wipe data
  67          $this->_id        = $id;
  68          $this->_data    = null;
  69      }
  70  
  71      /**
  72       * Method to get a weblink
  73       *
  74       * @since 1.5
  75       */
  76      function &getData()
  77      {
  78          // Load the weblink data
  79          if ($this->_loadData())
  80          {
  81              // Initialize some variables
  82              $user = &JFactory::getUser();
  83  
  84              // Check to see if the category is published
  85              if (!$this->_data->cat_pub) {
  86                  JError::raiseError( 404, JText::_("Resource Not Found") );
  87                  return;
  88              }
  89  
  90              // Check whether category access level allows access
  91              if ($this->_data->cat_access > $user->get('aid', 0)) {
  92                  JError::raiseError( 403, JText::_('ALERTNOTAUTH') );
  93                  return;
  94              }
  95          }
  96          else  $this->_initData();
  97  
  98          return $this->_data;
  99      }
 100  
 101      /**
 102       * Tests if weblink is checked out
 103       *
 104       * @access    public
 105       * @param    int    A user id
 106       * @return    boolean    True if checked out
 107       * @since    1.5
 108       */
 109  	function isCheckedOut( $uid=0 )
 110      {
 111          if ($this->_loadData())
 112          {
 113              if ($uid) {
 114                  return ($this->_data->checked_out && $this->_data->checked_out != $uid);
 115              } else {
 116                  return $this->_data->checked_out;
 117              }
 118          }
 119      }
 120  
 121      /**
 122       * Method to checkin/unlock the weblink
 123       *
 124       * @access    public
 125       * @return    boolean    True on success
 126       * @since    1.5
 127       */
 128  	function checkin()
 129      {
 130          if ($this->_id)
 131          {
 132              $weblink = & $this->getTable();
 133              if(! $weblink->checkin($this->_id)) {
 134                  $this->setError($this->_db->getErrorMsg());
 135                  return false;
 136              }
 137          }
 138          return false;
 139      }
 140  
 141      /**
 142       * Method to checkout/lock the weblink
 143       *
 144       * @access    public
 145       * @param    int    $uid    User ID of the user checking the article out
 146       * @return    boolean    True on success
 147       * @since    1.5
 148       */
 149  	function checkout($uid = null)
 150      {
 151          if ($this->_id)
 152          {
 153              // Make sure we have a user id to checkout the article with
 154              if (is_null($uid)) {
 155                  $user    =& JFactory::getUser();
 156                  $uid    = $user->get('id');
 157              }
 158              // Lets get to it and checkout the thing...
 159              $weblink = & $this->getTable();
 160              if(!$weblink->checkout($uid, $this->_id)) {
 161                  $this->setError($this->_db->getErrorMsg());
 162                  return false;
 163              }
 164  
 165              return true;
 166          }
 167          return false;
 168      }
 169  
 170      /**
 171       * Method to store the weblink
 172       *
 173       * @access    public
 174       * @return    boolean    True on success
 175       * @since    1.5
 176       */
 177  	function store($data)
 178      {
 179          $row =& $this->getTable();
 180  
 181          // Bind the form fields to the web link table
 182          if (!$row->bind($data)) {
 183              $this->setError($this->_db->getErrorMsg());
 184              return false;
 185          }
 186  
 187          // Create the timestamp for the date
 188          $row->date = gmdate('Y-m-d H:i:s');
 189  
 190          // if new item, order last in appropriate group
 191          if (!$row->id) {
 192              $where = 'catid = ' . (int) $row->catid ;
 193              $row->ordering = $row->getNextOrder( $where );
 194          }
 195  
 196          // Make sure the web link table is valid
 197          if (!$row->check()) {
 198              $this->setError($this->_db->getErrorMsg());
 199              return false;
 200          }
 201  
 202          // Store the web link table to the database
 203          if (!$row->store()) {
 204              $this->setError($this->_db->getErrorMsg());
 205              return false;
 206          }
 207  
 208          return true;
 209      }
 210  
 211      /**
 212       * Method to remove a weblink
 213       *
 214       * @access    public
 215       * @return    boolean    True on success
 216       * @since    1.5
 217       */
 218  	function delete($cid = array())
 219      {
 220          $result = false;
 221  
 222          if (count( $cid ))
 223          {
 224              JArrayHelper::toInteger($cid);
 225              $cids = implode( ',', $cid );
 226              $query = 'DELETE FROM #__weblinks'
 227                  . ' WHERE id IN ( '.$cids.' )';
 228              $this->_db->setQuery( $query );
 229              if(!$this->_db->query()) {
 230                  $this->setError($this->_db->getErrorMsg());
 231                  return false;
 232              }
 233          }
 234  
 235          return true;
 236      }
 237  
 238      /**
 239       * Method to (un)publish a weblink
 240       *
 241       * @access    public
 242       * @return    boolean    True on success
 243       * @since    1.5
 244       */
 245  	function publish($cid = array(), $publish = 1)
 246      {
 247          $user     =& JFactory::getUser();
 248  
 249          if (count( $cid ))
 250          {
 251              JArrayHelper::toInteger($cid);
 252              $cids = implode( ',', $cid );
 253  
 254              $query = 'UPDATE #__weblinks'
 255                  . ' SET published = '.(int) $publish
 256                  . ' WHERE id IN ( '.$cids.' )'
 257                  . ' AND ( checked_out = 0 OR ( checked_out = '.(int) $user->get('id').' ) )'
 258              ;
 259              $this->_db->setQuery( $query );
 260              if (!$this->_db->query()) {
 261                  $this->setError($this->_db->getErrorMsg());
 262                  return false;
 263              }
 264          }
 265  
 266          return true;
 267      }
 268  
 269      /**
 270       * Method to move a weblink
 271       *
 272       * @access    public
 273       * @return    boolean    True on success
 274       * @since    1.5
 275       */
 276  	function move($direction)
 277      {
 278          $row =& $this->getTable();
 279          if (!$row->load($this->_id)) {
 280              $this->setError($this->_db->getErrorMsg());
 281              return false;
 282          }
 283  
 284          if (!$row->move( $direction, ' catid = '.(int) $row->catid.' AND published >= 0 ' )) {
 285              $this->setError($this->_db->getErrorMsg());
 286              return false;
 287          }
 288  
 289          return true;
 290      }
 291  
 292      /**
 293       * Method to move a weblink
 294       *
 295       * @access    public
 296       * @return    boolean    True on success
 297       * @since    1.5
 298       */
 299  	function saveorder($cid = array(), $order)
 300      {
 301          $row =& $this->getTable();
 302          $groupings = array();
 303  
 304          // update ordering values
 305          for( $i=0; $i < count($cid); $i++ )
 306          {
 307              $row->load( (int) $cid[$i] );
 308              // track categories
 309              $groupings[] = $row->catid;
 310  
 311              if ($row->ordering != $order[$i])
 312              {
 313                  $row->ordering = $order[$i];
 314                  if (!$row->store()) {
 315                      $this->setError($this->_db->getErrorMsg());
 316                      return false;
 317                  }
 318              }
 319          }
 320  
 321          // execute updateOrder for each parent group
 322          $groupings = array_unique( $groupings );
 323          foreach ($groupings as $group){
 324              $row->reorder('catid = '.(int) $group);
 325          }
 326  
 327          return true;
 328      }
 329  
 330      /**
 331       * Method to load content weblink data
 332       *
 333       * @access    private
 334       * @return    boolean    True on success
 335       * @since    1.5
 336       */
 337  	function _loadData()
 338      {
 339          // Lets load the content if it doesn't already exist
 340          if (empty($this->_data))
 341          {
 342              $query = 'SELECT w.*, cc.title AS category,'.
 343                      ' cc.published AS cat_pub, cc.access AS cat_access'.
 344                      ' FROM #__weblinks AS w' .
 345                      ' LEFT JOIN #__categories AS cc ON cc.id = w.catid' .
 346                      ' WHERE w.id = '.(int) $this->_id;
 347              $this->_db->setQuery($query);
 348              $this->_data = $this->_db->loadObject();
 349              return (boolean) $this->_data;
 350          }
 351          return true;
 352      }
 353  
 354      /**
 355       * Method to initialise the weblink data
 356       *
 357       * @access    private
 358       * @return    boolean    True on success
 359       * @since    1.5
 360       */
 361  	function _initData()
 362      {
 363          // Lets load the content if it doesn't already exist
 364          if (empty($this->_data))
 365          {
 366              $weblink = new stdClass();
 367              $weblink->id                    = 0;
 368              $weblink->catid                = 0;
 369              $weblink->sid                = 0;
 370              $weblink->title                = null;
 371              $weblink->alias               = null;
 372              $weblink->url                = null;
 373              $weblink->description            = null;
 374              $weblink->date                = null;
 375              $weblink->hits                = 0;
 376              $weblink->published            = 0;
 377              $weblink->checked_out            = 0;
 378              $weblink->checked_out_time    = 0;
 379              $weblink->ordering            = 0;
 380              $weblink->archived            = 0;
 381              $weblink->approved            = 0;
 382              $weblink->params                = null;
 383              $weblink->category            = null;
 384              $this->_data                    = $weblink;
 385              return (boolean) $this->_data;
 386          }
 387          return true;
 388      }
 389  }


Generated: Wed Mar 28 15:54:07 2012 Cross-referenced by PHPXref 0.7.1