- md5
- 2fe6f5966996f19f64303c21dfb80092
- sha1
- e62c295bee42161568b1077c24b5f09a9d40d73d
- sha256
- cb5e76e204a06c6f9df04f12acf6b63f077dd87a2679d0b3d161d5bebda17d9e
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
<?php
/**
* @package Joomla.Site
* @subpackage com_banners
*
* @copyright Copyright (C) 2005 - 2020 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\Component\Banners\Site\Model;
\defined('_JEXEC') or die;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Factory;
use Joomla\CMS\MVC\Model\ListModel;
use Joomla\Database\DatabaseQuery;
use Joomla\Database\Exception\ExecutionFailureException;
use Joomla\Database\ParameterType;
use Joomla\Registry\Registry;
use Joomla\Utilities\ArrayHelper;
/**
* Banners model for the Joomla Banners component.
*
* @since 1.6
*/
class BannersModel extends ListModel
{
/**
* Method to get a store id based on model configuration state.
*
* This is necessary because the model is used by the component and
* different modules that might need different sets of data or different
* ordering requirements.
*
* @param string $id A prefix for the store id.
*
* @return string A store id.
*
* @since 1.6
*/
protected function getStoreId($id = '')
{
// Compile the store id.
$id .= ':' . $this->getState('filter.search');
$id .= ':' . $this->getState('filter.tag_search');
$id .= ':' . $this->getState('filter.client_id');
$id .= ':' . serialize($this->getState('filter.category_id'));
$id .= ':' . serialize($this->getState('filter.keywords'));
return parent::getStoreId($id);
}
/**
* Method to get a DatabaseQuery object for retrieving the data set from a database.
*
* @return DatabaseQuery A DatabaseQuery object to retrieve the data set.
*
* @since 1.6
*/
protected function getListQuery()
{
$db = $this->getDbo();
$query = $db->getQuery(true);
$ordering = $this->getState('filter.ordering');
$tagSearch = $this->getState('filter.tag_search');
$cid = (int) $this->getState('filter.client_id');
$categoryId = $this->getState('filter.category_id');
$keywords = $this->getState('filter.keywords');
$randomise = ($ordering === 'random');
$nowDate = Factory::getDate()->toSql();
$query->select(
[
$db->quoteName('a.id'),
$db->quoteName('a.type'),
$db->quoteName('a.name'),
$db->quoteName('a.clickurl'),
$db->quoteName('a.cid'),
$db->quoteName('a.description'),
$db->quoteName('a.params'),
$db->quoteName('a.custombannercode'),
$db->quoteName('a.track_impressions'),
$db->quoteName('cl.track_impressions', 'client_track_impressions'),
]
)
->from($db->quoteName('#__banners', 'a'))
->join('LEFT', $db->quoteName('#__banner_clients', 'cl'), $db->quoteName('cl.id') . ' = ' . $db->quoteName('a.cid'))
->where($db->quoteName('a.state') . ' = 1')
->extendWhere(
'AND',
[
$db->quoteName('a.publish_up') . ' IS NULL',
$db->quoteName('a.publish_up') . ' <= :nowDate1',
],
'OR'
)
->extendWhere(
'AND',
[
$db->quoteName('a.publish_down') . ' IS NULL',
$db->quoteName('a.publish_down') . ' >= :nowDate2',
],
'OR'
)
->extendWhere(
'AND',
[
$db->quoteName('a.imptotal') . ' = 0',
$db->quoteName('a.impmade') . ' <= ' . $db->quoteName('a.imptotal'),
],
'OR'
)
->bind([':nowDate1', ':nowDate2'], $nowDate);
if ($cid)
{
$query->where(
[
$db->quoteName('a.cid') . ' = :clientId',
$db->quoteName('cl.state') . ' = 1',
]
)
->bind(':clientId', $cid, ParameterType::INTEGER);
}
// Filter by a single or group of categories
if (is_numeric($categoryId))
{
$categoryId = (int) $categoryId;
$type = $this->getState('filter.category_id.include', true) ? ' = ' : ' <> ';
// Add subcategory check
if ($this->getState('filter.subcategories', false))
{
$levels = (int) $this->getState('filter.max_category_levels', '1');
// Create a subquery for the subcategory list
$subQuery = $db->getQuery(true);
$subQuery->select($db->quoteName('sub.id'))
->from($db->quoteName('#__categories', 'sub'))
->join(
'INNER',
$db->quoteName('#__categories', 'this'),
$db->quoteName('sub.lft') . ' > ' . $db->quoteName('this.lft')
. ' AND ' . $db->quoteName('sub.rgt') . ' < ' . $db->quoteName('this.rgt')
)
->where(
[
$db->quoteName('this.id') . ' = :categoryId1',
$db->quoteName('sub.level') . ' <= ' . $db->quoteName('this.level') . ' + :levels',
]
);
// Add the subquery to the main query
$query->extendWhere(
'AND',
[
$db->quoteName('a.catid') . $type . ':categoryId2',
$db->quoteName('a.catid') . ' IN (' . $subQuery . ')',
],
'OR'
)
->bind([':categoryId1', ':categoryId2'], $categoryId, ParameterType::INTEGER)
->bind(':levels', $levels, ParameterType::INTEGER);
}
else
{
$query->where($db->quoteName('a.catid') . $type . ':categoryId')
->bind(':categoryId', $categoryId, ParameterType::INTEGER);
}
}
elseif (is_array($categoryId) && (count($categoryId) > 0))
{
$categoryId = ArrayHelper::toInteger($categoryId);
if ($this->getState('filter.category_id.include', true))
{
$query->whereIn($db->quoteName('a.catid'), $categoryId);
}
else
{
$query->whereNotIn($db->quoteName('a.catid'), $categoryId);
}
}
if ($tagSearch)
{
if (!$keywords)
{
// No keywords, select nothing.
$query->where('0 != 0');
}
else
{
$temp = array();
$config = ComponentHelper::getParams('com_banners');
$prefix = $config->get('metakey_prefix');
if ($categoryId)
{
$query->join('LEFT', $db->quoteName('#__categories', 'cat'), $db->quoteName('a.catid') . ' = ' . $db->quoteName('cat.id'));
}
foreach ($keywords as $key => $keyword)
{
$condition1 = $db->quoteName('a.own_prefix') . ' = 1'
. ' AND ' . $db->quoteName('a.metakey_prefix')
. ' = SUBSTRING(:aprefix' . $key . ',1,LENGTH(' . $db->quoteName('a.metakey_prefix') . '))'
. ' OR ' . $db->quoteName('a.own_prefix') . ' = 0'
. ' AND ' . $db->quoteName('cl.own_prefix') . ' = 1'
. ' AND ' . $db->quoteName('cl.metakey_prefix')
. ' = SUBSTRING(:clprefix' . $key . ',1,LENGTH(' . $db->quoteName('cl.metakey_prefix') . '))'
. ' OR ' . $db->quoteName('a.own_prefix') . ' = 0'
. ' AND ' . $db->quoteName('cl.own_prefix') . ' = 0'
. ' AND ' . ($prefix == substr($keyword, 0, strlen($prefix)) ? '0 = 0' : '0 != 0');
$query->bind([':aprefix' . $key, ':clprefix' . $key], $keyword);
$regexp = '[[:<:]]' . $keyword . '[[:>:]]';
$condition2 = $db->quoteName('a.metakey') . ' ' . $query->regexp(':aregexp' . $key) . ' ';
$query->bind(':aregexp' . $key, $regexp);
if ($cid)
{
$condition2 .= ' OR ' . $db->quoteName('cl.metakey') . ' ' . $query->regexp(':clregexp' . $key) . ' ';
$query->bind(':clregexp' . $key, $regexp);
}
if ($categoryId)
{
$condition2 .= ' OR ' . $db->quoteName('cat.metakey') . ' ' . $query->regexp(':catregexp' . $key) . ' ';
$query->bind(':catregexp' . $key, $regexp);
}
$temp[] = "($condition1) AND ($condition2)";
}
$query->where('(' . implode(' OR ', $temp) . ')');
}
}
// Filter by language
if ($this->getState('filter.language'))
{
$query->whereIn($db->quoteName('a.language'), [Factory::getLanguage()->getTag(), '*']);
}
$query->order($db->quoteName('a.sticky') . ' DESC, ' . ($randomise ? $query->rand() : $db->quoteName('a.ordering')));
return $query;
}
/**
* Get a list of banners.
*
* @return array
*
* @since 1.6
*/
public function getItems()
{
if ($this->getState('filter.tag_search'))
{
// Filter out empty keywords.
$keywords = array_values(array_filter(array_map('trim', $this->getState('filter.keywords')), 'strlen'));
// Re-set state before running the query.
$this->setState('filter.keywords', $keywords);
// If no keywords are provided, avoid running the query.
if (!$keywords)
{
$this->cache['items'] = array();
return $this->cache['items'];
}
}
if (!isset($this->cache['items']))
{
$this->cache['items'] = parent::getItems();
foreach ($this->cache['items'] as &$item)
{
$item->params = new Registry($item->params);
}
}
return $this->cache['items'];
}
/**
* Makes impressions on a list of banners
*
* @return void
*
* @since 1.6
* @throws \Exception
*/
public function impress()
{
$trackDate = Factory::getDate()->toSql();
$items = $this->getItems();
$db = $this->getDbo();
$bid = [];
if (!count($items))
{
return;
}
foreach ($items as $item)
{
$bid[] = (int) $item->id;
}
// Increment impression made
$query = $db->getQuery(true);
$query->update($db->quoteName('#__banners'))
->set($db->quoteName('impmade') . ' = ' . $db->quoteName('impmade') . ' + 1')
->whereIn($db->quoteName('id'), $bid);
$db->setQuery($query);
try
{
$db->execute();
}
catch (ExecutionFailureException $e)
{
throw new \Exception($e->getMessage(), 500);
}
foreach ($items as $item)
{
// Track impressions
$trackImpressions = $item->track_impressions;
if ($trackImpressions < 0 && $item->cid)
{
$trackImpressions = $item->client_track_impressions;
}
if ($trackImpressions < 0)
{
$config = ComponentHelper::getParams('com_banners');
$trackImpressions = $config->get('track_impressions');
}
if ($trackImpressions > 0)
{
// Is track already created?
// Update count
$query = $db->getQuery(true);
$query->update($db->quoteName('#__banner_tracks'))
->set($db->quoteName('count') . ' = ' . $db->quoteName('count') . ' + 1')
->where(
[
$db->quoteName('track_type') . ' = 1',
$db->quoteName('banner_id') . ' = :id',
$db->quoteName('track_date') . ' = :trackDate',
]
)
->bind(':id', $item->id, ParameterType::INTEGER)
->bind(':trackDate', $trackDate);
$db->setQuery($query);
try
{
$db->execute();
}
catch (ExecutionFailureException $e)
{
throw new \Exception($e->getMessage(), 500);
}
if ($db->getAffectedRows() === 0)
{
// Insert new count
$query = $db->getQuery(true);
$query->insert($db->quoteName('#__banner_tracks'))
->columns(
[
$db->quoteName('count'),
$db->quoteName('track_type'),
$db->quoteName('banner_id'),
$db->quoteName('track_date'),
]
)
->values('1, 1, :id, :trackDate')
->bind(':id', $item->id, ParameterType::INTEGER)
->bind(':trackDate', $trackDate);
$db->setQuery($query);
try
{
$db->execute();
}
catch (ExecutionFailureException $e)
{
throw new \Exception($e->getMessage(), 500);
}
}
}
}
}
}
No other release has this exact content. See every version of this file.