<?php
// +----------------------------------------------------------------------+
// | Der Dirigent 1.0                                                     |
// | Last update : 2005-11-24                                             |
// | Part of     : Update to Version 01.00.02                             |
// +----------------------------------------------------------------------+
// | Copyright (c) 2003-2004 the dedi-group <dedi-group@der-dirigent.de>  |
// +----------------------------------------------------------------------+
// | This program is free software; you can redistribute it and/or modify |
// | it under the terms of the GNU General Public License                 |
// |                                                                      |
// | This program is subject to the GPL license, that is bundled with     |
// | this package in the file LICENSE.TXT.                                |
// | If you did not receive a copy of the GNU General Public License      |
// | along with this program write to the Free Software Foundation, Inc., |
// | 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA               |
// |                                                                      |
// | This program is distributed in the hope that it will be useful,      |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        |
// | GNU General Public License for more details.                         |
// |                                                                      |
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
if (!is_object($perm)) die();
function lang_new_language($idclient, $name, $desc, $charset, $copy_content = true) {
        global $db, $sess, $auth, $dedi_db, $lang, $user_msg;

        set_magic_quotes_gpc($name);
        set_magic_quotes_gpc($desc);

        // Eintrag in 'lang' Tabelle
        $from_lang = $lang;
        $sql = "INSERT INTO ". $dedi_db['lang'] ."
                                (name, description, author, charset, created, lastmodified)
                        VALUES
                                ('$name', '$desc', '".$auth->auth['uid']."', '".$charset."', '".time()."', '".time()."')";
        $db->query($sql);
        $lang = mysql_insert_id();

        // Eintrag in 'clients_lang' Tabelle
        $sql = "INSERT INTO $dedi_db[clients_lang] (idclient, idlang, author, created, lastmodified) VALUES ('$idclient','$lang', '".$auth->auth['uid']."', '".time()."', '".time()."')";
        $db->query($sql);

        // Webseitencontent duplizieren
        if ($from_lang != '' && $copy_content) {
                $db2 = &new DB_dedi;

                // Zeitinterval vergrößern
                @set_time_limit(0);

                // Ordner kopieren
                $sql = "SELECT idcat, name, description, author FROM $dedi_db[cat_lang] WHERE idlang='$from_lang'";
                $db->query($sql);
                while ($db->next_record()) {
                        $name = make_string_dump($db->f('name'));
                        $description = make_string_dump($db->f('description'));
                        // setze kopierte Seite auf Offline, keine Übernahme der Zeitsteuerung
                        // übernehme aber das Bit für Schutzrechte incl. der zukünftigen Level
                        $online = ((int) $db->f('visible') & 0xFC);
                        $sql2 = "INSERT INTO $dedi_db[cat_lang] (idcat, idlang, idtplconf, name, description, visible, author, created, lastmodified) VALUES ('".$db->f('idcat')."', '$lang', '0', '$name', '$description', '$online', '".$db->f('author')."', '".time()."', '".time()."')";
                        $db2->query($sql2);
                }

                // Seiten kopieren
                $sql = "SELECT * FROM $dedi_db[side_lang] WHERE idlang='$from_lang'";
                $db->query($sql);
                while ($db->next_record()) {
                        $title = make_string_dump($db->f('title'));
                        $summary = make_string_dump($db->f('summary'));
                        $meta_author = make_string_dump($db->f('meta_author'));
                        $meta_description = make_string_dump($db->f('meta_description'));
                        $meta_keywords = make_string_dump($db->f('meta_keywords'));
                        $meta_robots = make_string_dump($db->f('meta_robots'));
                        $meta_redirect_url = make_string_dump($db->f('meta_redirect_url'));
                        // change JB
                        // setze kopierte Seite auf Offline, keine Übernahme der Zeitsteuerung
                        // übernehme aber das Bit für Schutzrechte incl. der zukünftigen Level
                        $online = ((int) $db->f('online') & 0xFC);
                        $sql2 = "INSERT INTO $dedi_db[side_lang] (idside, idlang, idtplconf, title, meta_keywords, summary, online, meta_redirect, meta_redirect_url, author, created, lastmodified, user_protected, visited, edit_ttl, meta_author, meta_description, meta_robots, meta_redirect_time) VALUES ('".$db->f('idside')."', '$lang', '0', '$title', '$meta_keywords', '$summary', '$online', '".$db->f('meta_redirect')."', '$meta_redirect_url', '".$db->f('author')."', '".time()."', '".time()."', '".$db->f('user_protected')."', '".$db->f('visited')."', '".$db->f('edit_ttl')."', '$meta_author', '$meta_description', '$meta_robots', '".$db->f('meta_redirect_time')."')";
                        // change JB
                        $db2->query($sql2);
                }
        }

        //langstring for new client success userinfo
        $user_msg = 'success_new_lang';
}

function lang_rename_language($idlang, $name, $desc, $charset)
{
        global $db, $auth, $dedi_db, $perm;

        set_magic_quotes_gpc($name);
        set_magic_quotes_gpc($desc);

        $sql = "UPDATE
                                ".$dedi_db['lang'] ."
                        SET
                                name='$name',
                                description='$desc',
                                charset='$charset',
                                author='".$auth->auth['uid']."',
                                lastmodified='".time()."'
                        WHERE
                                idlang='$idlang'";

        $db->query($sql);

        //Rechte setzen
        if ($perm->have_perm(22, 'clientlangs', $idlang)) {
                global $dedi_gruppenids, $dedi_gruppenrechte;
                $perm->set_group_rights( 'clientlangs', $idlang, $dedi_gruppenids, $dedi_gruppenrechte );
        }
}

function lang_delete_language($idclient, $idlang) {
        global $db, $sess, $dedi_db;

        //************ check if there are still sides online
        // change JB: Berücksichtigt auch die zeitgesteuerten Seiten
        $sql = "SELECT * FROM $dedi_db[side_lang] A LEFT JOIN $dedi_db[side] B USING(idside) WHERE B.idclient='$idclient' AND idlang!='0' AND idlang='$idlang' AND (online & 0x03) > 0x00";
        $db->query($sql);
        if ($db->next_record()) {
                 return "0802";        //Deleting not possible because of online sides or visible categories
        }

        //************ check if there are visible categories
        // change JB: Berücksichtigt auch die zeitgesteuerten Seiten
        $sql = "SELECT * FROM $dedi_db[cat_lang] A LEFT JOIN $dedi_db[cat] B USING(idcat) WHERE B.idclient='$idclient' AND idlang!='0' AND idlang='$idlang' AND (visible & 0x03) > 0x00";
        $db->query($sql);
        if ($db->next_record()) return "0802"; //Deleting not possible because of online sides or visible categories


        // keine seiten oder kategorien online ... löschen
        // Event feuern
        fire_event('delete_lang', array ('idclient'    => $idclient,
                                         'idlang'      => $idlang));

        //********* check if this is the clients last language to be deleted, if yes delete from side, cat, and cat_side as well *******
        $last_language = 0;
        $sql = "SELECT COUNT(*) FROM $dedi_db[clients_lang] WHERE idclient='$idclient'";
        $db->query($sql);
        $db->next_record();
        if ($db->f(0) == 1) {
                $lastlanguage = 1;
        }

        //********** delete from 'side_lang'-table *************
        $sql = "SELECT idsidelang, A.idside FROM $dedi_db[side_lang] A LEFT JOIN $dedi_db[side] B USING(idside) WHERE B.idclient='$idclient' AND idlang!='0' AND idlang='$idlang'";
        $db->query($sql);
        while ($db->next_record()) {
                $a_idsidelang[] = $db->f('idsidelang');
                $a_idside[] = $db->f('idside');
        }
        if (is_array($a_idsidelang)) {
                foreach ($a_idsidelang as $value) {
                        $sql = "DELETE FROM $dedi_db[side_lang] WHERE idsidelang='$value'";
                        $db->query($sql);

                        $sql = "DELETE FROM $dedi_db[content] WHERE idsidelang='$value'";
                        $db->query($sql);
                }
        }
        if ($lastlanguage == 1) {
                if (is_array($a_idside)) {
                        foreach ($a_idside as $value) {
                                $sql = "DELETE FROM $dedi_db[side] WHERE idside='$value'";
                                $db->query($sql);
                                $sql = "DELETE FROM $dedi_db[cat_side] WHERE idside='$value'";
                                $db->query($sql);
                        }
                }
        }

        //********** delete from 'cat_lang'-table *************
        $sql = "SELECT idcatlang, A.idcat FROM $dedi_db[cat_lang] A LEFT JOIN $dedi_db[cat] B USING(idcat) WHERE B.idclient='$idclient' AND idlang!='0' AND idlang='$idlang'";
        $db->query($sql);
        while ($db->next_record()) {
                $a_idcatlang[] = $db->f("idcatlang");
                $a_idcat[] = $db->f("idcat");
        }
        if (is_array($a_idcatlang)) {
                foreach ($a_idcatlang as $value) {
                        $sql = "DELETE FROM $dedi_db[cat_lang] WHERE idcatlang='$value'";
                        $db->query($sql);
                }
        }
        if ($lastlanguage == 1) {
                if (is_array($a_idcat)) {
                        foreach ($a_idcat as $value) {
                                $sql = "DELETE FROM $dedi_db[cat] WHERE idcat='$value'";
                                $db->query($sql);
                        }
                }
        }

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

        // delete from 'code'-table
        $sql = "DELETE FROM $dedi_db[code] WHERE idlang='$idlang'";
        $db->query($sql);


        // delete from 'clients_lang'-table
        $sql = "DELETE FROM $dedi_db[clients_lang] WHERE idclient='$idclient' AND idlang='$idlang'";
        $db->query($sql);

        // delete from 'lang'-table
        $sql = "DELETE FROM $dedi_db[lang] WHERE idlang='$idlang'";
        $db->query($sql);

        // delete from 'perm'-table
        $sql = "DELETE FROM $dedi_db[perms] WHERE idlang='$idlang'";
        $db->query($sql);

        //header ('Status: 302 Moved Temporarily');
        //header ("Location:".$sess->url("main.php?area=lang&f_client=$idclient"));
}
?>