Code

Updated client dns handling
[gosa.git] / include / class_glpi.inc
1 <?php
4 class glpiDB{
6         var $user               ="";
7         var $password   ="";
8         var $server             ="";
9         var $db                 ="";
11         var $is_connected               = 0;
12         var $handle                     = NULL;
14         var $lasterror  ="";
16         var $deviceMappingGOsaGlpi;
17         var $deviceMappingTableNameID;
19         function glpiDB($server,$user,$pwd,$db){
20                 $this->server   = $server;
21                 $this->user     = $user;
22                 $this->password = $pwd;
23                 $this->db               = $db;
25                 $this->handle   = @mysql_connect($this->server,$this->user,$this->password);
27                 if($this->handle){
28                         $this->is_connected = true;
29                         $this->SelectDB($this->db);
30                 }       
31                 $this->deviceMappingGOsaGlpi = array(
32                                 "glpi_device_case"      => "case",
33                                 "glpi_device_control"   => "control",
34                                 "glpi_device_drive"     => "drive",
35                                 "glpi_device_gfxcard"   => "gfxcard",
36                                 "glpi_device_hdd"       => "hdd",
37                                 "glpi_device_iface"     => "iface",
38                                 "glpi_device_moboard"   => "moboard",
39                                 "glpi_device_pci"       => "pci",
40                                 "glpi_device_power"     => "power",
41                                 "glpi_device_processor" => "processor",
42                                 "glpi_device_ram"       => "ram",
43                                 "glpi_monitors"       => "monitor",
44                                 "glpi_device_sndcard"   => "sndcard");
46                 $this->deviceMappingTableNameID = array(                "moboard"       => 1,                                                                           
47                                 "processor"     => 2,                                                                           
48                                 "ram"           => 3,                                                                           
49                                 "hdd"           => 4,                                                                           
50                                 "iface"         => 5,                                                                           
51                                 "drive"         => 6,                                                                           
52                                 "control"       => 7,                                                                           
53                                 "gfxcard"       => 8,                                                                           
54                                 "sndcard"       => 9,                                                                           
55                                 "pci"           => 10,                                                                          
56                                 "case"          => 11,                                                                          
57                                 "power"         => 12);
60         }
62         function SelectDB()
63         {
64                 if($this->is_connected){
65                         mysql_select_db($this->db,$this->handle);
66                 }
67         }
70         /* This functions checks if the selected computer/network 
71            device is already available in the db
72          */
73         function is_account($dn)
74         {
75                 if(!$this->is_connected){
76                         $this->lasterror ="Can't query anything, if we aren't connected.";
77                         return(false);
78                 }else{
79                         $qry = "SELECT * FROM glpi_computers WHERE name='".$dn."';";
80                         $res = $this->query($qry);
81                         if(count($res)==0){
82                                 return(false);
83                         }else{
84                                 return(true);
85                         }
86                 }
87         }
89         /* this function queries everything 
90          */
91         function query($qry)
92         {
93                 if(!$this->is_connected){
94                         $this->lasterror ="Can't query anything, if we aren't connected.";
95                         return(false);
96                 }else{
97                         $ret =array();
98                         $res = mysql_query($qry,$this->handle);
100                         while($rs = @mysql_fetch_array($res,MYSQL_ASSOC)){
101                                 $ret[]=$rs;
102                         }
103                         return($ret);
104                 }
105         }
107         /* System types 
108        Returns all defined system types 
109          */
110         function getSystemTypes()
111         {
112                 if($this->is_connected){
113                         $ret = array();
114                         $tmp = ($this->query("SELECT * FROM glpi_type_computers;"));
115                         foreach($tmp as $t){
116                                 $ret[$t['ID']]=$t['name'];
117                         }
118                         asort($ret);
119                         return($ret);
120                 }else{
121                         echo "not connected";
122                         return(false);
123                 }
124         }
126         /* System types 
127            Update a system type         
128         */
129         function updateSystemType($name,$id)
130         {
131                 if($this->is_connected){
132                         $tmp = $this->query("SELECT * FROM glpi_type_computers WHERE ID=".$id.";");
133                         if(isset($tmp[0])){
134                                 return($this->query("UPDATE glpi_type_computers SET name='".$name."' WHERE ID=".$id.";"));      
135                         }else{
136                                 echo "can't update not existing entry";
137                                 return(false);  
138                         }
139                 }else{
140                         echo "not connected";
141                         return(false);
142                 }
143         }
145         /* System types 
146        Add one entry to the system types 
147      */
148         function addSystemType($name)
149         {
150                 if($this->is_connected){
151                         $tmp = $this->query("SELECT * FROM glpi_type_computers WHERE name='".$name."';");
152                         if(isset($tmp[0])){
153                                 echo "such an entry already exists";
154                                 return(false);
155                         }else{  
156                                 return($this->query("INSERT INTO glpi_type_computers (name) VALUES ('".$name."');"));
157                         }
158                 }else{
159                         echo "not connected";
160                         return(false);
161                 }
162         }
164         /* System types 
165        Remove one entry from the system types (specified by ID=$id)
166      */
167         function removeSystemType($id)
168         {
169                 if($this->is_connected){
170                         $tmp = $this->query("SELECT * FROM glpi_type_computers WHERE ID=".$id.";");
171                         if(isset($tmp[0])){
172                                 return($this->query("DELETE FROM glpi_type_computers WHERE ID=".$id.";"));      
173                         }else{
174                                 echo "can't remove not existing entry";
175                                 return(false);  
176                         }
177                 }else{
178                         echo "not connected";
179                         return(false);
180                 }
181         }
183         /* System type is used */
184         function is_systemTypeUsed($ID){
185                 if($this->is_connected){
186                         $ret = array();
187                         $qry="SELECT name,type FROM glpi_computers WHERE type=".$ID." LIMIT 3;";
188                         $res = $this->query($qry);
189                         foreach($res as $val){
190                                 $ret[$val['name']] = $val['name'];
191                         }
192                         return($ret);
193                 }else{
194                         echo "not connected";
195                         return(false);
196                 }
197         }
200         /* Manufacturer 
201            Returns all defined manufacturers
202         */      
203         function getEnterprises()
204         {
205                 if($this->is_connected){
206                         $ret = array();
207                         $tmp = $this->query("SELECT * FROM glpi_enterprises ORDER BY name;");
208                         foreach($tmp as $t){
209                                 $ret[$t['ID']]=$t['name'];
210                         }
211                         
212                         return($ret);
213                 }else{
214                         echo "not connected";
215                         return(false);
216                 }
217         }
219         /* Manufacturer 
220            Returns single manufacturer
221         */      
222         function getEnterprise($id)
223         {
224                 if($this->is_connected){
225                         $ret = array();
226                         $tmp = $this->query("SELECT * FROM glpi_enterprises WHERE ID=".$id.";");
227                         return($tmp);
228                 }else{
229                         echo "not connected";
230                         return(false);
231                 }
232         }
234         /* Manufacturer 
235        Updates already existing manufacturer
236     */
237         function updateEnterprise($array,$id)
238         {
239                 if(!is_array($array)){
240                         echo "updateEnterprisesType: first paraeter must be an array";
241                 }elseif($this->is_connected){
242                         $tmp = $this->query("SELECT * FROM glpi_enterprises WHERE ID='".$id."';");
243                         if(isset($tmp[0])){
244                                 $atr = array("ID","name","type","address","website","phonenumber","comments","deleted","fax","email");
246                                 $v = "";
247                                 foreach($atr as $at){
248                                         if(isset($array[$at])){
249                                                 $v .= " ".$at."='".$array[$at]."', ";
250                                         }
251                                 }
252                                 if(empty($v)){
253                                         echo "updateEnterprisesType: no attributes given ";
254                                         return(false);
255                                 }else{
256                                         $v = preg_replace("/, $/","",$v);
257                                         return($this->query("UPDATE glpi_enterprises SET ".$v." WHERE ID='".$id."';")); 
258                                 }
259                         }else{
260                                 echo "can't update not existing entry";
261                                 return(false);  
262                         }
263                 }else{
264                         echo "not connected";
265                         return(false);
266                 }
267         }
269         /* Manufacturer
270            Add new manufacturer
271          */
272         function addEnterprise($array)
273         {
274                 if(!is_array($array)){
275                         echo "addUser: first paraeter must be an array";
276                 }elseif($this->is_connected){
277                         $atr = array("ID","name","type","address","website","phonenumber","comments","deleted","fax","email");
278                         $v = "";
279                         $a = "";
280                         foreach($atr as $at){
281                                 if(isset($array[$at])){
282                                         $a .= $at.", ";
283                                         $v .= "'".$array[$at]."', ";
284                                 }
285                         }
286                         if(empty($v)){
287                                 echo "addUser: no attributes given ";
288                                 return(false);
289                         }else{
290                                 $a = preg_replace("/, $/","",$a);
291                                 $v = preg_replace("/, $/","",$v);
292                                 return($this->query("INSERT INTO glpi_enterprises (".$a.") VALUES (".$v.");"));
293                         }
294                 
295                 }else{
296                         echo "not connected";
297                         return(false);
298                 }
300         }
302         /*      Manufacturer
303                 remove manufacturer
304          */
305         function removeEnterprise($id)
306         {
307                 if($this->is_connected){
308                         $tmp = $this->query("SELECT * FROM glpi_enterprises WHERE ID=".$id.";");
309                         if(isset($tmp[0])){
310                                 return($this->query("DELETE FROM glpi_enterprises WHERE ID=".$id.";"));
311                         }else{
312                                 echo "can't remove not existing entry";
313                                 return(false);
314                         }
315                 }else{
316                         echo "not connected";
317                         return(false);
318                 }
319         }
321         /* Operating systems 
322            Returns all OSs
323          */
324         function getOSTypes($keys = false)
325         {
326                 if($this->is_connected){
327                         $ret = array();
328                         $tmp=($this->query("SELECT * FROM glpi_dropdown_os ORDER by name;"));
330                         if($keys){
331                                 foreach($tmp as $t){
332                                         $ret[$t['name']]=$t['ID'];
333                                 }
334                         }else{
335                                 foreach($tmp as $t){
336                                         $ret[$t['ID']]=$t['name'];
337                                 }
338                         }
339                         return($ret);
341                 }else{
342                         echo "not connected";
343                         return(false);
344                 }
345         }
347         /* Operating system is used ? */
348         function is_osUsed($ID){
349                 if($this->is_connected){
350                         $ret = array();
351                         $qry="SELECT name,type FROM glpi_computers WHERE os=".$ID." LIMIT 3;";
352                         $res = $this->query($qry);
353                         foreach($res as $val){
354                                 $ret[$val['name']] = $val['name'];
355                         }
356                         return($ret);
357                 }else{
358                         echo "not connected";
359                         return(false);
360                 }
361         }
364         /*  Operating systems
365                 Add a new operating system to the dropdown menus   
366          */
367     function addOS($name)
368     {
369         if($this->is_connected){
370             $tmp = $this->query("SELECT * FROM glpi_dropdown_os WHERE name='".$name."';");
371             if(isset($tmp[0])){
372                 echo "such an entry already exists";
373                 return(false);
374             }else{
375                 return($this->query("INSERT INTO glpi_dropdown_os (name) VALUES ('".$name."');"));
376             }
377         }else{
378             echo "not connected";
379             return(false);
380         }
381     }
383     /* Operating systems 
384        remove one OS entry
385      */
386     function removeOS_byID($id)
387     {
388         if($this->is_connected){
389             $tmp = $this->query("SELECT * FROM glpi_dropdown_os WHERE ID=".$id.";");
390             if(is_array($tmp[0])){
391                 return($this->query("DELETE FROM glpi_dropdown_os WHERE ID=".$id.";"));
392             }else{
393                 echo "can't remove not existing entry";
394                 return(false);
395             }
396         }else{
397             echo "not connected";
398             return(false);
399         }
400     }
402         /* Operating systems 
403            Update existing OS entry
404         */
405         function updateOS($name,$id)
406         {
407                 if($this->is_connected){
408                         $tmp = $this->query("SELECT * FROM glpi_dropdown_os WHERE ID=".$id.";");
409                         if(isset($tmp[0])){
410                                 return($this->query("UPDATE glpi_dropdown_os SET name='".$name."' WHERE ID=".$id.";")); 
411                         }else{
412                                 echo "can't update not existing entry";
413                                 return(false);  
414                         }
415                 }else{
416                         echo "not connected";
417                         return(false);
418                 }
419         }
421         /* This returns all available glpi users 
422      */
423         function getUsers()
424         {
425                 if($this->is_connected){
426                         $ret = array();
427                         $tmp = ($this->query("SELECT * FROM glpi_users"));
428                         foreach($tmp as $user){
429                                 $ret[$user['ID']]=$user['name'];
430                         }
431                         return($ret);
433                 }else{
434                         echo "not connected";
435                         return(false);
436                 }
437         }
439         /* this function adds a new glpi user
440      */
441         function addUser($array,$dn)
442         {
443                 if(!is_array($array)){
444                         echo "addUser: first paraeter must be an array";
445                 }elseif($this->is_connected){
446                         $array['name']=$dn;
447                         $atr = array("name","phone","email");
448                         $v = "";
449                         $a = "";
450                         foreach($atr as $at){
451                                 if(isset($array[$at])){
452                                         $a .= $at.", ";
453                                         $v .= "'".$array[$at]."', ";
454                                 }
455                         }
456                         if(empty($v)){
457                                 echo "addUser: no attributes given ";
458                                 return(false);
459                         }else{
460                                 $a = preg_replace("/, $/","",$a);
461                                 $v = preg_replace("/, $/","",$v);
462                                 return($this->query("INSERT INTO glpi_users (".$a.") VALUES (".$v.");"));
463                         }
464                 
465                 }else{
466                         echo "not connected";
467                         return(false);
468                 }
470         }
472         /* This function updates a glpi user 
473        with the given data
474      */
475         function updateUser($array,$dn)
476         {
477                 if(!is_array($array)){
478             echo "updateUser: first paraeter must be an array";
479         }elseif($this->is_connected){
480             $tmp = $this->query("SELECT * FROM  glpi_users WHERE name='".$dn."';");
481             if(isset($tmp[0])){
483                                 $atr = array("name","phone","email");
484                 $v = "";
485                 foreach($atr as $at){
486                     if(isset($array[$at])){
487                         $v .= " ".$at."='".$array[$at]."', ";
488                     }
489                 }
490                 if(empty($v)){
491                     echo "UpdateUser: no attributes given ";
492                     return(false);
493                 }else{
494                     $v = preg_replace("/, $/","",$v);
495                     return($this->query("UPDATE glpi_users SET ".$v." WHERE name='".$dn."';"));
496                 }
497             }else{
498                 echo "can't update not existing entry";
499                 return(false);
500             }
501         }else{
502             echo "not connected";
503             return(false);
504         }
506         }
508         /* This function returns all available data 
509        from a specified dn
510      */
511         function getComputerInformations($name)
512         {
513                 if($this->is_connected){        
514                         $ret = $this->query("SELECT * FROM glpi_computers WHERE name='".$name."';");
515                         return($ret);           
516                 }else{
517                         echo "not connected";
518                         return(false);
519                 }
520         }
522         /*  This fucntions updates an already existing entry 
523      */
524         function updateComputerInformations($array,$name)
525         {
526                 if(!is_array($array)){
527                         echo "updateComputerInformations: first paraeter must be an array";
528                 }elseif($this->is_connected){
529                         $tmp = $this->query("SELECT * FROM  glpi_computers WHERE name='".$name."';");
530                         if(isset($tmp[0])){
532                                 $atr = array(   "ID","name","serial","otherserial","contact","contact_num",
533                                                 "tech_num","comments","date_mod","os","location","domain","network",
534                                                 "model","type","is_template","tplname","FK_glpi_enterprise","deleted");
535                                 $v = "";
536                                 foreach($atr as $at){
537                                         if(isset($array[$at])){
538                                                 $v .= " ".$at."='".$array[$at]."', ";
539                                         }
540                                 }
541                                 if(empty($v)){
542                                         echo "updateComputerInformations: no attributes given ";
543                                         return(false);
544                                 }else{
545                                         $v = preg_replace("/, $/","",$v);
546                                         return($this->query("UPDATE glpi_computers SET ".$v." WHERE name='".$name."';"));
547                                 }
548                         }else{
549                                 echo "can't update not existing entry";
550                                 return(false);
551                         }
552                 }else{
553                         echo "not connected";
554                         return(false);
555                 }
557         }
559         /* This function adds a new inventory device (computer phone etc)
560      */
561         function addComputerInformations($array)
562         {
563                 if(!is_array($array)){
564                         echo "updateComputerInformations: first paraeter must be an array";
565                 }elseif($this->is_connected){
566                         $atr = array(   "ID","name","serial","otherserial","contact","contact_num",
567                                         "tech_num","comments","date_mod","os","location","domain","network",
568                                         "model","type","is_template","tplname","FK_glpi_enterprise","deleted");
569                         $v = "";
570                         $a = "";
571                         foreach($atr as $at){
572                                 if(isset($array[$at])){
573                                         $a .= $at.", ";
574                                         $v .= "'".$array[$at]."', ";
575                                 }
576                         }
577                         if(empty($v)){
578                                 echo "updateComputerInformations: no attributes given ";
579                                 return(false);
580                         }else{
581                                 $a = preg_replace("/, $/","",$a);
582                                 $v = preg_replace("/, $/","",$v);
583                                 return($this->query("INSERT INTO glpi_computers (".$a.") VALUES (".$v.");"));
584                         }
585                 
586                 }else{
587                         echo "not connected";
588                         return(false);
589                 }
591         }
593         /* this functions checks if the given Device 
594      * already exists 
595      */
596         function deviceExists($attr)
597         {
598                 $deviceMappingGOsaGlpi = $this->deviceMappingGOsaGlpi;
599                 if($this->is_connected){        
600                         $arr = array_flip($deviceMappingGOsaGlpi);
602                         $tbl_name = $arr[$attr['device_type']];
603                         if(!isset($attr['ID'])){
604                                 return(false);
605                         }else{
606                                 $qry = "SELECT * FROM ".$tbl_name." WHERE ID=".$attr['ID'].";";
607                                 $res = $this->query($qry);
608                                 if(count($res) != 0){
609                                         return(true);
610                                 }
611                         }
612                 }else{
613                         echo "not connected";
614                         return(false);
615                 }
617                 return(false);
618         }
620                 
621         /* Check if given device is used by some accounts
622      *  (helpfull to avoid removement of used devices)
623      */
624         function is_deviceUsed($item)
625         {
626                 $deviceMappingGOsaGlpi          = array_flip($this->deviceMappingGOsaGlpi);
627                 $deviceMappingTableNameID       = $this->deviceMappingTableNameID;
628                 if($this->is_connected){
629                         $tablename =  $deviceMappingGOsaGlpi[$item['device_type']];
630                         $type = $item['device_type'];
632                         $ret = array();
634                         if($type=="monitor"){
635                                 $str = "SELECT c.name FROM glpi_connect_wire as w, glpi_computers as c WHERE w.end1=".$item['ID']." AND w.end2 = c.ID AND w.type=4;"; 
636                         }else{
637                                 $str = "SELECT c.name FROM glpi_computer_device as d, glpi_computers as c WHERE d.FK_computers=c.ID AND FK_device=".$item['ID']." AND device_type=".$deviceMappingTableNameID[$type]." ;";
638                         }
640                         $res = $this->query($str);
641         
642                         foreach($res as $val){
643                                 $ret[$val['name']] = $val['name'];
644                         }       
646                         return($ret);//count($this->query($str)));
647                 }else{
648                         echo "not connected";
649                         return(false);
650                 }
651                         
652         }
655         /* This functions deletes a specified entry 
656      * from our device tables 
657      */
658         function deleteDevice($attr)
659         {
660                 $deviceMappingGOsaGlpi = $this->deviceMappingGOsaGlpi;
661                 if($this->is_connected){
662                         $arr = array_flip($deviceMappingGOsaGlpi);
663                         
664                         $device_type = $attr['device_type'];
665                         unset($attr['device_type']);                    
667                         $tbl_name = $arr[$device_type];
669                         $this->query("DELETE FROM ".$tbl_name." WHERE ID=".$attr['ID'].";");    
670                 }else{
671                         echo "not connected";
672                         return(false);
673                 }
674         }
676         /* This funtions updated an already existing device
677      */
678         function updateDevices($attr)
679         {
680                 $deviceMappingGOsaGlpi = $this->deviceMappingGOsaGlpi;
681                 if($this->is_connected){
682                         $arr = array_flip($deviceMappingGOsaGlpi);
683                         
684                         $device_type = $attr['device_type'];
685                         unset($attr['device_type']);                    
687                         $tbl_name = $arr[$device_type];
689                         $str = "UPDATE ".$tbl_name." SET ";
690                         foreach($attr as $name => $value){
691                                 $str.=$name."='".$value."', ";
692                         }
693                         $str = preg_replace("/, $/","",$str);
694                         $str .= " WHERE ID=".$attr['ID'].";";
695                         $this->query($str);     
696                 }else{
697                         echo "not connected";
698                         return(false);
699                 }
700         }
702         /* Returns all possible RAM types 
703      * like SDRAM , DIMM .....
704      */
705         function getRAMTypes()
706         {
707                 if($this->is_connected){
708                         $ret = array();
709                         $tmp = ($this->query("SELECT * FROM glpi_dropdown_ram_type;"));
710                         foreach($tmp as $t){
711                                 $ret[$t['ID']]=$t['name'];
712                         }
713                         return($ret);
714                 }else{
715                         echo "not connected";
716                         return(false);
717                 }
718         }
719         
720         /* Returns all possible HDD connection types 
721      * like IDE SCSI ...
722      */
723         function getGlpiDeviceControlTypes()
724         {
725                 if($this->is_connected){
726                         $ret = array();
727                         $tmp = ($this->query("SELECT * FROM glpi_dropdown_hdd_type;"));
728                         foreach($tmp as $t){
729                                 $ret[$t['ID']]=$t['name'];
730                         }
731                         return($ret);
732                 }else{
733                         echo "not connected";
734                         return(false);
735                 }
736         }
737         
738         /* Returns all possible gfx card connection types
739      * like PCI-X PCI AGP ....
740          */
741         function getGlpiGfxControlTypes()
742         {
743                 if($this->is_connected){
744                         $ret = array();
745                         $tmp = ($this->query("SELECT * FROM glpi_dropdown_hdd_type;"));
746                         foreach($tmp as $t){
747                                 $ret[$t['ID']]=$t['name'];
748                         }
749                         return($ret);
750                 }else{
751                         echo "not connected";
752                         return(false);
753                 }
754         }
755         
756         /* Devices 
757        Adds a new single device to our db
758     */  
759         function addDevice($attr)
760         {
761                 $deviceMappingGOsaGlpi = $this->deviceMappingGOsaGlpi;
762                 if($this->is_connected){        
763                         $arr = array_flip($deviceMappingGOsaGlpi);
764                         
765                         $device_type = $attr['device_type'];
766                         unset($attr['device_type']);                    
768                         $tbl_name = $arr[$device_type];
769             $v = "";
770             $a = "";
771             foreach($attr as $name => $value){
772                 $a .= $name.", ";
773                 $v .= "'".$value."', ";
774             }
775             if(empty($v)){
776                 echo "addDevice: no attributes given ";
777                 return(false);
778             }else{
779                 $a = preg_replace("/, $/","",$a);
780                 $v = preg_replace("/, $/","",$v);
781                 return($this->query("INSERT INTO ".$tbl_name." (".$a.") VALUES (".$v.");"));
782             }
784         }else{
785             echo "not connected";
786             return(false);
787         }
788         }
790         /* Return all available devices 
791      */
792         function getDevices()
793         {
794                 $deviceMappingGOsaGlpi = $this->deviceMappingGOsaGlpi;
795                 if($this->is_connected){
796                         $arr = $deviceMappingGOsaGlpi; 
797                 
798                         $res = array();
799                         foreach($arr as $glpi => $gosa){
800                                 $qry = "SELECT * FROM ".$glpi.";";
801                                 $ret = $this->query($qry);
802                                 foreach($ret as $id => $entry){
803                                         $entry['device_type'] = $gosa;
804         
805                                         if(isset($entry['designation'])){
806                                                 $res[$entry['designation']."-".$gosa] = $entry;
807                                         }else{
808                                                 $res[$entry['name']."-".$gosa] = $entry;
809                                         }
810                                 }
811                         }
812                         return($res);
813                 }else{
814                         echo "not connected";
815                         return(false);
816                 }
817         }
819         /* This function returns all used devices 
820      */
821         function getUsedDevices($computerID)
822         {
823                 $deviceMappingGOsaGlpi = array_flip($this->deviceMappingGOsaGlpi);
824                 $deviceMappingTableNameID       = $this->deviceMappingTableNameID;
825         
826                 if($this->is_connected){
827                         $qry = "SELECT * FROM glpi_computer_device WHERE FK_computers=".$computerID.";";
828                         $res = $this->query($qry);
829                 
830                         $ret = array();
831                         foreach($deviceMappingGOsaGlpi as $GOsa => $glpi){
832                                 $ret[$GOsa] = array();
833                         }
835                         $tbls = array_flip($deviceMappingTableNameID);
837                         foreach($res as $device){
838                                 $devtype = $tbls[$device['device_type']];
839                                 $tbl_name = $deviceMappingGOsaGlpi[$devtype];
840                                 $qry = ("SELECT * FROM ".$tbl_name." WHERE ID=".$device['FK_device'].";");
841                                 $res2 = $this->query($qry);
842                                 if(count($res2)!=0){
843                                         $ret[$devtype][$res2[0]['designation']]=$res2[0];
844                                 }
846                         $qry = "SELECT * FROM glpi_connect_wire WHERE type=4 AND end2=".$computerID.";";
847                         $res2 = $this->query($qry);
848                         foreach($res2 as $monitor){
849                                 $qry = "SELECT * FROM glpi_monitors WHERE ID=".$monitor['end1'].";";
850                                 $res3 = $this->query($qry);
851                                 foreach($res3 as $moni){
852                                         $ret['monitor'][$moni['name']]=$moni;
853                                 }
854                         }
858                         }
859                         return($ret);
860                 }else{
861                         echo "not connected";
862                         return(false);
863                 }
864         }
866         /* This function removes all given devices from a computer, specified by $id
867        In the next step all devices specified by devices will be added.
868          */
869         function addDevicesToComputer($devices, $id)
870         {
871                 $deviceMappingGOsaGlpi = array_flip($this->deviceMappingGOsaGlpi);
872                 $deviceMappingTableNameID       = $this->deviceMappingTableNameID;
873         
874                 if(($id == "" )||(!is_numeric($id))){
875                         return (false); 
876                 }
877                 if($this->is_connected){
878                         $qry = "DELETE FROM glpi_computer_device WHERE FK_computers=".$id.";";
879                         $this->query($qry);     
880                 
881                         foreach($devices as $type => $entries){
882                                 foreach($entries as $entry){
883                                         if($type=="monitor"){
884                                                 $str = "INSERT INTO glpi_connect_wire (end1,end2,type) 
885                                                 VALUES (".$entry['ID'].",".$id.",4);";
886                                         }else{
887                                                 $str = "INSERT INTO glpi_computer_device (device_type,FK_device,FK_computers) 
888                                                 VALUES (".$deviceMappingTableNameID[$type].",".$entry['ID'].",".$id.");";
889                                         }
890                                         $this->query($str);
891                                 }
892                         }
893                 
895                 }else{
896                         echo "not connected";
897                         return(false);
898                 }
900         }
902         function removeComputerInformations($name)
903         {
904                 if($this->is_connected){
905                         $tmp = $this->query("SELECT * FROM glpi_computers WHERE name='".$name."';");
906                         if(isset($tmp[0])){
907                                 $id = $tmp[0]['ID'];
908                                 $this->query("DELETE FROM glpi_connect_wire WHERE end2=".$id.";");
909                                 $this->query("DELETE FROM glpi_computer_device WHERE FK_computers=".$id.";");
910                                 return($this->query("DELETE FROM glpi_computers WHERE ID=".$id.";"));
911                         }else{
912                                 echo "can't remove not existing entry";
913                                 return(false);
914                         }
915                 }else{
916                         echo "not connected";
917                         return(false);
918                 }
919         }
921         function is_connected()
922         {
923                 return($this->is_connected);
924         }
926         function addAttachmentsToComputer($attr,$id)
927         {
928         if(($id == "" )||(!is_numeric($id))){
929             return (false);
930         }
931         if($this->is_connected){
932             $qry = "DELETE FROM glpi_doc_device WHERE (FK_device=".$id.") AND (device_type=1);";
933             $this->query($qry);
934                         
935                         foreach($attr as $aid => $entry){
936                                 $str = "INSERT INTO glpi_doc_device (FK_doc,FK_device,device_type,is_template) 
937                                                                                                 VALUES
938                                                                                                         ($aid,$id,1,'0');";
939                                 $this->query($str);
940                         }
941         }else{
942             echo "not connected";
943             return(false);
944         }
945         }
947         function getAssignAttachments($id)
948         {
950                 if($this->is_connected){
951                         $qry= "SELECT * FROM glpi_doc_device WHERE (device_type=1) AND (FK_device=".$id.");";
952                         $ret = $this->query($qry);
953                         return($ret);
954                 }else{
955             echo "not connected";
956             return(false);
957         }
958         }
960         function deleteAttachment($id)
961         {
962                 if($this->is_connected){
963                         $qry = "DELETE FROM glpi_docs WHERE ID=".$id."";
964                         $this->query($qry);
965                 }else{
966             echo "not connected";
967             return(false);
968         }
969         }
970         
971         function getAttachments()
972         {
973                 $ret = array();
974                 if($this->is_connected){
975                         $qry = "SELECT * FROM glpi_docs WHERE name!='';";
976                         $re = $this->query($qry);
978                         foreach($re as $entry){
979                                 $ret[$entry['ID']]=$entry;
980                         }
982                         return($ret);
983                 }else{
984             echo "not connected";
985             return(false);
986         }
987         }
988         
989         function saveAttachments($attrs,$id = -1)
990         {
991                 if($this->is_connected){
992                         $atr = array("name","filename","rubrique","mime","date_mod","comment","deleted","link");
993                         $tmp = array();
994                         foreach($atr as $at){
995                                 if(isset($attrs[$at])){
996                                         $tmp[$at] = $attrs[$at];
997                                 }
998                         }
999                         if(count($tmp)==0){
1000                                 return(false);
1001                         }else{
1003                                 // Add
1004                                 if($id == -1){
1005                                         $str = "INSERT INTO glpi_docs ";
1006                                         $namen = "";
1007                                         $values= "";
1008                                         foreach($tmp as $name => $value){       
1009                                                 $namen .= $name.", ";
1010                                                 if(is_numeric($value)){
1011                                                         $values .= $value.", ";
1012                                                 }else{
1013                                                         $values .= "'".$value."', ";
1014                                                 }
1015                                         }
1016                                         $values = preg_replace("/, $/","",$values);
1017                                         $namen  = preg_replace("/, $/","",$namen);
1018                                         $str .= "(".$namen.") VALUES (".$values.");";
1019                                 }else{
1020                                         $str = "UPDATE glpi_docs SET ";
1021                                         foreach($tmp as $name => $value){       
1022                                                 $str .= $name."= ";
1023                                                 if(is_numeric($value)){
1024                                                         $str .= $value.", ";
1025                                                 }else{
1026                                                         $str .= "'".$value."', ";
1027                                                 }
1028                                         }
1029                                         $str = preg_replace("/, $/","",$str);
1030                                         $str .= " WHERE ID=".$id.";";
1031                                 }
1032                                 $this->query($str);
1033                         }
1034                 }else{
1035                         echo "not connected";
1036                         return(false);
1037                 }
1038         }
1040         
1041         /* Check if given attachment id is used in any Device 
1042                 ( - avoid removing of used attachments)
1043      */
1044         function is_attachmentUsed($id)
1045         {
1046                 if($this->is_connected){
1047                         $ret = array();
1048                         $qry = "SELECT t.name FROM glpi_computers as t, glpi_doc_device WHERE t.ID = glpi_doc_device.FK_device AND FK_doc =".$id." LIMIT 3;";
1049                         $res = $this->query($qry);
1050                         foreach($res as $val){
1051                                 $ret[$val['name']] = $val['name'];
1052                         }
1053                         return($ret);
1054                 }else{
1055             echo "not connected";
1056             return(false);
1057         }
1058         }
1060                 
1061         /* Monitor handling 
1062      */
1063     function getMonitors()
1064         {
1065                 if($this->is_connected){
1066                         $qry= "SELECT * FROM glpi_monitors;";
1067                         return($this->query($qry));     
1068                 
1069                 }else{
1070             echo "not connected";
1071             return(false);
1072         }
1073         }
1075     function updatedMonitor()
1076         {
1077                 if($this->is_connected){
1078 //                      $qry= "SELECT * FROM glpi_monitors;";
1079 //                      return($this->query($qry));     
1080                 
1081                 }else{
1082             echo "not connected";
1083             return(false);
1084         }
1085         }
1087     function addMonitor()
1088         {
1089                 if($this->is_connected){
1090 //                      $qry= "SELECT * FROM glpi_monitors;";
1091 //                      return($this->query($qry));     
1092                 
1093                 }else{
1094             echo "not connected";
1095             return(false);
1096         }
1097         }
1099     function removeMonitor($id)
1100         {
1101                 if($this->is_connected){
1102                         $qry= "DELETE FROM glpi_monitors WHERE ID=".$id.";";
1103                         $this->query($qry);     
1104                 }else{
1105             echo "not connected";
1106             return(false);
1107         }
1108         }
1110     function getMonitorTypes()
1111         {
1112                 if($this->is_connected){
1113                         $qry= "SELECT * FROM glpi_type_monitors;";
1114                         return($this->query($qry));     
1115                 
1116                 }else{
1117             echo "not connected";
1118             return(false);
1119         }
1120         }
1122     function getLocationTypes()
1123         {
1124                 if($this->is_connected){
1125                         $qry= "SELECT * FROM glpi_dropdown_locations;";
1126                         return($this->query($qry));     
1127                 
1128                 }else{
1129             echo "not connected";
1130             return(false);
1131         }
1132         }
1134     function getStateTypes()
1135         {
1136                 if($this->is_connected){
1137                         $qry= "SELECT * FROM glpi_dropdown_state;";
1138                         return($this->query($qry));     
1139                 }else{
1140             echo "not connected";
1141             return(false);
1142         }
1143         }
1145         
1146         /* Printer functions
1147      */
1149         /* is printer type used ? 
1150      */
1151         function is_printerTypeUsed($id)
1152         {
1153                 if($this->is_connected){
1154                         $qry = "SELECT * FROM glpi_printers WHERE type=".$id.";";
1155                         $res = $this->query( $qry);
1156                         $ret =array();
1157                         foreach($res as $entry){
1158                                 $ret[$entry['ID']] = $entry['name'];
1159                         }
1160                         return($ret);
1161                 }else{
1162             echo "not connected";
1163             return(false);
1164         }
1165         }
1167         /* This functions checks if the selected computer/network 
1168            device is already available in the db
1169          */
1170         function is_printer_account($dn)
1171         {
1172                 if(!$this->is_connected){
1173                         $this->lasterror ="Can't query anything, if we aren't connected.";
1174                         return(false);
1175                 }else{
1176                         $qry = "SELECT * FROM glpi_printers WHERE name='".$dn."';";
1177                         $res = $this->query($qry);
1178                         if(count($res)==0){
1179                                 return(false);
1180                         }else{
1181                                 return(true);
1182                         }
1183                 }
1184         }
1186         /* This function returns all available data 
1187        from a specified dn
1188      */
1189         function getPrinterInformations($name)
1190         {
1191                 if($this->is_connected){        
1192                         $ret = $this->query("SELECT * FROM glpi_printers WHERE name='".$name."';");
1193                         return($ret);           
1194                 }else{
1195                         echo "not connected";
1196                         return(false);
1197                 }
1198         }
1200         /* Get Printer attachments
1201      */
1202         function getAssignPrinterAttachments($id)
1203         {
1205                 if($this->is_connected){
1206                         $qry= "SELECT * FROM glpi_doc_device WHERE (device_type=3) AND (FK_device=".$id.");";
1207                         $ret = $this->query($qry);
1208                         return($ret);
1209                 }else{
1210             echo "not connected";
1211             return(false);
1212         }
1213         }
1215         /* Printer types 
1216        Returns all defined printer types 
1217          */
1218         function getPrinterTypes()
1219         {
1220                 if($this->is_connected){
1221                         $ret = array();
1222                         $tmp = ($this->query("SELECT * FROM glpi_type_printers ORDER BY name; "));
1223                         foreach($tmp as $t){
1224                                 $ret[$t['ID']]=$t['name'];
1225                         }
1226                         return($ret);
1227                 }else{
1228                         echo "not connected";
1229                         return(false);
1230                 }
1231         }
1233         /* Add pritner types 
1234        Add one entry to the printer types 
1235      */
1236         function addPrinterType($name)
1237         {
1238                 if($this->is_connected){
1239                         $tmp = $this->query("SELECT * FROM glpi_type_printers WHERE name='".$name."';");
1240                         if(isset($tmp[0])){
1241                                 //echo "such an entry already exists";
1242                                 return(false);
1243                         }else{  
1244                                 return($this->query("INSERT INTO glpi_type_printers (name) VALUES ('".$name."');"));
1245                         }
1246                 }else{
1247                         echo "not connected";
1248                         return(false);
1249                 }
1250         }
1252         /* remove printer types 
1253        Remove one entry from the printer types (specified by ID=$id)
1254      */
1255         function removePrinterType($id)
1256         {
1257                 if($this->is_connected){
1258                         $tmp = $this->query("SELECT * FROM glpi_type_printers WHERE ID=".$id.";");
1259                         if(isset($tmp[0])){
1260                                 return($this->query("DELETE FROM glpi_type_printers WHERE ID=".$id.";"));       
1261                         }else{
1262                                 echo "can't remove not existing entry";
1263                                 return(false);  
1264                         }
1265                 }else{
1266                         echo "not connected";
1267                         return(false);
1268                 }
1269         }
1271         /* Update printer types 
1272            Update a printer type        
1273         */
1274         function updatePrinterType($name,$id)
1275         {
1277                 if($this->is_connected){
1278                         $tmp = $this->query("SELECT * FROM glpi_type_printers WHERE ID=".$id.";");
1279                         if(isset($tmp[0])){
1280                                 return($this->query("UPDATE glpi_type_printers SET name='".$name."' WHERE ID=".$id.";"));       
1281                         }else{
1282                                 echo "can't update not existing entry";
1283                                 return(false);  
1284                         }
1285                 }else{
1286                         echo "not connected";
1287                         return(false);
1288                 }
1289         }
1292         /*  This fucntions updates an already existing entry 
1293      */
1294         function updatePrinterInformations($array,$name)
1295         {
1296                 if(!is_array($array)){
1297                         echo "updatePrinterInformations: first paraeter must be an array";
1298                 }elseif($this->is_connected){
1299                         $tmp = $this->query("SELECT * FROM  glpi_printers WHERE name='".$name."';");
1300                         if(isset($tmp[0])){
1302                                 $atr = array(   "ID","name","serial","otherserial","contact","contact_num",
1303                                                 "tech_num","comments","date_mod","location","domain","network","ramSize","flags_serial","flags_par","flags_usb",
1304                                                 "model","type","is_template","tplname","FK_glpi_enterprise","deleted");
1305                                 $v = "";
1306                                 foreach($atr as $at){
1307                                         if(isset($array[$at])){
1308                                                 $v .= " ".$at."='".$array[$at]."', ";
1309                                         }
1310                                 }
1311                                 if(empty($v)){
1312                                         echo "updateSystemInformations: no attributes given ";
1313                                         return(false);
1314                                 }else{
1315                                         $v = preg_replace("/, $/","",$v);
1316                                         return($this->query("UPDATE glpi_printers SET ".$v." WHERE name='".$name."';"));
1317                                 }
1318                         }else{
1319                                 echo "can't update not existing entry";
1320                                 return(false);
1321                         }
1322                 }else{
1323                         echo "not connected";
1324                         return(false);
1325                 }
1327         }
1329         /* This function adds a new inventory settings for printers
1330      */
1331         function addPrinterInformations($array)
1332         {
1333                 if(!is_array($array)){
1334                         echo "updateComputerInformations: first paraeter must be an array";
1335                 }elseif($this->is_connected){
1336                         $atr = array(   "ID","name","serial","otherserial","contact","contact_num",
1337                                         "tech_num","comments","date_mod","os","location","domain","network","ramSize","flags_serial","flags_par","flags_usb",
1338                                         "model","type","is_template","tplname","FK_glpi_enterprise","deleted");
1339                         $v = "";
1340                         $a = "";
1341                         foreach($atr as $at){
1342                                 if(isset($array[$at])){
1343                                         $a .= $at.", ";
1344                                         $v .= "'".$array[$at]."', ";
1345                                 }
1346                         }
1347                         if(empty($v)){
1348                                 echo "updateComputerInformations: no attributes given ";
1349                                 return(false);
1350                         }else{
1351                                 $a = preg_replace("/, $/","",$a);
1352                                 $v = preg_replace("/, $/","",$v);
1353                                 return($this->query("INSERT INTO glpi_printers (".$a.") VALUES (".$v.");"));
1354                         }
1355                 
1356                 }else{
1357                         echo "not connected";
1358                         return(false);
1359                 }
1360         }
1362         /* add atachment to given printer */
1363         function addAttachmentsToPrinter($attr,$id)
1364         {
1365         if(($id == "" )||(!is_numeric($id))){
1366             return (false);
1367         }
1368         if($this->is_connected){
1369             $qry = "DELETE FROM glpi_doc_device WHERE (FK_device=".$id.") AND (device_type=3);";
1370             $this->query($qry);
1371                         
1372                         foreach($attr as $aid => $entry){
1373                                 $str = "INSERT INTO glpi_doc_device (FK_doc,FK_device,device_type,is_template) 
1374                                                                                                 VALUES
1375                                                                                                         ($aid,$id,3,'0');";
1376                                 $this->query($str);
1377                         }
1378         }else{
1379             echo "not connected";
1380             return(false);
1381         }
1382         }
1384         function removePrinterInformations($name)
1385         {
1386                 if($this->is_connected){
1387                         $tmp = $this->query("SELECT * FROM glpi_printers WHERE name='".$name."';");
1388                         if(isset($tmp[0])){
1389                                 $id = $tmp[0]['ID'];
1390 //                              $this->query("DELETE FROM glpi_connect_wire WHERE end2=".$id.";");
1391                                 $this->query("DELETE FROM glpi_doc_device WHERE FK_device=".$id." AND device_type=3;");
1392                                 return($this->query("DELETE FROM glpi_printers WHERE ID=".$id.";"));
1393                         }else{
1394                                 echo "can't remove not existing entry";
1395                                 return(false);
1396                         }
1397                 }else{
1398                         echo "not connected";
1399                         return(false);
1400                 }
1401         }
1404         /* Cartridges 
1405      */
1406         /* return all assigned cartridges */
1407         function getUsedCartridges($printerID)
1408         {
1409                 if($this->is_connected){
1410                         $ret = array();
1411                         $qry = "SELECT 
1412                                                 c.date_use                      as date_use,
1413                                                 c.ID                            as ID, 
1414                                                 t.ID                            as type_ID,
1415                                                 t.name                          as name, 
1416                                                 c.FK_glpi_printers      as FK_glpi_printers, 
1417                                                 d.name                          as type_name 
1418                                         FROM 
1419                                                 glpi_dropdown_cartridge_type as d,
1420                                                 glpi_cartridges as c, 
1421                                                 glpi_cartridges_type as t 
1422                                         WHERE   c.FK_glpi_cartridges_type = t.ID 
1423                                                 AND t.type = d.ID 
1424                                                 AND c.FK_glpi_printers = ".$printerID.";"; 
1425                         $res = $this->query($qry);
1426                         foreach($res as $entry){
1427                                 $ret[$entry['ID']] = $entry;
1428                         }
1429                         return($ret);
1430                 }else{
1431                         echo "not connected";
1432                         return(false);
1433                 }
1434         }
1436         /* return all assigned cartridges */
1437         function getAvailableCartridgeTypes($printerTypeID)
1438         {
1439                 if($this->is_connected){
1440                         $ret = array();
1441                         $qry= "
1442         SELECT  
1443                         ct.ID                   as cartridgeID,
1444                         ct.name                 as cartridgeName,
1445                         pt.ID                   as printerTypeID,
1446                         pt.name                 as printerTypeName,
1447                         ct.type                 as cartridgeTypeID,
1448                         dt.name                 as cartridgeTypeName 
1449         FROM    
1450                         glpi_type_printers                              as pt, 
1451                         glpi_cartridges_type                    as ct, 
1452                         glpi_dropdown_cartridge_type    as dt, 
1453                         glpi_cartridges_assoc                   as ac
1454          WHERE 
1455                                 ac.FK_glpi_type_printer = pt.ID 
1456                         AND ac.FK_glpi_cartridges_type = ct.ID 
1457                         AND ct.type=dt.ID 
1458                         AND pt.ID=".$printerTypeID.";";
1459                         $res = $this->query($qry);
1460                         foreach($res as $entry){
1461                                 $ret[$entry['cartridgeID']] = $entry;
1462                         }
1463                         return($ret);
1464                 }else{
1465                         echo "not connected";
1466                         return(false);
1467                 }
1468         }
1470         function removeCartridgeFromPrinter($cartridgeID)
1471         {
1472                 if($this->is_connected){
1473                         $qry = "DELETE FROM glpi_cartridges WHERE ID=".$cartridgeID.";";
1474                         return($this->query($qry));
1475                 }else{
1476                         echo "not connected";
1477                         return(false);
1478                 }
1479         }
1481         function addCartridgeFromPrinter($printerID,$cartridgeID)
1482         {
1483                 if($this->is_connected){
1484                         $qry ="INSERT INTO 
1485                                                 glpi_cartridges (FK_glpi_cartridges_type,FK_glpi_printers,date_in,date_use) 
1486                                    VALUES 
1487                                                 (".$cartridgeID.",".$printerID.",'".date("Y-m-d")."','".date("Y-m-d")."');";    
1488                         return($this->query($qry));
1489                 }else{
1490                         echo "not connected";
1491                         return(false);
1492                 }
1493         }
1495         function is_cartridgeTypeUsed($id){
1496                 if($this->is_connected){
1497                         $qry = "SELECT p.ID,p.name as name FROM glpi_cartridges as c,glpi_printers as p WHERE p.ID=c.FK_glpi_printers AND c.FK_glpi_cartridges_type=".$id.";";
1498                         $res = $this->query($qry);
1499                         $ret =array();
1500                         foreach($res as $entry){
1501                                 $ret[$entry['ID']] = $entry['name'];
1502                         }
1503                         return($ret);
1504                 }else{
1505                         echo "not connected";
1506                         return(false);
1507                 }
1508         }
1510         function getCartridgeTypeInformations($id = "all"){
1511                 if($this->is_connected){
1512                         $ret = array();                 
1513                         if($id != "all"){
1514                                 $qry = "SELECT * FROM glpi_cartridges_type WHERE ID = ".$id.";";
1515                         }else{
1516                                 $qry = "SELECT * FROM glpi_cartridges_type;";
1517                         }
1518                 
1519                         $res = ($this->query($qry));
1520                         foreach($res as $entry){
1521                                 $ret[$entry['ID']] = $entry;
1522                         }       
1523                         return($ret);
1525                 }else{
1526                         echo "not connected";
1527                         return(false);
1528                 }
1529         }
1531         function getCartridgeTypes(){
1532                 if($this->is_connected){
1533                         $ret = array();                 
1534                         $qry = "SELECT * FROM glpi_dropdown_cartridge_type;";
1535                         $res = ($this->query($qry));
1536                         foreach($res as $entry){
1537                                 $ret[$entry['ID']] = $entry['name'];
1538                         }       
1539                         return($ret);
1541                 }else{
1542                         echo "not connected";
1543                         return(false);
1544                 }
1545         }
1549         /* check if given manufacturer ID ist still in use.
1550            The problem is, that nearly every table uses manufacturers .... 
1551      */
1552         function is_manufacturerUsed($id)
1553         {
1554                 if($this->is_connected){
1555                         $tables = array();
1556                         foreach($this->deviceMappingGOsaGlpi as $entry => $table){
1557                                 $tables[] = $entry;
1558                         }
1559                         $tables[] ="glpi_computers";
1560                         $tables[] ="glpi_cartridges_type";
1561                         $ret = array();
1562                         $i = 3;
1563                         foreach($tables as $tbl){
1564                                 if($i <= 0 ) continue;
1565                                 $qry = "SELECT * FROM ".$tbl." WHERE FK_glpi_enterprise = ".$id.";";
1566                                 $res = $this->query($qry);
1567                                 foreach($res as $entry){
1568                                         if($i <= 0 ) continue;
1569                                         if(isset($entry['designation'])){
1570                                                 $entry['name'] = $entry['designation'];
1571                                         }
1572                                         $i --;
1573                                                 $ret[] = $entry['name'];
1574                                 }
1575                         }
1576                         return($ret);
1577                 }else{
1578                         echo "not connected";
1579                         return(false);
1580                 }
1581         }
1583         /* Manufacturer 
1584            Updates already existing manufacturer
1585          */
1586         function Add_UpdateCatrigdeType($array,$array_printer_types)
1587         {
1588                 if(!is_array($array)){
1589                         echo "Add_UpdateCatrigdeType: first paraeter must be an array";
1590                 }elseif($this->is_connected){
1593                         $atr = array("name","ref","location","type","FK_glpi_enterprise","tech_num","deleted","comments","alarm");
1595                         /* Entry was edited */
1596                         if($array['ID']>0){
1597                                 $qry = "DELETE FROM glpi_cartridges_assoc WHERE FK_glpi_cartridges_type=".$array['ID'].";";
1599                                 $v = "";
1600                                 foreach($atr as $at){
1601                                         if(isset($array[$at])){
1602                                                 $v .= " ".$at."='".$array[$at]."', ";
1603                                         }
1604                                 }
1605                                 if(empty($v)){
1606                                         echo "Add_UpdateCatrigdeType: no attributes given ";
1607                                         return(false);
1608                                 }else{
1609                                         $v = preg_replace("/, $/","",$v);
1610                                         $qry = "UPDATE glpi_cartridges_type SET ".$v." WHERE ID='".$array['ID']."';";
1611                                         $this->query($qry);     
1612                                 }
1613                         }else{
1615                                 /* skip if name is in use*/
1616                                 $qry = "SELECT * FROM glpi_cartridges_type WHERE name='".$array['name']."';";
1617                                 if(count($this->query($qry))){
1618                                         return;
1619                                 }
1621                                 $str = "INSERT INTO glpi_cartridges_type ";
1622                                 $namen = "";
1623                                 $values= "";
1624                                 foreach($array as $name => $value){     
1625                                         $namen .= $name.", ";
1626                                         if(is_numeric($value)){
1627                                                 $values .= $value.", ";
1628                                         }else{
1629                                                 $values .= "'".$value."', ";
1630                                         }
1631                                 }
1632                                 $values = preg_replace("/, $/","",$values);
1633                                 $namen  = preg_replace("/, $/","",$namen);
1634                                 $str .= "(".$namen.") VALUES (".$values.");";
1635                                 $this->query($str);     
1636                                 $IDs = $this->query("SELECT ID FROM glpi_cartridges_type WHERE name='".$array['name']."';");
1637                                 if(count($IDs) > 1){
1638                                         echo "internal db error";
1639                                         return;
1640                                 }
1641                                 $array['ID'] = $IDs[0]['ID'];
1642                         }
1644                         foreach($array_printer_types as $id){
1645                                 $qry = "INSERT INTO glpi_cartridges_assoc 
1646                                         (FK_glpi_cartridges_type,FK_glpi_type_printer) 
1647                                         VALUES 
1648                                         (".$array['ID'].",".$id.")";
1650                                 $this->query($qry);
1651                         }
1652                 }else{
1653                         echo "not connected";
1654                         return(false);
1655                 }
1656         }
1658         function getSupportedPrinterTypeIDsForCartridge($cid)
1659         {
1660                 if($this->is_connected){
1661                         $ret = array();
1662                         $qry = "SELECT FK_glpi_type_printer FROM glpi_cartridges_assoc WHERE FK_glpi_cartridges_type = ".$cid.";";      
1663                         $res = $this->query($qry);
1664                                 
1665                         foreach($res as $entry => $value){
1666                                 $ret[$value['FK_glpi_type_printer']] = $value['FK_glpi_type_printer'];
1667                         }
1668                         return($ret);
1669                 }else{
1670                         echo "not connected";
1671                         return(false);
1672                 }
1673         }
1675         function removeCartridgeType($id){
1676                 if($this->is_connected){
1677                         $qry = "DELETE FROM glpi_cartridges_assoc WHERE FK_glpi_cartridges_type=".$id.";";
1678                         $this->query($qry);     
1679                         $qry = "DELETE FROM glpi_cartridges_type WHERE ID=".$id.";";
1680                         return($this->query($qry));     
1681                 }else{
1682                         echo "not connected";
1683                         return(false);
1684                 }
1685         }
1687         function getCartridgesWhichUseThisType($id)
1688         {
1689                 if($this->is_connected){
1690                         $qry = "SELECT * FROM glpi_cartridges WHERE FK_glpi_cartridges_type=".$id.";";
1691                         $ret  = $this->query($qry);
1692                         return($ret);
1693                 }else{
1694                         echo "not connected";
1695                         return(false);
1696                 }
1697         }
1700         /* Add pritner types 
1701        Add one entry to the cartridgeType types 
1702      */
1703         function addCartridgeDropdownType($name)
1704         {
1705                 if($this->is_connected){
1706                         $tmp = $this->query("SELECT * FROM glpi_dropdown_cartridge_type WHERE name='".$name."';");
1707                         if(isset($tmp[0])){
1708                                 //echo "such an entry already exists";
1709                                 return(false);
1710                         }else{  
1711                                 return($this->query("INSERT INTO glpi_dropdown_cartridge_type (name) VALUES ('".$name."');"));
1712                         }
1713                 }else{
1714                         echo "not connected";
1715                         return(false);
1716                 }
1717         }
1719         /* remove cartridgeType types 
1720        Remove one entry from the cartridgeType types (specified by ID=$id)
1721      */
1722         function removeCartridgeDropdownType($id)
1723         {
1724                 if($this->is_connected){
1725                         $tmp = $this->query("SELECT * FROM glpi_dropdown_cartridge_type WHERE ID=".$id.";");
1726                         if(isset($tmp[0])){
1727                                 return($this->query("DELETE FROM glpi_dropdown_cartridge_type WHERE ID=".$id.";"));     
1728                         }else{
1729                                 echo "can't remove not existing entry";
1730                                 return(false);  
1731                         }
1732                 }else{
1733                         echo "not connected";
1734                         return(false);
1735                 }
1736         }
1738         /* Update cartridgeType  
1739            Update a cartridgeType       
1740         */
1741         function updateCartridgeDropdownType($name,$id)
1742         {
1744                 if($this->is_connected){
1745                         $tmp = $this->query("SELECT * FROM glpi_dropdown_cartridge_type WHERE ID=".$id.";");
1746                         if(isset($tmp[0])){
1747                                 return($this->query("UPDATE glpi_dropdown_cartridge_type SET name='".$name."' WHERE ID=".$id.";"));     
1748                         }else{
1749                                 echo "can't update not existing entry";
1750                                 return(false);  
1751                         }
1752                 }else{
1753                         echo "not connected";
1754                         return(false);
1755                 }
1756         }
1758         function getUsedDropdownTypes($id=false)
1759         {
1760                 if($this->is_connected){
1761                         if($id){
1762                                 $qry = "SELECT distinct(type) FROM glpi_cartridges_type WHERE type = ".$id.";";
1763                         }else{
1764                                 $qry = "SELECT distinct(type) FROM glpi_cartridges_type;";
1765                         }
1766                         return($this->query($qry));
1767                 }else{
1768                         echo "not connected";
1769                         return(false);
1770                 }
1771         }
1774 //$s = new glpiDB("vserver-01","glpi","tester","glpi");
1775 //print_r($s->query("SELECT * FROM glpi_computers"));
1776 //$s->getComputerInformations("1 OR (c.ID<10000)");
1777 ?>