<?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 - Permissions check                              
 Part of     : Update to Version 01.00.02    
******************************************************************************/
if (!is_object($perm)) die();
function lay_edit_layout($idlay, $name, $description, $code, $idclient) {
        global $db, $client, $auth, $dedi_db, $cfg_dedi, $css, $js, $dedi_lang, $cfg_client, $perm;

        // Eintrag in 'lay' Tabelle
        if ($name == '') $name = $dedi_lang['lay_defaultname'];
        set_magic_quotes_gpc($name);
        set_magic_quotes_gpc($description);
        set_magic_quotes_gpc($code);

        // Layout existiert noch nicht - neu erzeugen
        if (!$idlay) {
                $sql = "INSERT INTO
                                        ".$dedi_db['lay']."
                                        (name, description, deletable, code, idclient, author, created, lastmodified)
                                VALUES
                                        ('$name', '$description', '1', '$code', '$idclient', '".$auth->auth['uid']."',
                                        '".time()."', '".time()."')";
                $db->query($sql);

                // neue Layout-ID suchen
                $sql = "SELECT MAX(idlay) AS idlay FROM ".$dedi_db['lay'];
                $db->query($sql);
                $db->next_record();
                $idlay = $db->f('idlay');

                // Event neues Layout
                fire_event('lay_new', array('idlay' => $idlay, 'name' => $name));

        // Layout existiert - updaten
        } else {
                // hat sich das Layout geändert?
                $sql = "SELECT code FROM ".$dedi_db['lay'] ." WHERE idlay='$idlay'";
                $db->query($sql);
                $db->next_record();
                $code_old = $db->f('code');
                set_magic_quotes_gpc($code_old);
                 if ($code != $code_old) {
                        $sql = "UPDATE ". $dedi_db['lay']."
                                SET
                                        name='$name', description='$description', code='$code',
                                        author='".$auth->auth['uid']."', lastmodified='".time()."'
                                WHERE
                                        idlay='$idlay'";
                        $db->query($sql);
                        $change = 'true';
                } else {
                        $sql = "UPDATE ".$dedi_db['lay']." SET name='$name', description='$description'        WHERE idlay='$idlay'";
                        $db->query($sql);
                }

                //rechte setzen
                if ($perm->have_perm('6', 'lay', $idlay)) {
                        global $dedi_gruppenids, $dedi_gruppenrechte;
                        $perm->set_group_rights( 'lay', $idlay, $dedi_gruppenids, $dedi_gruppenrechte, '', 0xF5);
                }

                // Event
                fire_event('lay_edit', array('idlay' => $idlay, 'name' => $name));
        }

        // welche CSS-Dateien werden benutzt?
        $sql = "SELECT B.idupl FROM $dedi_db[lay_upl] A LEFT JOIN $dedi_db[upl] B USING(idupl) LEFT JOIN $dedi_db[filetype] C ON B.idfiletype=C.idfiletype WHERE idlay='$idlay' AND C.filetype='css'";
        $db->query($sql);
        while ($db->next_record()) $tmp_files['css'][] = $db->f('idupl');
        if (!is_array($tmp_files['css'])) $tmp_files['css']['0']='0';
        if (!is_array($css)) $css['0'] = '0';

        // benutzte CSS-Dateien in lay_upl schreiben
        foreach ($css as $value)
        {
                if (!in_array($value,$tmp_files['css'])) {
                        if ($value != '0') {
                                $sql = "INSERT INTO $dedi_db[lay_upl] (idlay, idupl) VALUES ('$idlay', '$value')";
                                $db->query($sql);
                                $change = 'true';
                        }
                }
        }

        // unbenutze CSS-Dateien aus lay_upl löschen
        foreach ($tmp_files['css'] as $value)
        {
                if (!in_array($value,$css)) {
                        $sql = "DELETE FROM $dedi_db[lay_upl] WHERE idupl='$value' AND idlay='$idlay'";
                        $db->query($sql);
                        $change = 'true';
                }
        }

        // welche JS-Dateien werden benutzt?
        $sql = "SELECT B.idupl FROM $dedi_db[lay_upl] A LEFT JOIN $dedi_db[upl] B USING(idupl) LEFT JOIN $dedi_db[filetype] C ON B.idfiletype=C.idfiletype WHERE idlay='$idlay' AND C.filetype='js'";
        $db->query($sql);
        while ($db->next_record()) $tmp_files['js'][] = $db->f('idupl');
        if (!is_array($tmp_files['js'])) $tmp_files['js']['0']='0';
        if (!is_array($js)) $js['0'] = '0';

        // benutzte JS-Dateien in lay_upl schreiben
        foreach ($js as $value)
        {
                if (!in_array($value,$tmp_files['js'])) {
                        if ($value != '0') {
                                $sql = "INSERT INTO $dedi_db[lay_upl] (idlay, idupl) VALUES ('$idlay', '$value')";
                                $db->query($sql);
                                $change = 'true';
                        }
                }
        }

        // unbenutze JS-Dateien aus lay_upl löschen
        foreach ($tmp_files['js'] as $value)
        {
                if (!in_array($value,$js)) {
                        $sql = "DELETE FROM $dedi_db[lay_upl] WHERE idupl='$value' AND idlay='$idlay'";
                        $db->query($sql);
                        $change = 'true';
                }
        }

        if ($change) {
                // Status der 'code' Tabelle ändern
                $list = get_idtplconf_by_using_type($idlay, 'lay');
                $list = get_idcode_by_idtplconf($list);
                change_code_status($list, '1');
                unset($list);
        }
}


function lay_copy($idlay, $from, $into)
{
        global $db, $dedi_db, $auth, $perm;

        if (!$from) $from='0';
        if (!$into) $into='0';

        // Layout kopieren
        $sql = "SELECT * FROM $dedi_db[lay] WHERE idlay='$idlay'";
        $db->query($sql);
        if ($db->next_record()) {
                $name = make_string_dump($db->f('name'));
                $description = make_string_dump($db->f('description'));
                $code = make_string_dump($db->f('code'));

                $sql = "INSERT INTO
                                        ".$dedi_db['lay'] ."
                                        (name, description, code, idclient, author, created, lastmodified)
                                VALUES
                                        ('$name', '$description', '$code', '$into', '".$auth->auth['uid']."',
                                        '".time()."', '".time()."')";
                $db->query($sql);

                //set perms
                // get last insert id
                $sql = "SELECT MAX(idlay) AS idlay FROM ".$dedi_db['lay'];
                $db->query($sql);
                $db->next_record();
                $last_insert_id = $db->f('idlay');

                $perm->xcopy_perm($idlay, 'lay', $last_insert_id, 'lay', 0xFFFFFFFF, 0, 0, true);  // make new userright


                // Event
                if ($from != '0') fire_event('lay_export', array('idlay' => $idlay, 'name' => $name));
                else fire_event('lay_import', array('idlay' => $idlay, 'name' => $name));
                return '0302';
        }
}

function lay_delete_layout($idlay) {
        global $db, $client, $dedi_db, $perm;

        // Wird Layout noch verwendet?
        $sql = "SELECT * FROM ". $dedi_db['tpl'] ." WHERE idlay='$idlay'";
        $db->query($sql);
        if ($db->affected_rows()) return '0301';
        else {
                // Layout löschen
                $sql = "DELETE FROM ". $dedi_db['lay'] ." WHERE idlay='$idlay'";
                $db->query($sql);

                // Einträge aus lay_upl löschen
                $sql = "DELETE FROM ". $dedi_db['lay_upl'] ." WHERE idlay='$idlay'";
                $db->query($sql);

                //delete perm
                $perm->delete_perms($idlay, 'lay');

                // Event
                fire_event('lay_delete', array('idlay' => $idlay));
        }
}
?>