Example #1
0
/**
* @desc Almacena los datos de una categoría
*/
function bxpress_save_category($edit = 0)
{
    global $xoopsConfig, $xoopsModuleConfig, $xoopsSecurity;
    $db = XoopsDatabaseFactory::getDatabaseConnection();
    $title = '';
    $friendname = '';
    $showdesc = 0;
    $status = 0;
    $id = 0;
    $desc = '';
    $order = '';
    $q = '';
    //Query string
    foreach ($_POST as $k => $v) {
        ${$k} = $v;
        if ($k == 'XOOPS_TOKEN_REQUEST' || $k == 'action') {
            continue;
        }
        $q = ($q == '' ? '' : '&') . $k . '=' . urlencode($v);
    }
    if (!$xoopsSecurity->check()) {
        RMUris::redirect_with_message(__('Session token expired!', 'bxpress'), 'categories.php', RMMSG_ERROR);
        die;
    }
    if ($title == '') {
        RMUris::redirect_with_message(__('Please provide a name for this category!', 'bxpress'), 'categories.php?' . $q, RMMSG_ERROR);
        die;
    }
    if ($edit) {
        if ($id <= 0) {
            RMUris::redirect_with_message(__('The specified category ID is not valid!', 'bxpress'), 'categories.php', RMMSG_WARN);
            die;
        }
        $catego = new bXCategory($id);
        if ($catego->isNew()) {
            RMUris::redirect_with_message(__('Specified category does not exists!', 'bxpress'), 'categories.php', RMMSG_ERROR);
            die;
        }
        // Comprobamos que no exista el nombre
        list($num) = $db->fetchRow($db->query("SELECT COUNT(*) FROM " . $db->prefix("mod_bxpress_categories") . " WHERE title='{$title}' AND id_cat<>'{$id}'"));
        if ($num > 0) {
            RMUris::redirect_with_message(__('Already exists a category with same name!', 'bxpress'), 'categories.php?' . $q, RMMSG_ERROR);
            die;
        }
    } else {
        $catego = new bXCategory();
    }
    // Asignamos valores
    $catego->setTitle($title);
    $friendname = $friendname != '' ? TextCleaner::getInstance()->sweetstring($friendname) : TextCleaner::getInstance()->sweetstring($title);
    // Comprobamos que el nombre no este asignada a otra categoría
    list($num) = $db->fetchRow($db->query("SELECT COUNT(*) FROM " . $db->prefix("mod_bxpress_categories") . " WHERE friendname='{$friendname}' AND id_cat<>'{$id}'"));
    if ($num > 0) {
        RMUris::redirect_with_message(__('Already exist a category with the same short name!', 'bxpress'), 'categories.php?op=edit&id=' . $id, RMMSG_WARN);
        die;
    }
    $catego->setDescription($desc);
    $catego->setFriendName($friendname);
    $catego->setGroups(!isset($groups) || is_array($groups) ? array(0) : $groups);
    $catego->setOrder($order <= 0 ? 0 : intval($order));
    $catego->setShowDesc($showdesc);
    $catego->setStatus($status);
    if ($catego->save()) {
        RMUris::redirect_with_message(__('Category saved succesfully!', 'bxpress'), 'categories.php', RMMSG_SUCCESS);
    } else {
        RMUris::redirect_with_message(__('Category could not be saved!', 'bxpress') . '<br />' . $catego->errors(), 'categories.php', RMMSG_ERROR);
    }
}