2017-08-30 11:37:48 +02:00

194 lines
6.5 KiB
PHP

<?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/libraries/TNTOfficiel_Debug.php';
require_once _PS_MODULE_DIR_.'tntofficiel/libraries/TNTOfficiel_Logger.php';
class TNTOfficiel_Address
{
/**
* @var TNTOfficiel_Logger
*/
private $logger;
/**
* Constructor.
*/
public function __construct()
{
TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__));
$this->logger = new TNTOfficiel_Logger();
}
/**
* Returns the default values for the extra address data fields.
*
* @param int $intArgAddressID
* @param int $intArgCustomerID
*
* @return array
*/
public static function getDefaultValues($intArgAddressID, $intArgCustomerID)
{
TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__));
// Retrieve the customer and address by its id.
$arrRowCustomer = Db::getInstance()->getRow(
'SELECT * FROM '._DB_PREFIX_.'customer WHERE id_customer = '.(int)$intArgCustomerID
);
$arrRowAddress = Db::getInstance()->getRow(
'SELECT * FROM '._DB_PREFIX_.'address WHERE id_address = '.(int)$intArgAddressID
);
// Get values from the customer and address object according to the configuration.
return array(
'email' => TNTOfficiel_Address::_getValue(
$arrRowAddress,
$arrRowCustomer,
'TNT_CARRIER_ADDRESS_EMAIL'
),
'mobile_phone' => TNTOfficiel_Address::_getValue(
$arrRowAddress,
$arrRowCustomer,
'TNT_CARRIER_ADDRESS_PHONE'
),
'building_number' => TNTOfficiel_Address::_getValue(
$arrRowAddress,
$arrRowCustomer,
'TNT_CARRIER_ADDRESS_BUILDING'
),
'intercom_code' => TNTOfficiel_Address::_getValue(
$arrRowAddress,
$arrRowCustomer,
'TNT_CARRIER_ADDRESS_INTERCOM'
),
'floor' => TNTOfficiel_Address::_getValue(
$arrRowAddress,
$arrRowCustomer,
'TNT_CARRIER_ADDRESS_FLOOR'
),
);
}
/**
* Gets the property from a customer or an address.
*
* @param array $arrAddress
* @param array $arrCustomer
* @param string $strArgConfigField
*
* @return mixed
*/
private static function _getValue($arrAddress, $arrCustomer, $strArgConfigField)
{
TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__));
$arrConfigValue = explode('.', Configuration::get($strArgConfigField));
$strValue = '';
if (count($arrConfigValue) > 1) {
$objectName = $arrConfigValue[0];
$objectProperty = $arrConfigValue[1];
if (in_array($objectProperty, array('id_customer', 'id_address'))) {
$objectProperty = 'id';
}
try {
$object = null;
if ($objectName == 'customer') {
$object = $arrCustomer;
}
elseif ($objectName == 'address') {
$object = $arrAddress;
}
$strValue = $object[$objectProperty];
} catch (Exception $objException) {
$objFileLogger = new FileLogger();
$objFileLogger->setFilename(_PS_ROOT_DIR_.'/log/'.date('Ymd').'_tnt_exception.log');
$objFileLogger->logError($objException->getMessage());
}
}
return $strValue;
}
/**
* Validate the extra address data form.
*
* @param $values
*
* @return bool
*/
public static function validate($values)
{
TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__));
$arrFormErrors = array();
//check if email is set and not empty
if (!isset($values['email']) || trim($values['email']) === '') {
$arrFormErrors[] = 'Le champ \'email\' est obligatoire';
}
//check if the email is valid
if (!filter_var($values['email'], FILTER_VALIDATE_EMAIL)) {
$arrFormErrors[] = 'L\'email saisi n\'est pas valide';
}
//check if mobile phone is set and not empty
if (!isset($values['mobile_phone']) || trim($values['mobile_phone']) === '') {
$arrFormErrors[] = 'Le champ \'Téléphone portable\' est obligatoire';
}
//check if mobile phone is set 10 digits and start by 06 or 07
if (!preg_match('/^((06|07){1}(\d){8})$/', $values['mobile_phone'])) {
$arrFormErrors[] = 'Le champ \'Téléphone portable\' doit être composé de 10 chiffres'
.' sans espace ni point, et commencer par un 06 ou 07';
}
$arrFieldList = array(
array(
'name' => 'email',
'label' => 'Email de contact',
'maxlength' => 80,
),
array(
'name' => 'mobile_phone',
'label' => 'Téléphone',
'maxlength' => 15,
),
array(
'name' => 'building_number',
'label' => 'N° de batiment',
'maxlength' => 3,
),
array(
'name' => 'intercom_code',
'label' => 'Code interphone',
'maxlength' => 7,
),
array(
'name' => 'floor',
'label' => 'Etage',
'maxlength' => 2,
),
);
foreach ($arrFieldList as $arrField) {
if ($values[$arrField['name']]) {
if ($arrField['maxlength'] < iconv_strlen($values[$arrField['name']])) {
$arrFormErrors[] = sprintf('Le champ "%s" doit avoir au maximum %s caractère(s)', $arrField['label'], $arrField['maxlength']);
}
}
}
return $arrFormErrors;
}
}