Code

Added classes
[gosa.git] / include / class_opengw.inc
1 <?php
3 require_once("class_pgsql_opengw.inc");
5 class ogw{
6   var $info;
7   var $ogo;
8   
9   var $validLocationTeam;
10   var $validTemplateUser;
11   var $validTeams;
14   var $InfoOK = false;
16   var $MUST = array("name","login","template_user_id");
17   
18   var $MAY = array( "salutation","firstname","description","degree",
19                     "birthday","sex","street","zip","country","zipcity",
20                     "state","name1","value_string","number","db_status",
21                     "object_version","is_locked","LocationTeamID","TeamIDis");
23   var $LastError  = "";
24   var $option     = "";
26   function ogw($username,$password,$host,$db)
27   {
28     $this->ogo  = new pgre_sql($username,$password,$host,$db);
30     $this->validLocationTeam  = $this->ogo->GetLocationTeam();
31     $this->validTemplateUser  = $this->ogo->GetTemplateUser();
32     $this->validTeams         = $this->ogo->GetTeams();
33   }
34  
35   function SetInfos($infos)
36   {   
37     $this->info = $infos;
38     $info['name1']              = $info['name'];
39   }
41   function GetInfos()
42   {   
43     return("NIY");
44   }
46   function Perform($option)
47   {
48     if(!in_array($option,array("ADD","EDIT","REMOVE"))){
49       $this->LastError = sprintf("Option '%s' is not allowed, possible options are 'ADD' 'EDIT' 'REMOVE'.",$option);
50       return(false);
51     }else{
52       $this->option = $option;
53       if(($this->option == "EDIT")||($this->option=="ADD")){
55         /* Static variables */
56         $this->info['is_person']          = 1;
57         $this->info['is_account']         = 1;
58         $this->info['is_intra_account']   = 1;
59         $this->info['is_extra_account']   = 0;
60         $this->info['owner_id']           = 10000;
61         $this->info['is_team']            = 0;
63         $this->InfoOK = $this->checkInfos();
65         if($this->InfoOK){
66           $qry = "SELECT * FROM person WHERE login='".$this->info['login']."';";
67           $ist = $this->ogo->FetchAllRows($this->ogo->Query($qry));
69           if($this->option == "ADD"){
70             if($this->CheckExistence()){
71               $this->LastError="Can't add entry already exists.";
72               return(false);
73             }else{
74               $this->info['db_status']          = "inserted";
75               $this->info['object_version']     = 1;
76               return($this->ADD());
77             }
78           }else{
79             if(!$this->CheckExistence()){
80               $this->LastError="Can't edit entry, entry doesn't exists.";
81               return(false);
82             }else{
83               $this->info['db_status']          = "edited";
84               $this->info['object_version']     = $ist[0]['object_version']++;
85               return($this->EDIT());
86             }
87           }
88         }else{
89           return($this->InfoOK);
90         }
91       }
92       if($this->option == "REMOVE"){
93     
94         if((!isset($this->info['login']))||(empty($this->info['login']))){
95           $this->LastError = "Require login to detect existence";
96           return(false);
97         }else{
98     
99           if($this->CheckExistence()){
100             return($this->REMOVE());
101           }else{
102             $this->LastError="Can't remove non existing entry";
103             return(false);
104           }
105         }
106       }
107     }
108   }
110   function CheckExistence()
111   {
112     /* Check if thios entry already exists */
113     $qry = "SELECT * FROM person WHERE login='".$this->info['login']."';";
114     $ist = $this->ogo->FetchAllRows($this->ogo->Query($qry));
115     if($ist){
116       return(true);
117     }else{
118       return(false);
119     }
120   }
122   function checkInfos()
123   {
124     foreach($this->MUST as $name){
125       if((!isset($this->info[$name]))||(empty($this->info[$name]))){
126         $this->LastError = sprintf("Must attribute '%s' wasn't set.",$name);
127         return(false);
128       }
129     }
131     foreach($this->MAY as $name){
132       if((!isset($this->info[$name]))||(empty($this->info[$name]))){
133         $info[$name] = false;
134       }
135     }
137     $tmp = array(false,"",0);
138     foreach($this->validLocationTeam as $id){
139       $tmp[]= $id['company_id'];
140     }
141     if(!in_array($this->info['LocationTeamID'],$tmp)){
142       $this->LastError = "Given 'Location Team' is invalid.";
143       return(false);
144     }
146     $tmp = array();
147     foreach($this->validTemplateUser as $id){
148       $tmp[]= $id['company_id'];
149     }
150     if(!in_array($this->info['template_user_id'],$tmp)){
151       $this->LastError = "Given 'Template User ID' is invalid.";
152       return(false);
153     }
155     
156     $tmp = array();
157     foreach($this->validTeams as $id){
158       $tmp[]= $id['company_id'];
159     }
160     foreach($this->info['TeamIDis'] as $id){
161       if(!in_array($id,$tmp)){
162         $this->LastError = sprintf("Given 'Team ID':%s is invalid.",$id);
163        return(false);
164       }
165     }
166     return(true);
167   }
169   function REMOVE()
170   {
171     $qry = "SELECT * FROM person WHERE login='".$this->info['login']."';";
172     $ist = $this->ogo->FetchAllRows($this->ogo->Query($qry));
173     if((!$ist)||(!isset($ist[0]['company_id']))||($ist[0]['company_id']<=0)){
174       $this->LastError(sprintf("Can't get company id for login %s",$this->info['login']));
175       return(false);
176     }else{
177       $company_id     = $ist[0]['company_id'];
178       $this->ogo->Query("DELETE FROM telephone WHERE company_id=".$company_id.";");
179       $this->ogo->Query("DELETE FROM address  WHERE company_id=".$company_id.";");
180       $this->ogo->Query("DELETE FROM company_info WHERE company_id=".$company_id.";");
181       $this->ogo->Query("DELETE FROM company_value WHERE company_id=".$company_id.";");
182       $this->ogo->Query("DELETE FROM company_assignment WHERE sub_company_id=".$company_id.";");
183       $this->ogo->Query("DELETE FROM person WHERE company_id=".$company_id.";");
184       $this->ogo->Query("DELETE FROM staff WHERE company_id=".$company_id.";");
185       return(true);
186     }
187   }
189   function ADD()
190   {
191     /* 
192        Entry settings for personm table 
193      */
194     $arr = array( "company_id","object_version","owner_id","template_user_id",
195                   "is_person","is_account","is_intra_account","is_extra_account",
196                   "number","description","is_locked","login","name","name","firstname",
197                   "salutation","degree","birthday","sex","db_status");
198     $this->info['company_id']  = $this->ogo->gen_id();
199     $userID             = "OGo".$this->info['company_id'];
200     foreach($arr as $attr){
201       if($attr == "number"){
202         $add_user[$attr]  = $this->info['userID'];
203       }else{
204         $add_user[$attr]  = $this->info[$attr];
205       }
206     }
207     $QUERY[] = gen_syntax($add_user,"person","ADD",false);
210     /*
211        Entry for staff table
212      */
213     $arr = array("staff_id","company_id","description","login","is_team","is_account","db_status");
214     $this->info['staff_id'] = $this->ogo->gen_id();
215     foreach($arr as $attr){
216       $add_staff[$attr] = $this->info[$attr];
217     }
218     $QUERY[] = gen_syntax($add_staff,"staff","ADD",false);
221     /* 
222        Create entries for company nfo 
223      */
224     $arr = array("company_info_id","company_id","db_status");
225     $this->info['company_info_id']  = $this->ogo->gen_id();
226     foreach($arr as $attr){
227       $add_company_info[$attr]  = $this->info[$attr];
228     }
229     $QUERY[] = gen_syntax($add_company_info,"company_info","ADD",false);
232     /* 
233        Create entries for company value 
234      */
235     $arr = array("db_status","value_string","attribute","company_id","company_value_id");
236     $this->info['attribute']        = "email1";
237     $this->info['company_value_id'] = $this->ogo->gen_id();
238     foreach($arr as $attr){
239       $add_company_value[$attr]  = $this->info[$attr];
240     }
241     $QUERY[] = gen_syntax($add_company_value,"company_value","ADD",false);
244     /* 
245        address entries 
246      */
247     $arr = array("company_id","address_id","street","zip","country","zipcity","state","db_status","name1","type");
248     foreach(array("private","mailing","location") as  $type){
249       
250       $this->info['address_id'] = $this->ogo->gen_id();
251       $this->info['type']       = $type;
252       foreach($arr as $attr){
253         $add_address[$attr]  = $this->info[$attr];
254       }
255       $QUERY[] = gen_syntax($add_address,"address","ADD",false);
256     }
259     /*
260        telephone entries 
261      */
262     $arr = array("telephone_id","object_version","company_id","number","type","db_status");
263     foreach(array("01_tel","02_tel","03_tel_funk","05_tel_private","10_fax","fax_private") as $type){
264       $this->info['type']       = $type;
265       $this->info['telephone_id']     = $this->ogo->gen_id();
266       foreach($arr as $attr){
267         $add_telephone[$attr]  = $this->info[$attr];
268       }
269       $QUERY[] = gen_syntax($add_telephone,"telephone","ADD",false);
270     }
272     /*
273        company_assignment entries (Location Team) 
274      */
275     $this->info['old_company_id']       = $this->info['company_id'];
276     $this->info['sub_company_id']       = $this->info['old_company_id'];
278     $this->info['company_assignment_id']= $this->ogo->gen_id();
279     $this->info['company_id']           = $this->info['LocationTeamID'];
280     $arr = array("company_assignment_id","company_id","sub_company_id","db_status");
281     foreach($arr as $attr){
282       $add_company_assignment[$attr]  = $this->info[$attr];
283     }
284     $QUERY[] = gen_syntax($add_company_assignment,"company_assignment","ADD",false);
287     /*
288        company_assignment entries (Teams) 
289      */
290     $arr = array("company_assignment_id","company_id","sub_company_id","db_status");
291     foreach($this->info['TeamIDis'] as $TeamID){
292       
293       $this->info['company_id']           = $TeamID;
294       $this->info['sub_company_id']       = $this->info['old_company_id'];
295       $this->info['company_assignment_id']= $this->ogo->gen_id();
296       foreach($arr as $attr){
297         $add_company_assignment[$attr]  = $this->info[$attr]; 
298       }    
299       $QUERY[] = gen_syntax($add_company_assignment,"company_assignment","ADD",false);
300     }
302     $remove_all = false;
303     foreach($QUERY as $q ){
304       if(!$this->ogo->Query($q)){
305         $remove_all = true;
306         break;
307       }
308     }
309     
310     if($remove_all== true){
311       $this->ogo->Query("DELETE FROM telephone WHERE company_id=".$this->info['company_id'].";");
312       $this->ogo->Query("DELETE FROM address WHERE company_id=".$this->info['company_id'].";");
313       $this->ogo->Query("DELETE FROM company_assignment WHERE company_id=".$this->info['company_id'].";");
314       $this->ogo->Query("DELETE FROM company_info WHERE company_id=".$this->info['company_id'].";");
315       $this->ogo->Query("DELETE FROM company_value WHERE company_id=".$this->info['company_id'].";");
316       $this->ogo->Query("DELETE FROM staff WHERE company_id=".$this->info['company_id'].";");
317       $this->ogo->Query("DELETE FROM person WHERE company_id=".$this->info['company_id'].";");
318       $this->LastError="Query failed, removed all added entries";
319       return(false);
320     }
321     return(true);
322   }
325   function EDIT()
326   {
327     $qry = "SELECT * FROM person WHERE login='".$this->info['login']."';";
328     $ist = $this->ogo->FetchAllRows($this->ogo->Query($qry));
329     /* 
330        Entry settings for personm table 
331      */
332     $arr = array( "company_id","object_version","owner_id",
333                   "template_user_id","is_person","is_account","is_intra_account",
334                   "is_extra_account","number","description","is_locked","login","name",
335                   "firstname","salutation","degree","birthday","sex","db_status");
336     $this->info['company_id'] = $ist[0]['company_id'];
337     $this->info['userID']     = "OGo".$this->info['company_id'];
338     foreach($arr as $attr){
339       if($attr == "number"){
340         $add_user[$attr]  = $this->info['userID'];
341       }else{
342         $add_user[$attr]  = $this->info[$attr];
343       }
344     }
345     $QUERY[] = gen_syntax($add_user,"person","EDIT",$ist);
348     /*
349        Entry for staff table
350      */
351     $arr = array("staff_id","company_id","description","login","is_team","is_account","db_status");
352     $this->info['staff_id'] = $this->ogo->gen_id();
353     foreach($arr as $attr){
354       $add_staff[$attr] = $this->info[$attr];
355     }
356     $QUERY[] = gen_syntax($add_staff,"staff","EDIT",$ist);
359     /* 
360        Create entries for company nfo 
361      */
362     $arr = array("company_info_id","company_id","db_status");
363     $this->info['company_info_id']    = $this->ogo->gen_id();
364     foreach($arr as $attr){
365       $add_company_info[$attr]  = $this->info[$attr];
366     }
367     $QUERY[] = gen_syntax($add_company_info,"company_info","EDIT",$ist);
370     /* 
371        Create entries for company value 
372      */
373     $arr = array("db_status","value_string","attribute","company_id","company_value_id");
374     $this->info['attribute']        = "email1";
375     $this->info['company_value_id'] = $this->ogo->gen_id();
376     foreach($arr as $attr){
377       $add_company_value[$attr]  = $this->info[$attr];
378     }
379     $QUERY[] = gen_syntax($add_company_value,"company_value","EDIT",$ist);
382     /* 
383        address entries 
384      */
385     $QUERY[] = "DELETE FROM address WHERE company_id=".$ist[0]['company_id'].";";
386     $this->info['company_id'] = $ist[0]['company_id'];
387     $arr = array("company_id","address_id","street","zip","country","zipcity","state","db_status","name1","type");
388     foreach(array("private","mailing","location") as  $type){
389       $this->info['type']       = $type;
390       $this->info['address_id'] = $this->ogo->gen_id();
391       foreach($arr as $attr){
392         $add_address[$attr]  = $this->info[$attr];
393       } 
394       $QUERY[] = gen_syntax($add_address,"address","ADD",false);
395     }
398     /*
399        telephone entries 
400      */
401     $QUERY[] = "DELETE FROM telephone WHERE company_id=".$ist[0]['company_id'].";";
402     $this->info['company_id'] = $ist[0]['company_id'];
403     $arr = array("telephone_id","object_version","company_id","number","type","db_status");
404     foreach(array("01_tel","02_tel","03_tel_funk","05_tel_private","10_fax","fax_private") as $type){
405       $this->info['type']           = $type;
406       $this->info['telephone_id']   = $this->ogo->gen_id();
407       foreach($arr as $attr){
408         $add_telephone[$attr]  = $this->info[$attr];
409       }
411       $QUERY[] = gen_syntax($add_telephone,"telephone","ADD",false);
412     }
415     /*
416        company_assignment entries (Location Team) 
417      */
418     $this->info['old_company_id'] = $this->info['company_id'];
419     
420     /* First remove location team */
421     $QUERY[] = "DELETE FROM company_assignment WHERE (sub_company_id=".$ist[0]['company_id'].") AND
422       company_id in (SELECT company_id FROM team WHERE is_location_team=1);";
424     $this->info['sub_company_id']       = $ist[0]['company_id'];
425     $this->info['company_assignment_id']= $this->ogo->gen_id();
426     $this->info['company_id']           = $this->info['LocationTeamID'];
427     $arr = array("company_assignment_id","company_id","sub_company_id","db_status");
428     foreach($arr as $attr){
429       $add_company_assignment[$attr]  = $this->info[$attr];
430     }
431     $QUERY[] = gen_syntax($add_company_assignment,"company_assignment","ADD",false);
434     /*
435        company_assignment entries (Teams) 
436      */
437     /* First remove location team */
438     $QUERY[] = "DELETE FROM company_assignment 
439                 WHERE 
440                     (sub_company_id=".$ist[0]['company_id'].") 
441                   AND
442                     company_id IN 
443                       (SELECT company_id FROM team  WHERE  (is_team=1) AND company_id NOT IN 
444                          (SELECT company_id FROM team WHERE is_location_team=1));";
446     $arr = array("company_assignment_id","company_id","sub_company_id","db_status");
447     foreach($this->info['TeamIDis'] as $TeamID){
448       $this->info['company_id']           = $TeamID;
449       $this->info['sub_company_id']       = $ist[0]['company_id'];
450       $this->info['company_assignment_id']= $this->ogo->gen_id();
451       foreach($arr as $attr){
452         $add_company_assignment[$attr]  = $this->info[$attr];
453       }    
454       $QUERY[] = gen_syntax($add_company_assignment,"company_assignment","ADD",false);
455     }
457     $remove_all = false;
459     foreach($QUERY as $q ){
460       if(!$this->ogo-> Query($q)){
461         print $q;
462         $remove_all = true;
463         break;
464       }
465     }
466   }
469 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
470 ?>