<?php

// datasource_users.php - PHProjekt Version 5.0
// copyright  ©  2000-2005 Albrecht Guenther  ag@phprojekt.com
// www.phprojekt.com
// Authors: Martin Brotzeller, $Author: paolo $
// $Id: datasource_users.php,v 1.17 2005/06/17 17:38:27 paolo Exp $

/** datasource_users.php
*
* Datenquelle für Anzeige von Benutzern
*
*
* @copyright (c) 2004 Mayflower GmbH
* @author Martin Brotzeller
* @package PHProjekt
*/
// check whether the lib has been included - authentication!
if (!defined('lib_included')) die('Not to be called directly!');


include_once($GLOBALS['lib_path']."/selector/selector.inc.php");


/** fetch_fields() - Funktion zum Anfragen der Datenquelle
*
* @param options Array mit optionen für die Datenquelle
*                'table'     - Tabelle die angefragt wird
*                'where'     - Array mit Zusatzbedingungen
*                'order'     - Komma-separierte Liste von Spalten nach denen sortiert werden soll
*                'direction' - Sortier-Richtung
*                'ID'        - Name der ID-Spalte
*                'display'   - Array der anzuzeigenden Felder
*                'dstring'   - Format-String für angezeigte Felder
*                'filter'    - Array mit Daten für die Anzeige und das Setzen der Filter
*                'limit'     - Limit für Anzeige
* @access public
*/
function usersfetch_fields($options){

    // default values if options fields are not set
    $maxdisp=(isset($options['limit']))?$options['limit']:PHPR_FILTER_MAXHITS;
    if(!isset($options['table'])) $options['table']='users';
    if(is_array($options['table'])) $options['table']=implode(", ".DB_PREFIX,$options['table']);
    $where=implode(" AND ",$options['where']);
    if($where=="") $where="1";
    if(!isset($options['order'])){
        $options['order']="nachname";
    }
    $order="ORDER BY ".$options['order']." ".$options['direction'];
    if(!isset($options['ID'])) $options['ID']="loginname";
    if(!isset($options['display'])) $options['display']=array("vorname","nachname");
    if(!isset($options['dstring'])){
        $options['dstring']="%s";
        for($i=1;$i<count($options['display']);$i++) $options['dstring'].=" %s";
    }

    // count hits and compare with limit
    $query = "SELECT COUNT(DISTINCT ".$options['ID'].")
                FROM ".DB_PREFIX.$options['table']."
               WHERE $where
             ".$order;
    //echo $query;
    $result=db_query($query) or db_die();

    $row=db_fetch_row($result);
    if($maxdisp>0 && $row[0]>$maxdisp) return $row[0];

    // collect data and return search hits
    $fields = $options['ID'].",".implode(",",$options['display']);
    $query = "SELECT DISTINCT $fields
                FROM ".DB_PREFIX.$options['table']."
               WHERE $where
                     $order";
    $result = db_query($query) or db_die();

    while($row=db_fetch_row($result)){
        $hit="\$hits[$row[0]]=sprintf('".$options['dstring'];
        for($i=1;$i<=count($options['display']);$i++) $hit.="','".$row[$i];
        $hit.="');\n";
        eval($hit);
    }
    return $hits;
}


/** Anzeige der Filter
*
* @param $options    - optionen wie bei fetch_fields
* @param $object     - Das Selektor-Objekt und die Ausgewählten Kontakte, serialisiert
* @param $name       - Name des Aktuellen Filterobjekts
* @access public
*/
function usersdisplay_filters1($options,$object,$name){
    global $path_pre,$filters,$usersextras;

    $_SESSION['filters'] =& $filters;
    $sarr=&$filters[$name];

    $fform="\n";
    $fform.="<input type='hidden' name='sthis' value='$object' />\n";
    $fform.="<input type='hidden' name='filterform' value='done' />\n";
    $fform.="<table border='0' class='selector_head'>\n<tr>\n";
    if(!empty($options['filter']['text']) || !empty($options['filter']['alternative'])){
        $fform.="<td class='selector_head'><table border='0' class='selector_filter'><tr>\n";
        $fform.="<td colspan='3' class='selector_filter'><b><u>".__("set filter")."</u></b></td></tr><tr>\n";
    }
    if(!empty($options['filter']['text'])){
        $fform.="<td class='selector_filter'><select class='selector_filter' name='textfilter' tabindex='10'>\n";
        foreach($options['filter']['text'] as $key => $value){
            $fform.="<option value='$key'>$value</option>\n";
        }
        $fform.="</select>\n";
        $fform.="</td><td class='selector_filter'><select name='textfiltermode' tabindex='11'>\n";
        $fform.="<option value='begins with'>".__('starts with')."</option>\n";
        $fform.="<option value='contains' selected='selected'>".__('contains')."</option>\n";
        $fform.="<option value='ends with'>".__('ends with')."</option>\n";
        $fform.="<option value='is equal'>".__('exact')."</option>\n";
        $fform.="</select>\n";
        $fform.="</td>\n<td class='selector_filter'><input type='text' name='textfilterstring' size='40' maxlength='20' tabindex='12' style='width:200px;' /></td>\n";
    }

    if(!empty($options['filter']['alternative'])){
        foreach($options['filter']['alternative'] as $key => $value){
            $fform.="<td>&nbsp;|&nbsp;</td>\n<td>";
            $fform.=$value['name'].": ";
            foreach($value['option'] as $k => $v){
                $fform.="<input type='radio' name='altfilter$key' value='$k' />$v ";
            }
            $fform.="</td>\n";
        }
    }
    if(!empty($options['filter']['text']) || !empty($options['filter']['alternative'])){
        $fform.="</tr></table>\n</td>\n";
    } else {
        $fform.="<td></td>\n";
    }
    $fform .= "<td rowspan='2' class='selector_head_submit_cell'>";
    //$fform .= "<input type=image src='$path_pre"."img/los.gif' name='filterset' value='set' tabindex='19' /></td>";
    //$fform .= "<input name='filterset' type='submit' class='submit' value='".__('go')."' /></td>\n";
    $fform .= get_go_button_with_name('filterset')."</td>\n";
    $fform .= "</tr>\n<tr><td class='selector_head'>\n";
    $fform .= "<table border=0 class='selector_filter'>\n";
    if(!empty($usersextras)) {
        $fform.="<tr><td class='selector_filter' colspan=3><b><u>".__("quickadd")."</u></b></td></tr>\n";
    }
    foreach($usersextras as $key=>$val){
        //error_log("display filters extras $key w/ options ".var_export($options['extra'],true));
        //error_log($val['getform']);
        $fform.=$val['getform']($options['extra'][$key]);
    }
    $fform.="</table></td>";
    $fform.="</tr></table>\n";
    if(!empty($sarr)){
        $ti=20;
        foreach($sarr  as $k => $v){
            $ti++;
            $fform.= "<input type='submit' name='filterdel' value=\"$k\" tabindex='$ti' />".str_replace("%","",$v)." | ";
        }
    }
    echo "<br />\n";
    echo $fform;
}


/** parsen des angezeigten Filters und ablegen der Filter in der Session
*
* @param  $object    - Selektor-Objekt in das die filter als Where-Bedingung abgelegt wird
* @access
*/
function usersparse_filters(&$object){
    global $textfilter,$textfilterstring,$textfiltermode,$filters,$filterform;

    if($filterform=="done"){
        $_SESSION['filters'] =& $filters;

        $sarr=&$filters[$object->name];
        if(isset($textfilterstring) && $textfilterstring!=""){
            $c="$textfilter ";
            switch($textfiltermode){
                case 'contains':
                $c.="like '%$textfilterstring%'";
                break;
                case 'begins with':
                $c.="like '$textfilterstring%'";
                break;
                case 'ends with':
                $c.="like '%$textfilterstring'";
                break;
            }
            $sarr[]=$c;
        }
    }
}


/** zeigt Eingabe und Selectbox für Profile
*
* @return string
* @access public
*/
function usersextra_profiles($extopt){
    global $user_ID;
    $ret="<tr><td class='selector_filter'>".__('add profile').":</td>
              <td class='selector_filter'><!-- <input type='text' name='usersextra_profileglob' /> --></td>
              <td class='selector_filter'><select class='selector_filter' name='usersextra_profile' onchange='selectme();submit();' style='width:200px'><option selected='selected'></option>\n";
    $query = "SELECT ID, bezeichnung
                FROM ".DB_PREFIX."profile
               WHERE von = '$user_ID'";
    $res = db_query($query) or db_die();
    while($row=db_fetch_row($res)){
        $ret.="<option value='".$row[0]."'>".$row[1]."</option>\n";
    }
    $ret.="</select></td></tr>";

    return $ret;
}


/** Wertet die Selectbox extra_profiles aus
*
* @param $options array der selektorklasse, benötigt wird 'save'
* @access public
*/
function userseval_extra_profiles($options){
    global $usersextra_profile,$usersextra_profileglob,$user_ID,$selektor_answer;
    $addprof=array();
    if(isset($usersextra_profile) && $usersextra_profile!=0){
        $profile=unserialize(slookup('profile','personen','ID',$usersextra_profile));
        foreach($profile as $k => $v){
            $addprof[$v]=$v;
        }
        $selektor_answer = __('Added profile')."<br /><br />\n";
    }
    if(isset($usersextra_profileglob) && $usersextra_profileglob!=""){
        $query = "SELECT p.ID, p.bezeichnung, p.personen
                    FROM ".DB_PREFIX."profile AS p
                   WHERE p.bezeichnung LIKE '".str_replace("%","\%",quote_runtime($usersextra_profileglob))."%'
                     AND p.von = '$user_ID'";
        $res = db_query($query) or db_die();
        while($row=db_fetch_row($res)){
            $profiles[$row[0]]=$row[1];
            foreach(unserialize($row[2]) as $val) $addprof[$val]=$val;
        }
        // $subjcnt=(count($profiles)>1)?"e":"";
        // $selektor_answer="Profil$subjcnt '".implode("' '",$profiles)."' hinzugefügt";
        if(empty($addprof)) $selektor_answer = __('No profile found')."<br /><br />\n"; // "kein Profil gefunden";

    }
    return $addprof;
}


/** zeigt Eingabe und Selectbox für Projekte
*
* @return string
* @access public
*/
function usersextra_projects($extopt){
    global $user_ID,$user_kurz,$sql_user_group;
    $ret="<tr><td>".__('add project participants').":</td><td align='right'><input type='text' name='usersextra_projectglob' /></td>";
    $ret.="<td align='right'><select name='usersextra_project' onChange='selectme();submit();' style='width:200px'><option selected='selected'></option>\n";
    $query="SELECT ID,name FROM ".DB_PREFIX."projekte WHERE
        (acc like 'system' or ((von = '$user_ID' or acc like 'group' or acc like '%\"$user_kurz\"%') and $sql_user_group))";
    $res=db_query($query) or db_die();
    while($row=db_fetch_row($res)){
        $ret.="<option value='".$row[0]."'>".$row[1]."</option>\n";
    }
    $ret.="</select></td>\n";
    if(!empty($extopt)){
        $ret.="<td><input type='submit' name='usersextra_projectbutton' value='".__('add project participants')."' /></td>\n";
    }
    $ret.="</tr>\n";

    return $ret;
}

/** Wertet die Selectbox extra_project aus
*
* @param $options array der selektorklasse, benötigt wird 'save'
* @access public
*/
function userseval_extra_projects($options){
    global $usersextra_project,$usersextra_projectglob,$usersextra_projectbutton,
    $selektor_answer,$user_ID,$user_kurz,$sql_user_group;
    $addproj=array();
    if(isset($usersextra_project) && $usersextra_project!=0){
        $projects=unserialize(slookup('projekte','personen','ID',$usersextra_project));
        foreach($projects as $k => $v){
            $addproj[$v]=$v;
        }
        $selektor_answer = __('Added project participants')."<br /><br />\n"; //"Projektteilnehmer hinzugefügt";
    }
    if(isset($usersextra_projectglob) && $usersextra_projectglob!=""){
        $query="SELECT ID,name,personen FROM ".DB_PREFIX."projekte
            WHERE name like '".str_replace("%","\%",addslashes($usersextra_projectglob))."%'
            AND  (acc like 'system' or ((von = '$user_ID' or acc like 'group' or acc like '%\"$user_kurz\"%') and $sql_user_group))";
        $res=db_query($query) or db_die();
        while($row=db_fetch_row($res)){
            $projekt[$row[0]]=$row[1];
            foreach(unserialize($row[2]) as $val) $addproj[$val]=$val;
        }
        $selektor_answer="Projekt$subjcnt '".implode("' '",$projekt)."' (".implode(' ',$addproj).") hinzugefügt";
    }
    if(isset($usersextra_projectbutton) && $usersextra_projectbutton!=""){
        $query="SELECT ID,name,personen FROM ".DB_PREFIX."projekte
            WHERE ID='".$options['extra']['projects']."'
            AND (acc like 'system' or ((von = '$user_ID' or acc like 'group' or acc like '%\"$user_kurz\"%') and $sql_user_group))";
        $res=db_query($query) or db_die();
        while($row=db_fetch_row($res)){
            $projekt[$row[0]]=$row[1];
            foreach(unserialize($row[2]) as $val) $addproj[$val]=$val;
        }
        //$selektor_answer="Projekt$subjcnt '".implode("' '",$projekt)."' hinzugefügt";
    }
    return $addproj;
}


/** zeigt Eingabe und Selectbox für Gruppen
*
* @return string
* @access public
*/
function usersextra_groups(){
    global $user_ID,$user_kurz,$sql_user_group;

    // are there group-restrictions in effect?
    $additional_where = "";
    if (PHPR_ACCESS_GROUPS != 2) { $additional_where = " WHERE ID IN ('".implode("','", selector_get_groupIds())."')"; }

    $ret="<tr><td class='selector_filter'>".__('add group of participants').":</td>
              <td class='selector_filter'></td>
              <td class='selector_filter'><select class='selector_filter' name='usersextra_group' onChange='selectme();submit();' style='width:200px'>
              <option selected='selected'></option>\n";
    $query="SELECT ID,name FROM ".DB_PREFIX."gruppen".$additional_where;
    $res=db_query($query) or db_die();
    while($row=db_fetch_row($res)){
        $ret.="<option value='".$row[0]."'>".$row[1]."</option>\n";
    }
    $ret.="</select></td></tr>\n";

    return $ret;
}

/** Wertet die Selectbox extra_group aus
*
* @param $options array der selektorklasse, benötigt wird 'save'
* @access public
*/
function userseval_extra_groups($options){
    global $usersextra_group,$selektor_answer;
    $addprof=array();
    if(isset($usersextra_group) && $usersextra_group!=0){
        // group-restrictions in effect?
        $additional_where = "";
        if (PHPR_ACCESS_GROUPS != 2) { $additional_where = " AND grup_ID IN ('".implode("','", selector_get_groupIds())."')"; }
//        $query="SELECT u.ID FROM ".DB_PREFIX."grup_user as g, ".DB_PREFIX."users as u
//            WHERE u.ID=g.user_ID AND g.grup_ID='$usersextra_group'";
        $query="SELECT u.ID FROM ".DB_PREFIX."grup_user as g, ".DB_PREFIX."users as u
                 WHERE u.ID=g.user_ID
                   AND g.grup_ID='$usersextra_group' ".$additional_where;
        $res=db_query($query) or db_die();
        while($row=db_fetch_row($res)){
            $addproj[$row[0]]=$row[0];
        }
        $selektor_answer = __('Added group of participants')."<br /><br />\n";
    }

    return $addproj;
}

/** zeigt Eingabe und Selectbox für Schnelle Namenssuche
*
* @return string
* @access public
*/
function usersextra_names(){
    $ret  = "<tr><td>".__('add user').":</td><td >Vorname:</td>\n";
    $ret .= "<td>Nachname:</td></tr>\n";
    $ret .= "<tr><td> </td>\n<td align='right'><input type='text' name='usersextra_namevor' /></td>\n";
    $ret .= "<td><input type='text' name='usersextra_namenach' /></td>\n";
    $ret .= "<td></td></tr>\n";

    return $ret;
}

/** Wertet die Selectbox extra_name aus
*
* @param $options array der selektorklasse, benötigt wird 'save'
* @access public
*/
function userseval_extra_names($options){
    global $usersextra_namevor,$usersextra_namenach,$selektor_answer;
    $addproj=array();
    if((isset($usersextra_namevor)||isset($usersextra_namenach)) && $usersextra_namevor.$usersextra_namenach!=""){
        // group-restrictions in effect?
        $additional_where = "";
        if (PHPR_ACCESS_GROUPS != 2) { $additional_where = " AND grup_ID IN ('".implode("','", selector_get_groupIds())."')"; }
//        $query="SELECT DISTINCT u.ID, u.vorname, u.nachname from ".DB_PREFIX."users u
//            WHERE vorname  like '".str_replace("%","\%",quote_runtime($usersextra_namevor))."%'
//              AND nachname like '".str_replace("%","\%",quote_runtime($usersextra_namenach))."%'
        $query="SELECT DISTINCT u.ID, u.vorname, u.nachname from ".DB_PREFIX."users u
             LEFT JOIN ".DB_PREFIX."grup_user gu ON u.ID=gu.user_ID
            WHERE vorname  like '".str_replace("%","\%",quote_runtime($usersextra_namevor))."%'
              AND nachname like '".str_replace("%","\%",quote_runtime($usersextra_namenach))."%'
              $additional_where ";
        $res=db_query($query) or db_die();
        while($row=db_fetch_row($res)){
            $addproj[$row[0]]=$row[0];
            $ansnam[]=$row[1]." ".$row[2];
        }
        //$selektor_answer=implode(", ",$ansnam)." hinzugefügt";
        //$selektor_answer=count($ansnam)." Namen hinzugefügt";
        $selektor_answer = count($ansnam)." ".__('Added user')."<br /><br />\n";
    }

    return $addproj;
}


$usersextras=array(
'names'=>
array('getform'=>'usersextra_names',
'evalform' =>'userseval_extra_names',
'formname' =>array('usersextra_namevor','usersextra_namenach')),
'profiles'=>
array('getform'=>'usersextra_profiles',
'evalform' =>'userseval_extra_profiles',
'formname' =>array('usersextra_profile','usersextra_profileglob')),
'projects'=>
array('getform'=>'usersextra_projects',
'evalform' =>'userseval_extra_projects',
'formname' =>array('usersextra_project','usersextra_projectglob','usersextra_projectbutton')),
'groups'=>
array('getform'=>'usersextra_groups',
'evalform' =>'userseval_extra_groups',
'formname' =>array('usersextra_group'))
);


?>
