Source for file tracks.php

Documentation is available at tracks.php

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

Documentation generated on Tue, 19 Nov 2013 15:15:58 +0100 by phpDocumentor 1.4.3