Serveur preprod f0c0c48223 first push
2016-04-14 16:14:31 +02:00

266 lines
9.4 KiB
PHP
Executable File

<?php
include_once(_PS_MODULE_DIR_.'antadismarketing/antadismarketing.php');
class AdminAntMarketingStatsController extends ModuleAdminController {
public $tracking_category = null;
public function __construct()
{
$this->bootstrap = true;
parent::__construct();
}
public function renderView()
{
$id_tracking = Tools::getValue('id_tracking');
if(Validate::isLoadedObject($tracking = new AntTracking($id_tracking)))
{
$array_tracking = array('name_tracking' => $tracking->name);
$nb_visits = AntTracking::getNbVisitsByTracking($id_tracking);
$list_customers = AntTracking::getCustomersByTracking($id_tracking);
$list_orders = AntTracking::getOrdersByTracking($id_tracking);
$this->context->smarty->assign(array(
'tracking' => $tracking,
'name_tracking' => $array_tracking,
'list_customers' => $list_customers,
'list_orders' => $list_orders,
'nb_visits' => $nb_visits,
'number_customers' => count($list_customers),
'number_orders' => count($list_orders)
));
}
return parent::renderView();
}
public function postProcess()
{
if(Tools::isSubmit('exportCustomer')){
$id_tracking = Tools::getValue('id_tracking');
$list_customers = AntTracking::getCustomersByTracking($id_tracking);
$name = Tools::getValue('name_tracking');
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=export_tracking_'.$name.'.csv');
$output = fopen('php://output', 'w');
fputcsv($output, array('Id', 'Email', 'Firstname', 'Lastname', 'Subscribe', 'Newsletter' ));
foreach ($list_customers as $key => $customer) {
fputcsv($output, $customer);
}
die();
}
return parent::postProcess();
}
public function setMedia()
{
parent::setMedia();
$this->addJS(_MODULE_DIR_.$this->module->name.'/js/admin.js');
$this->addCSS(_MODULE_DIR_.$this->module->name.'/css/admin.css', 'all');
$this->addJqueryUI(array('ui.widget', 'ui.slider', 'ui.datepicker'));
}
public function display()
{
$date_from = date('d/m/Y');
$date_to = date('d/m/Y');
$last_update_date = new DateTime(Configuration::get('ANT_MARKETING_STATS_LAST_UPDATE'));
$date_now = new DateTime(date('Y-m-d H:i:s'));
$interval = $date_now->diff($last_update_date);
$last_update_minutes = $interval->format('%i');
$this->context->smarty->assign(array(
'date_from' => $date_from,
'date_to' => $date_to,
'last_update_minutes' => $last_update_minutes
));
$this->setTemplate('index.tpl');
parent::display();
}
public function copyFromPost(&$object, $table)
{
parent::copyFromPost($object, $table);
if(Tools::isSubmit('submitTracking')){
$object->redirection = $object->redirection . '?tracking=' .$object->name;
}
}
public function run()
{
if(Tools::getValue('run_token') == 'fgsdfsoiubniuiuoqsoijPDMIOIUIOIIO455778954562JHIUGYDZSUDS')
{
AntStats::getOrdersTotalByDate();
AntStats::getOrdersCategoryTrackingTotal();
Configuration::updateValue('ANT_MARKETING_STATS_LAST_UPDATE', date('Y-m-d H:i:s'));
exit;
}
parent::run();
}
public function initTrackingCategory()
{
if(!Tools::getValue('id_tracking_category') || !Validate::isLoadedObject($this->tracking_category = new AntTrackingCategories(Tools::getValue('id_tracking_category'), $this->context->language->id)))
throw new PrestaShopException(Tools::displayError('There is an invalide category'));
return true;
}
public function displayAjaxError($string = 'error')
{
$array = array(
'hasError' => true,
'error' => $string
);
die ( json_encode($array) );
}
public function displayAjaxGetList()
{
try {
$this->initTrackingCategory();
$date_from = null;
$date_to = null;
if(Tools::getValue('check_by_date') && Tools::getValue('date_from') && Tools::getValue('date_to'))
{
$format = 'd/m/Y';
$date_from = DateTime::createFromFormat($format, Tools::getValue('date_from', '01/01/2013') );
$date_to = DateTime::createFromFormat($format, Tools::getValue('date_to', date('d/m/Y') ) );
$date_from = $date_from->format('Y-m-d');
$date_to = $date_to->format('Y-m-d');
}
if(Tools::getValue('order') != '' && in_array(Tools::getValue('orderby'), array('ASC', 'DESC')))
{
$orderby = Tools::getValue('order');
$ordered = Tools::getValue('orderby');
}
else
$ordered = $orderby = null;
$export = Tools::getValue('export') == 1 ? true : false;
$exportSource = Tools::getValue('exportSource') == 1 ? true : false;
if($this->context->cookie->profile && (int) $this->context->cookie->profile > 6)
$id_profile = (int) $this->context->cookie->profile;
else
$id_profile = 0;
$lines_tracking = $this->tracking_category->getStats('lines_tracking', $date_from, $date_to, $orderby, $ordered, $id_profile);
$lines = $this->tracking_category->getStats('lines', $date_from, $date_to, $orderby, $ordered, $id_profile);
$total = $this->tracking_category->getStats('global', $date_from, $date_to, null, null, $id_profile);
$columns = $this->tracking_category->getDisplayColumn();
if($export === true) {
$tpl = $this->createTemplate('export/tracking.tpl');
$tpl->assign(array(
'columns' => $columns,
'lines' => $lines,
'lines_tracking' => $lines_tracking,
'total' => $total
));
$name = $this->tracking_category->name;
if($date_from !== null && $date_to !== null)
$name .= ' '.sprintf($this->l('BETWEEN %s and %s'), $date_from, $date_to);
self::makeCSV($tpl->fetch(), $name);
} elseif ($exportSource === true ) {
$ids = AntTrackingCategories::getAllChildrenAndGrandChildren($this->tracking_category->id);
$export = AntTracking::getAllCustomersByTrackings($ids);
self::exportCsv($export, "export-stats-".date('Ymd'));
} else {
$tpl = $this->createTemplate('tracking_stats.tpl');
$tpl->assign(array(
'columns' => $columns,
'orderby' => $orderby,
'ordered' => $ordered,
'lines' => $lines,
'lines_tracking' => $lines_tracking,
'total' => $total,
'category_name' => $this->tracking_category->name,
'breadcrumb' => $this->tracking_category->getParentCategories(),
'export_link' => $_SERVER['REQUEST_URI'].'&export=1',
'export_source_link' => $_SERVER['REQUEST_URI'].'&exportSource=1'
));
die( json_encode($tpl->fetch()) );
}
}
catch(PrestashopException $e) {
$this->displayAjaxError($e->getMessage());
}
}
public static function makeCSV($content, $name = 'Export')
{
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$name.".csv\";" );
header("Content-Transfer-Encoding: binary");
echo $content;
exit;
}
public static function exportCsv($items){
header("Content-Type: text/csv; charset=UTF-8");
header("Content-Disposition: attachment;filename='export-stats-".date('Ymd').".csv'");
$row_definition = array(
'Tracking' => 'name',
'Id client' => 'id_customer',
'Email' => 'email',
'Date inscription' => 'date_add',
'Newsletter' => 'newsletter'
);
$fp = fopen("php://output", 'w');
$delim = ';';
$data=array();
foreach ($row_definition as $col => $index)
$data[]=$col;
fputcsv ($fp,$data,$delim);
foreach ($items as $item) {
if ($item['newsletter'] == 1) {
$item['newsletter'] = 'OUI';
} else {
$item['newsletter'] = '';
}
$data = array();
foreach ($row_definition as $index) {
$data[] = (isset($item[$index]) ? $item[$index] : '');
}
fputcsv ($fp,array_map('utf8_decode',array_values($data)),$delim);
}
fclose($fp);
}
}