From d2f1d99edbc3411e1091971e839724cb70c598ee Mon Sep 17 00:00:00 2001 From: rMalie Date: Tue, 25 Oct 2011 15:17:26 +0000 Subject: [PATCH] [*] Classes: calcul position in class when an attribute, an image or a scene is added --- classes/Attribute.php | 5 ++++- classes/AttributeGroup.php | 43 ++++++++++++++++++++------------------ classes/Image.php | 8 +++++++ classes/Scene.php | 1 + 4 files changed, 36 insertions(+), 21 deletions(-) diff --git a/classes/Attribute.php b/classes/Attribute.php index 01651981d..7565a57c5 100644 --- a/classes/Attribute.php +++ b/classes/Attribute.php @@ -124,6 +124,9 @@ class AttributeCore extends ObjectModel public function add($autodate = true, $null_values = false) { + if ($this->position <= 0) + $this->position = Attribute::getHigherPosition($this->id_attribute_group) + 1; + $return = parent::add($autodate, $null_values); if ($return) Module::hookExec('afterSaveAttribute', array('id_attribute' => $this->id)); @@ -339,7 +342,7 @@ class AttributeCore extends ObjectModel FROM `'._DB_PREFIX_.'attribute` WHERE id_attribute_group = '.(int)$id_attribute_group; $position = DB::getInstance()->getValue($sql); - return ($position !== false) ? $position : -1; + return (is_numeric($position)) ? $position : -1; } } diff --git a/classes/AttributeGroup.php b/classes/AttributeGroup.php index 0e82545c4..76e543b1f 100644 --- a/classes/AttributeGroup.php +++ b/classes/AttributeGroup.php @@ -1,6 +1,6 @@ 'isBool'); protected $fieldsRequiredLang = array('name', 'public_name'); protected $fieldsSizeLang = array('name' => 64, 'public_name' => 64); protected $fieldsValidateLang = array('name' => 'isGenericName', 'public_name' => 'isGenericName', 'position' => 'isInt'); - + protected $table = 'attribute_group'; protected $identifier = 'id_attribute_group'; - + protected $webserviceParameters = array( 'objectsNodeName' => 'product_options', 'objectNodeName' => 'product_option', @@ -68,21 +68,24 @@ class AttributeGroupCore extends ObjectModel return $fields; } - + public function add($autodate = true, $nullValues = false) { + if ($this->position <= 0) + $this->position = AttributeGroup::getHigherPosition() + 1; + $return = parent::add($autodate, true); Module::hookExec('afterSaveAttributeGroup', array('id_attribute_group' => $this->id)); return $return; } - + public function update($nullValues = false) { $return = parent::update($nullValues); Module::hookExec('afterSaveAttributeGroup', array('id_attribute_group' => $this->id)); return $return; } - + /** * Check then return multilingual fields for database interaction * @@ -129,7 +132,7 @@ class AttributeGroupCore extends ObjectModel Module::hookExec('afterDeleteAttributeGroup', array('id_attribute_group' => $this->id)); return $return; } - + /** * Get all attributes for a given language / group * @@ -148,7 +151,7 @@ class AttributeGroupCore extends ObjectModel WHERE a.`id_attribute_group` = '.(int)($id_attribute_group).' ORDER BY `position` ASC'); } - + /** * Get all attributes groups for a given language * @@ -165,7 +168,7 @@ class AttributeGroupCore extends ObjectModel LEFT JOIN `'._DB_PREFIX_.'attribute_group_lang` agl ON (ag.`id_attribute_group` = agl.`id_attribute_group` AND `id_lang` = '.(int)($id_lang).') ORDER BY `name` ASC'); } - + /** * Delete several objects from database * @@ -174,7 +177,7 @@ class AttributeGroupCore extends ObjectModel public function deleteSelection($selection) { /* Also delete Attributes */ - foreach ($selection AS $value) + foreach ($selection AS $value) { $obj = new AttributeGroup($value); if (!$obj->delete()) @@ -182,7 +185,7 @@ class AttributeGroupCore extends ObjectModel } return true; } - + public function setWsProductOptionValues($values) { $ids = array(); @@ -206,13 +209,13 @@ class AttributeGroupCore extends ObjectModel } return $ok; } - + public function getWsProductOptionValues() { $result = Db::getInstance()->executeS('SELECT id_attribute AS id from `'._DB_PREFIX_.'attribute` WHERE id_attribute_group = '.(int)$this->id); return $result; } - + /** * Move a group attribute * @param boolean $way Up (1) or Down (0) @@ -250,7 +253,7 @@ class AttributeGroupCore extends ObjectModel SET `position` = '.(int)$position.' WHERE `id_attribute_group`='.(int)$movedGroupAttribute['id_attribute_group'])); } - + /** * Reorder group attribute position * Call it after deleting a group attribute. @@ -278,9 +281,9 @@ class AttributeGroupCore extends ObjectModel /** * getHigherPosition - * + * * Get the higher group attribute position - * + * * @return integer $position */ public static function getHigherPosition() @@ -288,7 +291,7 @@ class AttributeGroupCore extends ObjectModel $sql = 'SELECT MAX(`position`) FROM `'._DB_PREFIX_.'attribute_group`'; $position = DB::getInstance()->getValue($sql); - return ($position !== false) ? $position : -1; + return (is_numeric($position)) ? $position : -1; } } diff --git a/classes/Image.php b/classes/Image.php index bfd6dee03..07475c305 100644 --- a/classes/Image.php +++ b/classes/Image.php @@ -94,6 +94,14 @@ class ImageCore extends ObjectModel return $this->getTranslationsFields(array('legend')); } + public function add($autodate = true, $null_values = false) + { + if ($this->position <= 0) + $this->position = Image::getHighestPosition($this->id_product) + 1; + + return parent::add($autodate, $null_values); + } + public function delete() { if (!parent::delete() || diff --git a/classes/Scene.php b/classes/Scene.php index d08b31ac4..be95b672c 100644 --- a/classes/Scene.php +++ b/classes/Scene.php @@ -173,6 +173,7 @@ class SceneCore extends ObjectModel $result = true; foreach ($zones AS $zone) { + // @todo use multiple insert $sql = 'INSERT INTO `'._DB_PREFIX_.'scene_products` ( `id_scene` , `id_product` , `x_axis` , `y_axis` , `zone_width` , `zone_height`) VALUES ('.(int)($this->id).', '.(int)($zone['id_product']).', '.(int)($zone['x1']).', '.(int)($zone['y1']).', '.(int)($zone['width']).', '.(int)($zone['height']).')'; if (!Db::getInstance()->execute($sql))