[*] Classes: refactoring of ObjectModel::validateFields() && ObjectModel::validateFieldsLang()
git-svn-id: http://dev.prestashop.com/svn/v1/branches/1.5.x@11273 b9a71923-0436-4b27-9f14-aed3839534dd
This commit is contained in:
@@ -243,7 +243,7 @@ class DiscountCore extends CartRule
|
|||||||
$now = time();
|
$now = time();
|
||||||
$voucher->date_from = date('Y-m-d H:i:s', $now);
|
$voucher->date_from = date('Y-m-d H:i:s', $now);
|
||||||
$voucher->date_to = date('Y-m-d H:i:s', $now + (3600 * 24 * 365.25)); /* 1 year */
|
$voucher->date_to = date('Y-m-d H:i:s', $now + (3600 * 24 * 365.25)); /* 1 year */
|
||||||
if (!$voucher->validateFieldsLang(false) OR !$voucher->add())
|
if (!$voucher->validateFieldsLang(false) || !$voucher->add())
|
||||||
return false;
|
return false;
|
||||||
// set correct name
|
// set correct name
|
||||||
$voucher->name = 'V'.(int)($voucher->id).'C'.(int)($order->id_customer).'O'.$order->id;
|
$voucher->name = 'V'.(int)($voucher->id).'C'.(int)($order->id_customer).'O'.$order->id;
|
||||||
|
|||||||
+98
-72
@@ -53,37 +53,46 @@ abstract class ObjectModelCore
|
|||||||
|
|
||||||
private $getShopFromContext = true;
|
private $getShopFromContext = true;
|
||||||
|
|
||||||
|
protected static $fieldsRequiredDatabase = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string SQL This property shouldn't be overloaded anymore in class, use static $definition['table'] property instead
|
* @deprecated 1.5.0 This property shouldn't be overloaded anymore in class, use static $definition['table'] property instead
|
||||||
* @deprecated 1.5.0
|
|
||||||
*/
|
*/
|
||||||
protected $table;
|
protected $table;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string SQL This property shouldn't be overloaded anymore in class, use static $definition['primary'] property instead
|
* @deprecated 1.5.0 This property shouldn't be overloaded anymore in class, use static $definition['primary'] property instead
|
||||||
* @deprecated 1.5.0
|
|
||||||
*/
|
*/
|
||||||
protected $identifier;
|
protected $identifier;
|
||||||
|
|
||||||
/** @var array Required fields for admin panel forms */
|
/**
|
||||||
|
* @deprecated 1.5.0 This property shouldn't be overloaded anymore in class, use static $definition['fields'] property instead
|
||||||
|
*/
|
||||||
protected $fieldsRequired = array();
|
protected $fieldsRequired = array();
|
||||||
|
|
||||||
/** @var fieldsRequiredDatabase */
|
/**
|
||||||
protected static $fieldsRequiredDatabase = null;
|
* @deprecated 1.5.0 This property shouldn't be overloaded anymore in class, use static $definition['fields'] property instead
|
||||||
|
*/
|
||||||
/** @var array Maximum fields size for admin panel forms */
|
|
||||||
protected $fieldsSize = array();
|
protected $fieldsSize = array();
|
||||||
|
|
||||||
/** @var array Fields validity functions for admin panel forms */
|
/**
|
||||||
|
* @deprecated 1.5.0 This property shouldn't be overloaded anymore in class, use static $definition['fields'] property instead
|
||||||
|
*/
|
||||||
protected $fieldsValidate = array();
|
protected $fieldsValidate = array();
|
||||||
|
|
||||||
/** @var array Multilingual required fields for admin panel forms */
|
/**
|
||||||
|
* @deprecated 1.5.0 This property shouldn't be overloaded anymore in class, use static $definition['fields'] property instead
|
||||||
|
*/
|
||||||
protected $fieldsRequiredLang = array();
|
protected $fieldsRequiredLang = array();
|
||||||
|
|
||||||
/** @var array Multilingual maximum fields size for admin panel forms */
|
/**
|
||||||
|
* @deprecated 1.5.0 This property shouldn't be overloaded anymore in class, use static $definition['fields'] property instead
|
||||||
|
*/
|
||||||
protected $fieldsSizeLang = array();
|
protected $fieldsSizeLang = array();
|
||||||
|
|
||||||
/** @var array Multilingual fields validity functions for admin panel forms */
|
/**
|
||||||
|
* @deprecated 1.5.0 This property shouldn't be overloaded anymore in class, use static $definition['fields'] property instead
|
||||||
|
*/
|
||||||
protected $fieldsValidateLang = array();
|
protected $fieldsValidateLang = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -121,12 +130,13 @@ abstract class ObjectModelCore
|
|||||||
{
|
{
|
||||||
$object = new $className();
|
$object = new $className();
|
||||||
return array(
|
return array(
|
||||||
'required' => $object->fieldsRequired,
|
'required' => $object->fieldsRequired,
|
||||||
'size' => $object->fieldsSize,
|
'size' => $object->fieldsSize,
|
||||||
'validate' => $object->fieldsValidate,
|
'validate' => $object->fieldsValidate,
|
||||||
'requiredLang' => $object->fieldsRequiredLang,
|
'requiredLang' => $object->fieldsRequiredLang,
|
||||||
'sizeLang' => $object->fieldsSizeLang,
|
'sizeLang' => $object->fieldsSizeLang,
|
||||||
'validateLang' => $object->fieldsValidateLang);
|
'validateLang' => $object->fieldsValidateLang,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -204,7 +214,7 @@ abstract class ObjectModelCore
|
|||||||
{
|
{
|
||||||
$fields = $this->getfieldsRequiredDatabase(true);
|
$fields = $this->getfieldsRequiredDatabase(true);
|
||||||
if ($fields)
|
if ($fields)
|
||||||
foreach ($fields AS $row)
|
foreach ($fields as $row)
|
||||||
self::$fieldsRequiredDatabase[$row['object_name']][(int)$row['id_required_field']] = pSQL($row['field_name']);
|
self::$fieldsRequiredDatabase[$row['object_name']][(int)$row['id_required_field']] = pSQL($row['field_name']);
|
||||||
else
|
else
|
||||||
self::$fieldsRequiredDatabase = array();
|
self::$fieldsRequiredDatabase = array();
|
||||||
@@ -226,6 +236,7 @@ abstract class ObjectModelCore
|
|||||||
/**
|
/**
|
||||||
* Prepare multilang fields
|
* Prepare multilang fields
|
||||||
*
|
*
|
||||||
|
* @since 1.5.0
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function getFieldsLang()
|
public function getFieldsLang()
|
||||||
@@ -247,10 +258,11 @@ abstract class ObjectModelCore
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @since 1.5.0
|
||||||
* @param int $id_lang If this parameter is given, only take lang fields
|
* @param int $id_lang If this parameter is given, only take lang fields
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function formatFields($id_lang = null)
|
protected function formatFields($id_lang = null)
|
||||||
{
|
{
|
||||||
$fields = array();
|
$fields = array();
|
||||||
|
|
||||||
@@ -613,32 +625,20 @@ abstract class ObjectModelCore
|
|||||||
*/
|
*/
|
||||||
public function validateFields($die = true, $error_return = false)
|
public function validateFields($die = true, $error_return = false)
|
||||||
{
|
{
|
||||||
$fieldsRequired = array_merge($this->fieldsRequired, (isset(self::$fieldsRequiredDatabase[get_class($this)]) ? self::$fieldsRequiredDatabase[get_class($this)] : array()));
|
foreach ($this->def['fields'] as $field => $data)
|
||||||
foreach ($fieldsRequired as $field)
|
{
|
||||||
if (Tools::isEmpty($this->{$field}) && !is_numeric($this->{$field}))
|
if (!empty($data['lang']))
|
||||||
{
|
continue;
|
||||||
if ($die)
|
|
||||||
throw new PrestashopException('property empty : '.get_class($this).'->'.$field);
|
|
||||||
return $error_return ? get_class($this).' -> '.$field.' is empty' : false;
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach ($this->fieldsSize as $field => $size)
|
$message = $this->validateField($field, $this->$field);
|
||||||
if (isset($this->{$field}) && Tools::strlen($this->{$field}) > $size)
|
if ($message !== true)
|
||||||
{
|
{
|
||||||
if ($die)
|
if ($die)
|
||||||
throw new PrestashopException('fieldsize error : '.get_class($this).'->'.$field.' > '.$size);
|
throw new PrestashopException($message);
|
||||||
return $error_return ? get_class($this).' -> '.$field.' Length '.$size : false;
|
return $error_return ? $message : false;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
foreach ($this->fieldsValidate as $field => $method)
|
|
||||||
if (!method_exists('Validate', $method))
|
|
||||||
throw new PrestashopException('Validation function not found. '.$method);
|
|
||||||
elseif (!empty($this->{$field}) && !call_user_func(array('Validate', $method), $this->{$field}))
|
|
||||||
{
|
|
||||||
if ($die)
|
|
||||||
throw new PrestashopException('Field not valid : '.get_class($this).'->'.$field.' = '.$this->{$field});
|
|
||||||
return $error_return ? get_class($this).' -> '.$field.' = '.$this->{$field} : false;
|
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -651,46 +651,72 @@ abstract class ObjectModelCore
|
|||||||
*/
|
*/
|
||||||
public function validateFieldsLang($die = true, $error_return = false)
|
public function validateFieldsLang($die = true, $error_return = false)
|
||||||
{
|
{
|
||||||
$defaultLanguage = (int)Configuration::get('PS_LANG_DEFAULT');
|
foreach ($this->def['fields'] as $field => $data)
|
||||||
foreach ($this->fieldsRequiredLang as $fieldArray)
|
|
||||||
{
|
{
|
||||||
if (!is_array($this->{$fieldArray}))
|
if (empty($data['lang']))
|
||||||
continue ;
|
continue;
|
||||||
if (!$this->{$fieldArray} || !count($this->{$fieldArray}) || ($this->{$fieldArray}[$defaultLanguage] !== '0' && empty($this->{$fieldArray}[$defaultLanguage])))
|
|
||||||
|
$values = $this->$field;
|
||||||
|
if (!is_array($values))
|
||||||
|
$values = array($this->id_lang => $values);
|
||||||
|
|
||||||
|
foreach ($values as $id_lang => $value)
|
||||||
{
|
{
|
||||||
if ($die)
|
$message = $this->validateField($field, $value, $id_lang);
|
||||||
throw new PrestashopException('empty for default language : '.get_class($this).'->'.$fieldArray);
|
if ($message !== true)
|
||||||
return $error_return ? get_class($this).'->'.$fieldArray.' '.Tools::displayError('is empty for default language.') : false;
|
{
|
||||||
|
if ($die)
|
||||||
|
throw new PrestashopException($message);
|
||||||
|
return $error_return ? $message : false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($this->fieldsSizeLang as $fieldArray => $size)
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validate a single field
|
||||||
|
*
|
||||||
|
* @since 1.5.0
|
||||||
|
* @param string $field Field name
|
||||||
|
* @param mixed $value Field value
|
||||||
|
* @param int $id_lang
|
||||||
|
* @return bool|string
|
||||||
|
*/
|
||||||
|
public function validateField($field, $value, $id_lang = null)
|
||||||
|
{
|
||||||
|
$data = $this->def['fields'][$field];
|
||||||
|
|
||||||
|
// Check if field is required
|
||||||
|
$required_fields = (isset(self::$fieldsRequiredDatabase[get_class($this)])) ? self::$fieldsRequiredDatabase[get_class($this)] : array();
|
||||||
|
if (!$id_lang || $id_lang == Configuration::get('PS_LANG_DEFAULT'))
|
||||||
|
if (!empty($data['required']) || in_array($field, $required_fields))
|
||||||
|
if (Tools::isEmpty($value))
|
||||||
|
return 'Property '.get_class($this).'->'.$field.' is empty';
|
||||||
|
|
||||||
|
// Check field size
|
||||||
|
if (!empty($data['size']))
|
||||||
{
|
{
|
||||||
if (!is_array($this->{$fieldArray}))
|
$size = $data['size'];
|
||||||
continue ;
|
if (!is_array($data['size']))
|
||||||
foreach ($this->{$fieldArray} as $k => $value)
|
$size = array('min' => 0, 'max' => $data['size']);
|
||||||
if (Tools::strlen($value) > $size)
|
|
||||||
{
|
$length = Tools::strlen($value);
|
||||||
if ($die)
|
if ($length < $size['min'] || $length > $size)
|
||||||
throw new PrestashopException('fieldsize error '.get_class($this).'->'.$fieldArray.' length of '.$size.' for language');
|
return 'Property '.get_class($this).'->'.$field.' has bad length ('.$length.') and must be between '.$size['min'].' and '.$size['max'];
|
||||||
return $error_return ? get_class($this).'->'.$fieldArray.' '.Tools::displayError('Length').' '.$size.' '.Tools::displayError('for language') : false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($this->fieldsValidateLang as $fieldArray => $method)
|
// Check field validator
|
||||||
|
if (!empty($data['validate']))
|
||||||
{
|
{
|
||||||
if (!is_array($this->{$fieldArray}))
|
if (!method_exists('Validate', $data['validate']))
|
||||||
continue ;
|
throw new PrestashopException('Validation function not found. '.$data['validate']);
|
||||||
foreach ($this->{$fieldArray} as $k => $value)
|
|
||||||
if (!method_exists('Validate', $method))
|
if (!empty($value) && !call_user_func(array('Validate', $data['validate']), $value))
|
||||||
throw new PrestashopException('Validation function not found for lang: '.$method);
|
return 'Property '.get_class($this).'->'.$field.' is not valid';
|
||||||
elseif (!empty($value) && !call_user_func(array('Validate', $method), $value))
|
|
||||||
{
|
|
||||||
if ($die)
|
|
||||||
throw new PrestashopException('Field not valid : '.get_class($this).'->'.$fieldArray.' = '.$value. 'for language '.$k);
|
|
||||||
return $error_return ? Tools::displayError('The following field is invalid according to the validate method ').'<b>'.$method.'</b>:<br/> ('. get_class($this).'->'.$fieldArray.' = '.$value.' '.Tools::displayError('for language').' '.$k : false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+4
-12
@@ -585,24 +585,16 @@ class ProductCore extends ObjectModel
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see ObjectModel::validateFieldsLang()
|
||||||
|
*/
|
||||||
public function validateFieldsLang($die = true, $error_return = false)
|
public function validateFieldsLang($die = true, $error_return = false)
|
||||||
{
|
{
|
||||||
$limit = (int)Configuration::get('PS_PRODUCT_SHORT_DESC_LIMIT');
|
$limit = (int)Configuration::get('PS_PRODUCT_SHORT_DESC_LIMIT');
|
||||||
if ($limit <= 0)
|
if ($limit <= 0)
|
||||||
$limit = 800;
|
$limit = 800;
|
||||||
if (!is_array($this->description_short))
|
$this->def['fields']['description_short']['size'] = $limit;
|
||||||
$this->description_short = array();
|
|
||||||
foreach ($this->description_short as $k => $value)
|
|
||||||
if (Tools::strlen(strip_tags($value)) > $limit)
|
|
||||||
{
|
|
||||||
if ($die) die (Tools::displayError().' ('.get_class($this).'->description_short: length > '.$limit.' for language '.$k.')');
|
|
||||||
|
|
||||||
$return = false;
|
|
||||||
if ($error_return)
|
|
||||||
$return = get_class($this).'->'.Tools::displayError('description_short: length >').' '.$limit.' '.Tools::displayError('for language').' '.$k;
|
|
||||||
|
|
||||||
return $return;
|
|
||||||
}
|
|
||||||
return parent::validateFieldsLang($die, $error_return);
|
return parent::validateFieldsLang($die, $error_return);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -61,12 +61,6 @@ class TabCore extends ObjectModel
|
|||||||
|
|
||||||
protected static $_getIdFromClassName = null;
|
protected static $_getIdFromClassName = null;
|
||||||
|
|
||||||
public function getTranslationsFieldsChild()
|
|
||||||
{
|
|
||||||
$this->validateFieldsLang();
|
|
||||||
return $this->getTranslationsFields(array('name'));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* additionnal treatments for Tab when creating new one :
|
* additionnal treatments for Tab when creating new one :
|
||||||
* - generate a new position
|
* - generate a new position
|
||||||
|
|||||||
Reference in New Issue
Block a user