// MERGE with 1.4 r7770
This commit is contained in:
+110
-33
@@ -45,7 +45,7 @@ class AddressFormatCore extends ObjectModel
|
||||
protected $table = 'address_format';
|
||||
protected $identifier = 'id_country';
|
||||
|
||||
static public $requireFormFieldsList = array(
|
||||
public static $requireFormFieldsList = array(
|
||||
'firstname',
|
||||
'name',
|
||||
'address1',
|
||||
@@ -54,7 +54,7 @@ class AddressFormatCore extends ObjectModel
|
||||
'Country:name',
|
||||
'State:name');
|
||||
|
||||
static public $forbiddenProperyList = array(
|
||||
public static $forbiddenPropertyList = array(
|
||||
'deleted',
|
||||
'date_add',
|
||||
'other',
|
||||
@@ -85,10 +85,12 @@ class AddressFormatCore extends ObjectModel
|
||||
'call_prefixes',
|
||||
'call_prefix');
|
||||
|
||||
static public $forbiddenClassList = array(
|
||||
public static $forbiddenClassList = array(
|
||||
'Manufacturer',
|
||||
'Supplier');
|
||||
|
||||
const _CLEANING_REGEX_ = '#([^\w:_]+)#i';
|
||||
|
||||
public function getFields()
|
||||
{
|
||||
parent::validateFields();
|
||||
@@ -124,7 +126,7 @@ class AddressFormatCore extends ObjectModel
|
||||
{
|
||||
$propertyName = $property->getName();
|
||||
if (($propertyName == $fieldName) && ($isIdField ||
|
||||
(!preg_match('#id|id_\w#', $propertyName, $match))))
|
||||
(!preg_match('#id|id_\w#', $propertyName))))
|
||||
$isValide = true;
|
||||
}
|
||||
|
||||
@@ -148,7 +150,6 @@ class AddressFormatCore extends ObjectModel
|
||||
private function _checkLiableAssociation($patternName, $fieldsValidate)
|
||||
{
|
||||
$patternName = trim($patternName);
|
||||
$cleanedLine = '';
|
||||
|
||||
if ($associationName = explode(':', $patternName))
|
||||
{
|
||||
@@ -158,8 +159,7 @@ class AddressFormatCore extends ObjectModel
|
||||
else if ($totalNameUsed == 1)
|
||||
{
|
||||
$associationName[0] = strtolower($associationName[0]);
|
||||
$cleanedLine = $associationName[0];
|
||||
if (in_array($associationName[0], self::$forbiddenProperyList) ||
|
||||
if (in_array($associationName[0], self::$forbiddenPropertyList) ||
|
||||
!$this->_checkValidateClassField('Address', $associationName[0], false))
|
||||
$this->_errorFormatList[] = Tools::displayError('This name isn\'t allowed').': '.
|
||||
$associationName[0];
|
||||
@@ -183,12 +183,10 @@ class AddressFormatCore extends ObjectModel
|
||||
|
||||
// Check if the field name exist in the class write by the user
|
||||
$this->_checkValidateClassField($associationName[0], $associationName[1], false);
|
||||
$cleanedLine = $associationName[0].':'.$associationName[1];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return (strlen($cleanedLine)) ? $cleanedLine.' ' : '';
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -196,27 +194,30 @@ class AddressFormatCore extends ObjectModel
|
||||
*/
|
||||
public function checkFormatFields()
|
||||
{
|
||||
$cleanedContent = '';
|
||||
$this->_errorFormatList = array();
|
||||
$fieldsValidate = Address::getFieldsValidate();
|
||||
$usedKeyList = array();
|
||||
|
||||
$multipleLineFields = explode("\n", $this->format);
|
||||
if ($multipleLineFields && is_array($multipleLineFields))
|
||||
foreach($multipleLineFields as $lineField)
|
||||
{
|
||||
$lineField = str_replace(array("\n", "\t", "\r\n", "\r"), '', $lineField);
|
||||
if (strlen($lineField))
|
||||
if (($patternsName = preg_split(self::_CLEANING_REGEX_, $lineField, -1, PREG_SPLIT_NO_EMPTY)))
|
||||
if (is_array($patternsName))
|
||||
{
|
||||
$patternsName = explode(' ', trim($lineField));
|
||||
if ($patternsName && is_array($patternsName))
|
||||
{
|
||||
foreach($patternsName as $patternName)
|
||||
$cleanedContent .= $this->_checkLiableAssociation($patternName, $fieldsValidate);
|
||||
$cleanedContent = trim($cleanedContent)."\r\n";
|
||||
{
|
||||
if (!in_array($patternName, $usedKeyList))
|
||||
{
|
||||
$this->_checkLiableAssociation($patternName, $fieldsValidate);
|
||||
$usedKeyList[] = $patternName;
|
||||
}
|
||||
else
|
||||
$this->_errorFormatList[] = Tools::displayError('This key is used too many times (once allowed').
|
||||
': '.$patternName;
|
||||
}
|
||||
}
|
||||
$this->format = $cleanedContent;
|
||||
}
|
||||
return (count($this->_errorFormatList)) ? false : true;
|
||||
}
|
||||
|
||||
@@ -228,6 +229,61 @@ class AddressFormatCore extends ObjectModel
|
||||
return $this->_errorFormatList;
|
||||
}
|
||||
|
||||
/*
|
||||
** Set the layout key with the liable value
|
||||
** example : (firstname) => 'Presta' will result (Presta)
|
||||
** : (firstname-lastname) => 'Presta' and 'Shop' result '(Presta-Shop)'
|
||||
*/
|
||||
private static function _setOriginalDisplayFormat(&$formattedValueList, $currentLine, $currentKeyList)
|
||||
{
|
||||
if ($currentKeyList && is_array($currentKeyList))
|
||||
if ($originalFormattedPatternList = explode(' ', $currentLine))
|
||||
// Foreach the available pattern
|
||||
foreach($originalFormattedPatternList as $patternNum => $pattern)
|
||||
{
|
||||
// Var allows to modify the good formatted key value when multiple key exist into the same pattern
|
||||
$mainFormattedKey = '';
|
||||
|
||||
// Multiple key can be found in the same pattern
|
||||
foreach($currentKeyList as $key)
|
||||
{
|
||||
// Check if we need to use an older modified pattern if a key has already be matched before
|
||||
$replacedValue = empty($mainFormattedKey) ? $pattern : $formattedValueList[$mainFormattedKey];
|
||||
if (($formattedValue = preg_replace('/'.$key.'/', $formattedValueList[$key], $replacedValue, -1, $count)))
|
||||
if ($count)
|
||||
{
|
||||
// Allow to check multiple key in the same pattern,
|
||||
if (empty($mainFormattedKey))
|
||||
$mainFormattedKey = $key;
|
||||
// Set the pattern value to an empty string if an older key has already been matched before
|
||||
if ($mainFormattedKey != $key)
|
||||
$formattedValueList[$key] = '';
|
||||
// Store the new pattern value
|
||||
$formattedValueList[$mainFormattedKey] = $formattedValue;
|
||||
unset($originalFormattedPatternList[$patternNum]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** Cleaned the layout set by the user
|
||||
*/
|
||||
public static function cleanOrderedAddress(&$orderedAddressField)
|
||||
{
|
||||
foreach($orderedAddressField as &$line)
|
||||
{
|
||||
$cleanedLine = '';
|
||||
if (($keyList = preg_split(self::_CLEANING_REGEX_, $line, -1, PREG_SPLIT_NO_EMPTY)))
|
||||
{
|
||||
foreach($keyList as $key)
|
||||
$cleanedLine .= $key.' ';
|
||||
$cleanedLine = trim($cleanedLine);
|
||||
$line = $cleanedLine;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Returns the formatted fields with associated values
|
||||
*
|
||||
@@ -244,9 +300,10 @@ class AddressFormatCore extends ObjectModel
|
||||
|
||||
// Check if $address exist and it's an instanciate object of Address
|
||||
if ($address && ($address instanceof Address))
|
||||
foreach($addressFormat as $lineNum => $line)
|
||||
foreach($addressFormat as $line)
|
||||
{
|
||||
if (($keyList = explode(' ', $line)) && is_array($keyList))
|
||||
if (($keyList = preg_split(self::_CLEANING_REGEX_, $line, -1, PREG_SPLIT_NO_EMPTY)) && is_array($keyList))
|
||||
{
|
||||
foreach($keyList as $pattern)
|
||||
if ($associateName = explode(':', $pattern))
|
||||
{
|
||||
@@ -274,9 +331,12 @@ class AddressFormatCore extends ObjectModel
|
||||
}
|
||||
}
|
||||
}
|
||||
self::_setOriginalDisplayFormat($tab, $line, $keyList);
|
||||
}
|
||||
}
|
||||
self::cleanOrderedAddress($addressFormat);
|
||||
// Free the instanciate objects
|
||||
foreach($temporyObject as $objectName => &$object)
|
||||
foreach($temporyObject as &$object)
|
||||
unset($object);
|
||||
return $tab;
|
||||
}
|
||||
@@ -295,7 +355,7 @@ class AddressFormatCore extends ObjectModel
|
||||
|
||||
$addressText = '';
|
||||
foreach ($addressFields as $line)
|
||||
if (($patternsList = explode(' ', $line)))
|
||||
if (($patternsList = preg_split(self::_CLEANING_REGEX_, $line, -1, PREG_SPLIT_NO_EMPTY)))
|
||||
{
|
||||
$tmpText = '';
|
||||
foreach($patternsList as $pattern)
|
||||
@@ -328,8 +388,8 @@ class AddressFormatCore extends ObjectModel
|
||||
foreach($publicProperties as $property)
|
||||
{
|
||||
$propertyName = $property->getName();
|
||||
if ((!in_array($propertyName, AddressFormat::$forbiddenProperyList)) &&
|
||||
(!preg_match('#id|id_\w#', $propertyName, $match)))
|
||||
if ((!in_array($propertyName, AddressFormat::$forbiddenPropertyList)) &&
|
||||
(!preg_match('#id|id_\w#', $propertyName)))
|
||||
$propertyList[] = $propertyName;
|
||||
}
|
||||
unset($object);
|
||||
@@ -355,7 +415,7 @@ class AddressFormatCore extends ObjectModel
|
||||
foreach($publicProperties as $property)
|
||||
{
|
||||
$propertyName = $property->getName();
|
||||
if (preg_match('#id_\w#', $propertyName, $match) && strlen($propertyName) > 3)
|
||||
if (preg_match('#id_\w#', $propertyName) && strlen($propertyName) > 3)
|
||||
{
|
||||
$nameObject = ucfirst(substr($propertyName, 3));
|
||||
if (!in_array($nameObject, self::$forbiddenClassList) &&
|
||||
@@ -375,19 +435,41 @@ class AddressFormatCore extends ObjectModel
|
||||
* @param Integer PS_COUNTRY.id if null using default country
|
||||
* @return Array String field address format
|
||||
*/
|
||||
public static function getOrderedAddressFields($id_country = 0, $split_all = false)
|
||||
public static function getOrderedAddressFields($id_country = 0, $split_all = false, $cleaned = false)
|
||||
{
|
||||
$out = array();
|
||||
$field_set = explode("\n", self::getAddressCountryFormat($id_country));
|
||||
foreach ($field_set as $field_item)
|
||||
if ($split_all)
|
||||
foreach(explode(' ',$field_item) as $word_item)
|
||||
{
|
||||
if ($cleaned)
|
||||
$keyList = ($cleaned) ? preg_split(self::_CLEANING_REGEX_, $field_item, -1, PREG_SPLIT_NO_EMPTY) :
|
||||
explode(' ', $field_item);
|
||||
foreach($keyList as $word_item)
|
||||
$out[] = trim($word_item);
|
||||
}
|
||||
else
|
||||
$out[] = trim($field_item);
|
||||
$out[] = ($cleaned) ? implode(' ', preg_split(self::_CLEANING_REGEX_, trim($field_item), -1, PREG_SPLIT_NO_EMPTY))
|
||||
: trim($field_item);
|
||||
return $out;
|
||||
}
|
||||
|
||||
/*
|
||||
** Return a data array containing ordered, formatedValue and object fields
|
||||
*/
|
||||
public static function getFormattedLayoutData($address)
|
||||
{
|
||||
$layoutData = array();
|
||||
|
||||
if ($address && $address instanceof Address)
|
||||
{
|
||||
$layoutData['ordered'] = AddressFormat::getOrderedAddressFields((int)$address->id_country);
|
||||
$layoutData['formated'] = AddressFormat::getFormattedAddressFieldsValues($address, $layoutData['ordered']);
|
||||
$layoutData['object'] = get_object_vars($address);
|
||||
}
|
||||
return $layoutData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns address format by country if not defined using default country
|
||||
*
|
||||
@@ -399,11 +481,6 @@ class AddressFormatCore extends ObjectModel
|
||||
$out = '';
|
||||
$id_country = (int) $id_country;
|
||||
|
||||
if ($id_country <= 0)
|
||||
{
|
||||
$selectedCountry = (int)(Configuration::get('PS_COUNTRY_DEFAULT'));
|
||||
}
|
||||
|
||||
$tmp_obj = new AddressFormat();
|
||||
$tmp_obj->id_country = $id_country;
|
||||
$out = $tmp_obj->getFormat($tmp_obj->id_country);
|
||||
|
||||
Reference in New Issue
Block a user