Beispiel #1
0
/**
* @desc Almacena la informaciín perteneciente a la categoría en la base de datos
**/
function saveCategories($edit = 0)
{
    global $xoopsSecurity;
    foreach ($_POST as $k => $v) {
        ${$k} = $v;
    }
    if (!$xoopsSecurity->check(true, false, $edit ? 'XOOPS_TOKEN' : 'XT')) {
        redirectMsg('categories.php', __('Session token expired!', 'dtransport'), 1);
    }
    $db = XoopsDatabaseFactory::getDatabaseConnection();
    if ($edit) {
        //Verificamos si categoría es válida
        if ($id <= 0) {
            redirectMsg('categories.php', __('Specified category is not valid!', 'dtransport'), 1);
        }
        //Verificamos si la categoría existe
        $cat = new DTCategory($id);
        if ($cat->isNew()) {
            redirectMsg('categories.php', __('Specified category does not exists!', 'dtransport'), 1);
        }
        //Comprueba que el nombre de la categoría no exista
        $sql = "SELECT COUNT(*) FROM " . $db->prefix('dtrans_categos') . " WHERE name='{$name}' AND id_cat<>'" . $id . "' AND parent={$parent}";
        list($num) = $db->fetchRow($db->queryF($sql));
        if ($num > 0) {
            redirectMsg('categories.php?action=edit&id=' . $id, __('Already exists another category with same name!', 'dtransport'), 1);
        }
        //Verificamos si el nameId existe
        if ($nameid) {
            $sql = "SELECT COUNT(*) FROM " . $db->prefix('dtrans_categos') . " WHERE nameid='{$nameid}' AND id_cat<>'" . $id . "' AND parent={$parent}";
            list($num) = $db->fetchRow($db->queryF($sql));
            if ($num > 0) {
                redirectMsg('categories.php?action=edit&id=' . $id, __('Already exists another category with same short name!', 'dtransport'), 1);
                die;
            }
        }
    } else {
        //Comprueba que el nombre de la categoría no exista
        $sql = "SELECT COUNT(*) FROM " . $db->prefix('dtrans_categos') . " WHERE name='{$name}' AND parent={$parent}";
        list($num) = $db->fetchRow($db->queryF($sql));
        if ($num > 0) {
            redirectMsg('categories.php', __('Already exists another category with same name!', 'dtransport'), 1);
            die;
        }
        $cat = new DTCategory();
    }
    //Genera $nameid Nombre identificador
    $found = false;
    $i = 0;
    if ($name != $cat->name() || empty($nameid)) {
        do {
            $nameid = TextCleaner::getInstance()->sweetstring($name) . ($found ? $i : '');
            $sql = "SELECT COUNT(*) FROM " . $db->prefix('dtrans_categos') . " WHERE nameid = '{$nameid}' AND parent={$parent}";
            list($num) = $db->fetchRow($db->queryF($sql));
            if ($num > 0) {
                $found = true;
                $i++;
            } else {
                $found = false;
            }
        } while ($found == true);
    }
    $cat->setName($name);
    $cat->setDesc($desc);
    $cat->setParent($parent);
    $cat->setActive($active);
    $cat->setNameId($nameid);
    if (!$cat->save()) {
        redirectMsg('categories.php', __('Category could not be saved!', 'dtransport'), 1);
        die;
    } else {
        redirectMsg('categories.php', __('Category saved successfully!', 'dtransport'), 0);
        die;
    }
}