public static function indexation($full = false, $product_id = false) { $db = JFactory::getDBO(); if ($product_id) { $full = false; } if ($full) { $db->execute('TRUNCATE ' . $db->quoteName('#__jeproshop_search_index')); $db->execute('TRUNCATE ' . $db->quoteName('#__jeproshop_search_word')); self::updateMultishopTable('Product', array('indexed' => 0)); } else { // Do it even if you already know the product id in order to be sure that it exists and it needs to be indexed $query = "SELECT product.product_id FROM " . $db->quoteName('#__jeproshop_product') . " AS product "; $query .= JeproshopShopModelShop::addSqlAssociation('product') . " WHERE product_shop.visibility IN (\"both\", \"search\") "; $query .= " AND product_shop." . $db->quoteName('published') . " = 1 AND "; $query .= $product_id ? "product.product_id = " . (int) $product_id : "product_shop.indexed = 0"; $db->setQuery($query); $products = $db->loadObjectList(); $ids = array(); if ($products) { foreach ($products as $product) { $ids[] = (int) $product->product_id; } } if (count($ids)) { $query = "DELETE FROM " . $db->quoteName('#__jeproshop_search_index') . " WHERE product_id IN (" . implode(',', $ids) . ")"; $db->setQuery($query); $db->query(); $data = " SET product." . $db->quoteName('indexed') . " = 0"; $where = ' WHERE product.product_id IN (' . implode(',', $ids) . ')'; JeproshopProductModelProduct::updateMultishopTable($data, $where, '', true); } } // Every fields are weighted according to the configuration in the backend $weight_array = array('product_name' => JeproshopSettingModelSetting::getValue('search_weight_product_name'), 'reference' => JeproshopSettingModelSetting::getValue('search_weight_reference'), 'product_attribute_reference' => JeproshopSettingModelSetting::getValue('search_weight_reference'), 'ean13' => JeproshopSettingModelSetting::getValue('search_weight_reference'), 'upc' => JeproshopSettingModelSetting::getValue('search_weight_reference'), 'short_description' => JeproshopSettingModelSetting::getValue('search_weight_short_description'), 'description' => JeproshopSettingModelSetting::getValue('search_weight_description'), 'category_name' => JeproshopSettingModelSetting::getValue('search_weight_category_name'), 'manufacture_name' => JeproshopSettingModelSetting::getValue('search_weight_manufacturer_name'), 'tag' => JeproshopSettingModelSetting::getValue('search_weight_tag'), 'attributes' => JeproshopSettingModelSetting::getValue('search_weight_attribute'), 'features' => JeproshopSettingModelSetting::getValue('search_weight_feature')); // Those are kind of global variables required to save the processed data in the database every X occurrences, in order to avoid overloading MySQL $count_words = 0; $query_array3 = array(); // Every indexed words are cached into a PHP array $query = "SELECT word_id, word, lang_id, shop_id FROM " . $db->quoteName('#__jeproshop_search_word'); $db->setQuery($query); $word_ids = $db->loadObjectList(); $word_ids_by_word = array(); while ($word_id = $db->nextRow($word_ids)) { if (!isset($word_ids_by_word[$word_id->shop_id][$word_id->lang_id])) { $word_ids_by_word[$word_id['id_shop']][$word_id['id_lang']] = array(); } $word_ids_by_word[$word_id->shop_id][$word_id->lang_id]['_' . $word_id->word] = (int) $word_id->word_id; } // Retrieve the number of languages $total_languages = count(JeproshopLanguageModelLanguage::getLanguages(false)); // Products are processed 50 by 50 in order to avoid overloading MySQL while (($products = JeproshopSearch::getProductsToIndex($total_languages, $product_id, 50, $weight_array)) && count($products) > 0) { $products_array = array(); // Now each non-indexed product is processed one by one, langage by langage foreach ($products as $product) { if ((int) $weight_array['tag']) { $product->tags = JeproshopSearch::getTags($db, (int) $product->product_id, (int) $product->lang_id); } if ((int) $weight_array['attributes']) { $product->attributes = JeproshopSearch::getAttributes($db, (int) $product->product_id, (int) $product->lang_id); } if ((int) $weight_array['features']) { $product->features = JeproshopSearch::getFeatures($db, (int) $product->product_id, (int) $product->lang_id); } // Data must be cleaned of html, bad characters, spaces and anything, then if the resulting words are long enough, they're added to the array $product_array = array(); foreach ($product as $key => $value) { if (strncmp($key, 'id_', 3) && isset($weight_array[$key])) { $words = explode(' ', JeproshopSearch::sanitize($value, (int) $product->lang_id, true, JeproshopContext::getContext()->language->iso_code)); foreach ($words as $word) { if (!empty($word)) { $word = substr($word, 0, PS_SEARCH_MAX_WORD_LENGTH); // Remove accents $word = JeproshopValidator::replaceAccentedChars($word); if (!isset($product_array[$word])) { $product_array[$word] = 0; } $product_array[$word] += $weight_array[$key]; } } } } // If we find words that need to be indexed, they're added to the word table in the database if (count($product_array)) { $query_array = $query_array2 = array(); foreach ($product_array as $word => $weight) { if ($weight && !isset($word_ids_by_word['_' . $word])) { $query_array[$word] = '(' . (int) $product->lang_id . ', ' . (int) $product->shop_id . ', ' . $db->quote($word) . ')'; $query_array2[] = $db->quote($word); $word_ids_by_word[$product->shop_id][$product->lang_id]['_' . $word] = 0; } } if ($query_array2) { $query = "SELECT DISTINCT word FROM " . $db->quoteName('#__jeproshop_search_word') . " WHERE word IN ("; $query .= implode(',', $query_array2) . ") AND lang_id = " . (int) $product->lang_id . " AND shop_id = " . (int) $product->shop_id; $db->setQuery($query); $existing_words = $db->loadObjectList(); foreach ($existing_words as $data) { unset($query_array[JeproshopValidator::replaceAccentedChars($data->word)]); } } if (count($query_array)) { // The words are inserted... $query = "INSERT IGNORE INTO " . $db->quoteName('#__jeproshop_search_word') . " (lang_id, shop_id, word) VALUES " . implode(',', $query_array); $db->setQuery($query); $db->query(); } if (count($query_array2)) { // ...then their IDs are retrieved and added to the cache $query = "SELECT search_word.word_id, search_word.word FROM " . $db->quoteName('#__jeproshop_search_word') . " AS search_word "; $query .= " WHERE search_word.word IN (" . implode(',', $query_array2) . ") AND search_word.lang_id = " . (int) $product->lang_id; $query .= " AND search_word.shop_id = " . (int) $product->shop_id . " LIMIT " . count($query_array2); $db->setQuery($query); $added_words = $db->loadObjectList(); // replace accents from the retrieved words so that words without accents or with differents accents can still be linked foreach ($added_words as $word_id) { $word_ids_by_word[$product->shop_id][$product->lang_id]['_' . JeproshopValidator::replaceAccentedChars($word_id->word)] = (int) $word_id->word_id; } } } foreach ($product_array as $word => $weight) { if (!$weight) { continue; } if (!isset($word_ids_by_word[$product->shop_id][$product->lang_id]['_' . $word])) { continue; } if (!$word_ids_by_word[$product->shop_id][$product->lang_id]['_' . $word]) { continue; } $query_array3[] = '(' . (int) $product->product_id . ',' . (int) $word_ids_by_word[$product->shop_id][$product->lang_id]['_' . $word] . ',' . (int) $weight . ')'; // Force save every 200 words in order to avoid overloading MySQL if (++$count_words % 200 == 0) { JeproshopSearch::saveIndex($query_array3); } } if (!in_array($product->product_id, $products_array)) { $products_array[] = (int) $product->product_id; } } JeproshopSearch::setProductsAsIndexed($products_array); // One last save is done at the end in order to save what's left JeproshopSearch::saveIndex($query_array3); } return true; }
public function updateProduct() { $db = JFactory::getDBO(); $app = JFactory::getApplication(); $languages = JeproshopLanguageModelLanguage::getLanguages(false); if (!isset($this->context)) { $this->context = JeproshopContext::getContext(); } $this->clearCache(); $this->date_upd = date('Y-m-d H:i:s'); if (JeproshopTools::isLoadedObject($this, 'product_id')) { $this->removeTaxFromEcotax(); $product_type_before = $this->getType(); $this->indexed = 0; $input = JRequest::get('post'); $product_data = isset($input['information']) ? $input['information'] : $input['jform']; $existingProduct = $this; if (JeproshopShopModelShop::isFeaturePublished() && JeproshopShopModelShop::getShopContext() != JeproshopShopModelShop::CONTEXT_SHOP) { $this->setFieldsToUpdate((array) $input['multishop_check']); } if ($this->context->shop->getShopContext() == JeproshopShopModelShop::CONTEXT_ALL && !$this->isAssociatedToShop()) { $isAssociatedToShop = false; $combinations = JeproshopProductModelProduct::getProductAttributesIds($this->product_id); if ($combinations) { foreach ($combinations as $combination_id) { $combination = new JeproshopCombinationModelCombination($combination_id); $default_combination = new JeproshopCombinationModelCombination($combination_id, null, $this->default_shop_id); $combination->product_id = (int) $default_combination->product_id; $combination->location = $db->quote($default_combination->location); $combination->ean13 = $db->quote($default_combination->ean13); $combination->upc = $db->quote($default_combination->upc); $combination->quantity = (int) $default_combination->quantity; $combination->reference = $db->quote($default_combination->reference); $combination->supplier_reference = $db->quote($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 = $default_combination->weight; $combination->unit_price_impact = (double) $default_combination->unit_price_impact; $combination->default_on = (int) $default_combination->default_on; $combination->available_date = $default_combination->available_date ? $db->quote($default_combination->available_date) : '0000-00-00'; $combination->save(); } } } else { $isAssociatedToShop = true; } $shop_list_ids = JeproshopShopModelShop::getCompleteListOfShopsId(); if (count($this->shop_list_id) > 0) { $shop_list_ids = $this->shop_list_id; } if (JeproshopShopModelShop::checkDefaultShopId('product') && !$this->default_shop_id) { $this->default_shop_id = min($shop_list_ids); } /*$manufacturer_id = $product_data['manufacturer_id']; $default_category_id = 1; $default_shop_id = JeproshopSettingModelSetting::getValue('default_shop'); */ //$tax_rules_group_id = $product_data['tax_rules_group_id']; $show_price = isset($input_data['show_price']) ? 1 : 0; $on_sale = isset($product_data['on_sale']) ? 1 : 0; $online_only = isset($product_data['online_only']) ? 1 : 0; $available_for_order = isset($product_data['available_for_order']) ? 1 : 0; $ean_13 = JeproshopTools::isEan13($product_data['ean13']) ? $product_data['ean13'] : ''; $upc = JeproshopTools::isUpc($product_data['upc']) ? $product_data['upc'] : ''; $reference = JeproshopTools::isReference($product_data['reference']) ? $product_data['reference'] : ''; //$product_type = $product_data['product_type']; $published = $product_data['published']; $redirect_type = $product_data['redirect_type']; /*$ecotax = $product_data['ecotax']; $unity = $product_data['unity']; $unit_price = (float)$product_data['unit_price']; $wholesale_price = (float)$product_data['wholesale_price']; */ $visibility = $product_data['visibility']; $condition = $product_data['condition']; $result = true; /** data base updating **/ $query = "UPDATE " . $db->quoteName('#__jeproshop_product') . " SET " . $db->quoteName('reference') . " = " . $db->quote($reference) . ", " . $db->quoteName('on_sale') . " = " . (int) $on_sale . ", "; $query .= $db->quoteName('online_only') . " = " . (int) $online_only . ", " . $db->quoteName('ean13') . " = " . $db->quote($ean_13) . ", " . $db->quoteName('upc') . " = " . $db->quote($upc) . ", "; $query .= $db->quoteName('published') . " = " . (int) $published . ", " . $db->quoteName('redirect_type') . "= " . $db->quote($redirect_type) . ", " . $db->quoteName('visibility') . " = " . $db->quote($visibility) . ", "; $query .= $db->quoteName('product_redirected_id') . " = " . $db->quote($product_data['product_redirected_id']) . ", " . $db->quoteName('available_for_order') . " = " . (int) $available_for_order . ", "; $query .= $db->quoteName('show_price') . " = " . (int) $show_price . ", " . $db->quoteName('condition') . " = " . $db->quote($condition) . ", " . $db->quoteName('date_upd') . " = " . $db->quote($this->date_upd); $query .= " WHERE " . $db->quoteName('product_id') . " = " . (int) $this->product_id; $db->setQuery($query); $result &= $db->query(); if ($result) { // Database insertion for multishop fields related to the object if (JeproshopShopModelShop::isTableAssociated('product')) { $default_category_id = 1; /* Shop fields */ foreach ($shop_list_ids as $shop_id) { $query = "UPDATE " . $db->quoteName('#__jeproshop_product_shop') . " SET " . $db->quoteName('default_category_id') . " = " . (int) $default_category_id; $query .= ", " . $db->quoteName('online_only') . " = " . (int) $online_only; $query .= ", " . $db->quoteName('redirect_type') . " = " . $db->quote($redirect_type) . ", " . $db->quoteName('product_redirected_id') . " = " . (int) $product_data['product_redirected_id']; $query .= ", " . $db->quoteName('available_for_order') . " = " . (int) $available_for_order . ", " . $db->quoteName('condition') . " = " . $db->quote($product_data['condition']); $query .= ", " . $db->quoteName('published') . " = " . (int) $product_data['published'] . ", " . $db->quoteName('show_price') . " = " . (int) $show_price; //$query .= ", " . $db->quoteName('additional_shipping_cost') . " = " . (float)$product_data['additional_shipping_cost']; $query .= ", " . $db->quoteName('visibility') . " = " . $db->quote($product_data['visibility']) . ", " . $db->quoteName('date_upd') . " = " . $db->quote($this->date_upd); $query .= " WHERE " . $db->quoteName('product_id') . " = " . $this->product_id . " AND " . $db->quoteName('shop_id') . " = " . $shop_id; $db->setQuery($query); $result &= $db->query(); if ($result) { /* Multilingual fields */ foreach ($languages as $language) { $query = "UPDATE " . $db->quoteName('#__jeproshop_product_lang') . " SET " . $db->quoteName('description') . " = "; $query .= $db->quote($product_data['description_' . $language->lang_id]) . ", " . $db->quoteName('short_description'); $query .= " = " . $db->quote($product_data['short_description_' . $language->lang_id]) . ", " . $db->quoteName('name'); $query .= " = " . $db->quote($product_data['name_' . $language->lang_id]) . " WHERE " . $db->quoteName('product_id') . " = "; $query .= (int) $this->product_id . " AND " . $db->quoteName('shop_id'); $query .= " = " . (int) $shop_id . " AND " . $db->quoteName('lang_id') . " = " . (int) $language->lang_id; $db->setQuery($query); $result &= $db->query(); } } } } } if ($result) { // If the product doesn't exist in the current shop but exists in another shop if (JeproshopShopModelShop::getShopContext() == JeproshopShopModelShop::CONTEXT_SHOP && $existingProduct->isAssociatedToShop($this->context->shop->shop_id)) { $outOfStock = JeproshopStockAvailableModelStockAvailable::outOfStock($existingProduct->product_id, $existingProduct->default_shop_id); $dependsOnStock = JeproshopStockAvailableModelStockAvailable::dependsOnStock($existingProduct->product_id, $existingProduct->default_shop_id); JeproshopStockAvailableModelStockAvailable::setProductOutOfStock((int) $this->product_id, $outOfStock, $this->context->shop->shop_id); JeproshopStockAvailableModelStockAvailable::setProductDependsOnStock((int) $this->product_id, $dependsOnStock, $this->context->shop->shop_id); } if (in_array($this->context->shop->getShopContext(), array(JeproshopShopModelShop::CONTEXT_SHOP, JeproshopShopModelShop::CONTEXT_ALL))) { $this->addCarriers(); $this->updateAccessories(); $this->processSuppliers(); $this->processFeatures(); $this->processProductAttribute(); $this->processProductAttribute(); $this->processPriceAddition(); $this->processSpecificPricePriorities(); $this->processCustomizationConfiguration(); $this->processAttachments(); $this->updatePackItems(); // Disallow advanced stock management if the product become a pack if ($product_type_before == JeproshopProductModelProduct::SIMPLE_PRODUCT && $this->getType() == JeproshopProductModelProduct::PACKAGE_PRODUCT) { JeproshopStockAvailableModelStockAvailable::setProductDependsOnStock($this->product_id, false); } $this->updateDownloadProduct(1); $this->updateTags(JeproshopLanguageModelLanguage::getLanguages(false)); if ($this->isProductFieldUpdated('category_box') && !$this->updateCategories($product_data['category_box'])) { JError::raiseError(500, JText::_('COM_JEPROSHOP_AN_ERROR_OCCURRED_WHILE_LINKING_THE_PRODUCT_TO_CATEGORIES_MESSAGE')); $this->context->controller->has_errors = true; } //TODO correct category update and check php $_POST limit to handle the form } $this->processWarehouses(); $category_link = $app->input->get('category_id') ? '&category_id=' . $app->input->get('category_id') : ''; if (!$this->context->controller->has_errors) { if (in_array($this->visibility, array('both', 'search')) && JeproshopSettingModelSetting::getValue('search_indexation')) { JeproshopSearch::indexation(false, $this->product_id); } //save and preview $message = JText::_('COM_JEPROSHOP_UPDATE_SUCCESSFULLY_MESSAGE'); if ($app->input->get('task') == 'save_preview') { $link = $this->getPreviewUrl(); } else { if ($app->input->get('task') == 'edit') { $link = JRoute::_('index.php?option=com_jeproshop&view=product&task=edit&product_id=' . $this->product_id . $category_link . JeproshopTools::getProductToken()); } else { $link = JRoute::_('index.php?option=com_jeproshop&view=product&' . $category_link); $message = JText::_('COM_JEPROSHOP_UPDATE_SUCCESSFULLY_MESSAGE'); } } $app->redirect($link, $message); } else { $app->input->set('task', 'edit'); $app->redirect('index.php?option=com_jeproshop&view=product&task=edit&product_id=' . $this->product_id . $category_link . JeproshopTools::getProductToken()); } } else { if (!$isAssociatedToShop && $combinations) { foreach ($combinations as $combination_id) { $combination = new JeproshopCombinationModelCombination((int) $combination_id); $combination->delete(); } JError::raiseError(500, JText::_('COM_JEPROSHOP_AN_ERROR_OCCURRED_WHILE_UPDATING_A_PRODUCT_MESSAGE')); } } $this->setGroupReduction(); if ($this->getType() == JeproshopProductModelProduct::VIRTUAL_PRODUCT && $this->published && !JeproshopSettingModelSetting::getValue('virtual_product_feature_active')) { JeproshopSettingModelSetting::updateValue('virtual_product_feature_active', '1'); } return true; } }