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