188 lines
6.2 KiB
PHP
Raw Normal View History

2017-09-12 10:48:51 +02:00
<?php
/**
* TNT OFFICIAL MODULE FOR PRESTASHOP.
*
* @author GFI Informatique <www.gfi.fr>
* @copyright 2016-2017 GFI Informatique, 2016-2017 TNT
* @license https://opensource.org/licenses/MIT MIT License
*/
require_once _PS_MODULE_DIR_.'tntofficiel/classes/TNTOfficielCart.php';
require_once _PS_MODULE_DIR_.'tntofficiel/libraries/TNTOfficiel_Debug.php';
require_once _PS_MODULE_DIR_.'tntofficiel/libraries/helper/TNTOfficiel_AddressHelper.php';
class TNTOfficielAddressModuleFrontController extends ModuleFrontController
{
/**
* These constants are use in cpCityHelper.js
* Any change must be impacted in this file as well.
*/
const CHECK_POSTCODE_KO = 0; // postcode/city is not valid
const CHECK_POSTCODE_OK = 1; // postcode/city is valid
const CHECK_POSTCODE_NOT_REQUIRED = 2; // postcode/city does not require validation
/**
* TNTOfficielAddressModuleFrontController constructor.
* Controller always used for AJAX response.
*/
public function __construct()
{
TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__));
parent::__construct();
// No header/footer.
$this->ajax = true;
}
/**
* Saves the extra address data.
*
* @return string
*/
public function displayAjaxStoreDeliveryInfo()
{
TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__));
// Saves the extra address data.
$arrFormErrors = TNTOfficiel_Address::validate($_POST);
if (count($arrFormErrors)) {
echo Tools::jsonEncode(array(
'result' => false,
'form_errors' => $arrFormErrors,
));
return false;
}
$objContext = $this->context;
$objCart = $objContext->cart;
$intCartID = (int)$objCart->id;
// Load TNT cart info or create a new one for it's ID.
$objTNTCartModel = TNTOfficielCart::loadCartID($intCartID);
$boolResult = false;
if (Validate::isLoadedObject($objTNTCartModel)) {
$objTNTCartModel->hydrate(array(
'customer_email' => pSQL(Tools::getValue('email')),
'customer_mobile' => pSQL(Tools::getValue('mobile_phone')),
'address_building' => pSQL(Tools::getValue('building_number')),
'address_accesscode' => pSQL(Tools::getValue('intercom_code')),
'address_floor' => pSQL(Tools::getValue('floor')),
));
$boolResult = $objTNTCartModel->save();
}
if (!$boolResult) {
echo Tools::jsonEncode(array(
'result' => false,
'form_errors' => 'Une erreur s\'est produite',
));
return false;
}
echo Tools::jsonEncode(array(
'result' => true,
));
return true;
}
/**
* Check if the city match the.
*
* @return string
*/
public function displayAjaxCheckPostcodeCity()
{
TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__));
$arrResult = array();
// check the country
$intCountryID = (int)Tools::getValue('countryId');
$strPostCode = pSQL(Tools::getValue('postcode'));
$strCity = pSQL(Tools::getValue('city'));
if (Country::getIsoById($intCountryID) != 'FR' || Tools::strlen($strPostCode) != 5) {
$arrResult['result'] = false;
$arrResult['resultCode'] = TNTOfficielAddressModuleFrontController::CHECK_POSTCODE_NOT_REQUIRED;
} else {
$objTNTAddressHelper = TNTOfficiel_AddressHelper::getInstance();
//check the city/postcode
$mdwResult = $objTNTAddressHelper->checkPostcodeCityFromMiddleware(
$strPostCode,
$strCity,
$this->context->shop->id
);
if (!$mdwResult['response']) {
//get cities from the middleware from the given postal code
$mdwResult = $objTNTAddressHelper->getCitiesFromMiddleware(
$strPostCode,
$this->context->shop->id
);
$arrResult['result'] = false;
$arrResult['cities'] = $mdwResult['cities'];
$arrResult['resultCode'] = TNTOfficielAddressModuleFrontController::CHECK_POSTCODE_KO;
} else {
$arrResult['result'] = true;
$arrResult['resultCode'] = TNTOfficielAddressModuleFrontController::CHECK_POSTCODE_OK;
}
}
echo Tools::jsonEncode($arrResult);
return true;
}
/**
* Get cities for a postcode.
*
* @return string
*/
public function displayAjaxGetCities()
{
TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__));
$cart = $this->context->cart;
$deliveryAddress = new Address($cart->id_address_delivery);
$mdwResult = TNTOfficiel_AddressHelper::getInstance()->getCitiesFromMiddleware(
$deliveryAddress->postcode,
$this->context->shop->id
);
echo Tools::jsonEncode(array(
'result' => true,
'cities' => $mdwResult['cities'],
'postcode' => $deliveryAddress->postcode
));
return true;
}
/**
* Update the city for the current delivery address.
*
* @return array
*/
public function displayAjaxUpdateDeliveryAddress()
{
TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__));
$strCity = pSQL(Tools::getValue('city'));
if ($strCity) {
$objCart = $this->context->cart;
$objAddressDelivery = new Address($objCart->id_address_delivery);
$objAddressDelivery->city = $strCity;
$objAddressDelivery->save();
}
echo Tools::jsonEncode(array(
'result' => true
));
return true;
}
}