248 lines
9.5 KiB
PHP
Executable File
248 lines
9.5 KiB
PHP
Executable File
<?php
|
|
|
|
class zipcodezone extends Module
|
|
{
|
|
public function __construct()
|
|
{
|
|
$this->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 '
|
|
<div style="background-color: #F3F3F3;width: 450px;margin-left: auto;margin-right: auto;border-radius: 3px;border: 1px solid #E6E6E6;margin-bottom: 10px;margin-top: 10px;">
|
|
<a class="button not_satisfied" style="float:right;margin-top: 9px;margin-right: 10px;">'.$this->l('NO').'</a>
|
|
<a class="button satisfied" style="float:right;margin-top: 9px;margin-right: 10px;">'.$this->l('YES').'</a>
|
|
<div class="satisfied_text" style="padding: 13px;text-shadow: 1px 1px white;">'.$this->l('Are you satisfied about this module?').'</div>
|
|
</div>
|
|
<script type="text/javascript">
|
|
$(".satisfied").click(function() {
|
|
$(this).remove();
|
|
$(".not_satisfied").remove();
|
|
$(".satisfied_text").html("<a target=\'_blank\' href=\'http://addons.prestashop.com/'.Language::getIsoById($this->context->language->id).'/ratings.php\'>'.$this->l('Happy to hear that! Please click here to leave a review on PrestaShop store. I would greatly appreciate!').'</a>");
|
|
});
|
|
$(".not_satisfied").click(function() {
|
|
$(this).remove();
|
|
$(".satisfied").remove();
|
|
$(".satisfied_text").html("<a target=\'_blank\' href=\'http://addons.prestashop.com/contact-community.php?id_product=5711\'>'.$this->l('Sorry to hear that. Please click here to send me a message in order to look into your problem.').'</a>");
|
|
});
|
|
</script>';
|
|
}
|
|
|
|
private function flash($content, $success = true)
|
|
{
|
|
if (_PS_VERSION_ < '1.5')
|
|
return '<div class="'.($success ? 'conf' : 'error').'"><img src="'._PS_ADMIN_IMG_.($success ? 'ok2' : 'error').'.png" alt="">'.$content.'</div>';
|
|
else
|
|
return '<div class="'.($success ? 'conf' : 'error').'">'.$content.'</div>';
|
|
}
|
|
|
|
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 .= '
|
|
<fieldset>
|
|
<legend><img src="../img/admin/add.gif" alt="" />'.$this->l('Add condition').'</legend>
|
|
<a href="../modules/'.$this->name.'/docs/readme_'.$doc_iso.'.pdf" target="_blank" style="float:right;margin-right: 20px;"><img src="../img/admin/information.png" />'.$this->l('Documentation').'</a>
|
|
<form action="'.$_SERVER['REQUEST_URI'].'" method="post">
|
|
<label>'.$this->l('Country').'</label>
|
|
<div class="margin-form">
|
|
<select id="country" name="country">';
|
|
$countries = Country::getCountries((int)$this->context->language->id, true);
|
|
foreach ($countries as $country)
|
|
$html .= '<option value="'.(int)$country['id_country'].'">'.Tools::safeOutput($country['name']).'</option>';
|
|
$html .= '</select>
|
|
</div>
|
|
<label>'.$this->l('Zone').'</label>
|
|
<div class="margin-form">
|
|
<select id="zone" name="zone">';
|
|
$zones = Zone::getZones(true);
|
|
foreach ($zones as $zone)
|
|
$html .= '<option value="'.(int)$zone['id_zone'].'">'.Tools::safeOutput($zone['name']).'</option>';
|
|
$html .= '</select>
|
|
</div>
|
|
<label>'.$this->l('Zip code min').'</label>
|
|
<div class="margin-form">
|
|
<input type="text" name="min" />
|
|
</div>
|
|
<label>'.$this->l('Zip code max').'</label>
|
|
<div class="margin-form">
|
|
<input type="text" name="max" />
|
|
</div>
|
|
<div class="margin-form space">
|
|
<input type="submit" name="addCondition" value="'.$this->l('Add').'" class="button" />
|
|
</div>
|
|
</form>
|
|
</fieldset>
|
|
<br />
|
|
<fieldset>
|
|
<legend><img src="../img/admin/add.gif" alt="" />'.$this->l('CSV import').'</legend>
|
|
<div style="width:50%; margin: 0 auto;">
|
|
<a href="../modules/zipcodezone/example.csv">'.$this->l('Click to see a CSV sample').'</a>
|
|
</div>
|
|
<br>
|
|
<form action="'.$_SERVER['REQUEST_URI'].'" method="post" enctype="multipart/form-data">
|
|
<label>'.$this->l('CSV file').'</label>
|
|
<div class="margin-form">
|
|
<input type="file" name="csv" />
|
|
</div>
|
|
<label>'.$this->l('Ignore first line').'</label>
|
|
<div class="margin-form">
|
|
<input type="checkbox" name="first" value="1" style="margin-top: 4px;"><br>
|
|
</div>
|
|
<label>'.$this->l('Separator').'</label>
|
|
<div class="margin-form">
|
|
<input type="input" name="separator" value=";" size="1" style="padding-left: 3px;" />
|
|
</div>
|
|
<div class="margin-form space">
|
|
<input type="submit" name="addCSV" value="'.$this->l('Add').'" class="button" />
|
|
</div>
|
|
</form>
|
|
</fieldset>';
|
|
|
|
$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 .= '
|
|
<br />
|
|
<fieldset>
|
|
<legend><img src="../img/admin/world.gif" alt="" />'.$this->l('Conditions').'</legend>
|
|
<table cellspacing="0" cellpadding="0" class="table" style="width: 68em;">
|
|
<tr>
|
|
<th>'.$this->l('Country').'</th>
|
|
<th>'.$this->l('Zone').'</th>
|
|
<th>'.$this->l('Zip code min').'</th>
|
|
<th>'.$this->l('Zip code max').'</th>
|
|
<th>'.$this->l('Actions').'</th>
|
|
</tr>';
|
|
|
|
while ($condition = $db->nextRow($conditions))
|
|
{
|
|
$html .= '
|
|
<form action="'.$_SERVER['REQUEST_URI'].'" method="post">
|
|
<input type="hidden" name="id_condition" value="'.(int)$condition['id'].'">
|
|
<tr>
|
|
<td>'.Tools::safeOutput($condition['country_name']).'</td>
|
|
<td>'.Tools::safeOutput($condition['zone_name']).'</td>
|
|
<td>'.(int)$condition['min'].'</td>
|
|
<td>'.(int)$condition['max'].'</td>
|
|
<td><input type="submit" name="deleteCondition" value="'.$this->l('Delete').'" class="button" /></td>
|
|
</tr>
|
|
</form>';
|
|
}
|
|
|
|
$html .= '
|
|
</table>
|
|
</fieldset>';
|
|
|
|
return $html;
|
|
}
|
|
} |