<?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 group_set_active($is_active) {
        global $db, $dedi_db, $idgroup;

        $sql = "UPDATE ".$dedi_db['groups']." SET is_active='$is_active' WHERE idgroup='$idgroup'";
        $db->query($sql);
}

function group_delete() {
        global $db, $dedi_db, $idgroup, $perm;

        // Systemadmin kann nicht gelöscht werden
        if ($idgroup >= 3) {
                $sql = "DELETE FROM ".$dedi_db['groups']." WHERE idgroup='$idgroup'";
                $db->query($sql);
                $perm->delete_perms_by_group($idgroup, '-1');
                $sql = "UPDATE ".$dedi_db['users_groups']." SET idgroup='1' WHERE idgroup='$idgroup'";
                $db->query($sql);
        }
}

function group_save() {
        global $dedi_db, $db, $idgroup, $name, $description, $oldname;

        // Kein Gruppenname
        if (trim($name) == '') return 'group_noname';

        // keine Sonderzeichen in Gruppenname
        if (!eregi("[0-9a-zA-Z]", $name)) return 'group_incorrectcharacter';

        // Username auf Existenz prüfen
        if ($name != $oldname) {
                $sql = "SELECT idgroup, name FROM ".$dedi_db['groups']." WHERE name='$name' LIMIT 0, 1";
                $db->query($sql);
                if ($db->next_record()){
                        if($db->f('idgroup') != $idgroup )
                                return 'group_existname';
                }
        }
        if ($idgroup != '') $sql = "UPDATE ".$dedi_db['groups']." SET name='$name', description='$description' WHERE idgroup ='$idgroup'";
        else $sql = "INSERT INTO ". $dedi_db['groups'] ." VALUES ('', '$name', '$description', '0', '1', '1')";
        $db->query($sql);
}

// jb_todo: workflow check, wegen löschen der rechte
function group_visible_lang() {
        global $dedi_db, $db, $idgroup, $idlang, $perm;

        // Gruppe suchen
        $sql = "SELECT idgroup FROM ".$dedi_db['perms']." WHERE idgroup='$idgroup' AND type = 'lang' AND id='$idlang' LIMIT 0, 1";
        $db->query($sql);

        // Rechte löschen/eintragen
        if ($db->affected_rows()) {
                $perm->delete_perms_by_group($idgroup, $idlang);
                $perm->delete_perms($idlang, 'lang', $idgroup);
        } else {
                $perm->new_perm( $idgroup, 'lang', $idlang, 1, 0);
        }
}

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

        foreach (array_keys($reindex) as $kat) {
                ksort($ssort[$kat]);
                $index = 0;
                foreach ($ssort[$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');
}

//
// Funktionen zur Verwaltung von Gruppenrechten
//

function group_save_perms() {
        global $perm, $dedi_perm_val, $db, $dedi_db, $idgroup, $idlang, $val_ct, $deb;

        $array      = $dedi_perm_val['dedi_access'];

        // zuerst dedi_access und bereiche speichern
        $sql_array = array();
        foreach ($array as $key => $value) {
                // für die übermittlung durch den browser muss der Punkt in der Versionsnummer durch einen unterstrich ersetzt werden
                // dies ist bei der ermittlung des neuen perm-wertes zu berücksichtigen ... die eintragung selbst erfolgt mit dem Punkt
                // und wird bei der Abfrage von Rechte auch mit der Punktnotation durchgeführt
                $perm_area = str_replace('.', '_', $key);
                $perm_val = &$GLOBALS['dedi_access_'.$perm_area];
                $sql = "SELECT idperm FROM ". $dedi_db['perms'] ." WHERE idgroup = $idgroup AND idlang = $idlang AND type = 'dedi_access' AND id = '$key'";
                $db->query($sql);
                $idperm = ($db->next_record()) ? $db->f("idperm"): '';
                if (!empty($idperm)) {
                        _update_or_delete_perm( $perm_val,  $idperm, $sql_array );
                } else {
                        _insert_perm( $perm_val, $idgroup, $idlang, 'dedi_access', $key, $sql_array );
                }
                // Perms der Bereiche speichern
                $perm_val = 0;
                if (isset($dedi_perm_val[$key])) {
                        foreach($dedi_perm_val[$key] AS $key2 => $value2) {
                                $perm_val |= $GLOBALS[$perm_area."_".$key2];
                        }
                }
                $sql = "SELECT idperm FROM ". $dedi_db['perms'] ." WHERE idgroup = $idgroup AND idlang = $idlang AND type = '$key' AND id = '0'";
                $db->query($sql);
                $idperm = ($db->next_record()) ? $db->f("idperm"): '';
                if (!empty($idperm)) {
                        _update_or_delete_perm( $perm_val,  $idperm, $sql_array );
                } else {
                        _insert_perm( $perm_val, $idgroup, $idlang, $key, '0', $sql_array );
                }
        }
        // führe alle sqls gesammelt durch
        _do_sql_queries($sql_array);
}

// helper function
function _do_sql_queries(&$sql_array ) {
        global $db;
        $max = count($sql_array);
        for($i = 0; $i < $max; $i++) {
                $db->query($sql_array[$i]);
        }
}

function _insert_perm( $perm, $idgroup, $idlang, $type, $key, &$sql_array ) {
        global $dedi_db;
        if ($perm != 0) {
                $sql_array[] = "INSERT INTO ". $dedi_db['perms'] ." VALUES('', '$idgroup', '$idlang',  '$type', '$key', '$perm')";
        }
}

function _update_or_delete_perm( $perm, $idperm, &$sql_array ) {
        global $dedi_db;
        if ($perm != 0) {
                // Rechte aktualisieren
                $sql_array[] = "UPDATE ". $dedi_db['perms'] ." SET perm = '$perm' WHERE idperm = " . $idperm;
        } else {
                // Rechte löschen
                $sql_array[] = 'DELETE FROM ' . $dedi_db['perms'] . ' WHERE idperm = ' . $idperm;
        }
}


// function create_area_checkbox($area, $name, $lang_name) {
function create_area_checkbox($area, $name, $lang_name, $plugin) {
        global $dedi_perm_val, $tmp, $dedi_lang, $perm, $sim_perm, $tpl;

        unset($tmp);

        $checkbox_cell = "<td width=\"14\" nowrap align=\"center\" onMouseOver=\"on('%s');return true;\" onMouseOut=\"off();return true;\" title=\"%s\"><input type=\"checkbox\" name=\"%s_%s\" id=\"%s_%s\" value=\"%s\" %s></td>";
        $box_off       = "<td class=\"content6\" width=\"14\" nowrap align=\"center\" onMouseOver=\"on('%s');return true;\" onMouseOut=\"off();return true;\" title=\"%s\"><input type=\"radio\" name=\"dedi_access_%s\" value=\"%s\" %s onclick=\"dedi_rm.set_area('%s', %s, %s)\"></td>";
        $box_on        = "<td width=\"14\" nowrap align=\"center\" onMouseOver=\"on('%s');return true;\" onMouseOut=\"off();return true;\" title=\"%s\"><input type=\"radio\" name=\"dedi_access_%s\" value=\"%s\" %s onclick=\"dedi_rm.set_area('%s', %s, %s)\"></td>";

        $i   = 0;
        $max = 0;
        // für die übermittlung durch den browser muss der Punkt in der Versionsnummer durch einen unterstrich ersetzt werden
        // dies ist bei der ermittlung des neuen perm-wertes zu berücksichtigen ... die eintragung selbst erfolgt mit dem Punkt
        // und wird bei der Abfrage von Rechte auch mit der Punktnotation durchgeführt
        $area_name  = ($plugin) ? str_replace('.', '_', $area): $area;
        if (!empty($dedi_perm_val[$area])) {
                $tpl->setCurrentBlock('MAIN_RIGHTS_ROW');
                $max = count($dedi_perm_val[$area]);
                if (isset($max) && $max > 0) {
                        foreach($dedi_perm_val[$area] AS $key2 => $value) {
                                $tooltip    = $dedi_lang[$lang_name.'_'.$key2];
                                $checkvalue = $dedi_perm_val[$area][$key2];
                                $checked    = $sim_perm->get_checkbox_status($area, '0', 9, $checkvalue);
                                $tmp['PERM_LABEL'] = $tooltip;
                                $tmp['PERM_BUTTON'] = sprintf($checkbox_cell, $tooltip, $tooltip, $area_name, $key2, $area_name, $key2, $checkvalue, $checked);
                                $tpl->setVariable($tmp);
                                $tpl->parseCurrentBlock();
                                unset($tmp);
                        }
                }
        }

        $tpl->setCurrentBlock('MAIN_RIGHTS');

        $area_on_checked  = $sim_perm->get_checkbox_status('dedi_access', $area, 8, '1');
        $area_off_checked = ($area_on_checked == '') ? ' checked ': '';
        $tooltip = $dedi_lang['group_access_area_granted'];
        $tmp['PERM_AREA_ON']  = sprintf($box_on, $tooltip, $tooltip, $area_name,  '1', $area_on_checked, $area_name, $max, 'true');
        $tooltip = $dedi_lang['group_access_area_denied'];
        $tmp['PERM_AREA_OFF'] = sprintf($box_off, $tooltip, $tooltip, $area_name, '-1', $area_off_checked, $area_name, $max, 'false');
        $tmp['PERM_AREA']      = $name;
        $tpl->setVariable($tmp);
        $tpl->parseCurrentBlock();
        unset($tmp);

}

?>