Source for file banners.php

Documentation is available at banners.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Site
  4.  * @subpackage  com_banners
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE.txt
  8.  */
  9.  
  10. defined('_JEXEC'or die;
  11.  
  12. JTable::addIncludePath(JPATH_COMPONENT_ADMINISTRATOR '/tables');
  13.  
  14. /**
  15.  * Banners model for the Joomla Banners component.
  16.  *
  17.  * @package     Joomla.Site
  18.  * @subpackage  com_banners
  19.  * @since       1.6
  20.  */
  21. {
  22.     /**
  23.      * Method to get a store id based on model configuration state.
  24.      *
  25.      * This is necessary because the model is used by the component and
  26.      * different modules that might need different sets of data or different
  27.      * ordering requirements.
  28.      *
  29.      * @param   string  $id    A prefix for the store id.
  30.      *
  31.      * @return  string  A store id.
  32.      * @since   1.6
  33.      */
  34.     protected function getStoreId($id '')
  35.     {
  36.         // Compile the store id.
  37.         $id .= ':' $this->getState('filter.search');
  38.         $id .= ':' $this->getState('filter.tag_search');
  39.         $id .= ':' $this->getState('filter.client_id');
  40.         $id .= ':' serialize($this->getState('filter.category_id'));
  41.         $id .= ':' serialize($this->getState('filter.keywords'));
  42.  
  43.         return parent::getStoreId($id);
  44.     }
  45.  
  46.     /**
  47.      * Gets a list of banners
  48.      *
  49.      * @return  array  An array of banner objects.
  50.      * @since   1.6
  51.      */
  52.     protected function getListQuery()
  53.     {
  54.         $db $this->getDbo();
  55.         $query $db->getQuery(true);
  56.         $ordering $this->getState('filter.ordering');
  57.         $tagSearch $this->getState('filter.tag_search');
  58.         $cid $this->getState('filter.client_id');
  59.         $categoryId $this->getState('filter.category_id');
  60.         $keywords $this->getState('filter.keywords');
  61.         $randomise ($ordering == 'random');
  62.         $nullDate $db->quote($db->getNullDate());
  63.  
  64.         $query->select(
  65.             'a.id as id,' .
  66.                 'a.type as type,' .
  67.                 'a.name as name,' .
  68.                 'a.clickurl as clickurl,' .
  69.                 'a.cid as cid,' .
  70.                 'a.params as params,' .
  71.                 'a.custombannercode as custombannercode,' .
  72.                 'a.track_impressions as track_impressions,' .
  73.                 'cl.track_impressions as client_track_impressions'
  74.         )
  75.             ->from('#__banners as a')
  76.             ->join('LEFT''#__banner_clients AS cl ON cl.id = a.cid')
  77.             ->where('a.state=1')
  78.             ->where('(' $query->currentTimestamp(' >= a.publish_up OR a.publish_up = ' $nullDate ')')
  79.             ->where('(' $query->currentTimestamp(' <= a.publish_down OR a.publish_down = ' $nullDate ')')
  80.             ->where('(a.imptotal = 0 OR a.impmade <= a.imptotal)');
  81.  
  82.         if ($cid)
  83.         {
  84.             $query->join('LEFT''#__categories as cat ON a.catid = cat.id')
  85.                 ->where('a.cid = ' . (int) $cid)
  86.                 ->where('cl.state = 1');
  87.         }
  88.  
  89.         // Filter by a single or group of categories
  90.         if (is_numeric($categoryId))
  91.         {
  92.             $type $this->getState('filter.category_id.include'true'= ' '<> ';
  93.  
  94.             // Add subcategory check
  95.             $includeSubcategories $this->getState('filter.subcategories'false);
  96.             $categoryEquals 'a.catid ' $type . (int) $categoryId;
  97.  
  98.             if ($includeSubcategories)
  99.             {
  100.                 $levels = (int) $this->getState('filter.max_category_levels''1');
  101.                 // Create a subquery for the subcategory list
  102.                 $subQuery $db->getQuery(true);
  103.                 $subQuery->select('sub.id')
  104.                     ->from('#__categories as sub')
  105.                     ->join('INNER''#__categories as this ON sub.lft > this.lft AND sub.rgt < this.rgt')
  106.                     ->where('this.id = ' . (int) $categoryId)
  107.                     ->where('sub.level <= this.level + ' $levels);
  108.  
  109.                 // Add the subquery to the main query
  110.                 $query->where('(' $categoryEquals ' OR a.catid IN (' $subQuery->__toString('))');
  111.             }
  112.             else
  113.             {
  114.                 $query->where($categoryEquals);
  115.             }
  116.         }
  117.         elseif ((is_array($categoryId)) && (count($categoryId0))
  118.         {
  119.             JArrayHelper::toInteger($categoryId);
  120.             $categoryId implode(','$categoryId);
  121.             if ($categoryId != '0')
  122.             {
  123.                 $type $this->getState('filter.category_id.include'true'IN' 'NOT IN';
  124.                 $query->where('a.catid ' $type ' (' $categoryId ')');
  125.             }
  126.         }
  127.  
  128.         if ($tagSearch)
  129.         {
  130.             if (count($keywords== 0)
  131.             {
  132.                 $query->where('0');
  133.             }
  134.             else
  135.             {
  136.                 $temp array();
  137.                 $config JComponentHelper::getParams('com_banners');
  138.                 $prefix $config->get('metakey_prefix');
  139.  
  140.                 foreach ($keywords as $keyword)
  141.                 {
  142.                     $keyword trim($keyword);
  143.                     $condition1 "a.own_prefix=1 AND a.metakey_prefix=SUBSTRING(" $db->quote($keyword",1,LENGTH( a.metakey_prefix)) OR a.own_prefix=0 AND cl.own_prefix=1 AND cl.metakey_prefix=SUBSTRING(" $db->quote($keyword",1,LENGTH(cl.metakey_prefix)) OR a.own_prefix=0 AND cl.own_prefix=0 AND " ($prefix == substr($keyword0strlen($prefix)) '1' '0');
  144.  
  145.                     $condition2 "a.metakey REGEXP '[[:<:]]" $db->escape($keyword"[[:>:]]'";
  146.  
  147.                     if ($cid)
  148.                     {
  149.                         $condition2 .= " OR cl.metakey REGEXP '[[:<:]]" $db->escape($keyword"[[:>:]]'";
  150.                     }
  151.  
  152.                     if ($categoryId)
  153.                     {
  154.                         $condition2 .= " OR cat.metakey REGEXP '[[:<:]]" $db->escape($keyword"[[:>:]]'";
  155.                     }
  156.  
  157.                     $temp["($condition1) AND ($condition2)";
  158.                 }
  159.  
  160.                 $query->where('(' implode(' OR '$temp')');
  161.             }
  162.         }
  163.  
  164.         // Filter by language
  165.         if ($this->getState('filter.language'))
  166.         {
  167.             $query->where('a.language in (' $db->quote(JFactory::getLanguage()->getTag()) ',' $db->quote('*'')');
  168.         }
  169.  
  170.         $query->order('a.sticky DESC,' ($randomise 'RAND()' 'a.ordering'));
  171.         return $query;
  172.     }
  173.  
  174.     /**
  175.      * Get a list of banners.
  176.      *
  177.      * @return  array 
  178.      * @since   1.6
  179.      */
  180.     public function getItems()
  181.     {
  182.         if (!isset($this->cache['items']))
  183.         {
  184.             $this->cache['items'parent::getItems();
  185.  
  186.             foreach ($this->cache['items'as &$item)
  187.             {
  188.                 $parameters new JRegistry;
  189.                 $parameters->loadString($item->params);
  190.                 $item->params $parameters;
  191.             }
  192.         }
  193.         return $this->cache['items'];
  194.     }
  195.  
  196.     /**
  197.      * Makes impressions on a list of banners
  198.      *
  199.      * @return  void 
  200.      * @since   1.6
  201.      */
  202.     public function impress()
  203.     {
  204.         $trackDate JFactory::getDate()->format('Y-m-d H');
  205.         $items $this->getItems();
  206.         $db $this->getDbo();
  207.         $query $db->getQuery(true);
  208.  
  209.         foreach ($items as $item)
  210.         {
  211.             // Increment impression made
  212.             $id $item->id;
  213.             $query->clear()
  214.                 ->update('#__banners')
  215.                 ->set('impmade = (impmade + 1)')
  216.                 ->where('id = ' . (int) $id);
  217.             $db->setQuery($query);
  218.  
  219.             try
  220.             {
  221.                 $db->execute();
  222.             }
  223.             catch (RuntimeException $e)
  224.             {
  225.                 JError::raiseError(500$e->getMessage());
  226.             }
  227.  
  228.             // track impressions
  229.             $trackImpressions $item->track_impressions;
  230.             if ($trackImpressions && $item->cid)
  231.             {
  232.                 $trackImpressions $item->client_track_impressions;
  233.             }
  234.  
  235.             if ($trackImpressions 0)
  236.             {
  237.                 $config JComponentHelper::getParams('com_banners');
  238.                 $trackImpressions $config->get('track_impressions');
  239.             }
  240.  
  241.             if ($trackImpressions 0)
  242.             {
  243.                 // is track already created ?
  244.                 $query->clear()
  245.                     ->select($db->quoteName('count'))
  246.                     ->from('#__banner_tracks')
  247.                     ->where('track_type=1')
  248.                     ->where('banner_id=' . (int) $id)
  249.                     ->where('track_date=' $db->quote($trackDate));
  250.  
  251.                 $db->setQuery($query);
  252.  
  253.                 try
  254.                 {
  255.                     $db->execute();
  256.                 }
  257.                 catch (RuntimeException $e)
  258.                 {
  259.                     JError::raiseError(500$e->getMessage());
  260.                 }
  261.  
  262.                 $count $db->loadResult();
  263.  
  264.                 $query->clear();
  265.  
  266.                 if ($count)
  267.                 {
  268.                     // update count
  269.                     $query->update('#__banner_tracks')
  270.                         ->set($db->quoteName('count'' = (' $db->quote('count'' + 1)')
  271.                         ->where('track_type=1')
  272.                         ->where('banner_id=' . (int) $id)
  273.                         ->where('track_date=' $db->quote($trackDate));
  274.                 }
  275.                 else
  276.                 {
  277.                     // insert new count
  278.                     //sqlsrv change
  279.                     $query->insert('#__banner_tracks')
  280.                         ->columns(
  281.                             array(
  282.                                 $db->quoteName('count')$db->quoteName('track_type'),
  283.                                 $db->quoteName('banner_id')$db->quoteName('track_date')
  284.                             )
  285.                         )
  286.                         ->values('1, 1, ' . (int) $id ', ' $db->quote($trackDate));
  287.                 }
  288.  
  289.                 $db->setQuery($query);
  290.  
  291.                 try
  292.                 {
  293.                     $db->execute();
  294.                 }
  295.                 catch (RuntimeException $e)
  296.                 {
  297.                     JError::raiseError(500$e->getMessage());
  298.                 }
  299.             }
  300.         }
  301.     }
  302. }

Documentation generated on Tue, 19 Nov 2013 14:54:21 +0100 by phpDocumentor 1.4.3