<?PHP
/******************************************************************************
 @Author:     Björn Brockmann, Paul Eppner (paul@twomoons.de)
                              Sven Bräutigam, Jürgen Brändle
 @Homepage    http://www.der-dirigent.de
 Last update : 2005-11-24                
 Part of     : Update to Version 01.00.02
******************************************************************************/
if (!is_object($perm)) die();
function con_delete_cache($idlang) {
        global $dedi_db, $db, $perm;

        // to do
        $perm->check_admin();
        $sql = "DELETE FROM ".$dedi_db['code']." WHERE idlang='$idlang'";
        $db->query($sql);
    // Cache löschen
    $db->delete_cache(true);
}

function con_deeper_categories($idcat_start, $check_perm) {
        global $tlo_tree, $deeper_list, $client, $dedi_db, $db, $perm;

        if (!$tlo_tree) {
                $sql = "SELECT idcat, parent, sortindex FROM $dedi_db[cat] WHERE idclient='$client' ORDER BY parent, sortindex";
                $db->query($sql);
                while ($db->next_record()) {
                        $tlo_tree[$db->f('parent')][$db->f('sortindex')] = $db->f('idcat');
        }
        }

        for ($i=1; !empty($tlo_tree[$idcat_start][$i]); $i++) {
                if ($perm -> have_perm($check_perm, 'cat', $tlo_tree[$idcat_start][$i])) {
                        $deeper_list[] = $tlo_tree[$idcat_start][$i];
                }
                con_deeper_categories($tlo_tree[$idcat_start][$i], $check_perm);
        }
        return $deeper_list;
}

function con_visible_cat ($idcat, $lang = '', $visible) {
        global $db, $dedi_db;

        $a_catstring = array();
           $a_catstring = con_deeper_categories($idcat, '7');
        $a_catstring[] = $idcat;
        $sql  = "UPDATE $dedi_db[cat_lang] SET visible='$visible' WHERE idcat IN (". implode(',', $a_catstring) . ")";
        $sql .= (!empty($lang)) ? " AND idlang='$lang'": '';
        $db->query($sql);

        // Navigationstree aus Cache löschen
        $db->delete_cache('frontend');
}


function con_lock ($type, $id, $visible) {
        global $db, $dedi_db, $client, $perm;

        switch ($type) {
                // Ordner sperren
                case 'cat':
                        $a_catstring = array();
                        $a_catstring = con_deeper_categories($id, '13');
                        $a_catstring[] = $id;

                        // entsperren / sperren aller Ordner unabhängig von der Sprache
                            if ($visible == '1') $sql  = "UPDATE $dedi_db[cat_lang] SET visible = (visible | 0x04) WHERE idcat IN (" . implode(',', $a_catstring) . ")";
                        else $sql  = "UPDATE $dedi_db[cat_lang] SET visible = (visible & 0xFB) WHERE idcat IN (" . implode(',', $a_catstring) . ")";
                        $db->query($sql);

                        $sql = "SELECT S.idside
                                FROM ". $dedi_db['side'] ." S
                                inner join ". $dedi_db['cat_side'] ." CS USING(idside)
                                WHERE idcat IN (" . implode(',', $a_catstring) . ")";
                        $db->query($sql);
                        while ($db->next_record()) $sides[] = $db->f("idside");

                        //prüfen ob sides vorhanden, bei leerer Kategorie nicht der fall
                        if (is_array($sides)) con_lock('side', $sides, $visible);
                        break;

                // Seite sperren
                case 'side':
            $sides_sql = (is_array($id)) ? ' IN (' . implode(',', $id) . ')': ' = ' . $id;

                        // entsperren / sperren aller Seiten unabhängig von der Sprache
            if ($visible == '1') $sql  = "UPDATE $dedi_db[side_lang] SET online = (online | 0x04) WHERE idside " . $sides_sql;
                        else $sql  = "UPDATE $dedi_db[side_lang] SET online = (online & 0xFB) WHERE idside " . $sides_sql;
                        $db->query($sql);
            break;
        }

        // Navigationstree aus Cache löschen
        $db->delete_cache('frontend');
}

function con_delete_cat ($idcat) {
        global $db, $dedi_db, $auth, $client, $perm;

        // gibt es noch Unterordner?
        $sql = "SELECT * FROM $dedi_db[cat] WHERE parent='$idcat'";
        $db->query($sql);
        if ($db->next_record()) return '0201';
        else {
                // gibt es noch Seiten in diesem Ordner?
                $sql = "SELECT * FROM $dedi_db[cat_side] WHERE idcat='$idcat'";
                $db->query($sql);
                if ($db->next_record()) return '0202';
                else {
                        // Parentid raussuchen zum neusortieren der Ordner
                        $sql = "SELECT parent, sortindex FROM $dedi_db[cat] WHERE idcat='$idcat'";
                        $db->query($sql);
                        $db->next_record();
                        $parent = $db->f('parent');
                        $sortindex = $db->f('sortindex');

            // Ordner löschen
            $sql = "DELETE FROM $dedi_db[cat] WHERE idcat='$idcat'";
                        $db->query($sql);

            // expand löschen
                        $sql = "DELETE FROM $dedi_db[cat_expand] WHERE idusers = '".$auth->auth['uid']."' AND idcat = '$idcat'";
            $db->query($sql);

            // die anderen Ordner neu sortieren
                        $sql = "UPDATE $dedi_db[cat] SET sortindex=sortindex-1 WHERE parent='$parent' AND sortindex>'$sortindex' AND idclient='$client'";
                        $db->query($sql);

                        // Liste der benutzten Templates erstellen
                        $sql = "SELECT idtplconf FROM $dedi_db[cat_lang] WHERE idcat='$idcat'";
                        $db->query($sql);
                        while ($db->next_record()) if ($db->f('idtplconf') != '0') $idtplconf[] = $db->f('idtplconf');

                        // Einträge aus der cat_lang Tabelle löschen
            $sql = "DELETE FROM $dedi_db[cat_lang] WHERE idcat='$idcat'";
                        $db->query($sql);

                        // Rechte löschen
                        $perm->delete_perms($idcat, 'cat', 0, 0, 0, true);

                        // benutzte Templates löschen
                        if (is_array($idtplconf)) {
                                foreach ($idtplconf as $value) {
                                        if ($value != '0') {
                                                // Templatekonfiguration löschen
                        $sql = "DELETE FROM $dedi_db[container_conf] WHERE idtplconf='$value'";
                                                $db->query($sql);

                                                // Templatekopie löschen
                        $sql = "DELETE FROM $dedi_db[tpl_conf] WHERE idtplconf='$value'";
                                                $db->query($sql);
                    }
                }
            }

                        // Cache-Group Frontend löschen
                        $db->delete_cache('frontend');
                }
        }
}

// change JB - sprache ist nicht mehr Pflicht ... für sperren von Seiten notwendig
function con_visible_side ($idside, $lang, $online) {
        global $db, $dedi_db;

        $sql  = "UPDATE $dedi_db[side_lang] SET online = $online WHERE idside='$idside'";
        $sql .= ($lang >= 0) ? " AND idlang='$lang'": '';
        $db->query($sql);

        // Cache-Group Frontend löschen
        $db->delete_cache('frontend');
}

function con_delete_side ($idcat, $idside) {
        global $db, $dedi_db, $perm;

        $sql = "SELECT idcatside FROM $dedi_db[cat_side] WHERE idside='$idside'";
        $db->query($sql);
        while ($db->next_record()){
                 $temp_idcatsides = $db->f('idcatside');
                 $idcatsides[] = $temp_idcatsides;
                 $perm-> check(21, 'side', $temp_idcatsides, $idcat);
        }

        // Event
        fire_event('delete_side', array ('idside' => $idside, 'idcat' => $idcat, 'idcatside' => $idcatsides));

        // aus 'code'-Tabelle löschen
        if (is_array($idcatsides)) {
                $sql = "DELETE FROM ". $dedi_db['code'] ." WHERE idcatside IN (". implode(',', $idcatsides).")";
                $db->query($sql);

                // Rechte löschen
                $perm->delete_perms($idcatsides, 'side', 0, 0, 0, true);
        }
        $sql = "SELECT idsidelang, idtplconf FROM $dedi_db[side_lang] WHERE idside='$idside'";
        $db->query($sql);
        while ($db->next_record()) $idsidelang[$db->f('idsidelang')] = $db->f('idtplconf');
        if (is_array($idsidelang)) {
                foreach ($idsidelang AS $key => $value) {
                        // Inhalt aus 'content'-Tabelle löschen
                        $sql = "DELETE FROM $dedi_db[content] WHERE idsidelang='$key'";
                        $db->query($sql);

                        // besitzt die Seite ein eigenes Template?
                        if ($value != '0') {
                                // Templatekonfiguation löschen
                                $sql = "DELETE FROM $dedi_db[container_conf] WHERE idtplconf='$value'";
                                $db->query($sql);

                                // Templatekopie löschen
                                $sql = "DELETE FROM $dedi_db[tpl_conf] WHERE idtplconf='$value'";
                                $db->query($sql);
            }

                        // Links, die auf diese Seite zeigen löschen
                        // Seitenstatus dieser Seiten auf geändert stellen
            // muß noch gemacht werden
                }
        }

        // Seite aus den Ordnern löschen
        $sql = "DELETE FROM $dedi_db[cat_side] WHERE idside='$idside'";
        $db->query($sql);

        // Seite löschen
        $sql = "DELETE FROM $dedi_db[side] WHERE idside='$idside'";
        $db->query($sql);

        // Seite aus den verschiedenen Sprachen löschen
        $sql = "DELETE FROM $dedi_db[side_lang] WHERE idside='$idside'";
        $db->query($sql);

        // restliche Seiten neu sortieren
        $sql = "UPDATE $dedi_db[cat_side] SET sortindex=sortindex-1 WHERE idcat=$idcat AND idside>'$idside'";
        $db->query($sql);

        // neue Startseite festlegen
        $sql = "SELECT * FROM $dedi_db[cat_side] WHERE idcat='$idcat' AND is_start='1'";
        $db->query($sql);
        if (!$db->affected_rows()) {
                $sql = "UPDATE $dedi_db[cat_side] SET is_start='1' WHERE idcat='$idcat' AND sortindex='1'";
                $db->query($sql);
    }

        // Cache-Group Frontend löschen
        $db->delete_cache('frontend');
}

function con_expand ($idcat, $expand_action) {
        global $db, $perm, $client, $DB_dedi, $dedi_db, $auth;

        $user_id = $auth->auth['uid'];
        switch($expand_action) {
                // Ordner öffnen
                case '0':
                        $sql = "INSERT INTO $dedi_db[cat_expand] (idusers, idcat) VALUES ('$user_id', '$idcat')";
            $db->query($sql);
            break;

                 // Ordner schließen
                 case '1':
                        $sql = "DELETE FROM $dedi_db[cat_expand] WHERE idusers = '$user_id' AND idcat = '$idcat'";
            $db->query($sql);
            break;

                // gesamten Ordner schließen
                case '2':
                        $list = con_deeper_categories($idcat, '0');
                        if (is_array($list)) array_push ($list, $idcat);
            else $list[] = $idcat;
                        $sql = "DELETE FROM $dedi_db[cat_expand] WHERE idusers = '$user_id' AND idcat IN(".implode(',', $list).")";
            $db->query($sql);
            break;

                // gesamten Ordner öffnen
                case '3':
                        $list = con_deeper_categories($idcat, '0');
                        if (is_array($list)) array_push ($list, $idcat);
            else $list[] = $idcat;

                        // alles löschen, damit doppelte Datensätze vermieden werden
                        $sql = "DELETE FROM $dedi_db[cat_expand] WHERE idusers = '$user_id' AND idcat IN(".implode(',', $list).")";
            $db->query($sql);

                        foreach($list as $value) {
                                if ($value != '0' && $perm->have_perm('0', 'cat', $value)) {
                                        $sql = "INSERT INTO $dedi_db[cat_expand] (idusers, idcat) VALUES ('$user_id', '$value')";
                    $db->query($sql);
                }
                        }
            break;
        }
}

function con_publish ($id, $type='all') {
        global $db, $dedi_db, $con_side, $con_tree, $perm, $idcat, $lang, $view, $sess, $idcatside, $cfg_client;

        switch($type) {
                // diese Ordner publizieren
                case 'all':
                        $tmp_list = con_deeper_categories($id, '7');
                        if (is_array($tmp_list)) array_push ($tmp_list, $id);
                        else $tmp_list[] = $id;
                        foreach ($tmp_list as $id) {
                                if (is_array($con_side[$id])) {
                                        foreach($con_side[$id] as $side) {
                                                if($perm->have_perm(23, 'side', $side['idcatside'], $idcat)) {
                                                         $list[] = $side['idcatside'];
                                                        unset($con_tree[$id]['status']);
                                                        unset($con_side[$id][$side['idcatside']]['status']);
                                                 }
                                         }
                                 }
                        }
                        break;

                // Seite publizieren
                case 'side':
                        if($perm->have_perm(23, 'side', $id, $idcat)) {
                                $list[] = $id;
                                unset($con_tree[$idcat]['status']);
                                unset($con_side[$idcat][$id]['status']);
                                $new_search = 'true';
                        }
                        break;

                // alle Seiten dieses Ordners publizieren
                case 'cat':
                        if (is_array($con_side[$id])) {
                                foreach($con_side[$id] as $side) {
                                        if ($perm->have_perm(23, 'side', $side['idcatside'], $idcat)) {
                        $list[] = $side['idcatside'];
                                                unset($con_tree[$id]['status']);
                                                unset($con_side[$id][$side['idcatside']]['status']);
                    }
                                }
                        }
                        break;
        }
        change_code_status($list, '1', 'publish');

        // Status neu einlesen
        if ($new_search) {
                $sql = "SELECT A.idcatside, idcat, changed FROM $dedi_db[cat_side] A LEFT JOIN $dedi_db[code] B USING(idcatside) WHERE B.idlang='$lang' AND changed='2' AND A.idcat='$idcat'";
                $db->query($sql);
                while($db->next_record()) {
                        if ($perm->have_perm(23, 'side', $db->f('idcatside'), $db->f('idcat'))) {
                    $con_side[$db->f('idcat')][$db->f('idcatside')]['status'] = 'true';
                                $con_tree[$db->f('idcat')]['status'] = 'true';
                        }
                }
        }

    // Cache-Group Frontend löschen
        $db->delete_cache('frontend');

        // Danach ins Frontend?
        if ($view) {
                header ('Status: 302 Moved Temporarily');
                header ('Location:'.$sess->url($cfg_client['htmlpath'].$cfg_client['contentfile'].'?lang='.$lang.'&idcatside='.$idcatside.'&view='.$view));
                exit;
        }
}

function con_make_start ($idcatside, $is_start) {
        global $db, $dedi_db;

        $sql = "SELECT idcat FROM $dedi_db[cat_side] WHERE idcatside='$idcatside'";
        $db->query($sql);
        $db->next_record();

        $sql = "UPDATE $dedi_db[cat_side] SET is_start='0' WHERE idcat='".$db->f('idcat')."'";
        $db->query($sql);

        $sql = "UPDATE $dedi_db[cat_side] SET is_start='$is_start' WHERE idcatside='$idcatside'";
        $db->query($sql);

        // Cache-Group Frontend löschen
        $db->delete_cache('frontend');
}

function con_move_side($dir,$idcat,$idside,$sortindex) {
        global $db, $dedi_db;

        if($dir == 'up'){
                if($sortindex > 1){
                        $sql = "UPDATE $dedi_db[cat_side] SET sortindex='$sortindex' WHERE idcat='$idcat' AND sortindex='".($sortindex - 1)."'";
                        $db->query($sql);
                        $sql = "UPDATE $dedi_db[cat_side] SET sortindex='".($sortindex - 1)."' WHERE idcat='$idcat' AND idside='$idside'";
                        $db->query($sql);
                }
        } else {
                $sql = "UPDATE $dedi_db[cat_side] SET sortindex='$sortindex' WHERE idcat='$idcat' AND sortindex='".($sortindex + 1)."'";
                if($db->query($sql)){
                        $sql = "UPDATE $dedi_db[cat_side] SET sortindex='".($sortindex + 1)."' WHERE idcat='$idcat' AND idside='$idside'";
                        $db->query($sql);
                }
        }

        // Cache-Group Frontend löschen
        $db->delete_cache('frontend');
}

function con_quick_sort($quicksort,$idcat) {
        global $db, $lang, $dedi_db;

        list($sort,$dir) = split(':',$quicksort);
        $sql  = "SELECT A.idcatside, A.sortindex, B.title, B.created, B.lastmodified, B.author FROM $dedi_db[cat_side] A LEFT JOIN $dedi_db[side_lang] B USING(idside) WHERE A.idcat = '$idcat' AND B.idlang = '$lang'";
        $db->query($sql);
        while($db->next_record()){
                $sortarray[$db->f('idcatside')] = $db->f($sort);
        }
        foreach ($sortarray AS $k=>$v){
                $sortarray[$k] = str_replace(array('Ä','ä','Ü','ü','Ö','ö'),
                                                                                array('Ae','ae','Ue','ue','Oe','oe'),
                                                                                $v);
        }

        natsort($sortarray);
        if($dir == 'DESC'){$sortarray = array_reverse($sortarray, TRUE); }
        $counter = 1;
        foreach (array_keys($sortarray) as $idcatside){
                $sql = "UPDATE $dedi_db[cat_side] SET sortindex='$counter' WHERE idcatside = '$idcatside'";
                $db->query($sql);
                $counter++;
        }

        // Navigationstree aus Cache löschen
        $db->delete_cache('frontend_tree');
}

function con_move_top_bottom($dir,$idcat,$idcatside,$sortindex) {
        global $db, $dedi_db;
        if($dir == 'top'){
                $sql = "UPDATE $dedi_db[cat_side] SET sortindex=sortindex+1 WHERE idcat=$idcat AND sortindex BETWEEN 1 AND $sortindex";
                $db->query($sql);
                $sql = "UPDATE $dedi_db[cat_side] SET sortindex=1 WHERE idcatside=$idcatside";
                $db->query($sql);
        } else {
                // Endwert holen
                $db->query("SELECT MAX(sortindex) AS max FROM $dedi_db[cat_side] WHERE idcat=$idcat");
                $db->next_record(); $lastindex=$db->f('max');
                // Alles eins höher legen
                $sql = "UPDATE $dedi_db[cat_side] SET sortindex=sortindex-1 WHERE idcat=$idcat AND sortindex > $sortindex";
                $db->query($sql);
                // Aktuelle Seite nach ganz unten
                $sql = "UPDATE $dedi_db[cat_side] SET sortindex=$lastindex WHERE idcatside=$idcatside";
                $db->query($sql);
        }

        // Navigationstree aus Cache löschen
        $db->delete_cache('frontend_tree');
}

function reindex_sort(&$sidelist) {
        global $db, $dedi_db, $reindex;

        foreach (array_keys($reindex) as $kat) {
                ksort($sidelist[$kat]);
                $index = 0;
                foreach ($sidelist[$kat] as $idcatside) {
                        $index++;
                        $sql = "UPDATE $dedi_db[cat_side] SET sortindex=$index WHERE idcatside = $idcatside";
                        $db->query($sql);
                }
        }

        // Navigationstree aus Cache löschen
        $db->delete_cache('frontend_tree');
}

function reindex_cat_sort($idcat, $idclient) {
        global $db, $dedi_db;

        if($idcat == 0 || ! (is_int($idcat)||is_numeric($idcat) ) )
                return;
        if($idclient < 1 || ! (is_int($idclient)||is_numeric($idclient) ) )
                return;

        //echo "XXXX $idcat";
        //parent finden
        $db->query("SELECT
                                        parent FROM ".$dedi_db['cat']."
                                WHERE
                                        idcat=". $idcat ."
                                        AND idclient=".$idclient);
        $db->next_record();
    $p=$db->f('parent');

        //alle idcats finden
        $db->query("SELECT
                                        idcat FROM ".$dedi_db['cat']."
                                WHERE
                                        parent=".$p."
                                        AND idclient=". $idclient ."
                                ORDER BY sortindex");
        $idcats = array();
        while($db->next_record()){
                array_push($idcats, $db->f('idcat'));
        }

        //neu sortieren
        $new_index = 1;
        if(is_array($idcats)){
                foreach($idcats AS $v){
                        $sql = "UPDATE ".$dedi_db['cat']." SET sortindex=$new_index WHERE idcat=$v";
                        $db->query($sql);
                        $new_index++;
                }
        }

    // Navigationstree aus Cache löschen
        //$db->delete_cache('frontend_tree');
}

function con_move_cat($idcat,$target, $idclient) {
        global $db, $dedi_db, $sort;
        $sort = true;

        if($idcat == 0 || ! (is_int($idcat)||is_numeric($idcat)) || ! (is_int($target)||is_numeric($target)) )
                return;
        if($idclient < 1 || ! (is_int($idclient)||is_numeric($idclient) ) )
                return;


        // Parent und Sortindex feststellen
        $db->query("SELECT parent, sortindex FROM $dedi_db[cat] WHERE idcat=$idcat AND idclient=". $idclient);
        $db->next_record();
         $parent=$db->f('parent');
         $sortindex=$db->f('sortindex');

        // neuen Sortindex suchen
        $db->query("SELECT MAX(sortindex) AS max FROM $dedi_db[cat] WHERE parent=$target AND idclient=". $idclient);
        $db->next_record();
         $lastindex=$db->f('max')+1;

        // rootparent holen
        $db->query("SELECT rootparent FROM $dedi_db[cat] WHERE idcat=$target AND idclient=". $idclient);
        $db->next_record();
         if( $db->f('rootparent') > 0){ $rootparent = $db->f('rootparent'); }
         else { $rootparent = $idcat; }

        // verschieben
        $sql = "UPDATE
                                $dedi_db[cat]
                        SET
                                parent='$target',
                                sortindex='$lastindex',
                                rootparent='$rootparent'
                        WHERE idcat=$idcat
                        AND idclient=". $idclient;
        $db->query($sql);

        // umsortieren in der alten Kategorie BUGGY?
        // $sql = "UPDATE $dedi_db[cat] SET sortindex=sortindex-1 WHERE parent=$parent AND sortindex > $sortindex";
        // $db->query($sql);

        //update old cats
        reindex_cat_sort($parent, $idclient);
        //update new cats
        reindex_cat_sort($target, $idclient);

    // Navigationstree aus Cache löschen
        $db->delete_cache('frontend_tree');
}

function con_sort_cat ($dir,$idcat,$sortindex,$parent, $idclient) {
        global $db, $dedi_db, $sort;

        $sort = true;
        switch($dir) {
                case 'up':
                        if($sortindex > 1){
                                $sql = "UPDATE $dedi_db[cat] SET sortindex='$sortindex' WHERE parent='$parent' AND sortindex='".($sortindex - 1)."' AND idclient=".$idclient;
                                $db->query($sql);
                                $sql = "UPDATE $dedi_db[cat] SET sortindex='".($sortindex - 1)."' WHERE parent='$parent' AND idcat='$idcat' AND idclient=".$idclient;
                                $db->query($sql);
                        }
                        break;
                case 'down':
                        $sql = "UPDATE $dedi_db[cat] SET sortindex='$sortindex' WHERE parent='$parent' AND sortindex='".($sortindex + 1)."' AND idclient=".$idclient;
                        if($db->query($sql)){
                                $sql = "UPDATE $dedi_db[cat] SET sortindex='".($sortindex + 1)."' WHERE parent='$parent' AND idcat='$idcat' AND idclient=".$idclient;
                                $db->query($sql);
                        }
                        break;
                case 'top':
                        $sql = "UPDATE $dedi_db[cat] SET sortindex=sortindex+1 WHERE parent=$parent AND sortindex BETWEEN 1 AND $sortindex AND idclient=".$idclient;
                        $db->query($sql);
                        $sql = "UPDATE $dedi_db[cat] SET sortindex=1 WHERE idcat=$idcat AND idclient=".$idclient;
                        $db->query($sql);
                        break;
                case 'bottom':
                        // Endwert holen
                        $db->query("SELECT MAX(sortindex) AS max FROM $dedi_db[cat] WHERE parent=$parent AND idclient=".$idclient);
                        $db->next_record(); $lastindex=$db->f('max');
                        // Alles eins höher legen
                        $sql = "UPDATE $dedi_db[cat] SET sortindex=sortindex-1 WHERE parent=$parent AND sortindex > $sortindex AND idclient=". $idclient;
                        $db->query($sql);
                        // Aktuelle Seite nach ganz unten
                        $sql = "UPDATE $dedi_db[cat] SET sortindex=$lastindex WHERE idcat=$idcat AND idclient=".$idclient;
                        $db->query($sql);
                        break;
        }

        // Navigationstree aus Cache löschen
        $db->delete_cache('frontend_tree');
}
?>