Files
2019-11-17 19:14:07 +01:00

246 lines
11 KiB
PHP

<?php
/**
* NOTICE OF LICENSE
*
* This source file is subject to a commercial license from 202 ecommerce
* Use, copy, modification or distribution of this source file without written
* license agreement from 202 ecommerce is strictly forbidden.
*
* @author 202 ecommerce <contact@202-ecommerce.com>
* @copyright Copyright (c) 202 ecommerce 2014
* @license Commercial license
*
* Support <support@202-ecommerce.com>
*/
if (!defined('_PS_VERSION_')) {
die(header('HTTP/1.0 404 Not Found'));
}
require_once _PS_MODULE_DIR_.'totloyaltyadvanced/LoyaltyStateModuleAdvanced.php';
require_once _PS_MODULE_DIR_.'totloyaltyadvanced/models/LoyaltyAdvanced.php';
require_once _PS_MODULE_DIR_.'totloyaltyadvanced/classes/TotLoyalty.php';
class TotLoyaltyAdvancedAdminController extends ModuleAdminController
{
public function __construct()
{
$this->bootstrap = true;
// add my css for cient
$this->context = Context::getContext();
$this->className = 'TotLoyalty';
parent::__construct();
$this->identifier = 'id_loyalty';
$this->context->controller->addCSS(_PS_MODULE_DIR_.'totloyaltyadvanced/views/css/banner.css');
$this->addRowAction('view');
$this->show_toolbar_options = true;
$this->table = 'totloyalty';
$this->_select = '
c.`firstname`,
c.`lastname`,
SUM(IF(a.`id_loyalty_state` = '.(int)LoyaltyStateModuleAdvanced::getValidationId().', a.points, 0)) AS conf,
SUM(IF(a.`id_loyalty_state` = '.(int)LoyaltyStateModuleAdvanced::getDefaultId().', a.points, 0)) AS wait,
gl.`name` AS civilite,
c.`email`
';
$this->_join = '
INNER JOIN `'._DB_PREFIX_.'customer` AS c
ON c.`id_customer` = a.`id_customer`
LEFT OUTER JOIN `'._DB_PREFIX_.'orders` AS o
ON o.`id_order` = a.`id_order` '.Shop::addSqlRestriction(false, 'o').'
LEFT OUTER JOIN `'._DB_PREFIX_.'gender_lang` AS gl
ON gl.`id_gender` = c.`id_gender` AND gl.`id_lang` = "'.$this->context->cookie->id_lang.'" ';
$this->_group = 'GROUP BY c.`id_customer`';
$this->_defaultOrderBy = 'conf';
$this->_defaultOrderWay = 'DESC';
$this->fields_list['civilite'] = array(
'title' => $this->module->l('Gender', 'TotLoyaltyAdvancedAdmin'),
'align' => 'center',
);
$this->fields_list['firstname'] = array(
'title' => $this->module->l('Firstname', 'TotLoyaltyAdvancedAdmin'),
'align' => 'center',
);
$this->fields_list['lastname'] = array(
'title' => $this->module->l('Lastname', 'TotLoyaltyAdvancedAdmin'),
'align' => 'center',
);
$this->fields_list['email'] = array(
'title' => $this->module->l('Email', 'TotLoyaltyAdvancedAdmin'),
'align' => 'center',
);
$this->fields_list['conf'] = array(
'title' => $this->module->l('confirmed', 'TotLoyaltyAdvancedAdmin'),
'align' => 'center',
);
$this->fields_list['wait'] = array(
'title' => $this->module->l('pending', 'TotLoyaltyAdvancedAdmin'),
'align' => 'center',
);
}
public function processSave()
{
parent::processSave();
}
public function renderView()
{
$loyalties = $this->getLoyaltiesById(Tools::getValue('id_loyalty'));
$available_pts = 0;
$valid_state = LoyaltyStateModuleAdvanced::getValidationId();
$default_state = LoyaltyStateModuleAdvanced::getDefaultId();
foreach ($loyalties as $loyalty) {
if ($loyalty['id_loyalty_state'] == $valid_state) {
$available_pts += (int)$loyalty['points'];
}
}
$this->context->smarty->assign('loyalties', $loyalties);
$this->context->smarty->assign('available_pts', ($available_pts > 0 ? $available_pts : 0));
$l = new Link();
$this->context->smarty->assign('valid_state', $valid_state);
$this->context->smarty->assign('default_state', $default_state);
$this->context->smarty->assign('linkOrder', $l->getAdminLink('AdminOrders'));
$this->context->smarty->assign('linkBack', $l->getAdminLink('TotLoyaltyAdvancedAdmin'));
$this->context->smarty->assign('linkEdit', $l->getAdminLink('TotLoyaltyAdvancedAdmin').'&updatetotloyalty&id_loyalty=');
$this->context->smarty->assign('linkDelete', $l->getAdminLink('TotLoyaltyAdvancedAdmin').'&deletetotloyalty&id_loyalty=');
$this->context->smarty->assign('linkAdd', $l->getAdminLink('TotLoyaltyAdvancedAdmin').'&addtotloyalty');
$this->context->smarty->assign('module_path', $this->module->getPathUri());
$this->template = 'Admin.tpl';
}
public function renderForm()
{
$this->_postProcess();
if (empty($this->toolbar_title)) {
$this->initToolbarTitle();
}
if (Tools::getIsset('addtotloyalty') || !Tools::getIsset('updatetotloyalty')) {
$legend_title = $this->module->l('Add loyalty', 'TotLoyaltyAdvancedAdmin');
} else {
$legend_title = $this->module->l('Edit loyalty', 'TotLoyaltyAdvancedAdmin');
}
if (Tools::isSubmit('addtotloyalty') && Tools::getValue('id_customer')) {
$sql = 'SELECT firstname, lastname FROM `'._DB_PREFIX_.'customer` WHERE id_customer = '.pSQL(Tools::getValue('id_customer'));
$customer = Db::getInstance()->getRow($sql);
}
$helper = new HelperForm();
$this->setHelperDisplay($helper);
$valid = LoyaltyStateModuleAdvanced::getValidationId();
$this->fields_form = array(
'legend' => array(
'title' => $legend_title,
'image' => '/modules/totloyaltyadvanced/logo.gif',
),
'submit' => array(
'name' => 'submitAddLoyalty',
'title' => $this->module->l('Save', 'TotLoyaltyAdvancedAdmin'),
'class' => (version_compare(_PS_VERSION_, '1.6', '<') ? 'button' : 'btn btn-default pull-right'),
),
'input' => array(
array(
'type' => (Tools::getIsset('addtotloyalty') || !Tools::getIsset('updatetotloyalty') ? 'text' : 'hidden'),
'label' => $this->module->l('Customer:', 'TotLoyaltyAdvancedAdmin'),
'name' => 'customer_name',
'default_value' => (Tools::getValue('id_customer') && isset($customer)) ? $customer['firstname'].' '.$customer['lastname'] : '',
'id' => 'customer_name',
'required' => true,
'autocomplete' => true,
),
array(
'type' => 'text',
'label' => $this->module->l('Points:', 'TotLoyaltyAdvancedAdmin'),
'name' => 'points',
'required' => true,
'desc' => $this->module->l('You can only enter positive numbers, example : 17 or 23', 'TotLoyaltyAdvancedAdmin'),
),
array(
'type' => 'hidden',
'name' => 'id_loyalty_state',
'required' => true,
'default_value' => $valid,
),
array(
'type' => 'hidden',
'name' => 'id_customer',
'id' => 'id_customer',
'default_value' => Tools::getValue('id_customer') ? Tools::getValue('id_customer') : '',
'required' => true,
),
),
);
$this->context->controller->addJS(_PS_JS_DIR_.'/jquery/plugins/autocomplete/jquery.autocomplete.js', 'all');
$this->context->controller->addCSS(_PS_JS_DIR_.'/jquery/plugins/autocomplete/jquery.autocomplete.css');
$this->context->smarty->assign('linkAjax', '../modules/totloyaltyadvanced/ajax/ajaxlistcustomer.php');
$this->context->smarty->assign('error_msg', $this->module->l('You can not enter a negative number', 'TotLoyaltyAdvancedAdmin'));
$customer_ajax = $this->context->smarty->fetch(dirname(__FILE__).'/../../views/templates/admin/loyaltycustomerajax.tpl');
return parent::renderForm().$customer_ajax;
}
public function _postProcess()
{
if (Tools::isSubmit('deletetotloyalty')) {
$l = new Link();
$sql = 'DELETE FROM `'._DB_PREFIX_.'loyalty` WHERE id_loyalty = '.pSQL(Tools::getValue('id_loyalty'));
Db::getInstance()->execute($sql);
}
}
private function getLoyaltiesById($id)
{
$sql = '
SELECT *, gl.name AS gender, l.`date_upd`, l.`id_loyalty_state`, l.`id_customer`, lsl.`name` AS lang_state
FROM `'._DB_PREFIX_.'totloyalty` AS l
INNER JOIN `'._DB_PREFIX_.'customer` AS c
ON c.`id_customer` = l.`id_customer`
INNER JOIN`'._DB_PREFIX_.'totloyalty_state_lang` AS lsl
ON lsl.`id_loyalty_state` = l.`id_loyalty_state` AND lsl.`id_lang` = \''.(int)$this->context->cookie->id_lang.'\'
LEFT OUTER JOIN `'._DB_PREFIX_.'orders` AS o
ON o.`id_order` = l.`id_order` '.Shop::addSqlRestriction(false, 'o').'
LEFT OUTER JOIN `'._DB_PREFIX_.'gender_lang` AS gl
ON gl.`id_gender` = c.`id_gender` AND gl.`id_lang` = \''.(int)$this->context->cookie->id_lang.'\'
WHERE l.`id_customer` = (SELECT `id_customer` FROM `'._DB_PREFIX_.'totloyalty` WHERE `id_loyalty` = '.(int)$id.')
ORDER BY l.`date_upd` DESC ';
$results = Db::getInstance()->executeS($sql);
foreach ($results as $custom) {
$this->id_customer = $custom['id_customer'];
$this->context->smarty->assign('customer', $custom['firstname'].' '.$custom['lastname']);
$this->context->smarty->assign('id_customer', $custom['id_customer']);
$this->context->smarty->assign('gender', isset($custom['gender']) ? '' : '');
}
return $results;
}
private function getCustomers()
{
$temp = array();
$customers = Customer::getCustomers();
foreach ($customers as $key => $value) {
$temp[$key]['names'] = $value['firstname'].' '.$value['lastname'];
$temp[$key]['id_customer'] = $value['id_customer'];
}
return $temp;
}
}