name = 'sonice_retour'; $this->tab = 'shipping_logistics'; $this->version = '2.0.09'; $this->author = 'Common-Services'; $this->need_instance = 0; $this->limited_countries = $this->available_countries; $this->module_key = 'c9b0bd56ba47b0d88b9964dacc6ab652'; parent::__construct(); $this->displayName = 'SoNice Retour'; $this->description = $this->l( 'This service permits to generate labels with or without proof of submission according to your contract.' ); $this->path = _PS_MODULE_DIR_.$this->name.'/'; $this->url = __PS_BASE_URI__.basename(_PS_MODULE_DIR_).'/'.$this->name.'/'; $this->download_folder = $this->path.'download/'; $this->http_download_folder = $this->url.'download/'; $this->function_folder = $this->url.'functions/'; $this->change_return_state_url = $this->function_folder.'changeReturnState.php'; $this->action_merchandise_return_url = $this->function_folder.'changeReturnState.php'; $this->get_label_url = $this->function_folder.'get_labels.php'; $this->bootstrap = true; $this->initContext(); } /** * Init Prestashop context for version 1.5 & 1.4 */ private function initContext() { if (version_compare(_PS_VERSION_, '1.6', '>=')) { $this->ps16x = true; } if (version_compare(_PS_VERSION_, '1.5', '>=')) { $this->context = Context::getContext(); $this->id_lang = (int)Context::getContext()->language->id; $this->ps15x = true; } else { require_once(_PS_MODULE_DIR_.'sonice_retour/backward_compatibility/backward.php'); $this->context = Context::getContext(); $this->id_lang = (int)Context::getContext()->language->id; $this->ps15x = false; } $this->context->smarty->assign(array( 'ps16x' => $this->ps16x, 'ps15x' => $this->ps15x, 'snr_url' => $this->url, 'snr_css' => $this->url.'views/css/', 'snr_js' => $this->url.'views/js/', 'snr_img' => $this->url.'views/img/' )); $this->debug = (bool)Configuration::get('ETIQUETAGE_RETURN_DEBUG'); } /** * Set up hooks * * @param string $action * @return bool */ private function hookSetup($action) { if ($this->ps15x) { $expected_hooks = array( 'displayAdminOrder', 'actionCarrierUpdate', 'actionOrderReturn' ); } else { $expected_hooks = array( 'adminOrder', 'updateCarrier', 'orderReturn' ); } $pass = true; if (in_array($action, array(self::REMOVE, self::UPDATE))) { foreach ($expected_hooks as $expected_hook) { if (!$this->unregisterHook($expected_hook)) { $pass = false; } } } if (in_array($action, array(self::ADD, self::UPDATE))) { foreach ($expected_hooks as $expected_hook) { if (!$this->registerHook($expected_hook)) { $pass = false; } } } return ($pass); } private function myAddJS($url) { if ($this->ps15x) { $this->context->controller->addJS($url); } else { echo ''; } } private function myAddJQueryUI($name = null) { if ($this->ps15x) { $this->context->controller->addJqueryUI($name); } else { echo ''; } return (true); } private function myAddCSS($url, $media) { if ($this->ps15x) { return ($this->context->controller->addCSS($url, $media)); } else { echo ''; } } private function backofficeInformations() { $php_infos = array(); $module_infos = array(); $module_config = unserialize(Configuration::get('ETIQUETAGE_RETURN_CONF')); $prestashop_infos = array(); // Web service settings if (!isset($module_config['ContractNumber']) || empty($module_config['ContractNumber'])) { $module_infos['ws_login']['message'] = $this->l( 'You did not set a login yet, please fill the login field in the Login tab.' ); $module_infos['ws_login']['level'] = $this->ps16x ? 'alert alert-warning' : 'warn'; } if (!isset($module_config['Password']) || empty($module_config['Password'])) { $module_infos['ws_pwd']['message'] = $this->l( 'You did not set a password yet, please fill the password field in the Login tab.' ); $module_infos['ws_pwd']['level'] = $this->ps16x ? 'alert alert-warning' : 'warn'; } if (!isset($module_config['Line2']) || empty($module_config['Line2']) || !isset($module_config['countryCode']) || empty($module_config['countryCode']) || !isset($module_config['City']) || empty($module_config['City']) || !isset($module_config['PostalCode']) || empty($module_config['PostalCode'])) { $module_infos['ws_address']['message'] = $this->l( 'You did not set an address yet, please fill the address fields in the Address tab.' ); $module_infos['ws_address']['level'] = $this->ps16x ? 'alert alert-warning' : 'warn'; } // PHP if (!is_writable(_PS_MODULE_DIR_.'sonice_retour/download')) { $php_infos['dl_w']['message'] = sprintf($this->l( 'You have to set write permissions to the %s directory' ), _PS_MODULE_DIR_.'sonice_retour/download'); $php_infos['dl_w']['level'] = $this->ps16x ? 'alert alert-warning' : 'warn'; } if (!method_exists('DOMDocument', 'createElement')) { $php_infos['domdocument']['message'] = $this->l( 'PHP DOMDocument (XML Library) must be installed on this server. '. 'The module require this library and can\'t work without it.' ); $php_infos['domdocument']['level'] = $this->ps16x ? 'alert alert-danger' : 'error'; $php_infos['domdocument']['link'] = 'http://php.net/manual/'.Language::getIsoById($this->id_lang). '/class.domdocument.php'; } if (!function_exists('curl_init')) { $php_infos['curl']['message'] = $this->l('cURL extension must be available on your server.'); $php_infos['cur;']['level'] = $this->ps16x ? 'alert alert-danger' : 'error'; $php_infos['curl']['link'] = 'http://php.net/manual/'.Language::getIsoById($this->id_lang).'/book.curl.php'; } if (in_array(@Tools::strtolower(ini_get('display_errors')), array('1', 'on'))) { $php_infos['display_errors']['message'] = $this->l('PHP variable display_errors is On.'); $php_infos['display_errors']['level'] = $this->ps16x ? 'alert alert-info' : 'info'; } if (!function_exists('mb_convert_encoding')) { $php_infos['mbstring']['message'] = $this->l( 'PHP mb_string functions must be installed on this server. '. 'The module require this library and can\'t work without it.' ); $php_infos['mbstring']['level'] = $this->ps16x ? 'alert alert-danger' : 'error'; $php_infos['mbstring']['link'] = 'http://php.net/manual/'.Language::getIsoById($this->id_lang). '/mbstring.setup.php'; } // PrestaShop if (!(int)Configuration::get('PS_SHOP_ENABLE')) { $prestashop_infos['maintenance']['message'] = $this->l( 'Be carefull, your shop is in maintenance mode, the module might not work in that mode' ); $prestashop_infos['maintenance']['level'] = $this->ps16x ? 'alert alert-warning' : 'warn'; } if (_PS_MODE_DEV_) { $prestashop_infos['dev_mode']['message'] = $this->l('The Prestashop constant _PS_MODE_DEV_ is enabled.'); $prestashop_infos['dev_mode']['level'] = $this->ps16x ? 'alert alert-info' : 'info'; } // URL issues for Ajax $pass = true; if (version_compare(_PS_VERSION_, '1.5', '>=')) { if (Shop::isFeatureActive()) { $shop = Context::getContext()->shop; if ($_SERVER['HTTP_HOST'] != $shop->domain && $_SERVER['HTTP_HOST'] != $shop->domain_ssl) { $pass = false; } } else { $urls = ShopUrl::getShopUrls($this->context->shop->id)->where('main', '=', 1)->getFirst(); if ($_SERVER['HTTP_HOST'] != $urls->domain && $_SERVER['HTTP_HOST'] != $urls->domain_ssl) { $pass = false; } } } elseif (version_compare(_PS_VERSION_, '1.4', '>=')) { if ($_SERVER['HTTP_HOST'] != Configuration::get('PS_SHOP_DOMAIN') && $_SERVER['HTTP_HOST'] != Configuration::get('PS_SHOP_DOMAIN_SSL')) { $pass = false; } } if (!$pass) { $prestashop_infos['wrong_domain']['message'] = $this->l( 'Your are currently connected with the following domain name:' ).' '.$_SERVER['HTTP_HOST'].'. '.$this->l( 'This one is different from the main shop domain name set in "Preferences > SEO & URLs":' ).' '.Configuration::get('PS_SHOP_DOMAIN').'...'; $prestashop_infos['wrong_domain']['level'] = $this->ps16x ? 'alert alert-danger' : 'error'; } $view_params = array(); $view_params['module_infos'] = $module_infos; $view_params['module_info_ok'] = !count($module_infos); $view_params['php_infos'] = $php_infos; $view_params['php_info_ok'] = !count($php_infos); $view_params['prestashop_infos'] = $prestashop_infos; $view_params['prestashop_info_ok'] = !count($prestashop_infos); ob_start(); try { @phpinfo(INFO_ALL & ~INFO_CREDITS & ~INFO_LICENSE & ~INFO_ENVIRONMENT & ~INFO_VARIABLES); } catch (Exception $excp) { echo 'phpinfo() has been disabled for security reasons. '.$excp->getMessage(); } $phpinfos = ob_get_clean(); $phpinfos = preg_replace( '/(a:link.*)|(body, td, th, h1, h2.*)|(img.*)|(td, th.*)|(a:hover.*)|(class="center")/', '', $phpinfos ); $view_params['phpinfo_str'] = empty($phpinfos) ? $this->l('phpinfo() has been disabled for security reasons.') : $phpinfos; $view_params['psinfo_str'] = $this->psInfo(); $view_params['dbinfo_str'] = $this->dbInfo(); return ($view_params); } public function psInfo() { // TODO $prestashop_info = ''; if ($this->ps15x) { $sort = 'ORDER by `name`,`id_shop`'; } else { $sort = 'ORDER by `name`'; } $results = Db::getInstance()->executeS( 'SELECT * FROM `'._DB_PREFIX_.'configuration` WHERE `name` LIKE "PS_%" OR `name` LIKE "ETIQUETAGE_RETURN_%" '.$sort ); $ps_configuration = null; foreach ($results as $result) { if (strpos($result['name'], 'KEY') || strpos($result['name'], 'EMAIL') || strpos($result['name'], 'PASSWORD') || strpos($result['name'], 'PASSWD') || strpos($result['name'], 'CONTEXT_DATA')) { continue; } $value = $result['value']; if (base64_encode(base64_decode($value, true)) === $value) { $value = base64_decode($value, true); } else { $value = $result['value']; } if (@serialize(@unserialize($value)) == $value) { $value = '
';
$prestashop_info .= 'Version: '._PS_VERSION_."\n\n";
$prestashop_info .= "\n";
$prestashop_info .= $ps_configuration;
$prestashop_info .= ''."\n\n";
return ($prestashop_info);
}
public function dbInfo()
{
$tables_to_check = array(
_DB_PREFIX_.'so_return_label'
);
$query = Db::getInstance()->executeS('SHOW TABLES');
$tables = array();
foreach ($query as $rows) {
foreach ($rows as $t) {
$tables[$t] = 1;
}
}
$not_existing_tables = array();
foreach ($tables_to_check as $to_check) {
if (!isset($tables[$to_check])) {
$not_existing_tables[] = $to_check;
ConfigureMessage::error(
$this->l(
'The table `'.$to_check.'` was not found in your database.'
)
);
}
}
$tables_column = array();
foreach ($tables_to_check as $to_check) {
if (in_array($to_check, $not_existing_tables)) {
$tables_column[] = null;
continue;
}
$result = Db::getInstance()->executeS('SHOW COLUMNS FROM `'.$to_check.'`');
foreach ($result as $id => $column) {
$result[$id] = $column['Field'];
}
$tables_column[] = $result;
}
$db_info = '';
foreach ($tables_to_check as $id => $to_check) {
$db_info .= 'SHOW COLUMNS FROM `'.$to_check.'` : '.(in_array($to_check, $not_existing_tables) ?
'N/A
' : print_r($tables_column[$id], true));
$db_info .= 'SELECT * FROM `'.$to_check.'` ORDER BY `date_add` DESC LIMIT 5 : ';
$db_info .= print_r(Db::getInstance()->executeS(
'SELECT *
FROM `'.$to_check.'`
ORDER BY `date_add`
DESC LIMIT 5'
), true);
}
$db_info .= '';
return ($db_info);
}
private function checkhttpDownloadFolder()
{
if (!is_writable($this->download_folder)) {
@chmod($this->download_folder, 7777);
}
$content = 'Hello World !'.PHP_EOL;
if (file_put_contents($this->download_folder.'TO_DELETE.txt', $content) === false) {
printf(
'%s/%d: %s - %s',
basename(__FILE__),
__LINE__,
$this->l('Failed to write to the directory'),
$this->download_folder
);
return (false);
}
return (true);
}
public function install()
{
$pass = true;
if (!parent::install()) {
$this->_errors[] = $this->l('An error occured while installing with parent::install().');
$pass = false;
}
if (!$this->hookSetup(self::ADD)) {
$this->_errors[] = $this->l('An error occured while registering hooks.');
$pass = false;
}
if (!$this->checkhttpDownloadFolder()) {
$this->_errors[] = $this->l('An error occured while checking folder permissions.');
$pass = false;
}
// Basic configuration setup
$shop_phone = Configuration::get('PS_SHOP_PHONE');
$shop_phone = preg_replace('/\D/', '', $shop_phone);
$default_shop_informations = array(
'ContractNumber' => null,
'Password' => null,
'Name' => null,
'Surname' => null,
'ServiceInfo' => null,
'companyName' => Configuration::get('PS_SHOP_NAME'),
'Line0' => Configuration::get('PS_SHOP_ADDR2'),
'Line1' => null,
'Line2' => Configuration::get('PS_SHOP_ADDR1'),
'Line3' => null,
'PostalCode' => Configuration::get('PS_SHOP_CODE'),
'City' => Configuration::get('PS_SHOP_CITY'),
'countryCode' => 'FR',
'phoneNumber' => $shop_phone,
'Mail' => Configuration::get('PS_SHOP_EMAIL'),
'demo' => null,
'insurance' => null,
'deposit_date' => null,
'output_print_type' => 'PDF_A4_300dpi'
);
Configuration::updateValue('ETIQUETAGE_RETURN_CONF', serialize($default_shop_informations));
// Database
$sql = 'CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'so_return_label` (
`id_label` INT(10) NOT NULL AUTO_INCREMENT,
`id_return` INT(10) NOT NULL,
`id_coliposte` VARCHAR(20) NOT NULL,
`date_add` DATETIME NOT NULL,
`date_upd` DATETIME NOT NULL,
PRIMARY KEY (`id_label`)
) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8;';
if (!Db::getInstance()->execute($sql)) {
$pass = false;
}
return ((bool)$pass);
}
public function uninstall()
{
$pass = true;
if (!$this->hookSetup(self::REMOVE)) {
$this->_errors[] = $this->l('An error occured while unregistering hooks.');
$pass = false;
}
if (!parent::uninstall()) {
$this->_errors[] = $this->l('An error occured while uninstalling with parent::uninstall().');
$pass = false;
}
Configuration::deleteByName('ETIQUETAGE_RETURN_CONF');
Db::getInstance()->execute('DROP TABLE IF EXISTS `'._DB_PREFIX_.'so_return_label`');
return ((bool)$pass);
}
public function getContent()
{
$output = null;
if (Tools::isSubmit('submit'.$this->name)) {
$pass = true;
$values = Tools::getValue('return_info');
$values['ContractNumber'] = trim($values['ContractNumber']);
$values['Password'] = trim($values['Password']);
$values['deposit_date'] = (int)$values['deposit_date'];
$pass += Configuration::updateValue('ETIQUETAGE_RETURN_CONF', serialize($values));
$pass += Configuration::updateValue('ETIQUETAGE_RETURN_DEBUG', Tools::getValue('sne_debug'));
$pass += Configuration::updateValue('ETIQUETAGE_RETURN_TEST', Tools::getValue('sne_test_mode'));
// Database
$sql =
'CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'so_return_label` (
`id_label` INT(10) NOT NULL AUTO_INCREMENT,
`id_return` INT(10) NOT NULL,
`id_coliposte` VARCHAR(20) NOT NULL,
`date_add` DATETIME NOT NULL,
`date_upd` DATETIME NOT NULL,
PRIMARY KEY (`id_label`)
) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8;';
$pass += Db::getInstance()->execute($sql);
if ($pass) {
$output .= $this->displayConfirmation($this->l('Options updated.'));
} else {
$output .= $this->displayError($this->l('The system failed to save your options.'));
}
$this->hookSetup(self::UPDATE);
}
return $output.$this->displayForm();
}
public function displayForm()
{
require_once(dirname(__FILE__).'/classes/shared/configure_tab.class.php');
$html = '';
$alert_class = array();
$alert_class['danger'] = $this->ps16x ? 'alert alert-danger' : 'error';
$alert_class['warning'] = $this->ps16x ? 'alert alert-warning' : 'warn';
$alert_class['success'] = $this->ps16x ? 'alert alert-success' : 'conf';
$alert_class['info'] = $this->ps16x ? 'alert alert-info' : 'hint';
$this->context->smarty->assign(
array(
'sne_name' => $this->displayName,
'sne_version' => $this->version,
'ps_version' => _PS_VERSION_,
'sne_description' => $this->description,
'sne_module_dir' => $this->url,
'sne_module_path' => $this->path,
'sne_check_login' => $this->url.'functions/check_login.php?token='.md5(_COOKIE_IV_),
'sne_config' => unserialize(Configuration::get('ETIQUETAGE_RETURN_CONF')),
'sne_info' => $this->backofficeInformations(),
'selected_tab' => Tools::getValue('selected_tab') ? Tools::getValue('selected_tab') : '0',
'sne_module_name' => $this->name,
'alert_class' => $alert_class,
'sne_debug' => Configuration::get('ETIQUETAGE_RETURN_DEBUG'),
'sne_test_mode' => Configuration::get('ETIQUETAGE_RETURN_TEST'),
'sne_mails' => $this->getTemplates('fr')
)
);
$tab_list = array();
$tab_list[] = array(
'id' => 'sonice_retour', 'img' => 'sonice_retour', 'name' => 'SoNice Retour', 'selected' => true
);
$tab_list[] = array(
'id' => 'informations', 'img' => 'information', 'name' => 'Informations', 'selected' => false
);
$tab_list[] = array('id' => 'account', 'img' => 'key', 'name' => $this->l('Account'), 'selected' => false);
$tab_list[] = array('id' => 'address', 'img' => 'house', 'name' => $this->l('Address'), 'selected' => false);
$tab_list[] = array('id' => 'conf', 'img' => 'cog_edit', 'name' => $this->l('Settings'), 'selected' => false);
$this->myAddCSS($this->url.'views/css/configuration16.css', 'all');
$this->myAddCSS($this->url.'views/css/jquery.qtip.min.css', 'all');
$this->myAddJS($this->url.'views/js/configuration.js');
$this->myAddJS($this->url.'views/js/jquery.qtip.min.js');
$html .= ConfigureMessage::display();
$html .= $this->context->smarty->fetch(dirname(__FILE__).'/views/templates/admin/configuration/header.tpl');
$html .= ConfigureTab::generateTabs($tab_list);
$html .= $this->context->smarty->fetch(
dirname(__FILE__).'/views/templates/admin/configuration/configuration.tpl'
);
return ($html);
}
/**
* @see AdminStatusesController
* @param null $lang
* @return array
*/
protected function getTemplates($lang = null)
{
$default_path = '../mails/';
$theme_path = '../themes/'._THEME_NAME_.'/mails/';
$array = array();
foreach (Language::getLanguages(false) as $language) {
$iso_code = $language['iso_code'];
if ($lang && Tools::strtolower($lang) != Tools::strtolower($iso_code)) {
continue;
}
if (!@filemtime(_PS_ADMIN_DIR_.'/'.$default_path.$iso_code) &&
!@filemtime(_PS_ADMIN_DIR_.'/'.$theme_path.$iso_code)) {
continue;
}
$theme_templates_dir = _PS_ADMIN_DIR_.'/'.$theme_path.$iso_code;
$theme_templates = is_dir($theme_templates_dir) ? scandir($theme_templates_dir) : array();
$templates = array_unique(array_merge(scandir(
_PS_ADMIN_DIR_.'/'.$default_path.$iso_code
), $theme_templates));
foreach ($templates as $template) {
if (!strncmp(strrev($template), 'lmth.', 5)) {
$search_result = array_search($template, $theme_templates);
$array[$iso_code][] = array(
'id' => Tools::substr($template, 0, -5),
'name' => Tools::substr($template, 0, -5),
'folder' => ((!empty($search_result)?$theme_path:$default_path))
);
}
}
}
if ($lang && isset($array[Tools::strtolower($lang)])) {
return $array[Tools::strtolower($lang)];
}
return $array;
}
public function hookActionOrderReturn($params)
{
$customer = new Customer($params['orderReturn']->id_customer);
$order = new Order($params['orderReturn']->id_order);
$returned_products = OrderReturn::getOrdersReturnDetail($params['orderReturn']->id);
$items_list = '';
foreach ($returned_products as $id_details) {
$order_details = new OrderDetail($id_details['id_order_detail']);
$items_list .= $this->l('Reference').' : '.$order_details->product_reference.'
'.$this->l('Product').' : '.$order_details->product_name.'
'.$this->l('Quantity').' : '.$order_details->product_quantity_return;
}
$items_table = '| '.$this->l('Reference').' | '.$this->l('Product').' | '.$this->l('Quantity').' |
|---|---|---|
| '.$order_details->product_reference.' | '.$order_details->product_name.' | '.$order_details->product_quantity_return.' |