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) and Product::duplicateTags($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['edit'] === '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['add'] === '1') {
                            if ($product->productAttributeExists($_POST['attribute_combinaison_list'])) {
                                $this->_errors[] = Tools::displayError('This combination already exists.');
                            } else {
                                $id_product_attribute = $product->addCombinationEntity(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') . '<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);
        }
    }
Пример #2
0
 /**
  * Attach an existing attachment to the product
  *
  * @return void
  */
 public function processAttachments()
 {
     if ($id = (int) Tools::getValue($this->identifier)) {
         $attachments = trim(Tools::getValue('arrayAttachments'), ',');
         $attachments = explode(',', $attachments);
         if (!Attachment::attachToProduct($id, $attachments)) {
             $this->errors[] = Tools::displayError('An error occurred while saving product attachments.');
         }
     }
 }
 public function processAttachments()
 {
     if (${${"GLOBALS"}["zmwegqm"]} = (int) Tools::getValue($this->identifier)) {
         $rfjgiirc = "id";
         ${"GLOBALS"}["okwpdejx"] = "attachments";
         ${${"GLOBALS"}["okwpdejx"]} = trim(Tools::getValue("arrayAttachments"), ",");
         ${${"GLOBALS"}["xqxiqjoiyypr"]} = explode(",", ${${"GLOBALS"}["xqxiqjoiyypr"]});
         if (!Attachment::attachToProduct(${$rfjgiirc}, ${${"GLOBALS"}["xqxiqjoiyypr"]})) {
             $this->errors[] = Tools::displayError("There was an error while saving product attachments.");
         }
     }
 }
Пример #4
0
    /**
     * postProcess handle every checks before saving products information
     *
     * @param mixed $token
     * @return void
     */
    public function postProcess($token = null)
    {
        global $cookie, $currentIndex;
        // Add a new product
        if (Tools::isSubmit('submitAddproduct') || Tools::isSubmit('submitAddproductAndStay') || Tools::isSubmit('submitAddProductAndPreview')) {
            if (Tools::getValue('id_product') && $this->tabAccess['edit'] === '1' || $this->tabAccess['add'] === '1' && !Tools::isSubmit('id_product')) {
                $this->submitAddproduct($token);
            } else {
                $this->_errors[] = Tools::displayError('You do not have permission to add 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 here.');
            }
        } elseif (Tools::isSubmit('submitAddAttachments')) {
            if ($this->tabAccess['add'] === '1') {
                $languages = Language::getLanguages(false);
                $is_attachment_name_valid = false;
                foreach ($languages as $language) {
                    $attachment_name_lang = Tools::getValue('attachment_name_' . (int) $language['id_lang']);
                    if (strlen($attachment_name_lang) > 0) {
                        $is_attachment_name_valid = true;
                    }
                    if (!Validate::isGenericName(Tools::getValue('attachment_name_' . (int) $language['id_lang']))) {
                        $this->_errors[] = Tools::displayError('Invalid Name');
                    } elseif (Tools::strlen(Tools::getValue('attachment_name_' . (int) $language['id_lang'])) > 32) {
                        $this->_errors[] = Tools::displayError('Name is too long');
                    }
                    if (!Validate::isCleanHtml(Tools::getValue('attachment_description_' . (int) $language['id_lang']))) {
                        $this->_errors[] = Tools::displayError('Invalid description');
                    }
                }
                if (!$is_attachment_name_valid) {
                    $this->_errors[] = Tools::displayError('Attachment Name Required');
                }
                if (empty($this->_errors)) {
                    if (isset($_FILES['attachment_file']) && is_uploaded_file($_FILES['attachment_file']['tmp_name'])) {
                        if ($_FILES['attachment_file']['size'] > Configuration::get('PS_ATTACHMENT_MAXIMUM_SIZE') * 1024 * 1024) {
                            $this->_errors[] = $this->l('File too large, maximum size allowed:') . ' ' . Configuration::get('PS_ATTACHMENT_MAXIMUM_SIZE') * 1024 . ' ' . $this->l('kb') . '. ' . $this->l('File size you\'re trying to upload is:') . number_format($_FILES['attachment_file']['size'] / 1024, 2, '.', '') . $this->l('kb');
                        } else {
                            do {
                                $uniqid = sha1(microtime());
                            } while (file_exists(_PS_DOWNLOAD_DIR_ . $uniqid));
                            if (!copy($_FILES['attachment_file']['tmp_name'], _PS_DOWNLOAD_DIR_ . $uniqid)) {
                                $this->_errors[] = $this->l('File copy failed');
                            }
                            @unlink($_FILES['attachment_file']['tmp_name']);
                        }
                    } elseif ((int) $_FILES['attachment_file']['error'] === 1) {
                        $max_upload = (int) ini_get('upload_max_filesize');
                        $max_post = (int) ini_get('post_max_size');
                        $upload_mb = min($max_upload, $max_post);
                        $this->_errors[] = $this->l('the File') . ' <b>' . $_FILES['attachment_file']['name'] . '</b> ' . $this->l('exceeds the size allowed by the server, this limit is set to') . ' <b>' . $upload_mb . $this->l('Mb') . '</b>';
                    }
                    if (empty($this->_errors) && isset($uniqid)) {
                        $attachment = new Attachment();
                        foreach ($languages as $language) {
                            if (isset($_POST['attachment_name_' . (int) $language['id_lang']])) {
                                $attachment->name[(int) $language['id_lang']] = pSQL($_POST['attachment_name_' . (int) $language['id_lang']]);
                            }
                            if (isset($_POST['attachment_description_' . (int) $language['id_lang']])) {
                                $attachment->description[(int) $language['id_lang']] = pSQL($_POST['attachment_description_' . (int) $language['id_lang']]);
                            }
                        }
                        $attachment->file = $uniqid;
                        $attachment->mime = $_FILES['attachment_file']['type'];
                        $attachment->file_name = pSQL($_FILES['attachment_file']['name']);
                        if (empty($attachment->mime) or Tools::strlen($attachment->mime) > 128) {
                            $this->_errors[] = Tools::displayError('Invalid file extension');
                        }
                        if (!Validate::isGenericName($attachment->file_name)) {
                            $this->_errors[] = Tools::displayError('Invalid file name');
                        }
                        if (Tools::strlen($attachment->file_name) > 128) {
                            $this->_errors[] = Tools::displayError('File name too long');
                        }
                        if (!sizeof($this->_errors)) {
                            $attachment->add();
                            Tools::redirectAdmin($currentIndex . '&id_product=' . (int) Tools::getValue($this->identifier) . '&id_category=' . (int) Tools::getValue('id_category') . '&addproduct&conf=4&tabs=6&token=' . ($token ? $token : $this->token));
                        } else {
                            $this->_errors[] = Tools::displayError('Invalid file');
                        }
                    }
                }
            } else {
                $this->_errors[] = Tools::displayError('You do not have permission to add here.');
            }
        } elseif (Tools::isSubmit('submitAttachments')) {
            if ($this->tabAccess['edit'] === '1') {
                if ($id = (int) Tools::getValue($this->identifier)) {
                    if (Attachment::attachToProduct($id, $_POST['attachments'])) {
                        Tools::redirectAdmin($currentIndex . '&id_product=' . (int) $id . (isset($_POST['id_category']) ? '&id_category=' . (int) $_POST['id_category'] : '') . '&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((int) Tools::getValue('id_product')))) {
                    $id_product_old = $product->id;
                    unset($product->id);
                    unset($product->id_product);
                    $product->indexed = 0;
                    $product->active = 0;
                    if ($product->add() and Category::duplicateProductCategories($id_product_old, $product->id) and ($combinationImages = Product::duplicateAttributes($id_product_old, $product->id)) !== false and GroupReduction::duplicateReduction($id_product_old, $product->id) and Product::duplicateAccessories($id_product_old, $product->id) and Product::duplicateFeatures($id_product_old, $product->id) and Product::duplicateSpecificPrices($id_product_old, $product->id) and Pack::duplicate($id_product_old, $product->id) and Product::duplicateCustomizationFields($id_product_old, $product->id) and Product::duplicateTags($id_product_old, $product->id) and Product::duplicateDownload($id_product_old, $product->id)) {
                        if ($product->hasAttributes()) {
                            Product::updateDefaultAttribute($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, $product->id);
                            Tools::redirectAdmin($currentIndex . '&id_category=' . (!empty($_REQUEST['id_category']) ? $_REQUEST['id_category'] : '1') . '&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 here.');
            }
        } elseif (isset($_GET['status']) and Tools::getValue($this->identifier)) {
            if ($this->tabAccess['edit'] === '1') {
                if (Validate::isLoadedObject($object = $this->loadObject())) {
                    if ($object->toggleStatus()) {
                        Tools::redirectAdmin($currentIndex . '&conf=5' . (($id_category = !empty($_REQUEST['id_category']) ? $_REQUEST['id_category'] : '1' and Tools::getValue('id_product')) ? '&id_category=' . $id_category : '') . '&token=' . $token);
                    } else {
                        $this->_errors[] = Tools::displayError('An error occurred while updating status.');
                    }
                } else {
                    $this->_errors[] = Tools::displayError('An error occurred while updating status for object.') . ' <b>' . $this->table . '</b> ' . Tools::displayError('(cannot load object)');
                }
            } else {
                $this->_errors[] = Tools::displayError('You do not have permission to edit here.');
            }
        } elseif (isset($_GET['delete' . $this->table])) {
            if ($this->tabAccess['delete'] === '1') {
                if (Validate::isLoadedObject($object = $this->loadObject()) and isset($this->fieldImageSettings)) {
                    // check if request at least one object with noZeroObject
                    if (isset($object->noZeroObject) and sizeof($taxes = call_user_func(array($this->className, $object->noZeroObject))) <= 1) {
                        $this->_errors[] = Tools::displayError('You need at least one object.') . ' <b>' . $this->table . '</b><br />' . Tools::displayError('You cannot delete all of the items.');
                    } else {
                        $id_category = Tools::getValue('id_category');
                        $category_url = empty($id_category) ? '' : '&id_category=' . $id_category;
                        if ($this->deleted) {
                            $object->deleteImages();
                            $object->deleted = 1;
                            if ($object->update()) {
                                Tools::redirectAdmin($currentIndex . '&conf=1&token=' . ($token ? $token : $this->token) . $category_url);
                            }
                        } elseif ($object->delete()) {
                            Tools::redirectAdmin($currentIndex . '&conf=1&token=' . ($token ? $token : $this->token) . $category_url);
                        }
                        $this->_errors[] = Tools::displayError('An error occurred during deletion.');
                    }
                } else {
                    $this->_errors[] = Tools::displayError('An error occurred while deleting object.') . ' <b>' . $this->table . '</b> ' . Tools::displayError('(cannot load object)');
                }
            } else {
                $this->_errors[] = Tools::displayError('You do not have permission to delete here.');
            }
        } elseif (Tools::getValue('submitDel' . $this->table)) {
            if ($this->tabAccess['delete'] === '1') {
                if (isset($_POST[$this->table . 'Box'])) {
                    $object = new $this->className();
                    if (isset($object->noZeroObject) and (sizeof(call_user_func(array($this->className, $object->noZeroObject))) <= 1 or sizeof($_POST[$this->table . 'Box']) == sizeof(call_user_func(array($this->className, $object->noZeroObject))))) {
                        $this->_errors[] = Tools::displayError('You need at least one object.') . ' <b>' . $this->table . '</b><br />' . Tools::displayError('You cannot delete all of the items.');
                    } else {
                        $result = true;
                        if ($this->deleted) {
                            foreach (Tools::getValue($this->table . 'Box') as $id) {
                                $toDelete = new $this->className($id);
                                $toDelete->deleted = 1;
                                $result = $result and $toDelete->update();
                            }
                        } else {
                            $result = $object->deleteSelection(Tools::getValue($this->table . 'Box'));
                        }
                        if ($result) {
                            $id_category = Tools::getValue('id_category');
                            $category_url = empty($id_category) ? '' : '&id_category=' . $id_category;
                            Tools::redirectAdmin($currentIndex . '&conf=2&token=' . $token . $category_url);
                        }
                        $this->_errors[] = Tools::displayError('An error occurred while deleting selection.');
                    }
                } else {
                    $this->_errors[] = Tools::displayError('You must select at least one element to delete.');
                }
            } else {
                $this->_errors[] = Tools::displayError('You do not have permission to delete here.');
            }
        } elseif ($id_image = (int) Tools::getValue('id_image') and Validate::isUnsignedId($id_image) and Validate::isLoadedObject($image = new Image($id_image))) {
            /* PrestaShop demo mode */
            if (_PS_MODE_DEMO_) {
                $this->_errors[] = Tools::displayError('This functionnality has been disabled.');
                return;
            }
            /* PrestaShop demo mode*/
            if ($this->tabAccess['edit'] === '1') {
                /* Delete product image */
                if (isset($_GET['deleteImage'])) {
                    $image->delete();
                    if (!Image::getCover($image->id_product)) {
                        $first_img = Db::getInstance()->getRow('
						SELECT `id_image` FROM `' . _DB_PREFIX_ . 'image`
						WHERE `id_product` = ' . (int) $image->id_product);
                        Db::getInstance()->Execute('
						UPDATE `' . _DB_PREFIX_ . 'image`
						SET `cover` = 1
						WHERE `id_image` = ' . (int) $first_img['id_image']);
                    }
                    @unlink(_PS_TMP_IMG_DIR_ . '/product_' . $image->id_product . '.jpg');
                    @unlink(_PS_TMP_IMG_DIR_ . '/product_mini_' . $image->id_product . '.jpg');
                    Tools::redirectAdmin($currentIndex . '&id_product=' . $image->id_product . '&id_category=' . (!empty($_REQUEST['id_category']) ? $_REQUEST['id_category'] : '1') . '&add' . $this->table . '&tabs=1' . '&token=' . ($token ? $token : $this->token));
                } elseif (isset($_GET['editImage'])) {
                    if ($image->cover) {
                        $_POST['cover'] = 1;
                    }
                    $languages = Language::getLanguages(false);
                    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();
                } elseif (isset($_GET['coverImage'])) {
                    Image::deleteCover($image->id_product);
                    $image->cover = 1;
                    if (!$image->update()) {
                        $this->_errors[] = Tools::displayError('Cannot change the product cover');
                    } else {
                        $productId = (int) Tools::getValue('id_product');
                        @unlink(_PS_TMP_IMG_DIR_ . '/product_' . $productId . '.jpg');
                        @unlink(_PS_TMP_IMG_DIR_ . '/product_mini_' . $productId . '.jpg');
                        Tools::redirectAdmin($currentIndex . '&id_product=' . $image->id_product . '&id_category=' . (!empty($_REQUEST['id_category']) ? $_REQUEST['id_category'] : '1') . '&addproduct&tabs=1' . '&token=' . ($token ? $token : $this->token));
                    }
                } elseif (isset($_GET['imgPosition']) and isset($_GET['imgDirection'])) {
                    $image->positionImage((int) Tools::getValue('imgPosition'), (int) Tools::getValue('imgDirection'));
                    Tools::redirectAdmin($currentIndex . '&id_product=' . $image->id_product . '&id_category=' . (!empty($_REQUEST['id_category']) ? $_REQUEST['id_category'] : '1') . '&add' . $this->table . '&tabs=1&token=' . ($token ? $token : $this->token));
                }
            } else {
                $this->_errors[] = Tools::displayError('You do not have permission to edit here.');
            }
        } elseif (Tools::isSubmit('submitProductAttribute')) {
            if (Validate::isLoadedObject($product = new Product((int) Tools::getValue('id_product')))) {
                if (!isset($_POST['attribute_price']) or $_POST['attribute_price'] == NULL) {
                    $this->_errors[] = Tools::displayError('Attribute price 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 = (int) Tools::getValue('id_product_attribute')) {
                        if ($this->tabAccess['edit'] === '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_unity') * Tools::getValue('attribute_unit_impact'), Tools::getValue('attribute_ecotax'), false, 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'), Tools::getValue('attribute_upc'), Tools::getValue('attribute_minimal_quantity'));
                                if ($id_reason = (int) Tools::getValue('id_mvt_reason') and (int) Tools::getValue('attribute_mvt_quantity') > 0 and $id_reason > 0) {
                                    $reason = new StockMvtReason((int) $id_reason);
                                    $qty = Tools::getValue('attribute_mvt_quantity') * $reason->sign;
                                    if (!$product->addStockMvt($qty, $id_reason, (int) $id_product_attribute, NULL, $cookie->id_employee)) {
                                        $this->_errors[] = Tools::displayError('An error occurred while updating qty.');
                                    }
                                }
                                Hook::updateProductAttribute((int) $id_product_attribute);
                            }
                        } else {
                            $this->_errors[] = Tools::displayError('You do not have permission to add here.');
                        }
                    } else {
                        if ($this->tabAccess['add'] === '1') {
                            if ($product->productAttributeExists($_POST['attribute_combinaison_list'])) {
                                $this->_errors[] = Tools::displayError('This combination already exists.');
                            } else {
                                $id_product_attribute = $product->addCombinationEntity(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_unity') * Tools::getValue('attribute_unit_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'), Tools::getValue('attribute_upc'), Tools::getValue('attribute_minimal_quantity'));
                            }
                        } else {
                            $this->_errors[] = Tools::displayError('You do not have permission to') . '<hr>' . Tools::displayError('Edit here.');
                        }
                    }
                    if (!sizeof($this->_errors)) {
                        $product->addAttributeCombinaison($id_product_attribute, Tools::getValue('attribute_combinaison_list'));
                        $product->checkDefaultAttributes();
                    }
                    if (!sizeof($this->_errors)) {
                        if (!$product->cache_default_attribute) {
                            Product::updateDefaultAttribute($product->id);
                        }
                        Tools::redirectAdmin($currentIndex . '&id_product=' . $product->id . '&id_category=' . (!empty($_REQUEST['id_category']) ? $_REQUEST['id_category'] : '1') . '&add' . $this->table . '&tabs=3&token=' . ($token ? $token : $this->token));
                    }
                }
            }
        } elseif (Tools::isSubmit('deleteProductAttribute')) {
            if ($this->tabAccess['delete'] === '1') {
                if ($id_product = (int) Tools::getValue('id_product') and Validate::isUnsignedId($id_product) and Validate::isLoadedObject($product = new Product($id_product))) {
                    $product->deleteAttributeCombinaison((int) Tools::getValue('id_product_attribute'));
                    $product->checkDefaultAttributes();
                    $product->updateQuantityProductWithAttributeQuantity();
                    if (!$product->hasAttributes()) {
                        $product->cache_default_attribute = 0;
                        $product->update();
                    } else {
                        Product::updateDefaultAttribute($id_product);
                    }
                    Tools::redirectAdmin($currentIndex . '&add' . $this->table . '&id_category=' . (!empty($_REQUEST['id_category']) ? $_REQUEST['id_category'] : '1') . '&tabs=3&id_product=' . $product->id . '&token=' . ($token ? $token : $this->token));
                } else {
                    $this->_errors[] = Tools::displayError('Cannot delete attribute');
                }
            } else {
                $this->_errors[] = Tools::displayError('You do not have permission to delete here.');
            }
        } elseif (Tools::isSubmit('deleteAllProductAttributes')) {
            if ($this->tabAccess['delete'] === '1') {
                if ($id_product = (int) Tools::getValue('id_product') and Validate::isUnsignedId($id_product) and Validate::isLoadedObject($product = new Product($id_product))) {
                    $product->deleteProductAttributes();
                    $product->updateQuantityProductWithAttributeQuantity();
                    if ($product->cache_default_attribute) {
                        $product->cache_default_attribute = 0;
                        $product->update();
                    }
                    Tools::redirectAdmin($currentIndex . '&add' . $this->table . '&id_category=' . (!empty($_REQUEST['id_category']) ? $_REQUEST['id_category'] : '1') . '&tabs=3&id_product=' . $product->id . '&token=' . ($token ? $token : $this->token));
                } else {
                    $this->_errors[] = Tools::displayError('Cannot delete attributes');
                }
            } else {
                $this->_errors[] = Tools::displayError('You do not have permission to delete here.');
            }
        } elseif (Tools::isSubmit('defaultProductAttribute')) {
            if (Validate::isLoadedObject($product = new Product((int) Tools::getValue('id_product')))) {
                $product->deleteDefaultAttributes();
                $product->setDefaultAttribute((int) Tools::getValue('id_product_attribute'));
                Tools::redirectAdmin($currentIndex . '&add' . $this->table . '&id_category=' . (!empty($_REQUEST['id_category']) ? $_REQUEST['id_category'] : '1') . '&tabs=3&id_product=' . $product->id . '&token=' . ($token ? $token : $this->token));
            } else {
                $this->_errors[] = Tools::displayError('Cannot make default attribute');
            }
        } elseif (Tools::isSubmit('submitProductFeature')) {
            if ($this->tabAccess['edit'] === '1') {
                if (Validate::isLoadedObject($product = new Product((int) Tools::getValue('id_product')))) {
                    // delete all objects
                    $product->deleteFeatures();
                    // add new objects
                    $languages = Language::getLanguages(false);
                    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, (int) $language['id_lang']);
                                    foreach ($languages as $language) {
                                        if ($cust = Tools::getValue('custom_' . $match[1] . '_' . (int) $language['id_lang'])) {
                                            $product->addFeaturesCustomToDB($id_value, (int) $language['id_lang'], $cust);
                                        } else {
                                            $product->addFeaturesCustomToDB($id_value, (int) $language['id_lang'], $default_value);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    if (!sizeof($this->_errors)) {
                        Tools::redirectAdmin($currentIndex . '&id_product=' . (int) $product->id . '&id_category=' . (!empty($_REQUEST['id_category']) ? $_REQUEST['id_category'] : '1') . '&add' . $this->table . '&tabs=4&conf=4&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 here.');
        } elseif (Tools::isSubmit('submitPricesModification')) {
            $_POST['tabs'] = 5;
            if ($this->tabAccess['edit'] === '1') {
                $id_specific_prices = Tools::getValue('spm_id_specific_price');
                $id_shops = Tools::getValue('spm_id_shop');
                $id_currencies = Tools::getValue('spm_id_currency');
                $id_countries = Tools::getValue('spm_id_country');
                $id_groups = Tools::getValue('spm_id_group');
                $prices = Tools::getValue('spm_price');
                $from_quantities = Tools::getValue('spm_from_quantity');
                $reductions = Tools::getValue('spm_reduction');
                $reduction_types = Tools::getValue('spm_reduction_type');
                $froms = Tools::getValue('spm_from');
                $tos = Tools::getValue('spm_to');
                foreach ($id_specific_prices as $key => $id_specific_price) {
                    if ($this->_validateSpecificPrice($id_shops[$key], $id_currencies[$key], $id_countries[$key], $id_groups[$key], $prices[$key], $from_quantities[$key], $reductions[$key], $reduction_types[$key], $froms[$key], $tos[$key])) {
                        $specificPrice = new SpecificPrice((int) $id_specific_price);
                        $specificPrice->id_shop = (int) $id_shops[$key];
                        $specificPrice->id_currency = (int) $id_currencies[$key];
                        $specificPrice->id_country = (int) $id_countries[$key];
                        $specificPrice->id_group = (int) $id_groups[$key];
                        $specificPrice->price = (double) $prices[$key];
                        $specificPrice->from_quantity = (int) $from_quantities[$key];
                        $specificPrice->reduction = (double) ($reduction_types[$key] == 'percentage' ? $reductions[$key] / 100 : $reductions[$key]);
                        $specificPrice->reduction_type = !$reductions[$key] ? 'amount' : $reduction_types[$key];
                        $specificPrice->from = !$froms[$key] ? '0000-00-00 00:00:00' : $froms[$key];
                        $specificPrice->to = !$tos[$key] ? '0000-00-00 00:00:00' : $tos[$key];
                        if (!$specificPrice->update()) {
                            $this->_errors = Tools::displayError('An error occurred while updating the specific price.');
                        }
                    }
                }
                if (!sizeof($this->_errors)) {
                    Tools::redirectAdmin($currentIndex . '&id_product=' . (int) Tools::getValue('id_product') . '&id_category=' . (!empty($_REQUEST['id_category']) ? $_REQUEST['id_category'] : '1') . '&update' . $this->table . '&tabs=2&token=' . ($token ? $token : $this->token));
                }
            } else {
                $this->_errors[] = Tools::displayError('You do not have permission to add here.');
            }
        } elseif (Tools::isSubmit('submitPriceAddition')) {
            if ($this->tabAccess['add'] === '1') {
                $id_product = (int) Tools::getValue('id_product');
                $id_shop = Tools::getValue('sp_id_shop');
                $id_currency = Tools::getValue('sp_id_currency');
                $id_country = Tools::getValue('sp_id_country');
                $id_group = Tools::getValue('sp_id_group');
                $price = Tools::getValue('sp_price');
                $from_quantity = Tools::getValue('sp_from_quantity');
                $reduction = (double) Tools::getValue('sp_reduction');
                $reduction_type = !$reduction ? 'amount' : Tools::getValue('sp_reduction_type');
                $from = Tools::getValue('sp_from');
                $to = Tools::getValue('sp_to');
                if ($this->_validateSpecificPrice($id_shop, $id_currency, $id_country, $id_group, $price, $from_quantity, $reduction, $reduction_type, $from, $to)) {
                    $specificPrice = new SpecificPrice();
                    $specificPrice->id_product = $id_product;
                    $specificPrice->id_shop = (int) $id_shop;
                    $specificPrice->id_currency = (int) $id_currency;
                    $specificPrice->id_country = (int) $id_country;
                    $specificPrice->id_group = (int) $id_group;
                    $specificPrice->price = (double) $price;
                    $specificPrice->from_quantity = (int) $from_quantity;
                    $specificPrice->reduction = (double) ($reduction_type == 'percentage' ? $reduction / 100 : $reduction);
                    $specificPrice->reduction_type = $reduction_type;
                    $specificPrice->from = !$from ? '0000-00-00 00:00:00' : $from;
                    $specificPrice->to = !$to ? '0000-00-00 00:00:00' : $to;
                    if (!$specificPrice->add()) {
                        $this->_errors = Tools::displayError('An error occurred while updating the specific price.');
                    } else {
                        Tools::redirectAdmin($currentIndex . (Tools::getValue('id_category') ? '&id_category=' . Tools::getValue('id_category') : '') . '&id_product=' . $id_product . '&add' . $this->table . '&tabs=2&conf=3&token=' . ($token ? $token : $this->token));
                    }
                }
            } else {
                $this->_errors[] = Tools::displayError('You do not have permission to add here.');
            }
        } elseif (Tools::isSubmit('deleteSpecificPrice')) {
            if ($this->tabAccess['delete'] === '1') {
                if (!($obj = $this->loadObject())) {
                    return;
                }
                if (!($id_specific_price = Tools::getValue('id_specific_price')) or !Validate::isUnsignedId($id_specific_price)) {
                    $this->_errors[] = Tools::displayError('Invalid specific price ID');
                } else {
                    $specificPrice = new SpecificPrice((int) $id_specific_price);
                    if (!$specificPrice->delete()) {
                        $this->_errors[] = Tools::displayError('An error occurred while deleting the specific price');
                    } else {
                        Tools::redirectAdmin($currentIndex . (Tools::getValue('id_category') ? '&id_category=' . Tools::getValue('id_category') : '') . '&id_product=' . $obj->id . '&add' . $this->table . '&tabs=2&conf=1&token=' . ($token ? $token : $this->token));
                    }
                }
            } else {
                $this->_errors[] = Tools::displayError('You do not have permission to delete here.');
            }
        } elseif (Tools::isSubmit('submitSpecificPricePriorities')) {
            if (!($obj = $this->loadObject())) {
                return;
            }
            if (!($priorities = Tools::getValue('specificPricePriority'))) {
                $this->_errors[] = Tools::displayError('Please specify priorities');
            } elseif (Tools::isSubmit('specificPricePriorityToAll')) {
                if (!SpecificPrice::setPriorities($priorities)) {
                    $this->_errors[] = Tools::displayError('An error occurred while updating priorities.');
                } else {
                    Tools::redirectAdmin($currentIndex . '&id_product=' . $obj->id . '&add' . $this->table . '&tabs=2&conf=4&token=' . ($token ? $token : $this->token));
                }
            } elseif (!SpecificPrice::setSpecificPriority((int) $obj->id, $priorities)) {
                $this->_errors[] = Tools::displayError('An error occurred while setting priorities.');
            } else {
                Tools::redirectAdmin($currentIndex . (Tools::getValue('id_category') ? '&id_category=' . Tools::getValue('id_category') : '') . '&id_product=' . $obj->id . '&add' . $this->table . '&tabs=2&conf=4&token=' . ($token ? $token : $this->token));
            }
        } elseif (Tools::isSubmit('submitCustomizationConfiguration')) {
            if ($this->tabAccess['edit'] === '1') {
                if (Validate::isLoadedObject($product = new Product((int) Tools::getValue('id_product')))) {
                    if (!$product->createLabels((int) $_POST['uploadable_files'] - (int) $product->uploadable_files, (int) $_POST['text_fields'] - (int) $product->text_fields)) {
                        $this->_errors[] = Tools::displayError('An error occurred while creating customization fields.');
                    }
                    if (!sizeof($this->_errors) and !$product->updateLabels()) {
                        $this->_errors[] = Tools::displayError('An error occurred while updating customization.');
                    }
                    $product->uploadable_files = (int) $_POST['uploadable_files'];
                    $product->text_fields = (int) $_POST['text_fields'];
                    $product->customizable = ((int) $_POST['uploadable_files'] > 0 or (int) $_POST['text_fields'] > 0) ? 1 : 0;
                    if (!sizeof($this->_errors) and !$product->update()) {
                        $this->_errors[] = Tools::displayError('An error occurred while updating customization configuration.');
                    }
                    if (!sizeof($this->_errors)) {
                        Tools::redirectAdmin($currentIndex . '&id_product=' . $product->id . '&id_category=' . (!empty($_REQUEST['id_category']) ? $_REQUEST['id_category'] : '1') . '&add' . $this->table . '&tabs=5&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 here.');
            }
        } elseif (Tools::isSubmit('submitProductCustomization')) {
            if ($this->tabAccess['edit'] === '1') {
                if (Validate::isLoadedObject($product = new Product((int) 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 occurred while updating customization.');
                    }
                    if (!sizeof($this->_errors)) {
                        Tools::redirectAdmin($currentIndex . '&id_product=' . $product->id . '&id_category=' . (!empty($_REQUEST['id_category']) ? $_REQUEST['id_category'] : '1') . '&add' . $this->table . '&tabs=5&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 here.');
            }
        } elseif (isset($_GET['position'])) {
            if ($this->tabAccess['edit'] !== '1') {
                $this->_errors[] = Tools::displayError('You do not have permission to edit here.');
            } elseif (!Validate::isLoadedObject($object = $this->loadObject())) {
                $this->_errors[] = Tools::displayError('An error occurred while updating status for object.') . ' <b>' . $this->table . '</b> ' . Tools::displayError('(cannot load object)');
            }
            if (!$object->updatePosition((int) Tools::getValue('way'), (int) Tools::getValue('position'))) {
                $this->_errors[] = Tools::displayError('Failed to update the position.');
            } else {
                Tools::redirectAdmin($currentIndex . '&' . $this->table . 'Orderby=position&' . $this->table . 'Orderway=asc&conf=5' . (($id_category = !empty($_REQUEST['id_category']) ? $_REQUEST['id_category'] : '1') ? '&id_category=' . $id_category : '') . '&token=' . Tools::getAdminTokenLite('AdminCatalog'));
            }
        } else {
            parent::postProcess(true);
        }
    }
 protected function importProducts()
 {
     $this->truncateTables(array('product', 'product_lang', 'product_shop', 'product_sale', 'product_supplier', 'product_tag', 'feature_product', 'category_product', 'product_carrier', 'compare_product', 'product_attachment', 'product_country_tax', 'product_download', 'product_group_reduction_cache', 'scene_products', 'warehouse_product_location', 'customization', 'customization_field', 'customization_field_lang', 'supply_order_detail', 'attribute_impact', 'pack'));
     $handle = $this->openCsvFile('products.csv');
     $languages = Language::getLanguages(false);
     for ($current_line = 0; $line = fgetcsv($handle, MAX_LINE_SIZE, ';'); $current_line++) {
         $res = false;
         $product = new Product((int) $line[0]);
         $product->id = (int) $line[0];
         $product->active = $line[1];
         foreach ($languages as $lang) {
             $product->name[$lang['id_lang']] = $line['2'];
         }
         $categories = explode(',', $line[3]);
         $product->id_category_default = $categories[0] ? $categories[0] : Configuration::get('PS_HOME_CATEGORY');
         if (isset($categories) && $categories && count($categories)) {
             $product->addToCategories($categories);
         }
         if (isset($line[4])) {
             $product->price_tex = $line[4];
         }
         if (isset($line[5])) {
             $product->price_tin = $line[5];
         }
         $product->id_tax_rules_group = trim($line[6]) ? $line[6] : 0;
         $product->wholesale_price = trim($line[7]) ? $line[7] : 0;
         $product->on_sale = trim($line[8]) ? $line[8] : 0;
         if (trim($line[13])) {
             $product->reference = $line[13];
         }
         if (trim($line[14])) {
             $product->supplier_reference = trim($line[14]);
         }
         if (trim($line[15])) {
             $product->id_supplier = (int) $line[15];
         }
         if (isset($product->id) && $product->id && isset($product->id_supplier) && property_exists($product, 'supplier_reference')) {
             $id_product_supplier = (int) ProductSupplier::getIdByProductAndSupplier((int) $product->id, 0, (int) $product->id_supplier);
             if ($id_product_supplier) {
                 $product_supplier = new ProductSupplier($id_product_supplier);
             } else {
                 $product_supplier = new ProductSupplier();
             }
             $product_supplier->id_product = (int) $product->id;
             $product_supplier->id_product_attribute = 0;
             $product_supplier->id_supplier = (int) $product->id_supplier;
             $product_supplier->product_supplier_price_te = $product->wholesale_price;
             $product_supplier->product_supplier_reference = $product->supplier_reference;
             $product_supplier->save();
         }
         if (trim($line[16])) {
             $product->id_manufacturer = $line[16];
         }
         if (!Tools::isEmpty(trim($line[17]))) {
             $product->ean13 = $line[17];
         }
         if (trim($line[18])) {
             $product->upc = $line[18];
         }
         if (trim($line[19])) {
             $product->ecotax = $line[19];
         }
         $product->width = $line[20];
         $product->height = $line[21];
         $product->depth = $line[22];
         $product->weight = $line[23];
         if ($line[24]) {
             StockAvailable::setQuantity((int) $product->id, 0, (int) $line[24], (int) $this->context->shop->id);
         }
         $product->minimal_quantity = $line[25];
         $product->visibility = $line[26];
         $product->additional_shipping_cost = $line[27];
         if (trim($line[28])) {
             $product->unity = $line[28];
         }
         if (trim($line[29])) {
             $product->unit_price = $line[29];
         }
         foreach ($languages as $lang) {
             $product->description_short[$lang['id_lang']] = $line[30];
             $product->description[$lang['id_lang']] = $line[31];
         }
         if ($line[32]) {
             foreach ($languages as $lang) {
                 Tag::addTags($lang['id_lang'], $product->id, $line[32]);
             }
         }
         foreach ($languages as $lang) {
             $product->meta_title[$lang['id_lang']] = $line[33];
             $product->meta_keywords[$lang['id_lang']] = $line[34];
             $product->meta_description[$lang['id_lang']] = $line[35];
             $product->link_rewrite[$lang['id_lang']] = $line[36];
             $product->available_now[$lang['id_lang']] = $line[37];
             $product->available_later[$lang['id_lang']] = $line[38];
         }
         $product->available_for_order = $line[39];
         $product->available_date = $line[40];
         $product->date_add = $line[41];
         $product->show_price = $line[42];
         // Features import
         $features = explode(',', $line[45]);
         if ($features) {
             foreach ($features as $feature) {
                 $value = explode(':', $feature);
                 if ($value[0] && $value[1]) {
                     Product::addFeatureProductImport((int) $product->id, (int) $value[0], (int) $value[1]);
                     SpecificPriceRule::applyAllRules(array((int) $product->id));
                 }
             }
         }
         $product->online_only = trim($line[46]) ? $line[46] : 0;
         $product->condition = $line[47];
         $product->customizable = trim($line[48]) ? $line[48] : 0;
         $product->uploadable_files = trim($line[49]) ? $line[49] : 0;
         $product->text_fields = trim($line[50]) ? $line[50] : 0;
         if ($product->getType() == Product::PTYPE_VIRTUAL) {
             StockAvailable::setProductOutOfStock((int) $product->id, 1);
         } else {
             StockAvailable::setProductOutOfStock((int) $product->id, (int) $line[51]);
         }
         $product->id_shop_default = $line[52];
         // add product accessories
         if ($line[56]) {
             $accessories = explode(',', $line[56]);
             foreach ($accessories as $accessory) {
                 $a[]['id'] = $accessory;
             }
             $product->setWsAccessories($a);
         }
         // add product carriers
         if ($line[57]) {
             $carriers = explode(',', $line[57]);
             $product->setCarriers($carriers);
         }
         // add costomisation fields
         if (!Tools::isEmpty($line[58]) && class_exists('CustomizationField')) {
             $customisation_fields_ids = explode(',', $line[58]);
             foreach ($customisation_fields_ids as $customisation_field) {
                 $result = false;
                 $customisation_data = explode(':', $customisation_field);
                 $cf = new CustomizationField();
                 $cf->id_product = $product->id;
                 $cf->type = $customisation_data[1];
                 $cf->required = $customisation_data[2];
                 foreach ($languages as $lang) {
                     $cf->name[$lang['id_lang']] = $customisation_data[3] ? $customisation_data[3] : ' ';
                 }
                 $cf->force_id = 1;
                 if (!$result) {
                     $result = $cf->add();
                 }
             }
         }
         // add attachments
         if ($line[59]) {
             $attachments = explode(',', $line[59]);
             if (isset($attachments) && count($attachments)) {
                 Attachment::attachToProduct($product->id, $attachments);
             }
         }
         if ($line[60]) {
             $product->date_upd = $line[60];
         }
         $product->price = $line[61];
         $product->force_id = 1;
         if (!$res) {
             $res = $product->add();
         }
     }
     $this->closeCsvFile($handle);
     Search::indexation(true);
     return true;
 }