Example #1
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), intval($_POST['schedules']) - intval($product->schedules))) {
                        $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->schedules = intval($_POST['schedules']);
                    $product->customizable = (intval($_POST['uploadable_files']) > 0 or intval($_POST['text_fields']) > 0 or intval($_POST['schedules']) > 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.');
            }
        } elseif (isset($_GET['schedule_editor_select'])) {
            global $cookie;
            $cookie->id_customization_field_schedule = $_GET['schedule_editor_select'];
        } elseif (Tools::isSubmit('submitScheduleEditorCancel')) {
            global $cookie;
            unset($cookie->id_customization_field_schedule);
        } elseif (Tools::isSubmit('submitScheduleEditorAddEntry')) {
            if ($this->tabAccess['edit'] === '1') {
                if (Validate::isLoadedObject($product = new Product(intval(Tools::getValue('id_product'))))) {
                    global $cookie;
                    $product->addScheduleEntry(array("id_customization_field" => $_POST["schedule_entry_editor_id_customization_field"], "id_customization_field_schedule" => $_POST["schedule_entry_editor_id_customization_field_schedule"], "start_time" => $_POST["schedule_entry_editor_start_time"], "end_time" => $_POST["schedule_entry_editor_end_time"], "venue" => $_POST["schedule_entry_editor_venue"], "seats" => $_POST["schedule_entry_editor_seats"], "teacher" => $_POST["schedule_entry_editor_teacher"]), array(array("id_lang" => $cookie->id_lang, "name" => $_POST["schedule_entry_editor_name"], "description" => $_POST["schedule_entry_editor_description"])));
                } 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('submitScheduleEditorDeleteEntry')) {
            if ($this->tabAccess['edit'] === '1') {
                if (Validate::isLoadedObject($product = new Product(intval(Tools::getValue('id_product'))))) {
                    $product->removeScheduleEntry($_POST['schedule_entry_editor_id_customization_field_schedule']);
                } 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.');
            }
        } else {
            parent::postProcess(true);
        }
    }