From 224e53ece47505aedeee873e3868959a85f56104 Mon Sep 17 00:00:00 2001 From: Marion Muszynski Date: Mon, 19 Oct 2015 10:44:02 +0200 Subject: [PATCH] export cat nw --- .../20151016135600_newsletter_migration.php | 41 +++++++++ ...1016142247_newsletter_cms_ps_migration.php | 36 ++++++++ .../admin/AdminCmsPsCategories.php | 90 +++++++++++++++++++ .../blocknewsletter/blocknewsletter.php | 90 ++++++++++++++----- 4 files changed, 237 insertions(+), 20 deletions(-) create mode 100644 migrations/20151016135600_newsletter_migration.php create mode 100644 migrations/20151016142247_newsletter_cms_ps_migration.php diff --git a/migrations/20151016135600_newsletter_migration.php b/migrations/20151016135600_newsletter_migration.php new file mode 100644 index 00000000..2db8b804 --- /dev/null +++ b/migrations/20151016135600_newsletter_migration.php @@ -0,0 +1,41 @@ +execute('CREATE TABLE `ps_newsletter_cmsps` ( + `id_newsletter` int(11) NOT NULL, + `id_category` int(11) NOT NULL + ) ENGINE=InnoDB DEFAULT CHARSET=utf8;'); + + $this->execute('ALTER TABLE `ps_newsletter_cmsps` + ADD UNIQUE KEY `id_newsletter` (`id_newsletter`,`id_category`)'); + + } + + +} diff --git a/migrations/20151016142247_newsletter_cms_ps_migration.php b/migrations/20151016142247_newsletter_cms_ps_migration.php new file mode 100644 index 00000000..8daf8866 --- /dev/null +++ b/migrations/20151016142247_newsletter_cms_ps_migration.php @@ -0,0 +1,36 @@ +execute('ALTER TABLE `ps_newsletter_cmsps` + ADD `id_customer` INT(11) NULL, + MODIFY `id_newsletter` INT(11) NULL, + ADD UNIQUE KEY `id_customer` (`id_customer`,`id_category`)'); + + } +} diff --git a/modules/cmsps/controllers/admin/AdminCmsPsCategories.php b/modules/cmsps/controllers/admin/AdminCmsPsCategories.php index ad496d93..2412db71 100644 --- a/modules/cmsps/controllers/admin/AdminCmsPsCategories.php +++ b/modules/cmsps/controllers/admin/AdminCmsPsCategories.php @@ -68,6 +68,55 @@ class AdminCmsPsCategoriesController extends ModuleAdminController { } } + public function initContent(){ + parent::initContent(); + if (Tools::getIsset('export'.$this->table)){ + + $this->display = 'export'; + $this->action = 'export'; + + + } + } + + public function processExport($delimiter=';'){ + + $items = $this->getCatNewsletterSubscriber(Tools::getValue('id_category')); + $filename = $this->table.'_'.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 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); + } + public function renderForm() { if (!($obj = $this->loadObject(TRUE))) return; @@ -199,6 +248,25 @@ class AdminCmsPsCategoriesController extends ModuleAdminController { } } + public function renderList(){ + $this->addRowAction('export'); + return parent::renderList(); + } + + public function displayExportLink($token = null, $id, $name = null) { + $tpl = $this->createTemplate('helpers/list/list_action_edit.tpl'); + if (!array_key_exists('Export', self::$cache_lang)) + self::$cache_lang['Export'] = $this->l('Export', 'Helper'); + + $tpl->assign(array( + 'href' => self::$currentIndex.'&'.$this->identifier.'='.$id.'&export'.$this->table.'&token='.($token != null ? $token : $this->token), + 'action' => self::$cache_lang['Export'], + 'id' => $id + )); + + return $tpl->fetch(); + } + public function getParentPossibility() { $categories_list = CmsPsCategory::getParentPossibility($this->context->language->id, $this->context->shop->id, $this->object->id_category); $categories = array( @@ -216,6 +284,28 @@ class AdminCmsPsCategoriesController extends ModuleAdminController { return $categories; } + public function getCatNewsletterSubscriber($id_category){ + + $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` = '.$this->context->shop->id; + + $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` = '.$this->context->shop->id; + + return Db::getInstance()->ExecuteS('('.$sqlCustomer.') UNION ('.$sqlGuest.')'); + + + } + protected function copyFromPost(&$object, $table) { parent::copyFromPost($object, $table); } diff --git a/override/modules/blocknewsletter/blocknewsletter.php b/override/modules/blocknewsletter/blocknewsletter.php index ff911730..93d73479 100644 --- a/override/modules/blocknewsletter/blocknewsletter.php +++ b/override/modules/blocknewsletter/blocknewsletter.php @@ -8,25 +8,15 @@ class BlockNewsletterOverride extends BlockNewsletter public function install() { if (!parent::install() || !$this->registerHook('displayNewsletterFooter')|| !$this->registerHook('displayleftPostCms')) return false; + /*return Db::getInstance()->execute(' + CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'newsletter_cmsps` ( + `id_customer` int(11) NULL , + `id_newsletter` int(11) NULL, + `id_category` int(11) NOT NULL + ) ENGINE='._MYSQL_ENGINE_.' default CHARSET=utf8');*/ } public function ajaxCall($email, $action = 0, $page, $id_post = false){ - /*$_POST['email'] = $email; - $_POST['action'] = $action; - $_POST['page'] = $page; - if($id_post) - $_POST['id_post'] = $id_post; - $result = $this->newsletterRegistration(); - if($result && $this->error != '') - die(Tools::jsonEncode(array( - 'has_error' => true, - 'message' => $this->error - ))); - elseif($result && $this->valid!='') - die(Tools::jsonEncode(array( - 'has_error' => false, - 'message' => $this->valid - )));*/ if (empty($email) || !Validate::isEmail($email)) return array( @@ -117,9 +107,8 @@ class BlockNewsletterOverride extends BlockNewsletter $register_status = $this->isNewsletterRegistered($_POST['email']); $email = pSQL($_POST['email']); if ($register_status > 0){ - - // Pas d'enregistrement mais envoie du pdf - if (Configuration::get('NW_CONFIRMATION_EMAIL') && $cmsCategory){ + if (Configuration::get('NW_CONFIRMATION_EMAIL') && $cmsCategory && $this->isNewsletterCmsPsRegistered($email, $cmsCategory['id_category'], $register_status)){ + // enregistrement pour la categorie si pas enregistré pour $this->valid = $this->l('You will recieve your PDF.'); $this->sendConfirmationEmail($email, $cmsCategory); } @@ -156,7 +145,7 @@ class BlockNewsletterOverride extends BlockNewsletter if ($code = Configuration::get('NW_VOUCHER_CODE') && !$cmsCategory && !$genericPdf) $this->sendVoucher($email, Configuration::get('NW_VOUCHER_CODE')); - elseif (Configuration::get('NW_CONFIRMATION_EMAIL') && $cmsCategory) + elseif (Configuration::get('NW_CONFIRMATION_EMAIL') && $cmsCategory && $this->isNewsletterCmsPsRegistered($email, $cmsCategory['id_category'], $register_status)) $this->sendConfirmationEmail($email, $cmsCategory); elseif (Configuration::get('NW_CONFIRMATION_EMAIL') && $genericPdf) $this->sendConfirmationEmail($email, false, $genericPdf); @@ -194,6 +183,66 @@ class BlockNewsletterOverride extends BlockNewsletter } + protected function isNewsletterCmsPsRegistered($email, $id_category, $register_status) + { + if($register_status == self::GUEST_REGISTERED) + $sql = 'SELECT n.`id` + FROM '._DB_PREFIX_.'newsletter n + LEFT JOIN '._DB_PREFIX_.'newsletter_cmsps nc ON (nc.`id_newsletter` = n.`id`) + WHERE n.`email` = \''.pSQL($email).'\' + AND n.`id_shop` = '.$this->context->shop->id.' + AND nc.`id_category`='.(int)$id_category; + elseif($register_status == self::CUSTOMER_REGISTERED) + $sql = 'SELECT c.`id_customer` + FROM '._DB_PREFIX_.'customer c + LEFT JOIN '._DB_PREFIX_.'newsletter_cmsps nc ON (nc.`id_customer` = c.`id_customer`) + WHERE c.`email` = \''.pSQL($email).'\' + AND c.`newsletter` = 1 + AND nc.`id_customer` IS NOT NULL + AND c.`id_shop` = '.$this->context->shop->id; + else + return false; + + if ($test = Db::getInstance()->getRow($sql)){ + return true; + } + else{ + return $this->registerNewsletterCmsPs($email, $id_category, $register_status); + } + + } + + protected function registerNewsletterCmsPs($email, $id_category, $register_status) + { + if($register_status == self::GUEST_REGISTERED) + $sql = 'INSERT INTO '._DB_PREFIX_.'newsletter_cmsps (id_newsletter, id_category) + VALUES + ( + ( + SELECT n.`id` + FROM '._DB_PREFIX_.'newsletter n + WHERE n.`email` = \''.pSQL($email).'\' + ), + '.(int)$id_category.' + )'; + elseif($register_status == self::CUSTOMER_REGISTERED) + $sql = 'INSERT INTO '._DB_PREFIX_.'newsletter_cmsps (id_category, id_customer) + VALUES + ( + '.(int)$id_category.', + ( + SELECT c.`id_customer` + FROM '._DB_PREFIX_.'customer c + WHERE c.`email` = \''.pSQL($email).'\' + AND c.`newsletter` = \'1\' + ) + )'; + + //echo "
";var_dump($sql);echo "
";die(); + + return Db::getInstance()->execute($sql); + } + /** * @Override @@ -204,6 +253,7 @@ class BlockNewsletterOverride extends BlockNewsletter $fileAttachment = array(); if($cmsCategory){ if(glob(_CMS_CAT_IMG_DIR_.'pdf/Tout-pratique-'.$cmsCategory['slug'].'.pdf')){ + echo "
";var_dump('in pdf cat');echo "
";die(); $fileAttachment['content'] = _CMS_CAT_IMG_DIR_.'pdf/Tout-pratique-'.$cmsCategory['slug'].'.pdf'; $fileAttachment['name'] = 'Tout-pratique-'.$cmsCategory['slug'].'.pdf'; $fileAttachment['mime'] = "application/pdf";