Ejemplo n.º 1
0
 /**
  * Add several tags in database and link it to a product
  *
  * @param integer $lang_id Language id
  * @param integer $product_id Product id to link tags with
  * @param string|array $tag_list List of tags, as array or as a string with comas
  * @param string $separator
  * @return bool Operation success
  */
 public static function addTags($lang_id, $product_id, $tag_list, $separator = ',')
 {
     $db = JFactory::getDBO();
     if (!JeproshopTools::isUnsignedInt($lang_id)) {
         return false;
     }
     if (!is_array($tag_list)) {
         $tag_list = array_filter(array_unique(array_map('trim', preg_split('#\\' . $separator . '#', $tag_list, null, PREG_SPLIT_NO_EMPTY))));
     }
     $list = array();
     if (is_array($tag_list)) {
         foreach ($tag_list as $tag) {
             if (!JeproshopTools::isGenericName($tag)) {
                 return false;
             }
             $tag = trim(substr($tag, 0, 32));
             $tag_obj = new JeproshopTagModelTag(null, $tag, (int) $lang_id);
             /* Tag does not exist in database */
             if (!JeproshopTools::isLoadedObject($tag_obj, 'tag_id')) {
                 $tag_obj->name = $tag;
                 $tag_obj->lang_id = (int) $lang_id;
                 $tag_obj->add();
             }
             if (!in_array($tag_obj->tag_id, $list)) {
                 $list[] = $tag_obj->tag_id;
             }
         }
     }
     $data = '';
     $result = true;
     foreach ($list as $tag_id) {
         $query = "INSERT INTO " . $db->quoteName('#__jeproshop_product_tag') . " ( " . $db->quoteName('tag_id') . ", ";
         $query .= $db->quoteName('product_id') . ") VALUES (" . (int) $tag_id . ", " . (int) $product_id . ")";
         $db->setQuery($query);
         $result &= $db->query();
     }
     return $result;
 }
Ejemplo n.º 2
0
 public function update()
 {
     $app = JFactory::getApplication();
     $db = JFactory::getDBO();
     $data = JRequest::get('post');
     $input_data = $data['jform'];
     if ($this->context == null) {
         $this->context = JeproshopContext::getContext();
     }
     $product = new JeproshopProductModelProduct((int) $app->input->get('product_id'));
     $existing_product = JeproshopTools::isLoadedObject($product, 'product_id');
     $this->checkProduct();
     if (!empty($this->context->controller->errors)) {
         $this->context->controller->layout = 'edit';
         return false;
     }
     $product_id = (int) $app->input->get('product_id');
     /* Update an existing product */
     if (isset($product_id) && !empty($product_id)) {
         if (JeproshopTools::isLoadedObject($product, 'product_id')) {
             $this->removeTaxFromEcotax();
             $product_type_before = $product->getType();
             //$this->copyFromPost($object, $this->table);
             $product->indexed = 0;
             if (JeproshopShopModelShop::isFeaturePublished() && JeproshopShopModelShop::getShopContext() != JeproshopShopModelShop::CONTEXT_SHOP) {
                 $product->setFieldsToUpdate((array) $app->input->get('multishop_check'));
             }
             // Duplicate combinations if not associated to shop
             if ($this->context->shop->getShopContext() == JeproshopShopModelShop::CONTEXT_SHOP && !$product->isAssociatedToShop()) {
                 $is_associated_to_shop = false;
                 $combinations = JeproshopProductModelProduct::getProductAttributesIds($product_id);
                 if ($combinations) {
                     foreach ($combinations as $combination_id) {
                         $combination = new JeproshopCombinationModelCombination((int) $combination_id->product_attribute_id);
                         $default_combination = new JeproshopCombinationModelCombination((int) $combination_id->product_attribute_id, null, (int) $this->product->default_shop_id);
                         //$def = ObjectModel::getDefinition($default_combination);
                         $combination->product_id = (int) $default_combination->product_id;
                         $location = JeproshopTools::isGenericName($default_combination->location) ? $default_combination->location : '';
                         $combination->location = $db->quote($location);
                         $ean13 = JeproshopTools::isEan13($default_combination->ean13) ? $default_combination->ean13 : '';
                         $combination->ean13 = $db->quote($ean13);
                         $combination->upc = JeproshopTools::isUpc($default_combination->upc) ? $default_combination->upc : '';
                         $combination->quantity = (int) $default_combination->quantity;
                         $combination->reference = $default_combination->reference;
                         $combination->supplier_reference = $default_combination->supplier_reference;
                         $combination->wholesale_price = (double) $default_combination->wholesale_price;
                         $combination->price = (double) $default_combination->price;
                         $combination->ecotax = (double) $default_combination->ecotax;
                         $combination->weight = (double) $default_combination->weight;
                         $combination->unit_price_impact = (double) $default_combination->unit_price_impact;
                         $combination->minimal_quantity = (int) $default_combination->default_on;
                         $combination->default_on = (int) $default_combination->default_on;
                         $combination->available_date = JeproshopTools::isDate($default_combination->available_date) ? $default_combination->available_date : null;
                         $combination->save();
                     }
                 }
             } else {
                 $is_associated_to_shop = true;
             }
             if ($product->updateProduct()) {
                 // If the product doesn't exist in the current shop but exists in another shop
                 if (JeproshopShopModelShop::getShopContext() == JeproshopShopModelShop::CONTEXT_SHOP && !$product->isAssociatedToShop($this->context->shop->shop_id)) {
                     $out_of_stock = JeproshopStockAvailableModelStockAvailable::outOfStock($existing_product->product_id, $existing_product->default_shop_id);
                     $depends_on_stock = JeproshopStockAvailableModelStockAvailable::dependsOnStock($existing_product->product_id, $existing_product->default_shop_id);
                     JeproshopStockAvailableModelStockAvailable::setProductOutOfStock((int) $product->product_id, $out_of_stock, $this->context->shop->shop_id);
                     JeproshopStockAvailableModelStockAvailable::setProductDependsOnStock((int) $product->product_id, $depends_on_stock, $this->context->shop->shop_id);
                 }
                 //PrestaShopLogger::addLog(sprintf($this->l('%s edition', 'AdminTab', false, false), $this->className), 1, null, $this->className, (int)$this->object->id, true, (int)$this->context->employee->id);
                 if (in_array($this->context->shop->getShopContext(), array(JeproshopShopModelShop::CONTEXT_SHOP, JeproshopShopModelShop::CONTEXT_ALL))) {
                     //if ($this->isTabSubmitted('Shipping'))
                     $product->addCarriers();
                     //if ($this->isTabSubmitted('Associations'))
                     $product->updateAccessories($product);
                     //if ($this->isTabSubmitted('Suppliers'))
                     $product->processSuppliers();
                     //if ($this->isTabSubmitted('Features'))
                     $product->processFeatures();
                     //if ($this->isTabSubmitted('Combinations'))
                     $product->processProductAttribute();
                     //if ($this->isTabSubmitted('Prices'))
                     //{
                     $product->processPriceAddition();
                     $product->processSpecificPricePriorities();
                     //}
                     //if ($this->isTabSubmitted('Customization'))
                     $product->processCustomizationConfiguration();
                     //if ($this->isTabSubmitted('Attachments'))
                     $product->processAttachments();
                     $product->updatePackItems();
                     // Disallow advanced stock management if the product become a pack
                     if ($product_type_before == JeproshopProductModelProduct::SIMPLE_PRODUCT && $product->getType() == JeproshopProductModelProduct::PACKAGE_PRODUCT) {
                         JeproshopStockAvailableModelAVailable::setProductDependsOnStock((int) $product->product_id, false);
                     }
                     $product->updateDownloadProduct(1);
                     $product->updateTags(JeproshopLanguageModelLanguage::getLanguages(false));
                     if ($product->isProductFieldUpdated('category_box') && !$product->updateCategories($input_data['category_box'])) {
                         $this->context->controller->has_errors = true;
                         JError::raiseError(500, JText::_('COM_JEPROSHOP_AN_ERROR_OCCURRED_WHILE_LINKING_THE_PRODUCT_TO_THE_CATEGORIES_MESSAGE'));
                     }
                 }
                 //if ($this->isTabSubmitted('Warehouses'))
                 $product->processWarehouses();
                 if (empty($this->context->controller->errors)) {
                     if (in_array($product->visibility, array('both', 'search')) && JeproshopSettingModelSetting::getValue('search_indexation')) {
                         JeproshopSearch::indexation(false, $product->product_id);
                     }
                     // Save and preview
                     if ($app->input->get('submitAddProductAndPreview')) {
                         $this->redirect_after = $this->getPreviewUrl($product);
                     } else {
                         // Save and stay on same form
                         //if ($this->context->controller->layout == 'edit'){
                         $message = JText::_('COM_JEPROSHOP_PRODUCT_SUCCESSFULLY_UPDATE_MESSAGE');
                         $category_id = $app->input->get('category_id');
                         $app->redirect('index.php?option=com_jeproshop&view=product&product_id=' . (int) $product->product_id . (isset($category_id) ? '&category_id=' . (int) $app->input->get('category_id') : '') . '&task=edit&' . JSession::getFormToken() . '=1', $message);
                         /*}else{
                         			// Default behavior (save and back)
                         			$category_id = $app->input->get('category_id');
                         			$app->redirect('index.php?option=com_jeproshop&view=product' . (isset($category_id) ? '&category_id=' . (int)$app->input->get('category_id') : '')); //.  self::$currentIndex.(Tools::getIsset('id_category') ? '&id_category='.(int)Tools::getValue('id_category') : '').'&conf=4&token='.$this->token;
                         		}*/
                     }
                 } else {
                     // if errors : stay on edit page
                     $this->context->controller->layout = 'edit';
                 }
             } else {
                 if (!$is_associated_to_shop && $combinations) {
                     foreach ($combinations as $combination_item) {
                         $combination = new JeproshopCombinationModelCombination((int) $combination_item->product_attribute_id);
                         $combination->delete();
                     }
                 }
                 $this->context->controller->has_errors = Tools::displayError('An error occurred while updating an object.') . ' <b>' . $this->table . '</b> (' . Db::getInstance()->getMsgError() . ')';
             }
         } else {
             $this->context->controller->has_errors = Tools::displayError('An error occurred while updating an object.') . ' <b>' . $this->table . '</b> (' . Tools::displayError('The object cannot be loaded. ') . ')';
         }
         $app->redirect('index.php?option=com_jeproshop&view=product&task=edit&product_id=' . $product_id);
     }
 }