/** * Almacenamos la categoría en la base de datos */ function shop_save_category($edit = 0) { global $xoopsSecurity; if (!$xoopsSecurity->check()) { redirectMsg('categories.php', __('Sorry, session token expired!', 'shop'), 1); die; } $db = XoopsDatabaseFactory::getDatabaseConnection(); $query = ''; foreach ($_POST as $k => $v) { ${$k} = $v; if ($k == 'action' || $k == 'XOOPS_TOKEN_REQUEST') { continue; } $query .= $query == '' ? "{$k}=" . urlencode($v) : '&' . $k . '=' . urlencode($v); } $query = $edit ? '&action=edit' : ''; if ($edit) { if ($id <= 0) { redirectMsg('categories.php', __('You must specify a valid category', 'shop'), 1); die; } $catego = new ShopCategory($id); if ($catego->isNew()) { redirectMsg('categories.php', __('Specified category not exists!', 'shop'), 1); die; } } else { $catego = new ShopCategory(); } if ($name == '') { redirectMsg('categories.php?' . $query, __('Please specify a name for this category!', 'shop'), 1); die; } $shortname = $shortname == '' ? TextCleaner::sweetstring($name) : $shortname; # Verificamos que no exista la categoría $result = $db->query("SELECT COUNT(*) FROM " . $db->prefix("shop_categories") . " WHERE parent='{$parent}'" . ($edit ? " AND id_cat<>{$id}" : '') . " AND (name='{$name}' OR shortname='{$shortname}')"); list($num) = $db->fetchRow($result); if ($num > 0) { redirectMsg('categories.php?' . $query, __('There is already a category with the same name!', 'shop'), 1); die; } # Si todo esta bien guardamos la categoría $catego->setVar('name', $name); $catego->setVar('shortname', $shortname); $catego->setVar('description', $desc); $catego->setVar('parent', $parent); if (!$edit) { $catego->setVar('products', 0); } $result = $catego->save(); if ($result) { redirectMsg('categories.php', __('Category created succesfully!', 'shop'), 0); } else { redirectMsg('categories.php?' . $query, __('There was an error!', 'shop') . "<br />" . $catego->errors(), 1); } }
# ID de la categoria padre $path = explode("/", $category); foreach ($path as $k) { if ($k == '') { continue; } $sql = "SELECT id_cat FROM {$tbl1} WHERE shortname='{$k}' AND parent='{$idp}'"; $result = $db->query($sql); if ($db->getRowsNum($result) > 0) { list($idp) = $db->fetchRow($result); } } $category = $idp; } $catego = new ShopCategory($category); if ($catego->isNew()) { redirect_header(ShopFunctions::get_url(), 1, __('Specified category could not be found', 'shop')); die; } // Category data $xoopsTpl->assign('category', array('id' => $catego->id(), 'name' => $catego->getVar('name'))); $xoopsTpl->assign('lang_prodsincat', sprintf(__('Products in ‘%s’ Category', 'shop'), $catego->getVar('name'))); $limit = $xoopsModuleConfig['numxpage']; list($num) = $db->fetchRow($db->query("SELECT COUNT({$tbl2}.product) FROM {$tbl2}, {$tbl3} WHERE {$tbl2}.cat='{$category}' \n AND {$tbl3}.id_product={$tbl2}.product")); $page = isset($page) && $page > 0 ? $page : 1; $tpages = ceil($num / $limit); $page = $page > $tpages ? $tpages : $page; $p = $page > 0 ? $page - 1 : $page; $start = $num <= 0 ? 0 : $p * $limit; $xoopsTpl->assign('page', $page); $nav = new RMPageNav($num, $limit, $page, 5);