bebeboutik/modules/ant_changeaddress/ant_changeaddress.php
2016-11-08 16:09:41 +01:00

149 lines
5.3 KiB
PHP

<?php
if (!defined('_PS_VERSION_'))
exit;
class Ant_Changeaddress extends Module
{
public function __construct()
{
$this->name = 'ant_changeaddress';
$this->tab = 'administration';
$this->author = 'Antadis';
$this->version = '1.0';
$this->need_instance = 0;
parent::__construct();
$this->displayName = $this->l('Change délivery address');
$this->description = $this->l('Allow to change the delivery address before order is sent');
}
public function install()
{
if(!(parent::install())) {
return false;
}
return true;
}
public function uninstall()
{
if(parent::uninstall() == false) {
return false;
}
return true;
}
public function getAddress($id_address)
{
$current_address = array();
$delivery_address = new Address((int)$id_address);
$delivery_address = AddressFormat::getFormattedLayoutData($delivery_address);
foreach ($delivery_address['ordered'] as $key => $value) {
$addressKey = explode(" ", $value);
foreach ($addressKey as $k => $v) {
if (!empty($delivery_address['formated'][$v]) && $delivery_address['formated'][$v]) {
if (!isset($current_address[$key])) {
$current_address[$key] = $delivery_address['formated'][$v];
} else {
$current_address[$key] .= " ".$delivery_address['formated'][$v];
}
}
}
}
return $current_address;
}
public function getAddresses($id_customer, $id_country, $id_lang)
{
return Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('
SELECT a.*, cl.`name` AS country, s.name AS state, s.iso_code AS state_iso
FROM `'._DB_PREFIX_.'address` a
LEFT JOIN `'._DB_PREFIX_.'country` c ON (a.`id_country` = c.`id_country`)
LEFT JOIN `'._DB_PREFIX_.'country_lang` cl ON (c.`id_country` = cl.`id_country`)
LEFT JOIN `'._DB_PREFIX_.'state` s ON (s.`id_state` = a.`id_state`)
WHERE `id_lang` = '.(int)($id_lang).'
AND `id_customer` = '.(int)($id_customer).'
AND a.`deleted` = 0
AND c.`id_country` = '.(int)($id_country));
}
public $validation = array(
'company' => '/[<>=#{}]*/',
'firstname' => '/[0-9!<>,;?=+()@#"°{}_$%:]*/',
'lastname' => '/[0-9!<>,;?=+()@#"°{}_$%:]*/',
'address1' => '/[!<>?=+@{}_$%]*/',
'address2' => '/[!<>?=+@{}_$%]*/',
'postcode' => '/[^a-zA-Z 0-9-]/',
'city' => '/[!<>;?=+@#"°{}_$%]*/',
'phone' => '/[a-zA-Z^%$#@!-]/',
'phone_mobile' => '/[a-zA-Z^%$#@!-]/'
);
public function createAddress($id_customer, $country, $libelle, $address1, $address2, $postcode, $city, $telephone, $lastname, $firstname, $id_address_delivery = null)
{
if ($id_address_delivery) {
$customer_address = new Address($id_address_delivery);
} else {
$customer_address = null;
}
$address = new Address();
$address_rules = Address::getValidationRules('Address');
$country = ($country && Tools::strlen($country) === 2) ?
$country : Country::getIsoById($customer_address->id_country);
$id_customer = $id_customer ? $id_customer : $customer_address->id_customer;
//$address->id_country = (int)Country::getByIso($this->country);
$address->id_country = strtolower($country) == 'fr'? 8: (int)Country::getByIso($country);
$address->id_customer = (int)$id_customer;
$address->firstname = $firstname ? $firstname : $customer_address->firstname;
$address->lastname = $lastname ? $lastname : $customer_address->lastname;
$address->company = $libelle;
$address->address1 = $address1 ? $address1 : $customer_address->address1;
$address->address2 = $address2;
$address->postcode = $postcode ? $postcode : $customer_address->postcode;
$address->city = $city ? $city : $customer_address->city;
$address->phone_mobile = $telephone ? $telephone : $customer_address->phone_mobile;
$address->phone = $telephone ? $telephone : $customer_address->phone;
if (!$address->phone_mobile) {
$address->phone_mobile = '0661123456';
}
$address_fields = array(
'company',
'firstname',
'lastname',
'address1',
'address2',
'postcode',
'city',
'phone',
'phone_mobile'
);
foreach ($address_fields as $field) {
$address->{$field} = preg_replace($this->validation[$field], '', $address->{$field});
if (isset($address_rules['required'][$field]) && $address_rules['required'][$field]) {
if (empty($address->{$field})) {
$address->{$field} = '-';
}
}
if (isset($address_rules['size'][$field]) && $address_rules['size'][$field]) {
$address->{$field} = Tools::substr($address->{$field}, 0, $address_rules['size'][$field]);
}
}
$address->alias = 'So Colissimo - '.date('d-m-Y');
$address->active = 1;
$address->deleted = 1;
$address->add();
return $address->id;
}
}