public function saveCategory() { $app = JFactory::getApplication(); $db = JFactory::getDBO(); $input = JRequest::get('post'); $category_data = $input['jform']; $languages = JeproshopLanguageModelLanguage::getLanguages(); $category_id = $app->input->get('category_id'); $context = JeproshopContext::getContext(); $view = $app->input->get('view'); $parent_id = (int) $category_data['parent_id']; $this->date_add = date('Y-m-d H:i:s'); $this->date_upd = date('Y-m-d H:i:s'); //if true, we are in a root category creation if (!$parent_id) { $this->is_root_category = $category_data['depth_level'] = 1; $parent_id = (int) JeproshopSettingModelSetting::getValue('root_category'); } if ($category_id) { if ($category_id != $parent_id) { if (!JeproshopCategoryModelCategory::checkBeforeMove($category_id, $parent_id)) { $context->controller->has_errors = true; JError::raiseError(500, JText::_('COM_JEPROSHOP_THE_CATEGORY_CANNOT_BE_MOVED_HERE_MESSAGE')); } } else { $context->controller->has_errors = true; JError::raiseError(500, JText::_('COM_JEPROSHOP_THE_CATEGORY_CANNOT_BE_A_PARENT_OF_ITSELF_MESSAGE')); } } if (!isset($view) || $view != 'category') { $app->input->set('category_id', null); $app->redirect('index.php?option=com_jeproshop&view=category'); return false; } if (!$context->controller->has_errors) { $published = (int) $category_data['published']; if (!isset($depth_level_)) { $depth_level = $this->calculateDepthLevel(); } $rootCategoryId = (int) JeproshopSettingModelSetting::getValue('root_category'); if ($this->is_root_category && $rootCategoryId) { $parent_id = $rootCategoryId; } $shop_list_ids = null; if (JeproshopShopModelShop::isTableAssociated('category')) { $shop_list_ids = JeproshopShopModelShop::getContextListShopIds(); if (count($this->shop_list_ids) > 0) { $shop_list_ids = $this->shop_list_ids; } } if (JeproshopShopModelShop::checkDefaultShopId('category')) { $this->default_shop_id = min($shop_list_ids); } $default_shop_id = JeproshopContext::getContext()->shop->shop_id; $position = 0; if (JeproshopShopModelShop::checkDefaultShopId('category')) { $default_shop_id = min($shop_list_ids); } $result = true; $query = "INSERT INTO " . $db->quoteName('#__jeproshop_category') . "(" . $db->quoteName('parent_id') . ", " . $db->quoteName('depth_level'); $query .= ", " . $db->quoteName('default_shop_id') . ", " . $db->quoteName('published') . ", " . $db->quoteName('date_add') . ", " . $db->quoteName('date_upd') . ", "; $query .= $db->quoteName('is_root_category') . ") VALUES (" . (int) $parent_id . ", " . (int) $depth_level . ", " . (int) $default_shop_id . ", " . (int) $published; $query .= ", " . $db->quote($this->date_add) . ", " . $db->quote($this->date_upd) . ", " . (int) $this->is_root_category . ")"; $db->setQuery($query); $result &= $db->query(); if ($result) { $this->category_id = $db->insertid(); foreach ($shop_list_ids as $shop_id) { $query = "INSERT INTO " . $db->quoteName('#__jeproshop_category_shop') . "(" . $db->quoteName('category_id') . ", " . $db->quoteName('shop_id'); $query .= ", " . $db->quoteName('position') . ") VALUES (" . (int) $this->category_id . ", " . (int) $shop_id . ", " . (int) $position . ")"; $db->setQuery($query); $result &= $db->query(); foreach ($languages as $language) { $query = "INSERT INTO " . $db->quoteName('#__jeproshop_category_lang') . "(" . $db->quoteName('category_id') . ", " . $db->quoteName('shop_id'); $query .= ", " . $db->quoteName('lang_id') . ", " . $db->quoteName('name') . ", " . $db->quoteName('description') . ", " . $db->quoteName('link_rewrite'); $query .= ", " . $db->quoteName('meta_title') . ", " . $db->quoteName('meta_keywords') . ", " . $db->quoteName('meta_description') . ") VALUES ("; $query .= (int) $this->category_id . ", " . (int) $shop_id . ", " . (int) $language->lang_id . ", " . $db->quote($category_data['name_' . (int) $language->lang_id]); $query .= ", " . $db->quote($category_data['description_' . $language->lang_id]) . ", " . $db->quote($category_data['link_rewrite_' . $language->lang_id]) . ", "; $query .= $db->quote($category_data['meta_title_' . $language->lang_id]) . ", " . $db->quote($category_data['meta_keywords_' . $language->lang_id]) . ", "; $query .= $db->quote($category_data['meta_description_' . $language->lang_id]) . ")"; $db->setQuery($query); $result &= $db->query(); } } } } if ($category_data['check_box_shop_associated_category']) { foreach ($category_data['check_box_shop_associated_category'] as $shop_id => $value) { $position = JeproshopCategoryModelCategory::getLastPosition((int) $this->parent_id, $shop_id); $this->addPosition($position, $shop_id); } } else { foreach (JeproshopShopModelShop::getShops() as $shop) { $position = JeproshopCategoryModelCategory::getLastPosition((int) $this->parent_id, $shop->shop_id); if (!$position) { $position = 1; } $this->addPosition($position, $shop->shop_id); } } if (!isset($this->doNotRegenerateNTree) || !$this->doNotRegenerateNTree) { JeproshopCategoryModelCategory::regenerateEntireNestedTree(); } $this->updateGroup($this->groupBox); //if we create a new root category you have to associate to a shop before to add sub categories in. So we redirect to AdminCategories listing if ($this->is_root_category) { $link = JRoute::_('index.php?option=com_jeproshop&view=category&category_id=' . (int) JeproshopCategoryModelCategory::getTopCategory()->category_id . JeproshopTools::getCategoryToken()); $message = ''; $app->redirect($link, $message); } }