Esempio n. 1
0
 public function processProductAttribute()
 {
     $app = JFactory::getApplication();
     // Don't process if the combination fields have not been submitted
     if (!JeproshopCombinationModelCombination::isFeaturePublished() || !$app->input->get('attribute_combination_list')) {
         return;
     }
     $product = new JeproshopProductModelProduct((int) $app->input->get('product_id'));
     if (JeproshopTools::isLoadedObject($product, 'product_id')) {
         if ($this->isProductFieldUpdated('attribute_price') && (!Tools::getIsset('attribute_price') || Tools::getIsset('attribute_price') == null)) {
             $this->context->controller->has_errors = Tools::displayError('The price attribute is required.');
         }
         if (!Tools::getIsset('attribute_combination_list') || Tools::isEmpty(Tools::getValue('attribute_combination_list'))) {
             $this->context->controller->has_errors = Tools::displayError('You must add at least one attribute.');
         }
         $array_checks = array('reference' => 'isReference', 'supplier_reference' => 'isReference', 'location' => 'isReference', 'ean13' => 'isEan13', 'upc' => 'isUpc', 'wholesale_price' => 'isPrice', 'price' => 'isPrice', 'ecotax' => 'isPrice', 'quantity' => 'isInt', 'weight' => 'isUnsignedFloat', 'unit_price_impact' => 'isPrice', 'default_on' => 'isBool', 'minimal_quantity' => 'isUnsignedInt', 'available_date' => 'isDateFormat');
         foreach ($array_checks as $property => $check) {
             if (Tools::getValue('attribute_' . $property) !== false && !call_user_func(array('Validate', $check), Tools::getValue('attribute_' . $property))) {
                 $this->context->controller->has_errors = sprintf(Tools::displayError('Field %s is not valid'), $property);
             }
         }
         if (!count($this->context->controller->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 (($product_attribute_id = (int) Tools::getValue('id_product_attribute')) || ($product_attribute_id = $product->productAttributeExists(Tools::getValue('attribute_combination_list'), false, null, true, true))) {
                 if ($this->tabAccess['edit'] === '1') {
                     if ($this->isProductFieldUpdated('available_date_attribute') && (Tools::getValue('available_date_attribute') != '' && !Validate::isDateFormat(Tools::getValue('available_date_attribute')))) {
                         $this->context->controller->has_errors = Tools::displayError('Invalid date format.');
                     } else {
                         $product->updateAttribute((int) $product_attribute_id, $this->isProductFieldUpdated('attribute_wholesale_price') ? Tools::getValue('attribute_wholesale_price') : null, $this->isProductFieldUpdated('attribute_price_impact') ? Tools::getValue('attribute_price') * Tools::getValue('attribute_price_impact') : null, $this->isProductFieldUpdated('attribute_weight_impact') ? Tools::getValue('attribute_weight') * Tools::getValue('attribute_weight_impact') : null, $this->isProductFieldUpdated('attribute_unit_impact') ? Tools::getValue('attribute_unity') * Tools::getValue('attribute_unit_impact') : null, $this->isProductFieldUpdated('attribute_ecotax') ? Tools::getValue('attribute_ecotax') : null, Tools::getValue('id_image_attr'), Tools::getValue('attribute_reference'), Tools::getValue('attribute_ean13'), $this->isProductFieldUpdated('attribute_default') ? Tools::getValue('attribute_default') : null, Tools::getValue('attribute_location'), Tools::getValue('attribute_upc'), $this->isProductFieldUpdated('attribute_minimal_quantity') ? Tools::getValue('attribute_minimal_quantity') : null, $this->isProductFieldUpdated('available_date_attribute') ? Tools::getValue('available_date_attribute') : null, false);
                         StockAvailable::setProductDependsOnStock((int) $product->product_id, $product->depends_on_stock, null, (int) $product_attribute_id);
                         StockAvailable::setProductOutOfStock((int) $product->product_id, $product->out_of_stock, null, (int) $product_attribute_id);
                     }
                 } else {
                     $this->context->controller->has_errors = true;
                     Tools::displayError('You do not have permission to add this.');
                 }
             } else {
                 // Add new
                 if ($this->tabAccess['add'] === '1') {
                     if ($product->productAttributeExists(Tools::getValue('attribute_combination_list'))) {
                         $this->context->controller->has_errors = true;
                         Tools::displayError('This combination already exists.');
                     } else {
                         $product_attribute_id = $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'), 0, Tools::getValue('id_image_attr'), Tools::getValue('attribute_reference'), null, Tools::getValue('attribute_ean13'), Tools::getValue('attribute_default'), Tools::getValue('attribute_location'), Tools::getValue('attribute_upc'), Tools::getValue('attribute_minimal_quantity'), array(), Tools::getValue('available_date_attribute'));
                         JeproshopStockAvailableModelStockAvailable::setProductDependsOnStock((int) $product->product_id, $product->depends_on_stock, null, (int) $product_attribute_id);
                         JeproshopStockAvailableModelStockAvailable::setProductOutOfStock((int) $product->product_id, $product->out_of_stock, null, (int) $product_attribute_id);
                     }
                 } else {
                     $this->context->controller->has_errors = true;
                     Tools::displayError('You do not have permission to') . '<hr>' . Tools::displayError('edit here.');
                 }
             }
             if (!$this->context->controller->has_errors) {
                 $combination = new JeproshopCombinationModelCombination((int) $product_attribute_id);
                 $combination->setAttributes(Tools::getValue('attribute_combination_list'));
                 // images could be deleted before
                 $id_images = Tools::getValue('id_image_attr');
                 if (!empty($id_images)) {
                     $combination->setImages($id_images);
                 }
                 $product->checkDefaultAttributes();
                 if (Tools::getValue('attribute_default')) {
                     Product::updateDefaultAttribute((int) $product->id);
                     if (isset($id_product_attribute)) {
                         $product->cache_default_attribute = (int) $id_product_attribute;
                     }
                     if ($available_date = Tools::getValue('available_date_attribute')) {
                         $product->setAvailableDate($available_date);
                     }
                 }
             }
         }
     }
 }