[ Index ]

PHP Cross Reference of Joomla 2.5.4 DE

title

Body

[close]

/administrator/components/com_banners/models/ -> tracks.php (source)

   1  <?php
   2  /**
   3   * @copyright    Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
   4   * @license        GNU General Public License version 2 or later; see LICENSE.txt
   5   */
   6  
   7  defined('_JEXEC') or die;
   8  
   9  jimport('joomla.application.component.modellist');
  10  
  11  /**
  12   * Methods supporting a list of tracks.
  13   *
  14   * @package        Joomla.Administrator
  15   * @subpackage    com_banners
  16   * @since        1.6
  17   */
  18  class BannersModelTracks extends JModelList
  19  {
  20      /**
  21       * Constructor.
  22       *
  23       * @param    array    An optional associative array of configuration settings.
  24       * @see        JController
  25       * @since    1.6
  26       */
  27  	public function __construct($config = array())
  28      {
  29          if (empty($config['filter_fields'])) {
  30              $config['filter_fields'] = array(
  31                  'name', 'b.name',
  32                  'cl.name', 'client_name',
  33                  'cat.title', 'category_title',
  34                  'track_type', 'a.track_type',
  35                  'count', 'a.count',
  36                  'track_date', 'a.track_date',
  37              );
  38          }
  39  
  40          parent::__construct($config);
  41      }
  42  
  43      /**
  44       * @since    1.6
  45       */
  46      protected $basename;
  47  
  48      /**
  49       * Method to auto-populate the model state.
  50       *
  51       * Note. Calling getState in this method will result in recursion.
  52       *
  53       * @since    1.6
  54       */
  55  	protected function populateState($ordering = null, $direction = null)
  56      {
  57          // Initialise variables.
  58          $app = JFactory::getApplication('administrator');
  59  
  60          // Load the filter state.
  61          $type = $this->getUserStateFromRequest($this->context.'.filter.type', 'filter_type');
  62          $this->setState('filter.type', $type);
  63  
  64          $begin = $this->getUserStateFromRequest($this->context.'.filter.begin', 'filter_begin', '', 'string');
  65          $this->setState('filter.begin', $begin);
  66  
  67          $end = $this->getUserStateFromRequest($this->context.'.filter.end', 'filter_end', '', 'string');
  68          $this->setState('filter.end', $end);
  69  
  70          $categoryId = $this->getUserStateFromRequest($this->context.'.filter.category_id', 'filter_category_id', '');
  71          $this->setState('filter.category_id', $categoryId);
  72  
  73          $clientId = $this->getUserStateFromRequest($this->context.'.filter.client_id', 'filter_client_id', '');
  74          $this->setState('filter.client_id', $clientId);
  75  
  76          // Load the parameters.
  77          $params = JComponentHelper::getParams('com_banners');
  78          $this->setState('params', $params);
  79  
  80          // List state information.
  81          parent::populateState('b.name', 'asc');
  82      }
  83  
  84      /**
  85       * Build an SQL query to load the list data.
  86       *
  87       * @return    JDatabaseQuery
  88       * @since    1.6
  89       */
  90  	protected function getListQuery()
  91      {
  92          // Get the application object
  93          $app = JFactory::getApplication();
  94  
  95          require_once  JPATH_COMPONENT.'/helpers/banners.php';
  96  
  97          // Create a new query object.
  98          $db        = $this->getDbo();
  99          $query    = $db->getQuery(true);
 100  
 101          // Select the required fields from the table.
 102          $query->select(
 103                  'a.track_date as track_date,'.
 104                  'a.track_type as track_type,'.
 105                  'a.'.$db->quoteName('count'), ' as '.$db->quoteName('count')
 106          );
 107          $query->from($db->quoteName('#__banner_tracks').' AS a');
 108  
 109          // Join with the banners
 110          $query->join('LEFT', $db->quoteName('#__banners').' as b ON b.id=a.banner_id');
 111          $query->select('b.name as name');
 112  
 113          // Join with the client
 114          $query->join('LEFT', $db->quoteName('#__banner_clients').' as cl ON cl.id=b.cid');
 115          $query->select('cl.name as client_name');
 116  
 117          // Join with the category
 118          $query->join('LEFT', $db->quoteName('#__categories').' as cat ON cat.id=b.catid');
 119          $query->select('cat.title as category_title');
 120  
 121          // Filter by type
 122          $type = $this->getState('filter.type');
 123          if (!empty($type)) {
 124              $query->where('a.track_type = '.(int) $type);
 125          }
 126  
 127          // Filter by client
 128          $clientId = $this->getState('filter.client_id');
 129          if (is_numeric($clientId)) {
 130              $query->where('b.cid = '.(int) $clientId);
 131          }
 132  
 133          // Filter by category
 134          $catedoryId = $this->getState('filter.category_id');
 135          if (is_numeric($catedoryId)) {
 136              $query->where('b.catid = '.(int) $catedoryId);
 137          }
 138  
 139          // Filter by begin date
 140  
 141          $begin = $this->getState('filter.begin');
 142          if (!empty($begin)) {
 143              $query->where('a.track_date >= '.$db->Quote($begin));
 144          }
 145  
 146          // Filter by end date
 147          $end = $this->getState('filter.end');
 148          if (!empty($end)) {
 149              $query->where('a.track_date <= '.$db->Quote($end));
 150          }
 151  
 152          // Add the list ordering clause.
 153          $orderCol = $this->getState('list.ordering', 'name');
 154          $query->order($db->escape($orderCol).' '.$db->escape($this->getState('list.direction', 'ASC')));
 155  
 156          return $query;
 157      }
 158  
 159      /**
 160       * Method to delete rows.
 161       *
 162       * @param    array    An array of item ids.
 163       *
 164       * @return    boolean    Returns true on success, false on failure.
 165       */
 166  	public function delete()
 167      {
 168          // Initialise variables
 169          $user        = JFactory::getUser();
 170          $categoryId    = $this->getState('category_id');
 171  
 172          // Access checks.
 173          if ($categoryId) {
 174              $allow = $user->authorise('core.delete', 'com_banners.category.'.(int) $categoryId);
 175          } else {
 176              $allow = $user->authorise('core.delete', 'com_banners');
 177          }
 178  
 179          if ($allow) {
 180              // Delete tracks from this banner
 181              $db = $this->getDbo();
 182              $query = $db->getQuery(true);
 183              $query->delete();
 184              $query->from($db->quoteName('#__banner_tracks'));
 185  
 186              // Filter by type
 187              $type = $this->getState('filter.type');
 188              if (!empty($type)) {
 189                  $query->where('track_type = '.(int) $type);
 190              }
 191  
 192              // Filter by begin date
 193              $begin = $this->getState('filter.begin');
 194              if (!empty($begin)) {
 195                  $query->where('track_date >= '.$db->Quote($begin));
 196              }
 197  
 198              // Filter by end date
 199              $end = $this->getState('filter.end');
 200              if (!empty($end)) {
 201                  $query->where('track_date <= '.$db->Quote($end));
 202              }
 203  
 204              $where = '1';
 205              // Filter by client
 206              $clientId = $this->getState('filter.client_id');
 207              if (!empty($clientId)) {
 208                  $where.=' AND cid = '.(int) $clientId;
 209              }
 210  
 211              // Filter by category
 212              if (!empty($categoryId)) {
 213                  $where.=' AND catid = '.(int) $categoryId;
 214              }
 215  
 216              $query->where('banner_id IN (SELECT id FROM '.$db->quoteName('#__banners').' WHERE '.$where.')');
 217  
 218              $db->setQuery((string)$query);
 219              $this->setError((string)$query);
 220              $db->query();
 221  
 222              // Check for a database error.
 223              if ($db->getErrorNum()) {
 224                  $this->setError($db->getErrorMsg());
 225                  return false;
 226              }
 227  
 228          } else {
 229              JError::raiseWarning(403, JText::_('JERROR_CORE_DELETE_NOT_PERMITTED'));
 230          }
 231  
 232          return true;
 233      }
 234  
 235      /**
 236       * Get file name
 237       *
 238       * @return    string    The file name
 239       * @since    1.6
 240       */
 241  	public function getBaseName()
 242      {
 243          if (!isset($this->basename)) {
 244  
 245              $app        = JFactory::getApplication();
 246              $basename    = $this->getState('basename');
 247              $basename    = str_replace('__SITE__', $app->getCfg('sitename'), $basename);
 248              $categoryId    = $this->getState('filter.category_id');
 249  
 250              if (is_numeric($categoryId)) {
 251                  if ($categoryId > 0) {
 252                      $basename = str_replace('__CATID__', $categoryId, $basename);
 253                  } else {
 254                      $basename = str_replace('__CATID__', '', $basename);
 255                  }
 256                  $categoryName = $this->getCategoryName();
 257                  $basename = str_replace('__CATNAME__', $categoryName, $basename);
 258              } else {
 259                  $basename = str_replace('__CATID__', '', $basename);
 260                  $basename = str_replace('__CATNAME__', '', $basename);
 261              }
 262  
 263              $clientId = $this->getState('filter.client_id');
 264              if (is_numeric($clientId)) {
 265  
 266                  if ($clientId > 0) {
 267                      $basename = str_replace('__CLIENTID__', $clientId, $basename);
 268                  } else {
 269                      $basename = str_replace('__CLIENTID__', '', $basename);
 270                  }
 271                  $clientName = $this->getClientName();
 272                  $basename = str_replace('__CLIENTNAME__', $clientName, $basename);
 273  
 274              } else {
 275  
 276                  $basename = str_replace('__CLIENTID__', '', $basename);
 277                  $basename = str_replace('__CLIENTNAME__', '', $basename);
 278              }
 279  
 280              $type = $this->getState('filter.type');
 281              if ($type > 0) {
 282  
 283                  $basename = str_replace('__TYPE__', $type, $basename);
 284                  $typeName = JText::_('COM_BANNERS_TYPE'.$type);
 285                  $basename = str_replace('__TYPENAME__', $typeName, $basename);
 286              } else {
 287                  $basename = str_replace('__TYPE__', '', $basename);
 288                  $basename = str_replace('__TYPENAME__', '', $basename);
 289              }
 290  
 291              $begin = $this->getState('filter.begin');
 292              if (!empty($begin)) {
 293                  $basename = str_replace('__BEGIN__', $begin, $basename);
 294              } else {
 295                  $basename = str_replace('__BEGIN__', '', $basename);
 296              }
 297  
 298              $end = $this->getState('filter.end');
 299              if (!empty($end)) {
 300                  $basename = str_replace('__END__', $end, $basename);
 301              } else {
 302                  $basename = str_replace('__END__', '', $basename);
 303              }
 304  
 305              $this->basename = $basename;
 306          }
 307  
 308          return $this->basename;
 309      }
 310  
 311      /**
 312       * Get the category name.
 313       *
 314       * @return    string    The category name
 315       * @since    1.6
 316       */
 317  	protected function getCategoryName()
 318      {
 319          $categoryId = $this->getState('filter.category_id');
 320  
 321          if ($categoryId) {
 322              $db = $this->getDbo();
 323              $query = $db->getQuery(true);
 324              $query->select('title');
 325              $query->from($db->quoteName('#__categories'));
 326              $query->where($db->quoteName('id').'='.$db->quote($categoryId));
 327              $db->setQuery((string)$query);
 328              $name = $db->loadResult();
 329  
 330              if ($db->getErrorNum()) {
 331                  $this->setError($db->getErrorMsg());
 332                  return false;
 333              }
 334          } else {
 335              $name = JText::_('COM_BANNERS_NOCATEGORYNAME');
 336          }
 337  
 338          return $name;
 339      }
 340  
 341      /**
 342       * Get the category name
 343       *
 344       * @return    string    The category name.
 345       * @since    1.6
 346       */
 347  	protected function getClientName()
 348      {
 349          $clientId = $this->getState('filter.client_id');
 350  
 351          if ($clientId) {
 352              $db = $this->getDbo();
 353              $query = $db->getQuery(true);
 354              $query->select('name');
 355              $query->from($db->quoteName('#__banner_clients'));
 356              $query->where($db->quoteName('id').'='.$db->quote($clientId));
 357              $db->setQuery((string)$query);
 358              $name = $db->loadResult();
 359              if ($db->getErrorNum()) {
 360                  $this->setError($db->getErrorMsg());
 361                  return false;
 362              }
 363          } else {
 364              $name = JText::_('COM_BANNERS_NOCLIENTNAME');
 365          }
 366  
 367          return $name;
 368      }
 369  
 370      /**
 371       * Get the file type.
 372       *
 373       * @return    string    The file type
 374       * @since    1.6
 375       */
 376  	public function getFileType()
 377      {
 378          return $this->getState('compressed') ? 'zip' : 'csv';
 379      }
 380  
 381      /**
 382       * Get the mime type.
 383       *
 384       * @return    string    The mime type.
 385       * @since    1.6
 386       */
 387  	public function getMimeType()
 388      {
 389          return $this->getState('compressed') ? 'application/zip' : 'text/csv';
 390      }
 391  
 392      /**
 393       * Get the content
 394       *
 395       * @return    string    The content.
 396       * @since    1.6
 397       */
 398  	public function getContent()
 399      {
 400          if (!isset($this->content)) {
 401  
 402              $this->content = '';
 403              $this->content.=
 404              '"'.str_replace('"', '""', JText::_('COM_BANNERS_HEADING_NAME')).'","'.
 405                  str_replace('"', '""', JText::_('COM_BANNERS_HEADING_CLIENT')).'","'.
 406                  str_replace('"', '""', JText::_('JCATEGORY')).'","'.
 407                  str_replace('"', '""', JText::_('COM_BANNERS_HEADING_TYPE')).'","'.
 408                  str_replace('"', '""', JText::_('COM_BANNERS_HEADING_COUNT')).'","'.
 409                  str_replace('"', '""', JText::_('JDATE')).'"'."\n";
 410  
 411              foreach($this->getItems() as $item) {
 412  
 413                  $this->content.=
 414                  '"'.str_replace('"', '""', $item->name).'","'.
 415                      str_replace('"', '""', $item->client_name).'","'.
 416                      str_replace('"', '""', $item->category_title).'","'.
 417                      str_replace('"', '""', ($item->track_type==1 ? JText::_('COM_BANNERS_IMPRESSION'): JText::_('COM_BANNERS_CLICK'))).'","'.
 418                      str_replace('"', '""', $item->count).'","'.
 419                      str_replace('"', '""', $item->track_date).'"'."\n";
 420              }
 421  
 422              if ($this->getState('compressed')) {
 423                  $app = JFactory::getApplication('administrator');
 424  
 425                  $files = array();
 426                  $files['track']=array();
 427                  $files['track']['name'] = $this->getBasename() . '.csv';
 428                  $files['track']['data'] = $this->content;
 429                  $files['track']['time'] = time();
 430                  $ziproot = $app->getCfg('tmp_path').'/' . uniqid('banners_tracks_') . '.zip';
 431  
 432                  // run the packager
 433                  jimport('joomla.filesystem.folder');
 434                  jimport('joomla.filesystem.file');
 435                  jimport('joomla.filesystem.archive');
 436                  $delete = JFolder::files($app->getCfg('tmp_path').'/', uniqid('banners_tracks_'), false, true);
 437  
 438                  if (!empty($delete)) {
 439                      if (!JFile::delete($delete)) {
 440                          // JFile::delete throws an error
 441                          $this->setError(JText::_('COM_BANNERS_ERR_ZIP_DELETE_FAILURE'));
 442                          return false;
 443                      }
 444                  }
 445  
 446                  if (!$packager = JArchive::getAdapter('zip')) {
 447                      $this->setError(JText::_('COM_BANNERS_ERR_ZIP_ADAPTER_FAILURE'));
 448                      return false;
 449                  } elseif (!$packager->create($ziproot, $files)) {
 450                      $this->setError(JText::_('COM_BANNERS_ERR_ZIP_CREATE_FAILURE'));
 451                      return false;
 452                  }
 453  
 454                  $this->content = file_get_contents($ziproot);
 455              }
 456          }
 457  
 458          return $this->content;
 459      }
 460  }


Generated: Tue Apr 3 11:40:28 2012 Cross-referenced by PHPXref 0.7.1