// Improve ObjectModel definitions (test on products)

git-svn-id: http://dev.prestashop.com/svn/v1/branches/1.5.x@11135 b9a71923-0436-4b27-9f14-aed3839534dd
This commit is contained in:
rMalie
2011-12-12 13:53:51 +00:00
parent 2ad9a12889
commit dc19003820
2 changed files with 137 additions and 112 deletions

View File

@@ -27,6 +27,21 @@
abstract class ObjectModelCore
{
/**
* List of field types
*/
const TYPE_INT = 1;
const TYPE_BOOL = 2;
const TYPE_STRING = 3;
const TYPE_FLOAT = 4;
const TYPE_DATE = 5;
/**
* List of association types
*/
const HAS_ONE = 1;
const HAS_MANY = 2;
/** @var integer Object id */
public $id;
@@ -203,7 +218,40 @@ abstract class ObjectModelCore
*/
public function getFields()
{
return array();
$this->validateFields();
$fields = array();
foreach ($this->def['fields'] as $field => $data)
if (!isset($data['lang']) || !$data['lang'])
switch ($data['type'])
{
case self::TYPE_INT :
$fields[$field] = (int)$this->$field;
break;
case self::TYPE_BOOL :
$fields[$field] = (bool)$this->$field;
break;
case self::TYPE_FLOAT :
$fields[$field] = (float)$this->$field;
break;
case self::TYPE_DATE :
$fields[$field] = pSQL($this->$field);
break;
case self::TYPE_STRING :
$fields[$field] = pSQL($this->$field);
break;
default :
if (method_exists($this, 'formatType'.$data['type']))
$fields[$field] = $this->{'formatType'.$data['type']}($this->$field);
break;
}
return $fields;
}
/**
@@ -1092,31 +1140,30 @@ abstract class ObjectModelCore
else
$this->def['primary'] = $this->identifier;
// Retrocompatibility with $fieldsValidate, $fieldsRequired and $fieldsSize properties ($definition['fields'] && $definition['fields_lang'])
foreach (array('fields' => '', 'fields_lang' => 'Lang') as $type => $suffix)
if (isset($this->def[$type]))
// Retrocompatibility with $fieldsValidate, $fieldsRequired and $fieldsSize properties ($definition['fields'])
if (isset($this->def['fields']))
{
foreach ($this->def['fields'] as $field => $data)
{
foreach ($this->def[$type] as $field => $data)
{
if (isset($data['validate']))
$this->{'fieldsValidate'.$suffix}[$field] = $data['validate'];
if (isset($data['required']) && $data['required'])
$this->{'fieldsRequired'.$suffix}[] = $field;
if (isset($data['size']))
$this->{'fieldsSize'.$suffix}[$field] = $data['size'];
}
}
else
{
if ($type == 'fields')
$this->def['fields'] = array();
foreach ($this->{'fieldsValidate'.$suffix} as $field => $validate)
$this->def[$type][$field]['validate'] = $validate;
foreach ($this->{'fieldsRequired'.$suffix} as $field)
$this->def[$type][$field]['required'] = true;
foreach ($this->{'fieldsSize'.$suffix} as $field => $size)
$this->def[$type][$field]['size'] = $size;
$suffix = (isset($data['lang']) && $data['lang']) ? 'Lang' : '';
if (isset($data['validate']))
$this->{'fieldsValidate'.$suffix}[$field] = $data['validate'];
if (isset($data['required']) && $data['required'])
$this->{'fieldsRequired'.$suffix}[] = $field;
if (isset($data['size']))
$this->{'fieldsSize'.$suffix}[$field] = $data['size'];
}
}
else
{
$this->def['fields'] = array();
$suffix = (isset($data['lang']) && $data['lang']) ? 'Lang' : '';
foreach ($this->{'fieldsValidate'.$suffix} as $field => $validate)
$this->def['fields'][$field]['validate'] = $validate;
foreach ($this->{'fieldsRequired'.$suffix} as $field)
$this->def['fields'][$field]['required'] = true;
foreach ($this->{'fieldsSize'.$suffix} as $field => $size)
$this->def['fields'][$field]['size'] = $size;
}
}
}

View File

@@ -208,59 +208,70 @@ class ProductCore extends ObjectModel
/** @var array cache stock data in getStock() method */
protected static $cacheStock = array();
protected $fieldsRequired = array('price');
protected $fieldsSize = array('reference' => 32, 'supplier_reference' => 32, 'location' => 64, 'ean13' => 13, 'upc' => 12, 'unity' => 10);
protected $fieldsValidate = array(
'id_tax_rules_group' => 'isUnsignedId',
'id_manufacturer' => 'isUnsignedId',
'id_supplier' => 'isUnsignedId',
'id_category_default' => 'isUnsignedId',
'minimal_quantity' => 'isUnsignedInt',
'price' => 'isPrice',
'additional_shipping_cost' => 'isPrice',
'wholesale_price' => 'isPrice',
'on_sale' => 'isBool',
'online_only' => 'isBool',
'ecotax' => 'isPrice',
'unit_price' => 'isPrice',
'unity' => 'isString',
'reference' => 'isReference',
'supplier_reference' => 'isReference',
'location' => 'isReference',
'width' => 'isUnsignedFloat',
'height' => 'isUnsignedFloat',
'depth' => 'isUnsignedFloat',
'weight' => 'isUnsignedFloat',
'quantity_discount' => 'isBool',
'customizable' => 'isUnsignedInt',
'uploadable_files' => 'isUnsignedInt',
'text_fields' => 'isUnsignedInt',
'active' => 'isBool',
'available_for_order' => 'isBool',
'available_date' => 'isDateFormat',
'condition' => 'isGenericName',
'show_price' => 'isBool',
'ean13' => 'isEan13',
'upc' => 'isUpc',
'indexed' => 'isBool',
'cache_is_pack' => 'isBool',
'is_virtual' => 'isBool',
'cache_has_attachments' => 'isBool'
);
protected $fieldsRequiredLang = array('link_rewrite', 'name');
/* Description short is limited to 800 chars (but can be configured in Preferences Tab), but without html, so it can't be generic */
protected $fieldsSizeLang = array('meta_description' => 255, 'meta_keywords' => 255,
'meta_title' => 128, 'link_rewrite' => 128, 'name' => 128, 'available_now' => 255, 'available_later' => 255);
protected $fieldsValidateLang = array(
'meta_description' => 'isGenericName', 'meta_keywords' => 'isGenericName',
'meta_title' => 'isGenericName', 'link_rewrite' => 'isLinkRewrite', 'name' => 'isCatalogName',
'description' => 'isString', 'description_short' => 'isString', 'available_now' => 'isGenericName', 'available_later' => 'IsGenericName');
public static $definition = array(
'table' => 'product',
'primary' => 'id_product',
'multilang' => true,
'multishop' => true,
'fields' => array(
// Classic fields
'id_tax_rules_group' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
'id_manufacturer' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
'id_supplier' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
'id_category_default' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
'minimal_quantity' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'),
'price' => array('type' => self::TYPE_FLOAT, 'validate' => 'isPrice', 'required' => true),
'additional_shipping_cost' => array('type' => self::TYPE_FLOAT, 'validate' => 'isPrice'),
'wholesale_price' => array('type' => self::TYPE_FLOAT, 'validate' => 'isPrice'),
'on_sale' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
'online_only' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
'ecotax' => array('type' => self::TYPE_FLOAT, 'validate' => 'isPrice'),
'unity' => array('type' => self::TYPE_STRING, 'validate' => 'isString'),
'unit_price_ratio' => array('type' => self::TYPE_FLOAT),
'reference' => array('type' => self::TYPE_STRING, 'validate' => 'isReference', 'size' => 32),
'supplier_reference' => array('type' => self::TYPE_STRING, 'validate' => 'isReference', 'size' => 32),
'location' => array('type' => self::TYPE_STRING, 'validate' => 'isReference', 'size' => 64),
'width' => array('type' => self::TYPE_FLOAT, 'validate' => 'isUnsignedFloat'),
'height' => array('type' => self::TYPE_FLOAT, 'validate' => 'isUnsignedFloat'),
'depth' => array('type' => self::TYPE_FLOAT, 'validate' => 'isUnsignedFloat'),
'weight' => array('type' => self::TYPE_FLOAT, 'validate' => 'isUnsignedFloat'),
'quantity_discount' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
'customizable' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'),
'uploadable_files' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'),
'text_fields' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'),
'active' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
'available_for_order' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
'available_date' => array('type' => self::TYPE_DATE, 'validate' => 'isDateFormat'),
'condition' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName'), // @todo enum ?
'show_price' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
'ean13' => array('type' => self::TYPE_STRING, 'validate' => 'isEan13', 'size' => 13),
'upc' => array('type' => self::TYPE_STRING, 'validate' => 'isUpc', 'size' => 12),
'indexed' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
'cache_is_pack' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
'cache_default_attribute' => array('type' => self::TYPE_INT),
'is_virtual' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
'cache_has_attachments' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
'date_add' => array('type' => self::TYPE_DATE, 'validate' => 'isDateFormat'),
'date_upd' => array('type' => self::TYPE_DATE, 'validate' => 'isDateFormat'),
// Lang fields
'meta_description' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'size' => 255),
'meta_keywords' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'size' => 255),
'meta_title' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'size' => 128),
'link_rewrite' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isLinkRewrite', 'required' => true, 'size' => 128),
'name' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isCatalogName', 'required' => true, 'size' => 128),
'description' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isString'),
'description_short' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isString'),
'available_now' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'size' => 255),
'available_later' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'IsGenericName', 'size' => 255),
),
'associations' => array(
'manufacturer' => array('type' => self::HAS_ONE),
'supplier' => array('type' => self::HAS_ONE),
'default_category' => array('type' => self::HAS_ONE, 'field' => 'id_category_default', 'object' => 'Category'),
'tax_rules_group' => array('type' => self::HAS_ONE),
'categories' => array('type' => self::HAS_MANY, 'field' => 'id_category', 'object' => 'Category', 'association' => 'category_product'),
),
);
protected $webserviceParameters = array(
@@ -384,50 +395,17 @@ class ProductCore extends ObjectModel
$this->category = Category::getLinkRewrite((int)$this->id_category_default, (int)$id_lang);
}
/**
* @see ObjectModel::getFields()
* @return array
*/
public function getFields()
{
$this->validateFields();
$fields = parent::getFields();
if (isset($this->id))
$fields['id_product'] = (int)$this->id;
$fields['id_tax_rules_group'] = (int)$this->id_tax_rules_group;
$fields['id_manufacturer'] = (int)$this->id_manufacturer;
$fields['id_supplier'] = (int)$this->id_supplier;
$fields['id_category_default'] = (int)$this->id_category_default;
$fields['quantity'] = (int)$this->quantity;
$fields['minimal_quantity'] = (int)$this->minimal_quantity;
$fields['price'] = (float)$this->price;
$fields['additional_shipping_cost'] = (float)$this->additional_shipping_cost;
$fields['wholesale_price'] = (float)$this->wholesale_price;
$fields['on_sale'] = (int)$this->on_sale;
$fields['online_only'] = (int)$this->online_only;
$fields['ecotax'] = (float)$this->ecotax;
$fields['unity'] = pSQL($this->unity);
$fields['unit_price_ratio'] = (float)$this->unit_price > 0 ? $this->price / $this->unit_price : 0;
$fields['ean13'] = pSQL($this->ean13);
$fields['upc'] = pSQL($this->upc);
$fields['reference'] = pSQL($this->reference);
$fields['supplier_reference'] = pSQL($this->supplier_reference);
$fields['location'] = pSQL($this->location);
$fields['width'] = (float)$this->width;
$fields['height'] = (float)$this->height;
$fields['depth'] = (float)$this->depth;
$fields['weight'] = (float)$this->weight;
$fields['quantity_discount'] = (int)$this->quantity_discount;
$fields['customizable'] = (int)$this->customizable;
$fields['uploadable_files'] = (int)$this->uploadable_files;
$fields['text_fields'] = (int)$this->text_fields;
$fields['active'] = (int)$this->active;
$fields['available_for_order'] = (int)$this->available_for_order;
$fields['available_date'] = pSQL($this->available_date);
$fields['condition'] = pSQL($this->condition);
$fields['show_price'] = (int)$this->show_price;
$fields['indexed'] = 0; // Reset indexation every times
$fields['cache_is_pack'] = (int)$this->cache_is_pack;
$fields['cache_has_attachments'] = (int)$this->cache_has_attachments;
$fields['is_virtual'] = (int)$this->is_virtual;
$fields['cache_default_attribute'] = (int)$this->cache_default_attribute;
$fields['date_add'] = pSQL($this->date_add);
$fields['date_upd'] = pSQL($this->date_upd);
return $fields;
}