public function __construct(Product $product, Attribute $attribute, AttributeValue $attributeValue)
 {
     $this->setId();
     $this->setCreated();
     $this->product = $product;
     $this->attribute = $attribute;
     $this->attributeValue = $attributeValue;
     $product->addProductAttribute($this);
     $attribute->addProductAttribute($this);
     $attributeValue->addProductAttribute($this);
 }
Example #2
0
 public function attributeImport()
 {
     global $cookie;
     $defaultLanguage = Configuration::get('PS_LANG_DEFAULT');
     $groups = array();
     foreach (AttributeGroup::getAttributesGroups($defaultLanguage) as $group) {
         $groups[$group['name']] = (int) $group['id_attribute_group'];
     }
     $attributes = array();
     foreach (Attribute::getAttributes($defaultLanguage) as $attribute) {
         $attributes[$attribute['attribute_group'] . '_' . $attribute['name']] = (int) $attribute['id_attribute'];
     }
     $this->receiveTab();
     $handle = $this->openCsvFile();
     $fsep = (is_null(Tools::getValue('multiple_value_separator')) or trim(Tools::getValue('multiple_value_separator')) == '') ? ',' : Tools::getValue('multiple_value_separator');
     self::setLocale();
     for ($current_line = 0; $line = fgetcsv($handle, MAX_LINE_SIZE, Tools::getValue('separator')); $current_line++) {
         if (Tools::getValue('convert')) {
             $line = $this->utf8_encode_array($line);
         }
         $info = self::getMaskedRow($line);
         $info = array_map('trim', $info);
         self::setDefaultValues($info);
         $product = new Product((int) $info['id_product'], false, $defaultLanguage);
         $id_image = null;
         if (isset($info['image_url']) && $info['image_url']) {
             $productHasImages = (bool) Image::getImages((int) $cookie->id_lang, (int) $product->id);
             $url = $info['image_url'];
             $image = new Image();
             $image->id_product = (int) $product->id;
             $image->position = Image::getHighestPosition($product->id) + 1;
             $image->cover = !$productHasImages ? true : false;
             $image->legend = self::createMultiLangField($product->name);
             if (($fieldError = $image->validateFields(UNFRIENDLY_ERROR, true)) === true and ($langFieldError = $image->validateFieldsLang(UNFRIENDLY_ERROR, true)) === true and $image->add()) {
                 if (!self::copyImg($product->id, $image->id, $url)) {
                     $this->_warnings[] = Tools::displayError('Error copying image: ') . $url;
                 } else {
                     $id_image = array($image->id);
                 }
             } else {
                 $this->_warnings[] = $image->legend[$defaultLanguageId] . (isset($image->id_product) ? ' (' . $image->id_product . ')' : '') . ' ' . Tools::displayError('Cannot be saved');
                 $this->_errors[] = ($fieldError !== true ? $fieldError : '') . ($langFieldError !== true ? $langFieldError : '') . mysql_error();
             }
         } elseif (isset($info['image_position']) && $info['image_position']) {
             $images = $product->getImages($defaultLanguage);
             if ($images) {
                 foreach ($images as $row) {
                     if ($row['position'] == (int) $info['image_position']) {
                         $id_image = array($row['id_image']);
                         break;
                     }
                 }
             }
             if (!$id_image) {
                 $this->_warnings[] = sprintf(Tools::displayError('No image found for combination with id_product = %s and image position = %s.'), $product->id, (int) $info['image_position']);
             }
         }
         $id_product_attribute = $product->addProductAttribute((double) $info['price'], (double) $info['weight'], 0, (double) $info['ecotax'], (int) $info['quantity'], $id_image, strval($info['reference']), strval($info['supplier_reference']), strval($info['ean13']), (int) $info['default_on'], strval($info['upc']));
         foreach (explode($fsep, $info['options']) as $option) {
             list($group, $attribute) = array_map('trim', explode(':', $option));
             if (!isset($groups[$group])) {
                 $obj = new AttributeGroup();
                 $obj->is_color_group = false;
                 $obj->name[$defaultLanguage] = $group;
                 $obj->public_name[$defaultLanguage] = $group;
                 if (($fieldError = $obj->validateFields(UNFRIENDLY_ERROR, true)) === true and ($langFieldError = $obj->validateFieldsLang(UNFRIENDLY_ERROR, true)) === true) {
                     $obj->add();
                     $groups[$group] = $obj->id;
                 } else {
                     $this->_errors[] = ($fieldError !== true ? $fieldError : '') . ($langFieldError !== true ? $langFieldError : '');
                 }
             }
             if (!isset($attributes[$group . '_' . $attribute])) {
                 $obj = new Attribute();
                 $obj->id_attribute_group = $groups[$group];
                 $obj->name[$defaultLanguage] = str_replace('\\n', '', str_replace('\\r', '', $attribute));
                 if (($fieldError = $obj->validateFields(UNFRIENDLY_ERROR, true)) === true and ($langFieldError = $obj->validateFieldsLang(UNFRIENDLY_ERROR, true)) === true) {
                     $obj->add();
                     $attributes[$group . '_' . $attribute] = $obj->id;
                 } else {
                     $this->_errors[] = ($fieldError !== true ? $fieldError : '') . ($langFieldError !== true ? $langFieldError : '');
                 }
             }
             Db::getInstance()->Execute('INSERT INTO ' . _DB_PREFIX_ . 'product_attribute_combination (id_attribute, id_product_attribute) VALUES (' . (int) $attributes[$group . '_' . $attribute] . ',' . (int) $id_product_attribute . ')');
         }
     }
     $this->closeCsvFile($handle);
 }
 public function attributeImport()
 {
     $defaultLanguage = Configuration::get('PS_LANG_DEFAULT');
     $groups = array();
     foreach (AttributeGroup::getAttributesGroups($defaultLanguage) as $group) {
         $groups[$group['name']] = (int) $group['id_attribute_group'];
     }
     $attributes = array();
     foreach (Attribute::getAttributes($defaultLanguage) as $attribute) {
         $attributes[$attribute['attribute_group'] . '_' . $attribute['name']] = (int) $attribute['id_attribute'];
     }
     $this->receiveTab();
     $handle = $this->openCsvFile();
     $fsep = (is_null(Tools::getValue('multiple_value_separator')) or trim(Tools::getValue('multiple_value_separator')) == '') ? ',' : Tools::getValue('multiple_value_separator');
     self::setLocale();
     for ($current_line = 0; $line = fgetcsv($handle, MAX_LINE_SIZE, Tools::getValue('separator')); $current_line++) {
         if (Tools::getValue('convert')) {
             $this->utf8_encode_array($line);
         }
         $info = self::getMaskedRow($line);
         $info = array_map('trim', $info);
         self::setDefaultValues($info);
         $product = new Product((int) $info['id_product'], false, $defaultLanguage);
         $id_product_attribute = $product->addProductAttribute((double) $info['price'], (double) $info['weight'], 0, (double) $info['ecotax'], (int) $info['quantity'], null, strval($info['reference']), strval($info['supplier_reference']), strval($info['ean13']), (int) $info['default_on'], strval($info['upc']));
         foreach (explode($fsep, $info['options']) as $option) {
             list($group, $attribute) = array_map('trim', explode(':', $option));
             if (!isset($groups[$group])) {
                 $obj = new AttributeGroup();
                 $obj->is_color_group = false;
                 $obj->name[$defaultLanguage] = $group;
                 $obj->public_name[$defaultLanguage] = $group;
                 if (($fieldError = $obj->validateFields(UNFRIENDLY_ERROR, true)) === true and ($langFieldError = $obj->validateFieldsLang(UNFRIENDLY_ERROR, true)) === true) {
                     $obj->add();
                     $groups[$group] = $obj->id;
                 } else {
                     $this->_errors[] = ($fieldError !== true ? $fieldError : '') . ($langFieldError !== true ? $langFieldError : '');
                 }
             }
             if (!isset($attributes[$group . '_' . $attribute])) {
                 $obj = new Attribute();
                 $obj->id_attribute_group = $groups[$group];
                 $obj->name[$defaultLanguage] = $attribute;
                 if (($fieldError = $obj->validateFields(UNFRIENDLY_ERROR, true)) === true and ($langFieldError = $obj->validateFieldsLang(UNFRIENDLY_ERROR, true)) === true) {
                     $obj->add();
                     $attributes[$group . '_' . $attribute] = $obj->id;
                 } else {
                     $this->_errors[] = ($fieldError !== true ? $fieldError : '') . ($langFieldError !== true ? $langFieldError : '');
                 }
             }
             Db::getInstance()->Execute('INSERT INTO ' . _DB_PREFIX_ . 'product_attribute_combination (id_attribute, id_product_attribute) VALUES (' . (int) $attributes[$group . '_' . $attribute] . ',' . (int) $id_product_attribute . ')');
         }
     }
     $this->closeCsvFile($handle);
 }
 /**
  * Updates product combinations on Prestashop.
  * 
  * @param integer $id_product
  * @param string $url_photo
  * @param array $triple_cod_col_siz
  * @param array $array_combinations
  * @param integer $language
  * @return bool
  * @see $this->createAttributeGroups
  * @see $this->setArrayElementForLinkRewrite
  * @see $this->getCodeColor
  * @see PrestashopImageHandler getIdImageByName()
  * @see PrestashopImageHandler insertImageInPrestashop()
  *
  */
 private function updateCombinantionsForPrestashop($id_product, $url_photo, $triple_cod_col_siz, $array_combinations, $language = 1)
 {
     $product = new Product((int) $id_product);
     $id_new_images = array();
     $price = '0.000';
     //(float)$product->price;
     $reference = $product->reference;
     $id_supplier = (int) $product->id_supplier;
     foreach ($triple_cod_col_siz as $triple) {
         $array_attributes_and_values = $array_combinations[$triple];
         $attributes = $array_attributes_and_values["Attributi"];
         $values = $array_attributes_and_values["Valori"];
         $image = trim($array_attributes_and_values["Immagine"]);
         $quantity = (int) $array_attributes_and_values["Qta"];
         $variable_tmp_attributes = explode(",", $attributes);
         $variable_tmp_values = explode(",", $values);
         $id_attributes_for_combinations = array();
         $flag_just_exist_color = 1;
         $flag_just_exist_size = 1;
         for ($i = 0; $i < sizeof($variable_tmp_attributes); $i++) {
             $code = "";
             if ($variable_tmp_values[$i] != "") {
                 $id_attribute_group = $this->createAttributeGroups($variable_tmp_attributes[$i], $language);
                 $id_attributes_for_not_repeat = $this->getAttributeColorAndSize($variable_tmp_values[$i], $language);
                 if (strtolower($variable_tmp_attributes[$i]) === "colore" || strtolower($variable_tmp_attributes[$i]) === "colori") {
                     $product_attribute_for_not_reply = $product->getAttributeCombinations($language);
                     if (empty($product_attribute_for_not_reply)) {
                         if ($id_attributes_for_not_repeat === '-1') {
                             $code = $this->getCodeColor(strtolower($variable_tmp_values[$i]));
                             if ($code != "") {
                                 $code = "#" . $code;
                                 $attribute_for_product = new Attribute();
                                 $attribute_for_product->name = $this->setArrayElementForLinkRewrite($variable_tmp_values[$i], true, $language);
                                 $attribute_for_product->color = $code;
                                 $attribute_for_product->id_attribute_group = $id_attribute_group;
                                 $attribute_for_product->add();
                                 array_push($id_attributes_for_combinations, $attribute_for_product->id);
                             }
                         } else {
                             array_push($id_attributes_for_combinations, (int) $id_attributes_for_not_repeat);
                         }
                     } else {
                         $flag = 1;
                         foreach ($product_attribute_for_not_reply as $more_attributes) {
                             if ($more_attributes['attribute_name'] === $variable_tmp_values[$i]) {
                                 array_push($id_attributes_for_combinations, (int) $more_attributes['id_attribute']);
                                 $flag = 0;
                                 $flag_just_exist_color = 0;
                                 break;
                             }
                         }
                         if ($flag) {
                             if ($id_attributes_for_not_repeat === '-1') {
                                 $code = $this->getCodeColor(strtolower($variable_tmp_values[$i]));
                                 if ($code != "") {
                                     $code = "#" . $code;
                                     $attribute_for_product = new Attribute();
                                     $attribute_for_product->name = $this->setArrayElementForLinkRewrite($variable_tmp_values[$i], true, $language);
                                     $attribute_for_product->color = $code;
                                     $attribute_for_product->id_attribute_group = $id_attribute_group;
                                     $attribute_for_product->add();
                                     array_push($id_attributes_for_combinations, $attribute_for_product->id);
                                 }
                             } else {
                                 array_push($id_attributes_for_combinations, (int) $id_attributes_for_not_repeat);
                             }
                         }
                     }
                 }
                 if (strtolower($variable_tmp_attributes[$i]) === "taglia" || strtolower($variable_tmp_attributes[$i]) === "taglie") {
                     $product_attribute_for_not_reply = $product->getAttributeCombinations($language);
                     if (empty($product_attribute_for_not_reply)) {
                         if ($id_attributes_for_not_repeat === '-1') {
                             $attribute_for_product = new Attribute();
                             $attribute_for_product->name = $this->setArrayElementForLinkRewrite($variable_tmp_values[$i], true, $language);
                             $attribute_for_product->id_attribute_group = $id_attribute_group;
                             $attribute_for_product->add();
                             array_push($id_attributes_for_combinations, $attribute_for_product->id);
                         } else {
                             array_push($id_attributes_for_combinations, (int) $id_attributes_for_not_repeat);
                         }
                     } else {
                         $flag = 1;
                         foreach ($product_attribute_for_not_reply as $more_attributes) {
                             $vrbls_1 = trim($more_attributes['attribute_name']);
                             $vrbls_2 = trim($variable_tmp_values[$i]);
                             if (gettype($vrbls_1) === "string" && gettype($vrbls_2) === "string") {
                                 if ($vrbls_1 === $vrbls_2) {
                                     array_push($id_attributes_for_combinations, (int) $more_attributes['id_attribute']);
                                     $flag = 0;
                                     $flag_just_exist_size = 0;
                                     break;
                                 }
                             }
                             if (gettype($vrbls_1) === "integer" && gettype($vrbls_2) === "integer") {
                                 if ($vrbls_1 == $vrbls_2) {
                                     array_push($id_attributes_for_combinations, (int) $more_attributes['id_attribute']);
                                     $flag = 0;
                                     $flag_just_exist_size = 0;
                                     break;
                                 }
                             }
                         }
                         if ($flag) {
                             if ($id_attributes_for_not_repeat === '-1') {
                                 $attribute_for_product = new Attribute();
                                 $attribute_for_product->name = $this->setArrayElementForLinkRewrite($variable_tmp_values[$i], true, $language);
                                 $attribute_for_product->id_attribute_group = $id_attribute_group;
                                 $attribute_for_product->add();
                                 array_push($id_attributes_for_combinations, $attribute_for_product->id);
                             } else {
                                 array_push($id_attributes_for_combinations, (int) $id_attributes_for_not_repeat);
                             }
                         }
                     }
                 }
             } else {
                 if (strtolower($variable_tmp_attributes[$i]) === "colore" || strtolower($variable_tmp_attributes[$i]) === "colori") {
                     $flag_just_exist_color = 0;
                 }
                 if (strtolower($variable_tmp_attributes[$i]) === "taglia" || strtolower($variable_tmp_attributes[$i]) === "taglie") {
                     $flag_just_exist_size = 0;
                 }
             }
         }
         if ($flag_just_exist_color || $flag_just_exist_size) {
             $id_images = array();
             $tmp_photo = explode(".jpg,", $image);
             for ($i = 0; $i < sizeof($tmp_photo); $i++) {
                 if (!empty($tmp_photo[$i])) {
                     $image_for_prestashop = new PrestashopImageHandler();
                     $id_image = $image_for_prestashop->getIdImageByName(trim($tmp_photo[$i]));
                     if (!empty($id_image)) {
                         array_push($id_images, $id_image);
                     } else {
                         $id_image = $image_for_prestashop->insertImageInPrestashop($id_product, trim($url_photo), trim($tmp_photo[$i]));
                         array_push($id_images, $id_image);
                         array_push($id_new_images, $id_image . ";" . trim($tmp_photo[$i]) . '.jpg');
                     }
                 }
             }
             $id_product_attributes = $product->addProductAttribute($price, 0, 0, 0, $quantity, "", $reference, $id_supplier, 0, 1);
             $combinations = new CombinationCore((int) $id_product_attributes);
             $combinations->setAttributes($id_attributes_for_combinations);
             $combinations->setImages($id_images);
         } else {
             $id_images = array();
             $tmp_photo = explode(".jpg,", $image);
             for ($i = 0; $i < sizeof($tmp_photo); $i++) {
                 if (!empty($tmp_photo[$i])) {
                     $image_for_prestashop = new PrestashopImageHandler();
                     $id_image = $image_for_prestashop->getIdImageByName(trim($tmp_photo[$i]));
                     if (empty($id_image)) {
                         $id_image = $image_for_prestashop->insertImageInPrestashop($id_product, trim($url_photo), trim($tmp_photo[$i]));
                         array_push($id_images, $id_image);
                         array_push($id_new_images, $id_image . ";" . trim($tmp_photo[$i]) . ".jpg");
                     }
                 }
             }
             if (!empty($id_images)) {
                 $array_product_attribute_combinations_get = $product->getCombinationImages($language);
                 for ($i = 0; $i < sizeof($id_images); $i++) {
                     $id_della_immagine = $id_images[$i];
                     foreach ($array_product_attribute_combinations_get as $single_array_of_attribute_product) {
                         foreach ($single_array_of_attribute_product as $array_di_combinazioni) {
                             $id_arr_comb = $array_di_combinazioni['id_product_attribute'];
                             $combinazioni_attributi = new CombinationCore((int) $id_arr_comb);
                             $attribute_combinations = $combinazioni_attributi->getAttributesName($language);
                             $attr_comb1 = $attribute_combinations[0];
                             $attr_comb2 = $attribute_combinations[1];
                             $id_attr_comb1 = $attr_comb1['id_attribute'];
                             $id_attr_comb2 = $attr_comb2['id_attribute'];
                             $id_attributi_input = $id_attributes_for_combinations;
                             $id_color_or_size = $id_attributi_input[0];
                             $id_size_or_color = $id_attributi_input[1];
                             if ($id_color_or_size == $id_attr_comb1 || $id_color_or_size == $id_attr_comb2) {
                                 if ($id_size_or_color == $id_attr_comb1 || $id_size_or_color == $id_attr_comb2) {
                                     $new_image = array();
                                     $new_array_image = $combinazioni_attributi->getWsImages();
                                     for ($j = 0; $j < sizeof($new_array_image); $j++) {
                                         $tmp_image_ = $new_array_image[$j];
                                         array_push($new_image, (int) $tmp_image_['id']);
                                     }
                                     array_push($new_image, $id_della_immagine);
                                     $fff = $product->updateProductAttribute($id_arr_comb, 0, $price, 0, 0, 0, $new_image, $reference, $id_supplier, 0, 1, null, null, 1, '0000-00-00');
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     return $id_new_images;
 }
Example #5
0
    public function postProcess($token = NULL)
    {
        global $currentIndex;
        /* Add a new product */
        if (Tools::isSubmit('submitAddproduct') or Tools::isSubmit('submitAddproductAndStay')) {
            if ($this->tabAccess['add'] === '1') {
                $this->submitAddproduct($token);
            } elseif (Tools::getValue('id_product') and $this->tabAccess['edit'] === '1') {
                $this->submitAddproduct($token);
            } else {
                $this->_errors[] = Tools::displayError('You do not have permission to add anything here.');
            }
        }
        /* Delete a product in the download folder */
        if (Tools::getValue('deleteVirtualProduct')) {
            if ($this->tabAccess['delete'] === '1') {
                $this->deleteVirtualProduct();
            } else {
                $this->_errors[] = Tools::displayError('You do not have permission to delete anything here.');
            }
        } elseif (Tools::isSubmit('submitAttachments')) {
            if ($this->tabAccess['edit'] === '1') {
                if ($id = intval(Tools::getValue($this->identifier))) {
                    if (Attachment::attachToProduct($id, $_POST['attachments'])) {
                        Tools::redirectAdmin($currentIndex . '&id_product=' . $id . '&conf=4&add' . $this->table . '&tabs=6&token=' . ($token ? $token : $this->token));
                    }
                }
            }
        } elseif (isset($_GET['duplicate' . $this->table])) {
            if ($this->tabAccess['add'] === '1') {
                if (Validate::isLoadedObject($product = new Product(intval(Tools::getValue('id_product'))))) {
                    $id_product_old = $product->id;
                    unset($product->id);
                    unset($product->id_product);
                    $product->indexed = 0;
                    if ($product->add() and Category::duplicateProductCategories($id_product_old, $product->id) and ($combinationImages = Product::duplicateAttributes($id_product_old, $product->id)) !== false and Product::duplicateAccessories($id_product_old, $product->id) and Product::duplicateFeatures($id_product_old, $product->id) and Product::duplicateQuantityDiscount($id_product_old, $product->id) and Pack::duplicate($id_product_old, $product->id) and Product::duplicateCustomizationFields($id_product_old, $product->id)) {
                        if (!Tools::getValue('noimage') and !Image::duplicateProductImages($id_product_old, $product->id, $combinationImages)) {
                            $this->_errors[] = Tools::displayError('an error occurred while copying images');
                        } else {
                            Hook::addProduct($product);
                            Search::indexation(false);
                            Tools::redirectAdmin($currentIndex . '&id_category=' . intval(Tools::getValue('id_category')) . '&conf=19&token=' . ($token ? $token : $this->token));
                        }
                    } else {
                        $this->_errors[] = Tools::displayError('an error occurred while creating object');
                    }
                }
            } else {
                $this->_errors[] = Tools::displayError('You do not have permission to add anything here.');
            }
        } elseif ($id_image = intval(Tools::getValue('id_image')) and Validate::isUnsignedId($id_image) and Validate::isLoadedObject($image = new Image($id_image))) {
            if ($this->tabAccess['edit'] === '1') {
                /* Delete product image */
                if (isset($_GET['deleteImage'])) {
                    $image->delete();
                    deleteImage($image->id_product, $image->id);
                    if (!Image::getCover($image->id_product)) {
                        $first_img = Db::getInstance()->getRow('
						SELECT `id_image` FROM `' . _DB_PREFIX_ . 'image`
						WHERE `id_product` = ' . intval($image->id_product));
                        Db::getInstance()->Execute('
						UPDATE `' . _DB_PREFIX_ . 'image`
						SET `cover` = 1
						WHERE `id_image` = ' . intval($first_img['id_image']));
                    }
                    @unlink(dirname(__FILE__) . '/../../img/tmp/product_' . $image->id_product . '.jpg');
                    @unlink(dirname(__FILE__) . '/../../img/tmp/product_mini_' . $image->id_product . '.jpg');
                    Tools::redirectAdmin($currentIndex . '&id_product=' . $image->id_product . '&id_category=' . intval(Tools::getValue('id_category')) . '&add' . $this->table . '&tabs=1' . '&token=' . ($token ? $token : $this->token));
                } elseif (isset($_GET['editImage'])) {
                    if ($image->cover) {
                        $_POST['cover'] = 1;
                    }
                    $languages = Language::getLanguages();
                    foreach ($languages as $language) {
                        if (isset($image->legend[$language['id_lang']])) {
                            $_POST['legend_' . $language['id_lang']] = $image->legend[$language['id_lang']];
                        }
                    }
                    $_POST['id_image'] = $image->id;
                    $this->displayForm($token ? $token : $this->token);
                } elseif (isset($_GET['coverImage'])) {
                    Image::deleteCover($image->id_product);
                    $image->cover = 1;
                    if (!$image->update()) {
                        $this->_errors[] = Tools::displayError('Impossible to change the product cover');
                    } else {
                        $productId = intval(Tools::getValue('id_product'));
                        @unlink(dirname(__FILE__) . '/../../img/tmp/product_' . $productId . '.jpg');
                        @unlink(dirname(__FILE__) . '/../../img/tmp/product_mini_' . $productId . '.jpg');
                        Tools::redirectAdmin($currentIndex . '&id_product=' . $image->id_product . '&id_category=' . intval(Tools::getValue('id_category')) . '&addproduct&tabs=1' . '&token=' . ($token ? $token : $this->token));
                    }
                } elseif (isset($_GET['imgPosition']) and isset($_GET['imgDirection'])) {
                    $image->positionImage(intval(Tools::getValue('imgPosition')), intval(Tools::getValue('imgDirection')));
                    Tools::redirectAdmin($currentIndex . '&id_product=' . $image->id_product . '&id_category=' . intval(Tools::getValue('id_category')) . '&add' . $this->table . '&tabs=1&token=' . ($token ? $token : $this->token));
                }
            } else {
                $this->_errors[] = Tools::displayError('You do not have permission to edit anything here.');
            }
        } elseif (Tools::isSubmit('submitProductAttribute')) {
            if (Validate::isLoadedObject($product = new Product(intval(Tools::getValue('id_product'))))) {
                if (!isset($_POST['attribute_quantity']) or $_POST['attribute_quantity'] == NULL) {
                    $this->_errors[] = Tools::displayError('attribute quantity is required');
                }
                if (!isset($_POST['attribute_price']) or $_POST['attribute_price'] == NULL) {
                    $this->_errors[] = Tools::displayError('attribute price is required');
                }
                if (!isset($_POST['attribute_combinaison_list']) or !sizeof($_POST['attribute_combinaison_list'])) {
                    $this->_errors[] = Tools::displayError('you must add at least one attribute');
                }
                if (!sizeof($this->_errors)) {
                    if (!isset($_POST['attribute_wholesale_price'])) {
                        $_POST['attribute_wholesale_price'] = 0;
                    }
                    if (!isset($_POST['attribute_price_impact'])) {
                        $_POST['attribute_price_impact'] = 0;
                    }
                    if (!isset($_POST['attribute_weight_impact'])) {
                        $_POST['attribute_weight_impact'] = 0;
                    }
                    if (!isset($_POST['attribute_ecotax'])) {
                        $_POST['attribute_ecotax'] = 0;
                    }
                    if (Tools::getValue('attribute_default')) {
                        $product->deleteDefaultAttributes();
                    }
                    // Change existing one
                    if ($id_product_attribute = intval(Tools::getValue('id_product_attribute'))) {
                        if ($this->tabAccess['add'] === '1') {
                            if ($product->productAttributeExists($_POST['attribute_combinaison_list'], $id_product_attribute)) {
                                $this->_errors[] = Tools::displayError('This attribute already exists.');
                            } else {
                                $product->updateProductAttribute($id_product_attribute, Tools::getValue('attribute_wholesale_price'), Tools::getValue('attribute_price') * Tools::getValue('attribute_price_impact'), Tools::getValue('attribute_weight') * Tools::getValue('attribute_weight_impact'), Tools::getValue('attribute_ecotax'), Tools::getValue('attribute_quantity'), Tools::getValue('id_image_attr'), Tools::getValue('attribute_reference'), Tools::getValue('attribute_supplier_reference'), Tools::getValue('attribute_ean13'), Tools::getValue('attribute_default'), Tools::getValue('attribute_location'));
                            }
                        } else {
                            $this->_errors[] = Tools::displayError('You do not have permission to add anything here.');
                        }
                    } else {
                        if ($this->tabAccess['edit'] === '1') {
                            if ($product->productAttributeExists($_POST['attribute_combinaison_list'])) {
                                $this->_errors[] = Tools::displayError('This combination already exists.');
                            } else {
                                $id_product_attribute = $product->addProductAttribute(Tools::getValue('attribute_price') * Tools::getValue('attribute_price_impact'), Tools::getValue('attribute_weight') * Tools::getValue('attribute_weight_impact'), Tools::getValue('attribute_ecotax'), Tools::getValue('attribute_quantity'), Tools::getValue('id_image_attr'), Tools::getValue('attribute_reference'), Tools::getValue('attribute_supplier_reference'), Tools::getValue('attribute_ean13'), Tools::getValue('attribute_default'), Tools::getValue('attribute_location'));
                            }
                        } else {
                            $this->_errors[] = Tools::displayError('You do not have permission to') . '<hr>' . Tools::displayError('edit something here.');
                        }
                    }
                    if (!sizeof($this->_errors)) {
                        $product->addAttributeCombinaison($id_product_attribute, Tools::getValue('attribute_combinaison_list'));
                        $product->checkDefaultAttributes();
                    }
                    if (!sizeof($this->_errors)) {
                        Tools::redirectAdmin($currentIndex . '&id_product=' . $product->id . '&id_category=' . intval(Tools::getValue('id_category')) . '&add' . $this->table . '&tabs=2&token=' . ($token ? $token : $this->token));
                    }
                }
            }
        } elseif (isset($_GET['deleteProductAttribute'])) {
            if ($this->tabAccess['delete'] === '1') {
                if ($id_product = intval(Tools::getValue('id_product')) and Validate::isUnsignedId($id_product) and Validate::isLoadedObject($product = new Product($id_product))) {
                    $product->deleteAttributeCombinaison(intval(Tools::getValue('id_product_attribute')));
                    $product->checkDefaultAttributes();
                    Tools::redirectAdmin($currentIndex . '&add' . $this->table . '&id_category=' . intval(Tools::getValue('id_category')) . '&tabs=2&id_product=' . $product->id . '&token=' . ($token ? $token : $this->token));
                } else {
                    $this->_errors[] = Tools::displayError('impossible to delete attribute');
                }
            } else {
                $this->_errors[] = Tools::displayError('You do not have permission to delete here.');
            }
        } elseif (Tools::isSubmit('submitProductFeature')) {
            if ($this->tabAccess['edit'] === '1') {
                if (Validate::isLoadedObject($product = new Product(intval(Tools::getValue('id_product'))))) {
                    // delete all objects
                    $product->deleteFeatures();
                    // add new objects
                    $languages = Language::getLanguages();
                    foreach ($_POST as $key => $val) {
                        if (preg_match("/^feature_([0-9]+)_value/i", $key, $match)) {
                            if ($val) {
                                $product->addFeaturesToDB($match[1], $val);
                            } else {
                                if ($default_value = $this->checkFeatures($languages, $match[1])) {
                                    $id_value = $product->addFeaturesToDB($match[1], 0, 1, $language['id_lang']);
                                    foreach ($languages as $language) {
                                        if ($cust = Tools::getValue('custom_' . $match[1] . '_' . $language['id_lang'])) {
                                            $product->addFeaturesCustomToDB($id_value, $language['id_lang'], $cust);
                                        } else {
                                            $product->addFeaturesCustomToDB($id_value, $language['id_lang'], $default_value);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    if (!sizeof($this->_errors)) {
                        Tools::redirectAdmin($currentIndex . '&id_product=' . $product->id . '&id_category=' . intval(Tools::getValue('id_category')) . '&add' . $this->table . '&tabs=3&token=' . ($token ? $token : $this->token));
                    }
                } else {
                    $this->_errors[] = Tools::displayError('product must be created before adding features');
                }
            }
            $this->_errors[] = Tools::displayError('You do not have permission to edit anything here.');
        } elseif (Tools::isSubmit('submitQuantityDiscount')) {
            $_POST['tabs'] = 5;
            if ($this->tabAccess['add'] === '1') {
                if (Validate::isLoadedObject($product = new Product(intval(Tools::getValue('id_product'))))) {
                    if (!($id_discount_type = intval(Tools::getValue('id_discount_type')))) {
                        $this->_errors[] = Tools::displayError('discount type not selected');
                    } else {
                        if (!($quantity_discount = intval(Tools::getValue('quantity_discount')))) {
                            $this->_errors[] = Tools::displayError('quantity is required');
                        } else {
                            if (!($value_discount = floatval(Tools::getValue('value_discount')))) {
                                $this->_errors[] = Tools::displayError('value is required');
                            } else {
                                $qD = new QuantityDiscount();
                                $qD->id_product = $product->id;
                                $qD->id_discount_type = $id_discount_type;
                                $qD->quantity = $quantity_discount;
                                $qD->value = $value_discount;
                                if ($qD->add() and !sizeof($this->_errors) and $qD->validateFields()) {
                                    Tools::redirectAdmin($currentIndex . '&id_product=' . $product->id . '&id_category=' . intval(Tools::getValue('id_category')) . '&add' . $this->table . '&tabs=5&conf=3&token=' . ($token ? $token : $this->token));
                                }
                                $this->_errors[] = Tools::displayError('an error occurred while creating object');
                            }
                        }
                    }
                } else {
                    $this->_errors[] = Tools::displayError('product must be created before adding quantity discounts');
                }
            } else {
                $this->_errors[] = Tools::displayError('You do not have permission to add anything here.');
            }
        } elseif (isset($_GET['deleteQuantityDiscount'])) {
            if ($this->tabAccess['delete'] === '1') {
                if (Validate::isLoadedObject($product = new Product(intval(Tools::getValue('id_product'))))) {
                    if (Validate::isLoadedObject($qD = new QuantityDiscount(intval(Tools::getValue('id_quantity_discount'))))) {
                        $qD->delete();
                        if (!sizeof($this->_errors)) {
                            Tools::redirectAdmin($currentIndex . '&id_product=' . $product->id . '&id_category=' . intval(Tools::getValue('id_category')) . '&add' . $this->table . '&tabs=5&conf=1&token=' . ($token ? $token : $this->token));
                        }
                    } else {
                        $this->_errors[] = Tools::displayError('not a valid quantity discount');
                    }
                } else {
                    $this->_errors[] = Tools::displayError('product must be created before delete quantity discounts');
                }
                $qD = new QuantityDiscount();
            } else {
                $this->_errors[] = Tools::displayError('You do not have permission to delete here.');
            }
        } elseif (Tools::isSubmit('submitCustomizationConfiguration')) {
            if ($this->tabAccess['edit'] === '1') {
                if (Validate::isLoadedObject($product = new Product(intval(Tools::getValue('id_product'))))) {
                    if (!$product->createLabels(intval($_POST['uploadable_files']) - intval($product->uploadable_files), intval($_POST['text_fields']) - intval($product->text_fields))) {
                        $this->_errors[] = Tools::displayError('an error occured while creating customization fields');
                    }
                    if (!sizeof($this->_errors) and !$product->updateLabels()) {
                        $this->_errors[] = Tools::displayError('an error occured while updating customization');
                    }
                    $product->uploadable_files = intval($_POST['uploadable_files']);
                    $product->text_fields = intval($_POST['text_fields']);
                    $product->customizable = (intval($_POST['uploadable_files']) > 0 or intval($_POST['text_fields']) > 0) ? 1 : 0;
                    if (!sizeof($this->_errors) and !$product->update()) {
                        $this->_errors[] = Tools::displayError('an error occured while updating customization configuration');
                    }
                    if (!sizeof($this->_errors)) {
                        Tools::redirectAdmin($currentIndex . '&id_product=' . $product->id . '&id_category=' . intval(Tools::getValue('id_category')) . '&add' . $this->table . '&tabs=4&token=' . ($token ? $token : $this->token));
                    }
                } else {
                    $this->_errors[] = Tools::displayError('product must be created before adding customization possibilities');
                }
            } else {
                $this->_errors[] = Tools::displayError('You do not have permission to edit anything here.');
            }
        } elseif (Tools::isSubmit('submitProductCustomization')) {
            if ($this->tabAccess['edit'] === '1') {
                if (Validate::isLoadedObject($product = new Product(intval(Tools::getValue('id_product'))))) {
                    foreach ($_POST as $field => $value) {
                        if (strncmp($field, 'label_', 6) == 0 and !Validate::isLabel($value)) {
                            $this->_errors[] = Tools::displayError('label fields are invalid');
                        }
                    }
                    if (!sizeof($this->_errors) and !$product->updateLabels()) {
                        $this->_errors[] = Tools::displayError('an error occured while updating customization');
                    }
                    if (!sizeof($this->_errors)) {
                        Tools::redirectAdmin($currentIndex . '&id_product=' . $product->id . '&id_category=' . intval(Tools::getValue('id_category')) . '&add' . $this->table . '&tabs=4&token=' . ($token ? $token : $this->token));
                    }
                } else {
                    $this->_errors[] = Tools::displayError('product must be created before adding customization possibilities');
                }
            } else {
                $this->_errors[] = Tools::displayError('You do not have permission to edit anything here.');
            }
        } elseif (isset($_GET['delete' . $this->table])) {
            if ($this->tabAccess['delete'] === '1') {
                if (Validate::isLoadedObject($product = new Product(intval(Tools::getValue('id_product'))))) {
                    if (!$this->deleteImage($product->id)) {
                        $this->_errors[] = Tools::displayError('an error occurred during product image deletion');
                    }
                    if ($product->delete()) {
                        Tools::redirectAdmin($currentIndex . '&id_category=' . intval(Tools::getValue('id_category')) . '&conf=1&token=' . ($token ? $token : $this->token));
                    }
                    $this->_errors[] = Tools::displayError('an error occurred during product deletion');
                }
            } else {
                $this->_errors[] = Tools::displayError('You do not have permission to delete here.');
            }
        } else {
            parent::postProcess(true);
        }
    }