Your IP : 216.73.216.240
<?php
/*
* @package Tech Fry Library
* @license https://www.gnu.org/licenses/gpl-3.0.en.html
*/
namespace TechFry\Library\Controller;
defined('_JEXEC') or die;
use Joomla\CMS\Application\CMSApplication;
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\CMS\MVC\Controller\BaseController;
use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
use Joomla\Input\Input;
use Joomla\CMS\Router\Route;
use TechFry\Library\TDb;
use TechFry\Library\TField;
class TfControllerExport extends BaseController
{
public $option;
public $info;
public $rows;
public $first_row; // Headers
public function __construct($config = [], ?MVCFactoryInterface $factory = null, ?CMSApplication $app = null, ?Input $input = null)
{
parent::__construct($config, $factory, $app, $input);
$this->option = $this->input->get('option');
}
// *****************************************************************************
public function expo()
{
$model = $this->getModel();
$this->info = $this->input->get('jform', [], 'array');
if (!$model->pro())
{
$this->setMessage(Text::_('COM_TF_PRO_UPGRADE_MESSAGE'), 'error');
$this->setRedirect(Route::_('index.php?option=' . $this->option . '&view=export&layout=edit&db_table=' . $this->info['db_table'], false));
return;
}
if (!$this->info['db_table'])
{
$this->setMessage(Text::_('Please enter database table name.'), 'error');
$this->setRedirect(Route::_('index.php?option=' . $this->option . '&view=export&layout=edit&db_table=' . $this->info['db_table'], false));
return;
}
if (empty($this->info['select_fields']))
{
$this->setMessage(Text::_('Please select or add columns to export.'), 'error');
$this->setRedirect(Route::_('index.php?option=' . $this->option . '&view=export&layout=edit&db_table=' . $this->info['db_table'], false));
return;
}
if (!\is_array($this->info['select_fields']))
{
$this->info['select_fields'] = explode(',', $this->info['select_fields']);
}
if (isset($this->info['json_fields']))
{
if (!\is_array($this->info['json_fields']))
{
$this->info['json_fields'] = explode(',', $this->info['json_fields']);
}
}
else
{
$this->info['json_fields'] = [];
}
$this->first_row = $this->info['select_fields'];
// Conditions
$conditions = [];
if ($this->info['filter'])
{
foreach ($this->info['filter'] as $filter)
{
if ($filter['where_field'] != '' && $filter['where_value'] != '')
{
$conditions[] = array($filter['where_field'], $filter['where_operator'], $filter['where_value']);
}
elseif ($filter['where_field'] != '' && $filter['where_value'] == '')
{
$conditions[] = array($filter['where_field'], $filter['where_operator']);
}
}
}
$db = new TDb($this->info['db_table']);
$select = array_merge($this->info['select_fields'], $this->info['json_fields']);
$this->rows = $db->get_items(array('select' => $select, 'conditions' => $conditions, 'order' => $this->info['order'], 'limit' => $this->info['limit']));
// Add custom fields
$this->add_custom_fields();
// Handle JSON fields
$this->handle_json_fields();
// Export data
$filename = $this->info['db_table'] . '.csv';
// Create a file pointer
$fp = fopen('php://output', 'w');
// First row is heading
fputcsv($fp, $this->first_row);
foreach ($this->rows as $row)
{
$row = (array) $row;
fputcsv($fp, $row);
}
// Set headers to download file rather than display
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="' . $filename . '";');
exit;
}
// Add cistom fields
public function add_custom_fields()
{
if (!empty($this->info['custom_fields']))
{
$fields = str_getcsv($this->info['custom_fields']);
$this->first_row = array_merge($this->first_row, $fields);
}
else
{
return;
}
foreach ($this->rows as $i => $row)
{
$row = (array) $row;
foreach ($fields as $field)
{
// Field is custom field
if (\is_numeric($field))
{
$cf = new TField();
$this->rows[$i]->$field = $cf->get_custom_field($field, $row['id']);
}
}
}
}
// Handle JSON fields
public function handle_json_fields()
{
if (!empty($this->info['json_fields']))
{
$jsons = $this->info['json_fields'];
foreach ($this->rows as $i => $row)
{
$row = (array) $row;
foreach ($jsons as $json)
{
$arr = json_decode($row[$json], true);
if ($i == 0)
{
$this->first_row = array_merge($this->first_row, array_keys($arr));
}
foreach ($arr as $k => $v)
{
$this->rows[$i]->$k = $v;
}
unset($this->rows[$i]->$json);
}
}
}
}
public function cancel($key = null)
{
$this->checkToken();
$this->setRedirect(Route::_('index.php?option=' . $this->option . '&view=dashboard', false));
return true;
}
}