Skip to content

Instantly share code, notes, and snippets.

@dongilbert
Created December 3, 2012 15:00
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save dongilbert/4195504 to your computer and use it in GitHub Desktop.
Save dongilbert/4195504 to your computer and use it in GitHub Desktop.
Joomla Export to CSV
<?php
/**
* @version 1.0.0
* @package com_stats
* @copyright Copyright (C) 2012. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
// No direct access.
defined('_JEXEC') or die;
jimport('joomla.application.component.controlleradmin');
class StatsControllerLeads extends JControllerAdmin
{
/**
* Proxy for getModel.
* @since 1.6
*/
public function getModel($name = 'Leads', $prefix = 'StatsModel')
{
$model = parent::getModel($name, $prefix, array('ignore_request' => true));
return $model;
}
public function export()
{
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=leads.csv");
header("Pragma: no-cache");
header("Expires: 0");
$this->getModel()->getCsv();
jexit();
}
}
<?php
/**
* @version 1.0.0
* @package com_stats
* @copyright Copyright (C) 2012. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
// No direct access.
defined('_JEXEC') or die;
jimport('joomla.application.component.modellist');
class StatsModelLeads extends JModelList
{
/**
* Constructor.
*
* @param array An optional associative array of configuration settings.
* @see JController
* @since 1.6
*/
public function __construct($config = array())
{
if (empty($config['filter_fields']))
{
$config['filter_fields'] = array(
'id', 'a.id',
'created_time', 'a.created_time',
'form_type', 'a.form_type',
'first_name', 'a.first_name',
'last_name', 'a.last_name',
'email', 'a.email'
);
}
parent::__construct($config);
}
/**
* Method to auto-populate the model state.
*
* Note. Calling getState in this method will result in recursion.
*/
protected function populateState($ordering = null, $direction = null)
{
// Initialise variables.
$app = JApplication::getInstance('administrator');
// Load the filter state.
$filter_form_type = $app->getUserStateFromRequest($this->context.'.filter.form_type', 'filter_form_type');
$this->setState('filter.form_type', $filter_form_type);
$filter_start_date = $app->getUserStateFromRequest($this->context.'.filter.start_date', 'filter_start_date');
$this->setState('filter.start_date', $filter_start_date);
$filter_end_date = $app->getUserStateFromRequest($this->context.'.filter.end_date', 'filter_end_date');
$this->setState('filter.end_date', $filter_end_date);
// Load the parameters.
$params = JComponentHelper::getParams('com_stats');
$this->setState('params', $params);
// List state information.
parent::populateState('a.first_name', 'asc');
}
/**
* Build an SQL query to load the list data.
*
* @return JDatabaseQuery
* @since 1.6
*/
protected function getListQuery()
{
// Create a new query object.
$db = $this->getDbo();
$query = $db->getQuery(true);
// Select the required fields from the table.
$query->select('a.*');
$query->from('`#__stats_leads` AS a');
// Filtering form_type
$filter_form_type = $this->getState("filter.form_type");
if ($filter_form_type)
{
$query->where("a.form_type = '{$filter_form_type}'");
}
$filter_start_date = (string) $this->getState("filter.start_date");
$filter_end_date = (string) $this->getState("filter.end_date");
// Filtering start_date
if ($filter_start_date !== '')
{
$query->where("a.created_time >= '{$filter_start_date} 00:00:00'");
}
// Filtering end_date
if ($filter_end_date !== '')
{
$query->where("a.created_time <= '{$filter_end_date} 23:59:59'");
}
return $query;
}
public function getCsv()
{
$this->populateState();
$db = $this->getDbo();
$cols = array_keys($db->getTableColumns('#__stats_leads'));
$items = $db->setQuery($this->getListQuery())->loadObjectList();
$csv = fopen('php://output', 'w');
fputcsv($csv, $cols);
foreach ($items as $line)
{
fputcsv($csv, (array) $line);
}
return fclose($csv);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment