[ Index ] |
PHP Cross Reference of Joomla 4.2.2 documentation |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * @package Joomla.Site 5 * @subpackage com_banners 6 * 7 * @copyright (C) 2006 Open Source Matters, Inc. <https://www.joomla.org> 8 * @license GNU General Public License version 2 or later; see LICENSE.txt 9 */ 10 11 namespace Joomla\Component\Banners\Site\Model; 12 13 use Joomla\CMS\Cache\Exception\CacheExceptionInterface; 14 use Joomla\CMS\Component\ComponentHelper; 15 use Joomla\CMS\Factory; 16 use Joomla\CMS\Language\Text; 17 use Joomla\CMS\MVC\Model\BaseDatabaseModel; 18 use Joomla\Database\ParameterType; 19 20 // phpcs:disable PSR1.Files.SideEffects 21 \defined('_JEXEC') or die; 22 // phpcs:enable PSR1.Files.SideEffects 23 24 /** 25 * Banner model for the Joomla Banners component. 26 * 27 * @since 1.5 28 */ 29 class BannerModel extends BaseDatabaseModel 30 { 31 /** 32 * Cached item object 33 * 34 * @var object 35 * @since 1.6 36 */ 37 protected $_item; 38 39 /** 40 * Clicks the URL, incrementing the counter 41 * 42 * @return void 43 * 44 * @since 1.5 45 * @throws \Exception 46 */ 47 public function click() 48 { 49 $item = $this->getItem(); 50 51 if (empty($item)) { 52 throw new \Exception(Text::_('JERROR_PAGE_NOT_FOUND'), 404); 53 } 54 55 $id = (int) $this->getState('banner.id'); 56 57 // Update click count 58 $db = $this->getDatabase(); 59 $query = $db->getQuery(true); 60 61 $query->update($db->quoteName('#__banners')) 62 ->set($db->quoteName('clicks') . ' = ' . $db->quoteName('clicks') . ' + 1') 63 ->where($db->quoteName('id') . ' = :id') 64 ->bind(':id', $id, ParameterType::INTEGER); 65 66 $db->setQuery($query); 67 68 try { 69 $db->execute(); 70 } catch (\RuntimeException $e) { 71 throw new \Exception($e->getMessage(), 500); 72 } 73 74 // Track clicks 75 $trackClicks = $item->track_clicks; 76 77 if ($trackClicks < 0 && $item->cid) { 78 $trackClicks = $item->client_track_clicks; 79 } 80 81 if ($trackClicks < 0) { 82 $config = ComponentHelper::getParams('com_banners'); 83 $trackClicks = $config->get('track_clicks'); 84 } 85 86 if ($trackClicks > 0) { 87 $trackDate = Factory::getDate()->format('Y-m-d H:00:00'); 88 $trackDate = Factory::getDate($trackDate)->toSql(); 89 90 $query = $db->getQuery(true); 91 92 $query->select($db->quoteName('count')) 93 ->from($db->quoteName('#__banner_tracks')) 94 ->where( 95 [ 96 $db->quoteName('track_type') . ' = 2', 97 $db->quoteName('banner_id') . ' = :id', 98 $db->quoteName('track_date') . ' = :trackDate', 99 ] 100 ) 101 ->bind(':id', $id, ParameterType::INTEGER) 102 ->bind(':trackDate', $trackDate); 103 104 $db->setQuery($query); 105 106 try { 107 $db->execute(); 108 } catch (\RuntimeException $e) { 109 throw new \Exception($e->getMessage(), 500); 110 } 111 112 $count = $db->loadResult(); 113 114 $query = $db->getQuery(true); 115 116 if ($count) { 117 // Update count 118 $query->update($db->quoteName('#__banner_tracks')) 119 ->set($db->quoteName('count') . ' = ' . $db->quoteName('count') . ' + 1') 120 ->where( 121 [ 122 $db->quoteName('track_type') . ' = 2', 123 $db->quoteName('banner_id') . ' = :id', 124 $db->quoteName('track_date') . ' = :trackDate', 125 ] 126 ) 127 ->bind(':id', $id, ParameterType::INTEGER) 128 ->bind(':trackDate', $trackDate); 129 } else { 130 // Insert new count 131 $query->insert($db->quoteName('#__banner_tracks')) 132 ->columns( 133 [ 134 $db->quoteName('count'), 135 $db->quoteName('track_type'), 136 $db->quoteName('banner_id'), 137 $db->quoteName('track_date'), 138 ] 139 ) 140 ->values('1, 2 , :id, :trackDate') 141 ->bind(':id', $id, ParameterType::INTEGER) 142 ->bind(':trackDate', $trackDate); 143 } 144 145 $db->setQuery($query); 146 147 try { 148 $db->execute(); 149 } catch (\RuntimeException $e) { 150 throw new \Exception($e->getMessage(), 500); 151 } 152 } 153 } 154 155 /** 156 * Get the data for a banner. 157 * 158 * @return object 159 * 160 * @since 1.6 161 */ 162 public function &getItem() 163 { 164 if (!isset($this->_item)) { 165 /** @var \Joomla\CMS\Cache\Controller\CallbackController $cache */ 166 $cache = Factory::getCache('com_banners', 'callback'); 167 168 $id = (int) $this->getState('banner.id'); 169 170 // For PHP 5.3 compat we can't use $this in the lambda function below, so grab the database driver now to use it 171 $db = $this->getDatabase(); 172 173 $loader = function ($id) use ($db) { 174 $query = $db->getQuery(true); 175 176 $query->select( 177 [ 178 $db->quoteName('a.clickurl'), 179 $db->quoteName('a.cid'), 180 $db->quoteName('a.track_clicks'), 181 $db->quoteName('cl.track_clicks', 'client_track_clicks'), 182 ] 183 ) 184 ->from($db->quoteName('#__banners', 'a')) 185 ->join('LEFT', $db->quoteName('#__banner_clients', 'cl'), $db->quoteName('cl.id') . ' = ' . $db->quoteName('a.cid')) 186 ->where($db->quoteName('a.id') . ' = :id') 187 ->bind(':id', $id, ParameterType::INTEGER); 188 189 $db->setQuery($query); 190 191 return $db->loadObject(); 192 }; 193 194 try { 195 $this->_item = $cache->get($loader, array($id), md5(__METHOD__ . $id)); 196 } catch (CacheExceptionInterface $e) { 197 $this->_item = $loader($id); 198 } 199 } 200 201 return $this->_item; 202 } 203 204 /** 205 * Get the URL for a banner 206 * 207 * @return string 208 * 209 * @since 1.5 210 */ 211 public function getUrl() 212 { 213 $item = $this->getItem(); 214 $url = $item->clickurl; 215 216 // Check for links 217 if (!preg_match('#http[s]?://|index[2]?\.php#', $url)) { 218 $url = "http://$url"; 219 } 220 221 return $url; 222 } 223 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Sep 7 05:41:13 2022 | Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer |