name = 'zipcodezone'; $this->tab = 'shipping_logistics'; $this->version = '1.1.3'; $this->module_key = '6c7a3d34fa01934e71f53aae16f2698b'; $this->author = 'MARICHAL Emmanuel'; parent::__construct (); $this->displayName = $this->l('Shipping fees based on zipcodes'); $this->description = $this->l('Assign zip codes to zones easily'); /* Backward compatibility */ if (_PS_VERSION_ < '1.5') require(_PS_MODULE_DIR_.$this->name.'/backward_compatibility/backward.php'); } public function install() { if (_PS_VERSION_ >= '1.5') @unlink(_PS_ROOT_DIR_.'/cache/class_index.php'); if (file_exists(_PS_ROOT_DIR_.'/override/classes/Address.php')) @rename(_PS_ROOT_DIR_.'/override/classes/Address.php', _PS_ROOT_DIR_.'/override/classes/'.mktime().'_Freelivery_Address.php'); return Db::getInstance()->Execute(' CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'zip_code_zone` ( `id` int(10) NOT NULL AUTO_INCREMENT, `id_country` int(10) DEFAULT NULL, `id_zone` int(10) DEFAULT NULL, `min` int(10) DEFAULT NULL, `max` int(10) DEFAULT NULL, PRIMARY KEY (`id`), KEY `ZCZCountryIndex` (`id_country`), KEY `ZCZZoneIndex` (`id_zone`) ) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;') && copy(dirname(__FILE__).'/Address.php', _PS_ROOT_DIR_.'/override/classes/Address.php') && parent::install(); } public function uninstall() { @rename(_PS_ROOT_DIR_.'/override/classes/Address.php', _PS_ROOT_DIR_.'/override/classes/'.mktime().'_Freelivery_Address.php'); return parent::uninstall(); } private function satisfactionWidget() { return '
'.$this->l('NO').' '.$this->l('YES').'
'.$this->l('Are you satisfied about this module?').'
'; } private function flash($content, $success = true) { if (_PS_VERSION_ < '1.5') return '
'.$content.'
'; else return '
'.$content.'
'; } private function addCondition($country, $zone, $min, $max) { if (Db::getInstance()->execute(' INSERT INTO '._DB_PREFIX_.'zip_code_zone (id_country, id_zone, min, max) VALUES('.(int)$country.', '.(int)$zone.', '.(int)$min.', '.(int)$max.')')) return $this->flash($this->l('Condition added with success')); else return $this->flash($this->l('An error occurred'), false); } private function delCondition($id) { if (Db::getInstance()->execute('DELETE FROM '._DB_PREFIX_.'zip_code_zone WHERE id = '.(int)$id)) return $this->flash($this->l('Condition deleted with success')); else return $this->flash($this->l('An error occurred'), false); } private function addCSV($skip = false, $separator = ';') { ini_set("auto_detect_line_endings", true); $lines = file($_FILES['csv']['tmp_name']); if ($lines === FALSE) return $this->flash($this->l('An error occurred'), false); $values = array(); foreach ($lines as $line_num => $line) { if (!($skip && $line_num == 0)) $values[] = explode($separator, $line); } $sql = ''; foreach ($values as $line) { if ($sql == '') $sql = 'INSERT INTO '._DB_PREFIX_.'zip_code_zone (id_country, id_zone, min, max) VALUES'; else $sql .= ','; $sql .= '('.implode(',',$line).')'; } if (Db::getInstance()->execute($sql)) return $this->flash($this->l('Condition added with success')); else return $this->flash($this->l('An error occurred'), false); } public function getContent() { $db = Db::getInstance(); $html = $this->satisfactionWidget(); if (Tools::isSubmit('addCondition')) $html .= $this->addCondition(Tools::getValue('country'), Tools::getValue('zone'), Tools::getValue('min'), Tools::getValue('max')); if (Tools::isSubmit('deleteCondition') && (int)Tools::getValue('id_condition')) $html .= $this->delCondition(Tools::getValue('id_condition')); if (Tools::isSubmit('addCSV')) $html .= $this->addCSV(Tools::getIsset('first'), Tools::getValue('separator')); $doc_iso = file_exists('../modules/'.$this->name.'/docs/readme_'.Language::getIsoById($this->context->language->id).'.pdf') ? Language::getIsoById($this->context->language->id) : 'en'; $html .= '
'.$this->l('Add condition').' '.$this->l('Documentation').'

'.$this->l('CSV import').'
'.$this->l('Click to see a CSV sample').'


'; $conditions = $db->ExecuteS(' SELECT zcz.id, zcz.id_country, zcz.id_zone, zcz.min, zcz.max, z.name zone_name, cl.name country_name FROM '._DB_PREFIX_.'zip_code_zone zcz LEFT JOIN '._DB_PREFIX_.'zone z ON (z.id_zone = zcz.id_zone) LEFT JOIN '._DB_PREFIX_.'country_lang cl ON (cl.id_country = zcz.id_country AND cl.id_lang = '.(int)$this->context->language->id.') ORDER BY zcz.id_country, zcz.id_zone, zcz.min ASC', false); if ($db->numRows() == 0) return $html; $html .= '
'.$this->l('Conditions').' '; while ($condition = $db->nextRow($conditions)) { $html .= ' '; } $html .= '
'.$this->l('Country').' '.$this->l('Zone').' '.$this->l('Zip code min').' '.$this->l('Zip code max').' '.$this->l('Actions').'
'.Tools::safeOutput($condition['country_name']).' '.Tools::safeOutput($condition['zone_name']).' '.(int)$condition['min'].' '.(int)$condition['max'].'
'; return $html; } }