Code

Big commit for opengroupware account
[gosa.git] / include / class_pgsql_opengw.inc
1 <?php
3 class pgre_sql{
5   var $handle;
6   var $query_log;
7   var $user;
8   var $server;
9   var $db;
10   var $pwd;
11   var $is_connected = false;
13   function pgre_sql($user,$pwd,$server,$db)
14   {
15     $this->user   = $user;
16     $this->pwd  = $pwd;
17     $this->server = $server;
18     $this->db   = $db;
20     if($this->_connect()){
21       $this->is_connected = true;
22     }else{
23       $this->is_connected = false;
24         }
25   }
27   function _connect()
28   {
29         error_reporting(E_ALL);
30         if(is_callable("pg_connect")){
31                 $this->handle = @pg_connect("dbname=".$this->db." host=".$this->server." user=".$this->user);
32                 if(!$this->handle){
33                         $this->handle = false;
34                 }
35         }else{
36                 $this->handle = false;
37         }
38         return($this->handle);
39   }
41   function Query($a_query)
42   {
43     if(is_array($a_query)){
44       foreach($a_query as $nr => $query){
45         return($this->_query($query));
46       }
47     }else{
48       return($this->_query($a_query));
49     }
50   }
51         
52   function _query($query)
53   {
54     return(pg_query($this->handle,$query));
55   }
57   function FetchAllRows($res)
58   {
59     return(pg_fetch_all($res))  ;
60   }
62   function gen_id()
63   {
64           $tmp = $this->_query("select nextval('key_generator');");
65           $tmp = ($this->FetchAllRows($tmp));
66           return($tmp[0]['nextval']);
67   }
70   function GetTemplateUser(){
71           $data = array();
72           $qry = "SELECT description,name,company_id FROM company WHERE is_template_user=1;";
73           $res = $this->_query($qry);
74           $tmp = $this->FetchAllRows($res);
75           foreach($tmp as $attr){
76                   $data[$attr['name']] = $attr;
77           }
78           return $data;
79   }
80   function GetLocationTeam(){
81           $data = array();
82           $qry = "SELECT description,name,company_id FROM team WHERE is_location_team=1;";
83           $res = $this->_query($qry);
84           $tmp = $this->FetchAllRows($res);
85           foreach($tmp as $attr){
86                   $data[$attr['description']] = $attr;
87           }
88           return $data;
89   }
90   function GetTeams(){
91           $data = array();
92           $qry = "SELECT description,name,company_id FROM team 
93                                 WHERE (is_team=1) AND company_id 
94                                         NOT IN (SELECT company_id FROM company WHERE is_location_team=1);";
95           $res = $this->_query($qry);
96           $tmp = $this->FetchAllRows($res);
97           foreach($tmp as $attr){
98                   $data[$attr['description']] = $attr;
99           }
100           return $data;
101   }
105 function gen_syntax($array,$tablename,$act,$ist)
107         if($act == "EDIT"){
108                 $str = "UPDATE ".$tablename." SET ";
109                 $company_id = $ist[0]['company_id'];
111                 foreach($array as $name => $value){
112                         if((empty($value))&&(!preg_match("/^is_/i",$name))) continue;
113         
114                         if((empty($value))&&(preg_match("/^is_/i",$name))){
115                                 $value= 0;
116                         }
118                         if(!is_numeric($value)){
119                                 $str.= " ".$name."='".$value."', ";
120                         }else{
121                                 $str.= " ".$name."=".$value.", ";
122                         }
123                 }
124                 $str = preg_replace("/, $/","",$str);
125                 $str .= " WHERE company_id=".$company_id.";\n";
126                 return $str;
127         }
128         if($act == "ADD"){
129                 $str = "INSERT into ".$tablename." (";
130                 $attrs  = "";
131                 $values = "";
132                 foreach($array as $name => $attribute){
133                         if((empty($attribute))&&(!preg_match("/^is_/i",$name))) continue;
134         
135                         if((empty($attribute))&&(preg_match("/^is_/i",$name))){
136                                 $attribute= 0;
137                         }
139                         if(is_numeric($attribute)){
140                                 $attrs  .= $name.", ";
141                                 $values .= $attribute.", ";
142                         }else{
143                                 $attrs  .= $name.", ";
144                                 $values .= "'".$attribute."', ";
145                         }
146                 }
147                 $attrs = preg_replace("/, $/","",$attrs);
148                 $values= preg_replace("/, $/","",$values);
149                 $str .= $attrs." ) \nVALUES\n (".$values.");\n";
150                 return $str;
151         }
154 ?>