/**
 * Verschieben einer Kategorie in eine andere
 * 
 * @param $from_cat_id KategorieId der Kategorie, die verschoben werden soll (Quelle)
 * @param $to_cat_id   KategorieId der Kategorie, IN die verschoben werden soll (Ziel)
 */
function rex_moveCategory($from_cat, $to_cat)
{
    global $REX;
    $from_cat = (int) $from_cat;
    $to_cat = (int) $to_cat;
    if ($from_cat == $to_cat) {
        // kann nicht in gleiche kategroie kopiert werden
        return false;
    } else {
        // kategorien vorhanden ?
        // ist die zielkategorie im pfad der quellkategeorie ?
        $fcat = new sql();
        $fcat->setQuery("select * from " . $REX['TABLE_PREFIX'] . "article where startpage=1 and id={$from_cat} and clang=0");
        $tcat = new sql();
        $tcat->setQuery("select * from " . $REX['TABLE_PREFIX'] . "article where startpage=1 and id={$to_cat} and clang=0");
        if ($fcat->getRows() != 1 or $tcat->getRows() != 1 && $to_cat != 0) {
            // eine der kategorien existiert nicht
            return false;
        } else {
            if ($to_cat > 0) {
                $tcats = explode("|", $tcat->getValue("path"));
                if (in_array($from_cat, $tcats)) {
                    // zielkategorie ist in quellkategorie -> nicht verschiebbar
                    return false;
                }
            }
            // ----- folgende cats regenerate
            $RC = array();
            $RC[$fcat->getValue("re_id")] = 1;
            $RC[$from_cat] = 1;
            $RC[$to_cat] = 1;
            if ($to_cat > 0) {
                $to_path = $tcat->getValue("path") . $to_cat . "|";
                $to_re_id = $tcat->getValue("re_id");
            } else {
                $to_path = "|";
                $to_re_id = 0;
            }
            $from_path = $fcat->getValue("path") . $from_cat . "|";
            $gcats = new sql();
            // $gcats->debugsql = 1;
            $gcats->setQuery("select * from " . $REX['TABLE_PREFIX'] . "article where path like '" . $from_path . "%' and clang=0");
            for ($i = 0; $i < $gcats->getRows(); $i++) {
                // make update
                $new_path = $to_path . $from_cat . "|" . str_replace($from_path, "", $gcats->getValue("path"));
                $icid = $gcats->getValue("id");
                $irecid = $gcats->getValue("re_id");
                // path aendern und speichern
                $up = new sql();
                // $up->debugsql = 1;
                $up->setTable($REX['TABLE_PREFIX'] . "article");
                $up->where("id={$icid}");
                $up->setValue("path", $new_path);
                $up->update();
                // cat in gen eintragen
                $RC[$icid] = 1;
                $gcats->next();
            }
            // ----- clang holen, max catprio holen und entsprechen updaten
            $CL = $REX['CLANG'];
            reset($CL);
            for ($i = 0; $i < count($CL); $i++) {
                $clang = key($CL);
                $gmax = new sql();
                $gmax->setQuery("select max(catprior) from " . $REX['TABLE_PREFIX'] . "article where re_id={$to_cat} and clang={$clang}");
                $catprior = (int) $gmax->getValue("max(catprior)");
                $up = new sql();
                // $up->debugsql = 1;
                $up->setTable($REX['TABLE_PREFIX'] . "article");
                $up->where("id={$from_cat} and clang={$clang} ");
                $up->setValue("path", $to_path);
                $up->setValue("re_id", $to_cat);
                $up->setValue("catprior", $catprior + 1);
                $up->update();
                next($CL);
            }
            // ----- generiere artikel neu - ohne neue inhaltsgenerierung
            foreach ($RC as $id => $key) {
                rex_generateArticle($id, false);
            }
            $CL = $REX['CLANG'];
            reset($CL);
            for ($j = 0; $j < count($CL); $j++) {
                $mlang = key($CL);
                rex_newCatPrio($fcat->getValue("re_id"), $mlang, 0, 1);
                next($CL);
            }
            return true;
        }
    }
}
/**
 * Löscht eine Kategorie und reorganisiert die Prioritäten verbleibender Geschwister-Kategorien
 *
 * @param int $category_id Id der Kategorie die gelöscht werden soll
 *
 * @return array Ein Array welches den status sowie eine Fehlermeldung beinhaltet
 */
function rex_deleteCategoryReorganized($category_id)
{
    global $REX, $I18N;
    $return = array();
    $return['state'] = false;
    $return['message'] = '';
    $clang = 0;
    $thisCat = rex_sql::factory();
    $thisCat->setQuery('SELECT * FROM ' . $REX['TABLE_PREFIX'] . 'article WHERE id=' . $category_id . ' and clang=' . $clang);
    // Prüfen ob die Kategorie existiert
    if ($thisCat->getRows() == 1) {
        $KAT = rex_sql::factory();
        $KAT->setQuery('select * from ' . $REX['TABLE_PREFIX'] . "article where re_id='{$category_id}' and clang='{$clang}' and startpage=1");
        // Prüfen ob die Kategorie noch Unterkategorien besitzt
        if ($KAT->getRows() == 0) {
            $KAT->setQuery('select * from ' . $REX['TABLE_PREFIX'] . "article where re_id='{$category_id}' and clang='{$clang}' and startpage=0");
            // Prüfen ob die Kategorie noch Artikel besitzt (ausser dem Startartikel)
            if ($KAT->getRows() == 0) {
                $thisCat = rex_sql::factory();
                $thisCat->setQuery('SELECT * FROM ' . $REX['TABLE_PREFIX'] . 'article WHERE id=' . $category_id);
                $re_id = $thisCat->getValue('re_id');
                $return = rex_deleteArticle($category_id);
                while ($thisCat->hasNext()) {
                    $_clang = $thisCat->getValue('clang');
                    // ----- PRIOR
                    rex_newCatPrio($re_id, $_clang, 0, 1);
                    // ----- EXTENSION POINT
                    $return = rex_register_extension_point('CAT_DELETED', $return, array('id' => $category_id, 're_id' => $re_id, 'clang' => $_clang, 'name' => $thisCat->getValue('catname'), 'prior' => $thisCat->getValue('catprior'), 'path' => $thisCat->getValue('path'), 'status' => $thisCat->getValue('status')));
                    $thisCat->next();
                }
                $users = rex_sql::factory();
                $users->setQuery('UPDATE ' . $REX['TABLE_PREFIX'] . 'user SET rights = REPLACE(rights, "#csw[' . $category_id . ']#", "#")');
            } else {
                $return['message'] = $I18N->msg('category_could_not_be_deleted') . ' ' . $I18N->msg('category_still_contains_articles');
            }
        } else {
            $return['message'] = $I18N->msg('category_could_not_be_deleted') . ' ' . $I18N->msg('category_still_contains_subcategories');
        }
    } else {
        $return['message'] = $I18N->msg('category_could_not_be_deleted');
    }
    return array($return['state'], $return['message']);
}
        $AART->setValue("catname", "{$category_name}");
        $AART->setValue("catprior", $Position_New_Category);
        $AART->setValue("re_id", $category_id);
        $AART->setValue("prior", 1);
        $AART->setValue("path", $KATPATH);
        $AART->setValue("startpage", 1);
        $AART->setValue("status", 0);
        $AART->setValue("online_from", time());
        $AART->setValue("online_to", mktime(0, 0, 0, 1, 1, 2010));
        $AART->setValue("createdate", time());
        $AART->setValue("createuser", $REX_USER->getValue("login"));
        $AART->setValue("updatedate", time());
        $AART->setValue("updateuser", $REX_USER->getValue("login"));
        $AART->insert();
        // ----- PRIOR
        rex_newCatPrio($category_id, $key, 0, $Position_New_Category);
        // ----- EXTENSION POINT
        $message = rex_register_extension_point('CAT_ADDED', $message, array("id" => $id, "re_id" => $category_id, "clang" => $key, "name" => $category_name, "prior" => $Position_New_Category, "path" => $KATPATH, "status" => 0));
    }
    rex_generateArticle($id);
}
// --------------------------------------------- ARTIKEL FUNKTIONEN
if (isset($function) && $function == "status_article" && $article_id != "" && ($REX_USER->isValueOf("rights", "admin[]") || $KATPERM && $REX_USER->isValueOf("rights", "publishArticle[]"))) {
    // --------------------- ARTICLE STATUS
    $GA = new sql();
    $GA->setQuery("select * from " . $REX['TABLE_PREFIX'] . "article where id='{$article_id}' and clang={$clang}");
    if ($GA->getRows() == 1) {
        if ($GA->getValue("status") == 1) {
            $newstatus = 0;
        } else {
            $newstatus = 1;
/**
 * Verschieben einer Kategorie in eine andere
 *
 * @param int $from_cat_id KategorieId der Kategorie, die verschoben werden soll (Quelle)
 * @param int $to_cat_id   KategorieId der Kategorie, IN die verschoben werden soll (Ziel)
 *
 * @return boolean TRUE bei Erfolg, sonst FALSE
 */
function rex_moveCategory($from_cat, $to_cat)
{
    global $REX;
    $from_cat = (int) $from_cat;
    $to_cat = (int) $to_cat;
    if ($from_cat == $to_cat) {
        // kann nicht in gleiche kategroie kopiert werden
        return false;
    } else {
        // kategorien vorhanden ?
        // ist die zielkategorie im pfad der quellkategeorie ?
        $fcat = rex_sql::factory();
        $fcat->setQuery('select * from ' . $REX['TABLE_PREFIX'] . "article where startpage=1 and id={$from_cat} and clang=0");
        $tcat = rex_sql::factory();
        $tcat->setQuery('select * from ' . $REX['TABLE_PREFIX'] . "article where startpage=1 and id={$to_cat} and clang=0");
        if ($fcat->getRows() != 1 or $tcat->getRows() != 1 && $to_cat != 0) {
            // eine der kategorien existiert nicht
            return false;
        } else {
            if ($to_cat > 0) {
                $tcats = explode('|', $tcat->getValue('path'));
                if (in_array($from_cat, $tcats)) {
                    // zielkategorie ist in quellkategorie -> nicht verschiebbar
                    return false;
                }
            }
            // ----- folgende cats regenerate
            $RC = array();
            $RC[$fcat->getValue('re_id')] = 1;
            $RC[$from_cat] = 1;
            $RC[$to_cat] = 1;
            if ($to_cat > 0) {
                $to_path = $tcat->getValue('path') . $to_cat . '|';
                $to_re_id = $tcat->getValue('re_id');
            } else {
                $to_path = '|';
                $to_re_id = 0;
            }
            $from_path = $fcat->getValue('path') . $from_cat . '|';
            $gcats = rex_sql::factory();
            // $gcats->debugsql = 1;
            $gcats->setQuery('select * from ' . $REX['TABLE_PREFIX'] . "article where path like '" . $from_path . "%' and clang=0");
            $up = rex_sql::factory();
            // $up->debugsql = 1;
            for ($i = 0; $i < $gcats->getRows(); $i++) {
                // make update
                $new_path = $to_path . $from_cat . '|' . str_replace($from_path, '', $gcats->getValue('path'));
                $icid = $gcats->getValue('id');
                $irecid = $gcats->getValue('re_id');
                // path aendern und speichern
                $up->setTable($REX['TABLE_PREFIX'] . 'article');
                $up->setWhere("id={$icid}");
                $up->setValue('path', $new_path);
                $up->update();
                // cat in gen eintragen
                $RC[$icid] = 1;
                $gcats->next();
            }
            // ----- clang holen, max catprio holen und entsprechen updaten
            $gmax = rex_sql::factory();
            $up = rex_sql::factory();
            // $up->debugsql = 1;
            foreach ($REX['CLANG'] as $clang => $clang_name) {
                $gmax->setQuery('select max(catprior) from ' . $REX['TABLE_PREFIX'] . "article where re_id={$to_cat} and clang=" . $clang);
                $catprior = (int) $gmax->getValue('max(catprior)');
                $up->setTable($REX['TABLE_PREFIX'] . 'article');
                $up->setWhere("id={$from_cat} and clang={$clang} ");
                $up->setValue('path', $to_path);
                $up->setValue('re_id', $to_cat);
                $up->setValue('catprior', $catprior + 1);
                $up->update();
            }
            // ----- generiere artikel neu - ohne neue inhaltsgenerierung
            foreach ($RC as $id => $key) {
                rex_deleteCacheArticle($id);
            }
            foreach ($REX['CLANG'] as $clang => $clang_name) {
                rex_newCatPrio($fcat->getValue('re_id'), $clang, 0, 1);
            }
        }
    }
    return true;
}