[ Index ]

PHP Cross Reference of Joomla 2.5.4 DE

title

Body

[close]

/components/com_banners/models/ -> banner.php (source)

   1  <?php
   2  /**
   3   * @package        Joomla.Site
   4   * @subpackage    com_banners
   5   * @copyright    Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
   6   * @license        GNU General Public License version 2 or later; see LICENSE.txt
   7   */
   8  
   9  // No direct access
  10  defined('_JEXEC') or die;
  11  
  12  jimport('joomla.application.component.model');
  13  jimport('joomla.application.component.helper');
  14  
  15  JTable::addIncludePath(JPATH_COMPONENT_ADMINISTRATOR . '/tables');
  16  
  17  /**
  18   * Banner model for the Joomla Banners component.
  19   *
  20   * @package        Joomla.Site
  21   * @subpackage    com_banners
  22   */
  23  class BannersModelBanner extends JModel
  24  {
  25      protected $_item;
  26  
  27      /**
  28       * Clicks the URL, incrementing the counter
  29       *
  30       * @return    void
  31       */
  32  	function click()
  33      {
  34          $id = $this->getState('banner.id');
  35  
  36          // update click count
  37          $db        = $this->getDbo();
  38          $query    = $db->getQuery(true);
  39          $query->update('#__banners');
  40          $query->set('clicks = (clicks + 1)');
  41          $query->where('id = ' . (int) $id);
  42  
  43          $db->setQuery((string) $query);
  44  
  45          if (!$db->query()) {
  46              JError::raiseError(500, $db->getErrorMsg());
  47          }
  48  
  49          // track clicks
  50  
  51          $item =  $this->getItem();
  52  
  53          $trackClicks = $item->track_clicks;
  54  
  55          if ($trackClicks < 0 && $item->cid) {
  56              $trackClicks = $item->client_track_clicks;
  57          }
  58  
  59          if ($trackClicks < 0) {
  60              $config = JComponentHelper::getParams('com_banners');
  61              $trackClicks = $config->get('track_clicks');
  62          }
  63  
  64          if ($trackClicks > 0) {
  65              $trackDate = JFactory::getDate()->format('Y-m-d H');
  66  
  67              $query->clear();
  68              $query->select($db->quoteName('count'));
  69              $query->from('#__banner_tracks');
  70              $query->where('track_type=2');
  71              $query->where('banner_id='.(int)$id);
  72              $query->where('track_date='.$db->Quote($trackDate));
  73  
  74              $db->setQuery((string) $query);
  75  
  76              if (!$db->query()) {
  77                  JError::raiseError(500, $db->getErrorMsg());
  78              }
  79  
  80              $count = $db->loadResult();
  81  
  82              $query->clear();
  83  
  84              if ($count) {
  85                  // update count
  86                  $query->update('#__banner_tracks');
  87                  $query->set($db->quoteName('count').' = ('.$db->quoteName('count') . ' + 1)');
  88                  $query->where('track_type=2');
  89                  $query->where('banner_id='.(int)$id);
  90                  $query->where('track_date='.$db->Quote($trackDate));
  91              }
  92              else {
  93                  // insert new count
  94                  //sqlsrv change
  95                  $query->insert('#__banner_tracks');
  96                  $query->columns(array($db->quoteName('count'), $db->quoteName('track_type'),
  97                                  $db->quoteName('banner_id') , $db->quoteName('track_date')));
  98                  $query->values( '1, 2,' . (int)$id . ',' . $db->Quote($trackDate));
  99              }
 100  
 101              $db->setQuery((string) $query);
 102  
 103              if (!$db->query()) {
 104                  JError::raiseError(500, $db->getErrorMsg());
 105              }
 106          }
 107      }
 108  
 109      /**
 110       * Get the data for a banner.
 111       *
 112       * @return    object
 113       */
 114      function &getItem()
 115      {
 116          if (!isset($this->_item))
 117          {
 118              $cache = JFactory::getCache('com_banners', '');
 119  
 120              $id = $this->getState('banner.id');
 121  
 122              $this->_item =  $cache->get($id);
 123  
 124              if ($this->_item === false) {
 125                  // redirect to banner url
 126                  $db        = $this->getDbo();
 127                  $query    = $db->getQuery(true);
 128                  $query->select(
 129                      'a.clickurl as clickurl,'.
 130                      'a.cid as cid,'.
 131                      'a.track_clicks as track_clicks'
 132                      );
 133                  $query->from('#__banners as a');
 134                  $query->where('a.id = ' . (int) $id);
 135  
 136                  $query->join('LEFT', '#__banner_clients AS cl ON cl.id = a.cid');
 137                  $query->select('cl.track_clicks as client_track_clicks');
 138  
 139                  $db->setQuery((string) $query);
 140  
 141                  if (!$db->query()) {
 142                      JError::raiseError(500, $db->getErrorMsg());
 143                  }
 144  
 145                  $this->_item = $db->loadObject();
 146                  $cache->store($this->_item, $id);
 147              }
 148          }
 149  
 150          return $this->_item;
 151      }
 152  
 153      /**
 154       * Get the URL for a banner
 155       *
 156       * @return    string
 157       */
 158  	function getUrl()
 159      {
 160          $item = $this->getItem();
 161          $url = $item->clickurl;
 162  
 163          // check for links
 164          if (!preg_match('#http[s]?://|index[2]?\.php#', $url)) {
 165              $url = "http://$url";
 166          }
 167  
 168          return $url;
 169      }
 170  }


Generated: Tue Apr 3 11:40:28 2012 Cross-referenced by PHPXref 0.7.1