Location: PHPKode > projects > cPassMan > cpassman_1.55/sources/functions.queries.php
<?php
####################################################################################################
## File : views.queries.php
## Author : Nils Laumaillé
## Description : File contains queries for ajax
## 
## DON'T CHANGE !!!
## 
####################################################################################################

session_start();

include('../includes/language/'.$_SESSION['user_language'].'.php'); 
include('../includes/settings.php');
header("Content-type: text/html; charset=".$k['charset']); 

//Connect to mysql server
require_once("class.database.php"); 
$db = new Database($server, $user, $pass, $database, $pre);
$db->connect(); 

// Construction de la requĂȘte en fonction du type de valeur
if ( !empty($_POST['type']) ){
    switch($_POST['type'])
    {
        #CASE adding a new role
        case "add_new_function":
            $db->query("INSERT INTO ".$pre."functions SET title = '".mysql_real_escape_string(stripslashes(($_POST['name'])))."'");
            //Actualize the variable
            $_SESSION['nb_roles'] ++;
            //reload page
            echo 'document.form_fonctions.submit();';
        break;
        
        #-------------------------------------------
        #CASE delete a role
        case "delete_role":
            $db->query("DELETE FROM ".$pre."functions WHERE id = ".$_POST['id']);
            //Actualize the variable
            $_SESSION['nb_roles'] --;
            //reload page
            echo 'document.form_fonctions.submit();';
        break;
        
        #-------------------------------------------
        #CASE update allowed/forbidden groups for a Function
        case "groupes_visibles":
        case "groupes_interdits":
            $val = explode(';',$_POST['valeur']);
            $valeur = $_POST['valeur'];
            //Check if ID is already stored
            $data = $db->fetch_row("SELECT ".$_POST['type']." FROM ".$pre."functions WHERE id = ".$val[0]);
            $new_groupes = $data[0];
            if ( !empty($data[0]) ){
                $groupes = explode(';',$data[0]);
                if ( in_array($val[1],$groupes ) ) $new_groupes = str_replace($val[1],"",$new_groupes);
                else $new_groupes .= ";".$val[1];
            }else{
                $new_groupes = $val[1];
            }
            while ( substr_count($new_groupes,";;") > 0 ) 
                $new_groupes = str_replace(";;",";",$new_groupes);
            
            //Update DB
            $db->query_update(
                "functions",
                array(
                    $_POST['type'] => $new_groupes
                ),
                'id = '.$val[0]
            );
        break;
                
        #-------------------------------------------
        #CASE refresh the matrix
        case "rafraichir_matrice": 
            echo '$("#ajax_loader_matrix").show();';
            echo 'document.getElementById(\'matrice_droits\').innerHTML = "";';
            require_once ("NestedTree.class.php");
            $tree = new NestedTree($pre.'nested_tree', 'id', 'parent_id', 'title');
            $tst = $tree->getDescendants();
            $texte = '<table><thead><tr><th>'.$txt['group'].'s</th>';
            $gpes_ok = array();
            $gpes_nok = array();
            $tab_fonctions = array();
            $rows = $db->fetch_all_array("SELECT title,id,groupes_visibles,groupes_interdits FROM ".$pre."functions ORDER BY title ASC");
            foreach( $rows as $reccord ){
                $texte .= '<th style="font-size:10px;">'.$reccord['title'].'</th>';
                //Get all descendents groups
                $gpok = $reccord['groupes_visibles'];
                $gpnok = $reccord['groupes_interdits'];
                $tmp_ok = explode(';',$reccord['groupes_visibles']);
                $tmp_nok = explode(';',$reccord['groupes_interdits']);
                foreach($tmp_ok as $t){
                    if ( !empty($t) ){
                        $desc = $tree->getDescendants($t);
                        foreach($desc as $d)
                            $gpok .= ';'.$d->id;
                    }
                }
                foreach($tmp_nok as $t){
                    if ( !empty($t) ){
                        $desc = $tree->getDescendants($t);
                        foreach($desc as $d)
                            $gpnok .= ';'.$d->id;
                    }
                }
                //save into array
                $tab_fonctions[$reccord['id']] = array(
                    "ok" => $gpok,
                    "nok" => $gpnok,
                    "id" => $reccord['id'],
                    "titre" => $reccord['title']
                );
            }
            $texte .= '</tr></thead><tbody>';
            //construire tableau des groupes
            $tab_groupes = array();
            foreach($tst as $t){
                if ( in_array($t->id,$_SESSION['groupes_visibles']) ) {
                    $ident="";
                    for($a=1;$a<$t->nlevel;$a++) $ident .= "&nbsp;&nbsp;";
                    $tab_groupes[$t->id] = array(
                            'id' => $t->id,
                            'titre' => $t->title,
                            'ident' => $ident
                            );
                }
            } 
            
            //afficher
            foreach ($tab_groupes as $groupe){
                $visibilite = "";
                $texte .= '<tr><td style="font-size:10px; font-family:arial;">'.$groupe['ident'].$groupe['titre'].'</td>';
                foreach ($tab_fonctions as $fonction){  
                    if ( !empty($fonction) ){          
                        if ( !empty($fonction['ok']) ) $gpes_ok = explode(';',$fonction['ok']);else $gpes_ok = array();
                        if ( !empty($fonction['nok']) ) $gpes_nok = explode(';',$fonction['nok']);else $gpes_nok = array();
                        if ( in_array($groupe['id'],$gpes_ok) ) $couleur = '#008000';
                        else $couleur = '#FF0000';
                        if ( count($gpes_nok)>0 && in_array($groupe['id'],$gpes_nok) ) $couleur = '#FF0000';
                        $texte .= '<td align="center" style="background-color:'.$couleur.'"></td>';
                        if ( $couleur != '#FF0000') {
                            if ( empty($visibilite) ) $visibilite =  $fonction['id'];
                            else $visibilite .= ";".$fonction['id'];
                        }
                    }
                }
                $texte .= '</tr>';
                
                //Store in DB
                $data = $db->fetch_row("SELECT COUNT(*) FROM ".$pre."misc WHERE type='visibilite' AND intitule = '".$groupe['id']."'");
                if ( $data[0] == 0 ){
                    $db->query_insert(
                        'misc',
                        array(
                            'type' => 'visibilite',
                            'intitule' => $groupe['id'],
                            'valeur' => $visibilite
                        )
                    );
                }else{
                    $db->query_update(
                        'misc',
                        array(
                            'valeur' => $visibilite
                        ),
                        "type='visibilite' AND intitule = '".$groupe['id']."'"
                    );
                }
                
            }
            $texte .= '</tbody></table>';
            echo 'document.getElementById(\'matrice_droits\').innerHTML = "'.addslashes($texte).'";';
            echo '$("#ajax_loader_matrix").hide();';
        break;
        
        #-------------------------------------------
        #CASE display the div for allowed groups
        case "open_div_autgroups";
            $text = "";
            
            //get list of authorized/forbidden groups for this Function
            $data_group = $db->fetch_row("SELECT groupes_visibles FROM ".$pre."functions WHERE id = ".$_POST['id']);
            $autgroups = explode(';',$data_group[0]);
            
            //Refresh list of existing groups
            require_once ("NestedTree.class.php");
            $tree = new NestedTree($pre.'nested_tree', 'id', 'parent_id', 'title');
            $descendants = $tree->getDescendants();
            
            foreach($descendants as $t){
                if ( !@in_array($t->id,$_SESSION['groupes_interdits']) && @in_array($t->id,$_SESSION['groupes_visibles']) ){
                    $ident="";
                    for($y=1;$y<$t->nlevel;$y++) $ident .= "&nbsp;&nbsp;";
                    
                    $text .= '<input type=\"checkbox\" id=\"cb_change_group-'.$t->id.'\"';
                    if ( in_array($t->id,$autgroups) )  $text .= ' checked';
                    $text .= '>'.$ident.addslashes($t->title).'<br />';
                         
                    $prev_level = $t->nlevel;
                }
            }
            
            echo 'document.getElementById("change_group_autgroups_list").innerHTML = "'.$text.'";';
            echo 'document.getElementById("selected_function").value = "'.$_POST['id'].'";';
                        
            //display dialogbox
            echo '$("#change_group_autgroups").dialog("open");';
            echo '$("#div_loading").hide()';  //hide loading div
        break;
        
        #-------------------------------------------
        #CASE change the allowed groups
        case "change_function_autgroups";
            //save data
            $db->query_update(
                'functions',
                array(
                    'groupes_visibles' => $_POST['list']
                ),
                "id = ".$_POST['id']
            );
                                          
            //display information
            $text = "";
            $val = str_replace(';',',',$_POST['list']);
            $rows = $db->fetch_all_array("SELECT title FROM ".$pre."nested_tree WHERE id IN (".$val.")");
            foreach( $rows as $reccord ){
                $text .= '<img src=\"includes/images/arrow-000-small.png\" />'.$reccord['title']."<br />";
            }
             echo 'document.getElementById("list_autgroups_function_'.$_POST['id'].'").innerHTML = "'.$text.'";';
            //refresh matrix
            echo 'refresh_matrice();';
        break;
        
        #-------------------------------------------
        #CASE display the list of forbidden groups
        case "open_div_forgroups";
            $text = "";            
            
            //get list of forbidden groups for this Function
            $data_group = $db->fetch_row("SELECT groupes_interdits FROM ".$pre."functions WHERE id = ".$_POST['id']);
            $autgroups = explode(';',$data_group[0]);
                        
            //Refresh list of existing groups
            require_once ("NestedTree.class.php");
            $tree = new NestedTree($pre.'nested_tree', 'id', 'parent_id', 'title');
            $descendants = $tree->getDescendants();
            
            foreach($descendants as $t){
                if ( !in_array($t->id,$_SESSION['groupes_interdits']) && in_array($t->id,$_SESSION['groupes_visibles']) ){
                    $ident="";
                    for($y=1;$y<$t->nlevel;$y++) $ident .= "&nbsp;&nbsp;";
                    
                    $text .= '<input type=\"checkbox\" id=\"cb_change_group-'.$t->id.'\"';
                    if ( in_array($t->id,$autgroups) )  $text .= ' checked';
                    $text .= '>'.$ident.addslashes($t->title).'<br />';
                         
                    $prev_level = $t->nlevel;
                }
            }
            
            echo 'document.getElementById("change_group_forgroups_list").innerHTML = "'.$text.'";';
            echo 'document.getElementById("selected_function").value = "'.$_POST['id'].'";';
                        
            //display dialogbox
            echo '$("#change_group_forgroups").dialog("open");';
            echo '$("#div_loading").hide()';  //hide loading div
        break;
        
        #-------------------------------------------
        #CASE change the forbidden groups
        case "change_function_forgroups";
            //save data
            $db->query_update(
                'functions',
                array(
                    'groupes_interdits' => $_POST['list']
                ),
                "id = ".$_POST['id']
            );
                        
            //display information
            $text = "";
            $val = str_replace(';',',',$_POST['list']);
            if ( !empty($val) ){
                $rows = $db->fetch_all_array("SELECT title FROM ".$pre."nested_tree WHERE id IN (".$val.")");
                foreach( $rows as $reccord ){
                    $text .= '<img src=\"includes/images/arrow-000-small.png\" />'.$reccord['title']."<br />";
                }
            }
             echo 'document.getElementById("list_forgroups_function_'.$_POST['id'].'").innerHTML = "'.$text.'";';
             echo '$("#div_loading").hide()';  //hide loading div
        break;
    }
}else if ( !empty($_POST['edit_fonction']) ){
    $id = explode('_',$_POST['id']);
    //Update DB
    $db->query_update(
        'functions',
        array(
            'title' => mysql_real_escape_string(stripslashes(utf8_decode($_POST['edit_fonction'])))
        ),
        "id = ".$id[1]
    );
    //Show value
    echo $_POST['edit_fonction'];
}
?>
Return current item: cPassMan