[ Index ] |
PHP Cross Reference of Joomla 4.2.2 documentation |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * @package Joomla.Administrator 5 * @subpackage com_banners 6 * 7 * @copyright (C) 2009 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\Administrator\Model; 12 13 use Joomla\Archive\Archive; 14 use Joomla\CMS\Component\ComponentHelper; 15 use Joomla\CMS\Factory; 16 use Joomla\CMS\Filesystem\File; 17 use Joomla\CMS\Filesystem\Folder; 18 use Joomla\CMS\Language\Text; 19 use Joomla\CMS\MVC\Model\ListModel; 20 use Joomla\Database\ParameterType; 21 use Joomla\String\StringHelper; 22 23 // phpcs:disable PSR1.Files.SideEffects 24 \defined('_JEXEC') or die; 25 // phpcs:enable PSR1.Files.SideEffects 26 27 /** 28 * Methods supporting a list of tracks. 29 * 30 * @since 1.6 31 */ 32 class TracksModel extends ListModel 33 { 34 /** 35 * The base name 36 * 37 * @var string 38 * @since 1.6 39 */ 40 protected $basename; 41 42 /** 43 * Constructor. 44 * 45 * @param array $config An optional associative array of configuration settings. 46 * 47 * @since 1.6 48 */ 49 public function __construct($config = array()) 50 { 51 if (empty($config['filter_fields'])) { 52 $config['filter_fields'] = array( 53 'b.name', 'banner_name', 54 'cl.name', 'client_name', 'client_id', 55 'c.title', 'category_title', 'category_id', 56 'track_type', 'a.track_type', 'type', 57 'count', 'a.count', 58 'track_date', 'a.track_date', 'end', 'begin', 59 'level', 'c.level', 60 ); 61 } 62 63 parent::__construct($config); 64 } 65 66 /** 67 * Method to auto-populate the model state. 68 * 69 * Note. Calling getState in this method will result in recursion. 70 * 71 * @param string $ordering An optional ordering field. 72 * @param string $direction An optional direction (asc|desc). 73 * 74 * @return void 75 * 76 * @since 1.6 77 */ 78 protected function populateState($ordering = 'b.name', $direction = 'asc') 79 { 80 // Load the parameters. 81 $this->setState('params', ComponentHelper::getParams('com_banners')); 82 83 // List state information. 84 parent::populateState($ordering, $direction); 85 } 86 87 /** 88 * Build an SQL query to load the list data. 89 * 90 * @return \Joomla\Database\DatabaseQuery 91 * 92 * @since 1.6 93 */ 94 protected function getListQuery() 95 { 96 // Create a new query object. 97 $db = $this->getDatabase(); 98 $query = $db->getQuery(true); 99 100 // Select the required fields from the table. 101 $query->select( 102 [ 103 $db->quoteName('a.track_date'), 104 $db->quoteName('a.track_type'), 105 $db->quoteName('a.count'), 106 $db->quoteName('b.name', 'banner_name'), 107 $db->quoteName('cl.name', 'client_name'), 108 $db->quoteName('c.title', 'category_title'), 109 ] 110 ); 111 112 // From tracks table. 113 $query->from($db->quoteName('#__banner_tracks', 'a')); 114 115 // Join with the banners. 116 $query->join('LEFT', $db->quoteName('#__banners', 'b'), $db->quoteName('b.id') . ' = ' . $db->quoteName('a.banner_id')); 117 118 // Join with the client. 119 $query->join('LEFT', $db->quoteName('#__banner_clients', 'cl'), $db->quoteName('cl.id') . ' = ' . $db->quoteName('b.cid')); 120 121 // Join with the category. 122 $query->join('LEFT', $db->quoteName('#__categories', 'c'), $db->quoteName('c.id') . ' = ' . $db->quoteName('b.catid')); 123 124 // Filter by type. 125 126 if ($type = (int) $this->getState('filter.type')) { 127 $query->where($db->quoteName('a.track_type') . ' = :type') 128 ->bind(':type', $type, ParameterType::INTEGER); 129 } 130 131 // Filter by client. 132 $clientId = $this->getState('filter.client_id'); 133 134 if (is_numeric($clientId)) { 135 $clientId = (int) $clientId; 136 $query->where($db->quoteName('b.cid') . ' = :clientId') 137 ->bind(':clientId', $clientId, ParameterType::INTEGER); 138 } 139 140 // Filter by category. 141 $categoryId = $this->getState('filter.category_id'); 142 143 if (is_numeric($categoryId)) { 144 $categoryId = (int) $categoryId; 145 $query->where($db->quoteName('b.catid') . ' = :categoryId') 146 ->bind(':categoryId', $categoryId, ParameterType::INTEGER); 147 } 148 149 // Filter by begin date. 150 if ($begin = $this->getState('filter.begin')) { 151 $query->where($db->quoteName('a.track_date') . ' >= :begin') 152 ->bind(':begin', $begin); 153 } 154 155 // Filter by end date. 156 if ($end = $this->getState('filter.end')) { 157 $query->where($db->quoteName('a.track_date') . ' <= :end') 158 ->bind(':end', $end); 159 } 160 161 // Filter on the level. 162 if ($level = (int) $this->getState('filter.level')) { 163 $query->where($db->quoteName('c.level') . ' <= :level') 164 ->bind(':level', $level, ParameterType::INTEGER); 165 } 166 167 // Filter by search in banner name or client name. 168 if ($search = $this->getState('filter.search')) { 169 $search = '%' . StringHelper::strtolower($search) . '%'; 170 $query->where('(LOWER(' . $db->quoteName('b.name') . ') LIKE :search1 OR LOWER(' . $db->quoteName('cl.name') . ') LIKE :search2)') 171 ->bind([':search1', ':search2'], $search); 172 } 173 174 // Add the list ordering clause. 175 $query->order( 176 $db->quoteName($db->escape($this->getState('list.ordering', 'b.name'))) . ' ' . $db->escape($this->getState('list.direction', 'ASC')) 177 ); 178 179 return $query; 180 } 181 182 /** 183 * Method to delete rows. 184 * 185 * @return boolean Returns true on success, false on failure. 186 */ 187 public function delete() 188 { 189 $user = Factory::getUser(); 190 $categoryId = (int) $this->getState('category_id'); 191 192 // Access checks. 193 if ($categoryId) { 194 $allow = $user->authorise('core.delete', 'com_banners.category.' . $categoryId); 195 } else { 196 $allow = $user->authorise('core.delete', 'com_banners'); 197 } 198 199 if ($allow) { 200 // Delete tracks from this banner 201 $db = $this->getDatabase(); 202 $query = $db->getQuery(true) 203 ->delete($db->quoteName('#__banner_tracks')); 204 205 // Filter by type 206 if ($type = (int) $this->getState('filter.type')) { 207 $query->where($db->quoteName('track_type') . ' = :type') 208 ->bind(':type', $type, ParameterType::INTEGER); 209 } 210 211 // Filter by begin date 212 if ($begin = $this->getState('filter.begin')) { 213 $query->where($db->quoteName('track_date') . ' >= :begin') 214 ->bind(':begin', $begin); 215 } 216 217 // Filter by end date 218 if ($end = $this->getState('filter.end')) { 219 $query->where($db->quoteName('track_date') . ' <= :end') 220 ->bind(':end', $end); 221 } 222 223 $subQuery = $db->getQuery(true); 224 $subQuery->select($db->quoteName('id')) 225 ->from($db->quoteName('#__banners')); 226 227 // Filter by client 228 if ($clientId = (int) $this->getState('filter.client_id')) { 229 $subQuery->where($db->quoteName('cid') . ' = :clientId'); 230 $query->bind(':clientId', $clientId, ParameterType::INTEGER); 231 } 232 233 // Filter by category 234 if ($categoryId) { 235 $subQuery->where($db->quoteName('catid') . ' = :categoryId'); 236 $query->bind(':categoryId', $categoryId, ParameterType::INTEGER); 237 } 238 239 $query->where($db->quoteName('banner_id') . ' IN (' . $subQuery . ')'); 240 241 $db->setQuery($query); 242 $this->setError((string) $query); 243 244 try { 245 $db->execute(); 246 } catch (\RuntimeException $e) { 247 $this->setError($e->getMessage()); 248 249 return false; 250 } 251 } else { 252 Factory::getApplication()->enqueueMessage(Text::_('JERROR_CORE_DELETE_NOT_PERMITTED'), 'error'); 253 } 254 255 return true; 256 } 257 258 /** 259 * Get file name 260 * 261 * @return string The file name 262 * 263 * @since 1.6 264 */ 265 public function getBaseName() 266 { 267 if (!isset($this->basename)) { 268 $basename = str_replace('__SITE__', Factory::getApplication()->get('sitename'), $this->getState('basename')); 269 $categoryId = $this->getState('filter.category_id'); 270 271 if (is_numeric($categoryId)) { 272 if ($categoryId > 0) { 273 $basename = str_replace('__CATID__', $categoryId, $basename); 274 } else { 275 $basename = str_replace('__CATID__', '', $basename); 276 } 277 278 $categoryName = $this->getCategoryName(); 279 $basename = str_replace('__CATNAME__', $categoryName, $basename); 280 } else { 281 $basename = str_replace(array('__CATID__', '__CATNAME__'), '', $basename); 282 } 283 284 $clientId = $this->getState('filter.client_id'); 285 286 if (is_numeric($clientId)) { 287 if ($clientId > 0) { 288 $basename = str_replace('__CLIENTID__', $clientId, $basename); 289 } else { 290 $basename = str_replace('__CLIENTID__', '', $basename); 291 } 292 293 $clientName = $this->getClientName(); 294 $basename = str_replace('__CLIENTNAME__', $clientName, $basename); 295 } else { 296 $basename = str_replace(array('__CLIENTID__', '__CLIENTNAME__'), '', $basename); 297 } 298 299 $type = $this->getState('filter.type'); 300 301 if ($type > 0) { 302 $basename = str_replace('__TYPE__', $type, $basename); 303 $typeName = Text::_('COM_BANNERS_TYPE' . $type); 304 $basename = str_replace('__TYPENAME__', $typeName, $basename); 305 } else { 306 $basename = str_replace(array('__TYPE__', '__TYPENAME__'), '', $basename); 307 } 308 309 $begin = $this->getState('filter.begin'); 310 311 if (!empty($begin)) { 312 $basename = str_replace('__BEGIN__', $begin, $basename); 313 } else { 314 $basename = str_replace('__BEGIN__', '', $basename); 315 } 316 317 $end = $this->getState('filter.end'); 318 319 if (!empty($end)) { 320 $basename = str_replace('__END__', $end, $basename); 321 } else { 322 $basename = str_replace('__END__', '', $basename); 323 } 324 325 $this->basename = $basename; 326 } 327 328 return $this->basename; 329 } 330 331 /** 332 * Get the category name. 333 * 334 * @return string The category name 335 * 336 * @since 1.6 337 */ 338 protected function getCategoryName() 339 { 340 $categoryId = (int) $this->getState('filter.category_id'); 341 342 if ($categoryId) { 343 $db = $this->getDatabase(); 344 $query = $db->getQuery(true) 345 ->select($db->quoteName('title')) 346 ->from($db->quoteName('#__categories')) 347 ->where($db->quoteName('id') . ' = :categoryId') 348 ->bind(':categoryId', $categoryId, ParameterType::INTEGER); 349 $db->setQuery($query); 350 351 try { 352 $name = $db->loadResult(); 353 } catch (\RuntimeException $e) { 354 $this->setError($e->getMessage()); 355 356 return false; 357 } 358 359 return $name; 360 } 361 362 return Text::_('COM_BANNERS_NOCATEGORYNAME'); 363 } 364 365 /** 366 * Get the client name 367 * 368 * @return string The client name. 369 * 370 * @since 1.6 371 */ 372 protected function getClientName() 373 { 374 $clientId = (int) $this->getState('filter.client_id'); 375 376 if ($clientId) { 377 $db = $this->getDatabase(); 378 $query = $db->getQuery(true) 379 ->select($db->quoteName('name')) 380 ->from($db->quoteName('#__banner_clients')) 381 ->where($db->quoteName('id') . ' = :clientId') 382 ->bind(':clientId', $clientId, ParameterType::INTEGER); 383 $db->setQuery($query); 384 385 try { 386 $name = $db->loadResult(); 387 } catch (\RuntimeException $e) { 388 $this->setError($e->getMessage()); 389 390 return false; 391 } 392 393 return $name; 394 } 395 396 return Text::_('COM_BANNERS_NOCLIENTNAME'); 397 } 398 399 /** 400 * Get the file type. 401 * 402 * @return string The file type 403 * 404 * @since 1.6 405 */ 406 public function getFileType() 407 { 408 return $this->getState('compressed') ? 'zip' : 'csv'; 409 } 410 411 /** 412 * Get the mime type. 413 * 414 * @return string The mime type. 415 * 416 * @since 1.6 417 */ 418 public function getMimeType() 419 { 420 return $this->getState('compressed') ? 'application/zip' : 'text/csv'; 421 } 422 423 /** 424 * Get the content 425 * 426 * @return string The content. 427 * 428 * @since 1.6 429 */ 430 public function getContent() 431 { 432 if (!isset($this->content)) { 433 $this->content = '"' . str_replace('"', '""', Text::_('COM_BANNERS_HEADING_NAME')) . '","' 434 . str_replace('"', '""', Text::_('COM_BANNERS_HEADING_CLIENT')) . '","' 435 . str_replace('"', '""', Text::_('JCATEGORY')) . '","' 436 . str_replace('"', '""', Text::_('COM_BANNERS_HEADING_TYPE')) . '","' 437 . str_replace('"', '""', Text::_('COM_BANNERS_HEADING_COUNT')) . '","' 438 . str_replace('"', '""', Text::_('JDATE')) . '"' . "\n"; 439 440 foreach ($this->getItems() as $item) { 441 $this->content .= '"' . str_replace('"', '""', $item->banner_name) . '","' 442 . str_replace('"', '""', $item->client_name) . '","' 443 . str_replace('"', '""', $item->category_title) . '","' 444 . str_replace('"', '""', ($item->track_type == 1 ? Text::_('COM_BANNERS_IMPRESSION') : Text::_('COM_BANNERS_CLICK'))) . '","' 445 . str_replace('"', '""', $item->count) . '","' 446 . str_replace('"', '""', $item->track_date) . '"' . "\n"; 447 } 448 449 if ($this->getState('compressed')) { 450 $app = Factory::getApplication(); 451 452 $files = array( 453 'track' => array( 454 'name' => $this->getBaseName() . '.csv', 455 'data' => $this->content, 456 'time' => time() 457 ) 458 ); 459 $ziproot = $app->get('tmp_path') . '/' . uniqid('banners_tracks_') . '.zip'; 460 461 // Run the packager 462 $delete = Folder::files($app->get('tmp_path') . '/', uniqid('banners_tracks_'), false, true); 463 464 if (!empty($delete)) { 465 if (!File::delete($delete)) { 466 // File::delete throws an error 467 $this->setError(Text::_('COM_BANNERS_ERR_ZIP_DELETE_FAILURE')); 468 469 return false; 470 } 471 } 472 473 $archive = new Archive(); 474 475 if (!$packager = $archive->getAdapter('zip')) { 476 $this->setError(Text::_('COM_BANNERS_ERR_ZIP_ADAPTER_FAILURE')); 477 478 return false; 479 } elseif (!$packager->create($ziproot, $files)) { 480 $this->setError(Text::_('COM_BANNERS_ERR_ZIP_CREATE_FAILURE')); 481 482 return false; 483 } 484 485 $this->content = file_get_contents($ziproot); 486 487 // Remove tmp zip file, it's no longer needed. 488 File::delete($ziproot); 489 } 490 } 491 492 return $this->content; 493 } 494 }
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 |