toutpratique/modules/cmsps/controllers/admin/AdminCmsPsExport.php

146 lines
5.0 KiB
PHP
Raw Normal View History

2015-10-19 17:58:23 +02:00
<?php
include_once dirname(__FILE__).'/../../cmsps.php';
class AdminCmsPsExportController extends ModuleAdminController {
public function __construct() {
$this->display = 'view';
$this->bootstrap = true;
parent::__construct();
}
public function initContent(){
parent::initContent();
if (Tools::getValue('exportByCat')) {
$items = $this->getCatNewsletterSubscriber(Tools::getValue('id_category'), $this->context->shop->id);
$filename = 'category_suscribers_'.date('Y-m-d_His').'.csv';
header('Content-Type: application/force-download; charset=UTF-8');
header('Content-disposition: attachment; filename="'.$filename.'"');
$this->exportCsv($items, $filename);
die();
} elseif (Tools::getValue('exportByShop')) {
$items = $this->getCatNewsletterSubscriber(9999, $this->context->shop->id);
$filename = 'shop_suscribers_'.date('Y-m-d_His').'.csv';
header('Content-Type: application/force-download; charset=UTF-8');
header('Content-disposition: attachment; filename="'.$filename.'"');
$this->exportCsv($items, $filename);
die();
} elseif (Tools::getValue('exportAll')) {
$items = $this->getAllSubscribers($this->context->shop->id);
$filename = 'all_suscribers_'.date('Y-m-d_His').'.csv';
header('Content-Type: application/force-download; charset=UTF-8');
header('Content-disposition: attachment; filename="'.$filename.'"');
$this->exportCsv($items, $filename);
die();
}
}
public function renderView() {
$pathTemplate = dirname(dirname(__DIR__)).'/views/templates/admin';
$exportByCat = array(
'submit' => $this->l('Exporter'),
);
$exportByShop = array(
'submit' => $this->l('Exporter'),
);
$exportAll = array(
'submit' => $this->l('Exporter tous les inscrits'),
);
$categories = $this->getNewsletterCategories($this->context->language->id, $this->context->shop->id);
$this->context->smarty->assign(array(
'exportByCat'=> $exportByCat,
'exportByShop'=> $exportByShop,
'exportAll'=> $exportAll,
'categories' => $categories,
));
return $this->context->smarty->fetch($pathTemplate.'/cmsExport.tpl');
}
public function getNewsletterCategories($id_lang, $id_shop){
$sql = 'SELECT DISTINCT c.`id_category`, cl.`title`
FROM '._DB_PREFIX_.'cmsps_categories c
LEFT JOIN '._DB_PREFIX_.'cmsps_categories_lang cl ON (cl.`id_category` = c.`id_category`)
LEFT JOIN '._DB_PREFIX_.'newsletter_cmsps nc ON (nc.`id_category` = c.`id_category`)
WHERE nc.`id_category` IS NOT NULL AND cl.`id_lang`='.(int)$id_lang;
return Db::getInstance()->executeS($sql);
}
public function getAllSubscribers($id_shop)
{
$sqlCustomer = 'SELECT c.`email`, ("oui") as is_customer
FROM '._DB_PREFIX_.'customer c
WHERE c.`newsletter` = 1
AND c.`id_shop` = '.(int)$id_shop;
$sqlGuest = 'SELECT n.`email`, ("non") as is_customer
FROM '._DB_PREFIX_.'newsletter n
WHERE n.`id_shop` = '.(int)$id_shop;
return Db::getInstance()->ExecuteS('('.$sqlCustomer.') UNION ('.$sqlGuest.')');
}
public function getCatNewsletterSubscriber($id_category, $id_shop)
{
$sqlCustomer = 'SELECT c.`email`, ("oui") as is_customer
FROM '._DB_PREFIX_.'customer c
LEFT JOIN '._DB_PREFIX_.'newsletter_cmsps nc ON (nc.`id_customer` = c.`id_customer`)
WHERE nc.`id_category` = '.(int)$id_category.'
AND c.`newsletter` = 1
AND nc.`id_customer` IS NOT NULL
AND c.`id_shop` = '.(int)$id_shop;
$sqlGuest = 'SELECT n.`email`, ("non") as is_customer
FROM '._DB_PREFIX_.'newsletter n
LEFT JOIN '._DB_PREFIX_.'newsletter_cmsps nc ON (nc.`id_newsletter` = n.`id`)
WHERE nc.`id_category` = '.(int)$id_category.'
AND nc.`id_newsletter` IS NOT NULL
AND n.`id_shop` = '.(int)$id_shop;
return Db::getInstance()->ExecuteS('('.$sqlCustomer.') UNION ('.$sqlGuest.')');
}
public function exportCsv($items, $filename)
{
$row_definition = array(
'email' => 'email',
'is_customer' => 'is_customer'
);
//open file pointer to standard output
$fp = fopen("php://output", 'w+');
$delim = ';';
// first row
$data = array();
foreach ($row_definition as $col => $index) {
$data[] = $col;
}
fputcsv ($fp,$data,$delim);
foreach ($items as $item) {
$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);
}
}