Code

Updated System type & os type management, can't create existing entries, can't delete...
[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;");
208                         foreach($tmp as $t){
209                                 $ret[$t['ID']]=$t['name'];
210                         }
211                         return($ret);
212                 }else{
213                         echo "not connected";
214                         return(false);
215                 }
216         }
218         /* Manufacturer 
219            Returns single manufacturer
220         */      
221         function getEnterprise($id)
222         {
223                 if($this->is_connected){
224                         $ret = array();
225                         $tmp = $this->query("SELECT * FROM glpi_enterprises WHERE ID=".$id.";");
226                         return($tmp);
227                 }else{
228                         echo "not connected";
229                         return(false);
230                 }
231         }
233         /* Manufacturer 
234        Updates already existing manufacturer
235     */
236         function updateEnterprise($array,$id)
237         {
238                 if(!is_array($array)){
239                         echo "updateEnterprisesType: first paraeter must be an array";
240                 }elseif($this->is_connected){
241                         $tmp = $this->query("SELECT * FROM glpi_enterprises WHERE ID='".$id."';");
242                         if(isset($tmp[0])){
243                                 $atr = array("ID","name","type","address","website","phonenumber","comments","deleted","fax","email");
245                                 $v = "";
246                                 foreach($atr as $at){
247                                         if(isset($array[$at])){
248                                                 $v .= " ".$at."='".$array[$at]."', ";
249                                         }
250                                 }
251                                 if(empty($v)){
252                                         echo "updateEnterprisesType: no attributes given ";
253                                         return(false);
254                                 }else{
255                                         $v = preg_replace("/, $/","",$v);
256                                         return($this->query("UPDATE glpi_enterprises SET ".$v." WHERE ID='".$id."';")); 
257                                 }
258                         }else{
259                                 echo "can't update not existing entry";
260                                 return(false);  
261                         }
262                 }else{
263                         echo "not connected";
264                         return(false);
265                 }
266         }
268         /* Manufacturer
269            Add new manufacturer
270          */
271         function addEnterprise($array)
272         {
273                 if(!is_array($array)){
274                         echo "addUser: first paraeter must be an array";
275                 }elseif($this->is_connected){
276                         $atr = array("ID","name","type","address","website","phonenumber","comments","deleted","fax","email");
277                         $v = "";
278                         $a = "";
279                         foreach($atr as $at){
280                                 if(isset($array[$at])){
281                                         $a .= $at.", ";
282                                         $v .= "'".$array[$at]."', ";
283                                 }
284                         }
285                         if(empty($v)){
286                                 echo "addUser: no attributes given ";
287                                 return(false);
288                         }else{
289                                 $a = preg_replace("/, $/","",$a);
290                                 $v = preg_replace("/, $/","",$v);
291                                 return($this->query("INSERT INTO glpi_enterprises (".$a.") VALUES (".$v.");"));
292                         }
293                 
294                 }else{
295                         echo "not connected";
296                         return(false);
297                 }
299         }
301         /*      Manufacturer
302                 remove manufacturer
303          */
304         function removeEnterprise($id)
305         {
306                 if($this->is_connected){
307                         $tmp = $this->query("SELECT * FROM glpi_enterprises WHERE ID=".$id.";");
308                         if(isset($tmp[0])){
309                                 return($this->query("DELETE FROM glpi_enterprises WHERE ID=".$id.";"));
310                         }else{
311                                 echo "can't remove not existing entry";
312                                 return(false);
313                         }
314                 }else{
315                         echo "not connected";
316                         return(false);
317                 }
318         }
320         /* Operating systems 
321            Returns all OSs
322          */
323         function getOSTypes($keys = false)
324         {
325                 if($this->is_connected){
326                         $ret = array();
327                         $tmp=($this->query("SELECT * FROM glpi_dropdown_os ORDER by name;"));
329                         if($keys){
330                                 foreach($tmp as $t){
331                                         $ret[$t['name']]=$t['ID'];
332                                 }
333                         }else{
334                                 foreach($tmp as $t){
335                                         $ret[$t['ID']]=$t['name'];
336                                 }
337                         }
338                         return($ret);
340                 }else{
341                         echo "not connected";
342                         return(false);
343                 }
344         }
346         /* Operating system is used ? */
347         function is_osUsed($ID){
348                 if($this->is_connected){
349                         $ret = array();
350                         $qry="SELECT name,type FROM glpi_computers WHERE os=".$ID." LIMIT 3;";
351                         $res = $this->query($qry);
352                         foreach($res as $val){
353                                 $ret[$val['name']] = $val['name'];
354                         }
355                         return($ret);
356                 }else{
357                         echo "not connected";
358                         return(false);
359                 }
360         }
363         /*  Operating systems
364                 Add a new operating system to the dropdown menus   
365          */
366     function addOS($name)
367     {
368         if($this->is_connected){
369             $tmp = $this->query("SELECT * FROM glpi_dropdown_os WHERE name='".$name."';");
370             if(isset($tmp[0])){
371                 echo "such an entry already exists";
372                 return(false);
373             }else{
374                 return($this->query("INSERT INTO glpi_dropdown_os (name) VALUES ('".$name."');"));
375             }
376         }else{
377             echo "not connected";
378             return(false);
379         }
380     }
382     /* Operating systems 
383        remove one OS entry
384      */
385     function removeOS_byID($id)
386     {
387         if($this->is_connected){
388             $tmp = $this->query("SELECT * FROM glpi_dropdown_os WHERE ID=".$id.";");
389             if(is_array($tmp[0])){
390                 return($this->query("DELETE FROM glpi_dropdown_os WHERE ID=".$id.";"));
391             }else{
392                 echo "can't remove not existing entry";
393                 return(false);
394             }
395         }else{
396             echo "not connected";
397             return(false);
398         }
399     }
401         /* Operating systems 
402            Update existing OS entry
403         */
404         function updateOS($name,$id)
405         {
406                 if($this->is_connected){
407                         $tmp = $this->query("SELECT * FROM glpi_dropdown_os WHERE ID=".$id.";");
408                         if(isset($tmp[0])){
409                                 return($this->query("UPDATE glpi_dropdown_os SET name='".$name."' WHERE ID=".$id.";")); 
410                         }else{
411                                 echo "can't update not existing entry";
412                                 return(false);  
413                         }
414                 }else{
415                         echo "not connected";
416                         return(false);
417                 }
418         }
420         /* This returns all available glpi users 
421      */
422         function getUsers()
423         {
424                 if($this->is_connected){
425                         $ret = array();
426                         $tmp = ($this->query("SELECT * FROM glpi_users"));
427                         foreach($tmp as $user){
428                                 $ret[$user['ID']]=$user['name'];
429                         }
430                         return($ret);
432                 }else{
433                         echo "not connected";
434                         return(false);
435                 }
436         }
438         /* this function adds a new glpi user
439      */
440         function addUser($array,$dn)
441         {
442                 if(!is_array($array)){
443                         echo "addUser: first paraeter must be an array";
444                 }elseif($this->is_connected){
445                         $array['name']=$dn;
446                         $atr = array("name","phone","email");
447                         $v = "";
448                         $a = "";
449                         foreach($atr as $at){
450                                 if(isset($array[$at])){
451                                         $a .= $at.", ";
452                                         $v .= "'".$array[$at]."', ";
453                                 }
454                         }
455                         if(empty($v)){
456                                 echo "addUser: no attributes given ";
457                                 return(false);
458                         }else{
459                                 $a = preg_replace("/, $/","",$a);
460                                 $v = preg_replace("/, $/","",$v);
461                                 return($this->query("INSERT INTO glpi_users (".$a.") VALUES (".$v.");"));
462                         }
463                 
464                 }else{
465                         echo "not connected";
466                         return(false);
467                 }
469         }
471         /* This function updates a glpi user 
472        with the given data
473      */
474         function updateUser($array,$dn)
475         {
476                 if(!is_array($array)){
477             echo "updateUser: first paraeter must be an array";
478         }elseif($this->is_connected){
479             $tmp = $this->query("SELECT * FROM  glpi_users WHERE name='".$dn."';");
480             if(isset($tmp[0])){
482                                 $atr = array("name","phone","email");
483                 $v = "";
484                 foreach($atr as $at){
485                     if(isset($array[$at])){
486                         $v .= " ".$at."='".$array[$at]."', ";
487                     }
488                 }
489                 if(empty($v)){
490                     echo "UpdateUser: no attributes given ";
491                     return(false);
492                 }else{
493                     $v = preg_replace("/, $/","",$v);
494                     return($this->query("UPDATE glpi_users SET ".$v." WHERE name='".$dn."';"));
495                 }
496             }else{
497                 echo "can't update not existing entry";
498                 return(false);
499             }
500         }else{
501             echo "not connected";
502             return(false);
503         }
505         }
507         /* This function returns all available data 
508        from a specified dn
509      */
510         function getComputerInformations($name)
511         {
512                 if($this->is_connected){        
513                         $ret = $this->query("SELECT * FROM glpi_computers WHERE name='".$name."';");
514                         return($ret);           
515                 }else{
516                         echo "not connected";
517                         return(false);
518                 }
519         }
521         /*  This fucntions updates an already existing entry 
522      */
523         function updateComputerInformations($array,$name)
524         {
525                 if(!is_array($array)){
526                         echo "updateComputerInformations: first paraeter must be an array";
527                 }elseif($this->is_connected){
528                         $tmp = $this->query("SELECT * FROM  glpi_computers WHERE name='".$name."';");
529                         if(isset($tmp[0])){
531                                 $atr = array(   "ID","name","serial","otherserial","contact","contact_num",
532                                                 "tech_num","comments","date_mod","os","location","domain","network",
533                                                 "model","type","is_template","tplname","FK_glpi_enterprise","deleted");
534                                 $v = "";
535                                 foreach($atr as $at){
536                                         if(isset($array[$at])){
537                                                 $v .= " ".$at."='".$array[$at]."', ";
538                                         }
539                                 }
540                                 if(empty($v)){
541                                         echo "updateComputerInformations: no attributes given ";
542                                         return(false);
543                                 }else{
544                                         $v = preg_replace("/, $/","",$v);
545                                         return($this->query("UPDATE glpi_computers SET ".$v." WHERE name='".$name."';"));
546                                 }
547                         }else{
548                                 echo "can't update not existing entry";
549                                 return(false);
550                         }
551                 }else{
552                         echo "not connected";
553                         return(false);
554                 }
556         }
558         /* This function adds a new inventory device (computer phone etc)
559      */
560         function addComputerInformations($array)
561         {
562                 if(!is_array($array)){
563                         echo "updateComputerInformations: first paraeter must be an array";
564                 }elseif($this->is_connected){
565                         $atr = array(   "ID","name","serial","otherserial","contact","contact_num",
566                                         "tech_num","comments","date_mod","os","location","domain","network",
567                                         "model","type","is_template","tplname","FK_glpi_enterprise","deleted");
568                         $v = "";
569                         $a = "";
570                         foreach($atr as $at){
571                                 if(isset($array[$at])){
572                                         $a .= $at.", ";
573                                         $v .= "'".$array[$at]."', ";
574                                 }
575                         }
576                         if(empty($v)){
577                                 echo "updateComputerInformations: no attributes given ";
578                                 return(false);
579                         }else{
580                                 $a = preg_replace("/, $/","",$a);
581                                 $v = preg_replace("/, $/","",$v);
582                                 return($this->query("INSERT INTO glpi_computers (".$a.") VALUES (".$v.");"));
583                         }
584                 
585                 }else{
586                         echo "not connected";
587                         return(false);
588                 }
590         }
592         /* this functions checks if the given Device 
593      * already exists 
594      */
595         function deviceExists($attr)
596         {
597                 $deviceMappingGOsaGlpi = $this->deviceMappingGOsaGlpi;
598                 if($this->is_connected){        
599                         $arr = array_flip($deviceMappingGOsaGlpi);
601                         $tbl_name = $arr[$attr['device_type']];
602                         if(!isset($attr['ID'])){
603                                 return(false);
604                         }else{
605                                 $qry = "SELECT * FROM ".$tbl_name." WHERE ID=".$attr['ID'].";";
606                                 $res = $this->query($qry);
607                                 if(count($res) != 0){
608                                         return(true);
609                                 }
610                         }
611                 }else{
612                         echo "not connected";
613                         return(false);
614                 }
616                 return(false);
617         }
619                 
620         /* Check if given device is used by some accounts
621      *  (helpfull to avoid removement of used devices)
622      */
623         function is_deviceUsed($item)
624         {
625                 $deviceMappingGOsaGlpi          = array_flip($this->deviceMappingGOsaGlpi);
626                 $deviceMappingTableNameID       = $this->deviceMappingTableNameID;
627                 if($this->is_connected){
628                         $tablename =  $deviceMappingGOsaGlpi[$item['device_type']];
629                         $type = $item['device_type'];
631                         $ret = array();
633                         if($type=="monitor"){
634                                 $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;"; 
635                         }else{
636                                 $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]." ;";
637                         }
639                         $res = $this->query($str);
640         
641                         foreach($res as $val){
642                                 $ret[$val['name']] = $val['name'];
643                         }       
645                         return($ret);//count($this->query($str)));
646                 }else{
647                         echo "not connected";
648                         return(false);
649                 }
650                         
651         }
654         /* This functions deletes a specified entry 
655      * from our device tables 
656      */
657         function deleteDevice($attr)
658         {
659                 $deviceMappingGOsaGlpi = $this->deviceMappingGOsaGlpi;
660                 if($this->is_connected){
661                         $arr = array_flip($deviceMappingGOsaGlpi);
662                         
663                         $device_type = $attr['device_type'];
664                         unset($attr['device_type']);                    
666                         $tbl_name = $arr[$device_type];
668                         $this->query("DELETE FROM ".$tbl_name." WHERE ID=".$attr['ID'].";");    
669                 }else{
670                         echo "not connected";
671                         return(false);
672                 }
673         }
675         /* This funtions updated an already existing device
676      */
677         function updateDevices($attr)
678         {
679                 $deviceMappingGOsaGlpi = $this->deviceMappingGOsaGlpi;
680                 if($this->is_connected){
681                         $arr = array_flip($deviceMappingGOsaGlpi);
682                         
683                         $device_type = $attr['device_type'];
684                         unset($attr['device_type']);                    
686                         $tbl_name = $arr[$device_type];
688                         $str = "UPDATE ".$tbl_name." SET ";
689                         foreach($attr as $name => $value){
690                                 $str.=$name."='".$value."', ";
691                         }
692                         $str = preg_replace("/, $/","",$str);
693                         $str .= " WHERE ID=".$attr['ID'].";";
694                         $this->query($str);     
695                 }else{
696                         echo "not connected";
697                         return(false);
698                 }
699         }
701         /* Returns all possible RAM types 
702      * like SDRAM , DIMM .....
703      */
704         function getRAMTypes()
705         {
706                 if($this->is_connected){
707                         $ret = array();
708                         $tmp = ($this->query("SELECT * FROM glpi_dropdown_ram_type;"));
709                         foreach($tmp as $t){
710                                 $ret[$t['ID']]=$t['name'];
711                         }
712                         return($ret);
713                 }else{
714                         echo "not connected";
715                         return(false);
716                 }
717         }
718         
719         /* Returns all possible HDD connection types 
720      * like IDE SCSI ...
721      */
722         function getGlpiDeviceControlTypes()
723         {
724                 if($this->is_connected){
725                         $ret = array();
726                         $tmp = ($this->query("SELECT * FROM glpi_dropdown_hdd_type;"));
727                         foreach($tmp as $t){
728                                 $ret[$t['ID']]=$t['name'];
729                         }
730                         return($ret);
731                 }else{
732                         echo "not connected";
733                         return(false);
734                 }
735         }
736         
737         /* Returns all possible gfx card connection types
738      * like PCI-X PCI AGP ....
739          */
740         function getGlpiGfxControlTypes()
741         {
742                 if($this->is_connected){
743                         $ret = array();
744                         $tmp = ($this->query("SELECT * FROM glpi_dropdown_hdd_type;"));
745                         foreach($tmp as $t){
746                                 $ret[$t['ID']]=$t['name'];
747                         }
748                         return($ret);
749                 }else{
750                         echo "not connected";
751                         return(false);
752                 }
753         }
754         
755         /* Devices 
756        Adds a new single device to our db
757     */  
758         function addDevice($attr)
759         {
760                 $deviceMappingGOsaGlpi = $this->deviceMappingGOsaGlpi;
761                 if($this->is_connected){        
762                         $arr = array_flip($deviceMappingGOsaGlpi);
763                         
764                         $device_type = $attr['device_type'];
765                         unset($attr['device_type']);                    
767                         $tbl_name = $arr[$device_type];
768             $v = "";
769             $a = "";
770             foreach($attr as $name => $value){
771                 $a .= $name.", ";
772                 $v .= "'".$value."', ";
773             }
774             if(empty($v)){
775                 echo "addDevice: no attributes given ";
776                 return(false);
777             }else{
778                 $a = preg_replace("/, $/","",$a);
779                 $v = preg_replace("/, $/","",$v);
780                 return($this->query("INSERT INTO ".$tbl_name." (".$a.") VALUES (".$v.");"));
781             }
783         }else{
784             echo "not connected";
785             return(false);
786         }
787         }
789         /* Return all available devices 
790      */
791         function getDevices()
792         {
793                 $deviceMappingGOsaGlpi = $this->deviceMappingGOsaGlpi;
794                 if($this->is_connected){
795                         $arr = $deviceMappingGOsaGlpi; 
796                 
797                         $res = array();
798                         foreach($arr as $glpi => $gosa){
799                                 $qry = "SELECT * FROM ".$glpi.";";
800                                 $ret = $this->query($qry);
801                                 foreach($ret as $id => $entry){
802                                         $entry['device_type'] = $gosa;
803         
804                                         if(isset($entry['designation'])){
805                                                 $res[$entry['designation']."-".$gosa] = $entry;
806                                         }else{
807                                                 $res[$entry['name']."-".$gosa] = $entry;
808                                         }
809                                 }
810                         }
811                         return($res);
812                 }else{
813                         echo "not connected";
814                         return(false);
815                 }
816         }
818         /* This function returns all used devices 
819      */
820         function getUsedDevices($computerID)
821         {
822                 $deviceMappingGOsaGlpi = array_flip($this->deviceMappingGOsaGlpi);
823                 $deviceMappingTableNameID       = $this->deviceMappingTableNameID;
824         
825                 if($this->is_connected){
826                         $qry = "SELECT * FROM glpi_computer_device WHERE FK_computers=".$computerID.";";
827                         $res = $this->query($qry);
828                 
829                         $ret = array();
830                         foreach($deviceMappingGOsaGlpi as $GOsa => $glpi){
831                                 $ret[$GOsa] = array();
832                         }
834                         $tbls = array_flip($deviceMappingTableNameID);
836                         foreach($res as $device){
837                                 $devtype = $tbls[$device['device_type']];
838                                 $tbl_name = $deviceMappingGOsaGlpi[$devtype];
839                                 $qry = ("SELECT * FROM ".$tbl_name." WHERE ID=".$device['FK_device'].";");
840                                 $res2 = $this->query($qry);
841                                 if(count($res2)!=0){
842                                         $ret[$devtype][$res2[0]['designation']]=$res2[0];
843                                 }
845                         $qry = "SELECT * FROM glpi_connect_wire WHERE type=4 AND end2=".$computerID.";";
846                         $res2 = $this->query($qry);
847                         foreach($res2 as $monitor){
848                                 $qry = "SELECT * FROM glpi_monitors WHERE ID=".$monitor['end1'].";";
849                                 $res3 = $this->query($qry);
850                                 foreach($res3 as $moni){
851                                         $ret['monitor'][$moni['name']]=$moni;
852                                 }
853                         }
857                         }
858                         return($ret);
859                 }else{
860                         echo "not connected";
861                         return(false);
862                 }
863         }
865         /* This function removes all given devices from a computer, specified by $id
866        In the next step all devices specified by devices will be added.
867          */
868         function addDevicesToComputer($devices, $id)
869         {
870                 $deviceMappingGOsaGlpi = array_flip($this->deviceMappingGOsaGlpi);
871                 $deviceMappingTableNameID       = $this->deviceMappingTableNameID;
872         
873                 if(($id == "" )||(!is_numeric($id))){
874                         return (false); 
875                 }
876                 if($this->is_connected){
877                         $qry = "DELETE FROM glpi_computer_device WHERE FK_computers=".$id.";";
878                         $this->query($qry);     
879                 
880                         foreach($devices as $type => $entries){
881                                 foreach($entries as $entry){
882                                         if($type=="monitor"){
883                                                 $str = "INSERT INTO glpi_connect_wire (end1,end2,type) 
884                                                 VALUES (".$entry['ID'].",".$id.",4);";
885                                         }else{
886                                                 $str = "INSERT INTO glpi_computer_device (device_type,FK_device,FK_computers) 
887                                                 VALUES (".$deviceMappingTableNameID[$type].",".$entry['ID'].",".$id.");";
888                                         }
889                                         $this->query($str);
890                                 }
891                         }
892                 
894                 }else{
895                         echo "not connected";
896                         return(false);
897                 }
899         }
901         function removeComputerInformations($name)
902         {
903                 if($this->is_connected){
904                         $tmp = $this->query("SELECT * FROM glpi_computers WHERE name='".$name."';");
905                         if(isset($tmp[0])){
906                                 $id = $tmp[0]['ID'];
907                                 $this->query("DELETE FROM glpi_connect_wire WHERE end2=".$id.";");
908                                 $this->query("DELETE FROM glpi_computer_device WHERE FK_computers=".$id.";");
909                                 return($this->query("DELETE FROM glpi_computers WHERE ID=".$id.";"));
910                         }else{
911                                 echo "can't remove not existing entry";
912                                 return(false);
913                         }
914                 }else{
915                         echo "not connected";
916                         return(false);
917                 }
918         }
920         function is_connected()
921         {
922                 return($this->is_connected);
923         }
925         function addAttachmentsToComputer($attr,$id)
926         {
927         if(($id == "" )||(!is_numeric($id))){
928             return (false);
929         }
930         if($this->is_connected){
931             $qry = "DELETE FROM glpi_doc_device WHERE (FK_device=".$id.") AND (device_type=1);";
932             $this->query($qry);
933                         
934                         foreach($attr as $aid => $entry){
935                                 $str = "INSERT INTO glpi_doc_device (FK_doc,FK_device,device_type,is_template) 
936                                                                                                 VALUES
937                                                                                                         ($aid,$id,1,'0');";
938                                 $this->query($str);
939                         }
940         }else{
941             echo "not connected";
942             return(false);
943         }
944         }
946         function getAssignAttachments($id)
947         {
949                 if($this->is_connected){
950                         $qry= "SELECT * FROM glpi_doc_device WHERE (device_type=1) AND (FK_device=".$id.");";
951                         $ret = $this->query($qry);
952                         return($ret);
953                 }else{
954             echo "not connected";
955             return(false);
956         }
957         }
959         function deleteAttachment($id)
960         {
961                 if($this->is_connected){
962                         $qry = "DELETE FROM glpi_docs WHERE ID=".$id."";
963                         $this->query($qry);
964                 }else{
965             echo "not connected";
966             return(false);
967         }
968         }
969         
970         function getAttachments()
971         {
972                 $ret = array();
973                 if($this->is_connected){
974                         $qry = "SELECT * FROM glpi_docs WHERE name!='';";
975                         $re = $this->query($qry);
977                         foreach($re as $entry){
978                                 $ret[$entry['ID']]=$entry;
979                         }
981                         return($ret);
982                 }else{
983             echo "not connected";
984             return(false);
985         }
986         }
987         
988         function saveAttachments($attrs,$id = -1)
989         {
990                 if($this->is_connected){
991                         $atr = array("name","filename","rubrique","mime","date_mod","comment","deleted","link");
992                         $tmp = array();
993                         foreach($atr as $at){
994                                 if(isset($attrs[$at])){
995                                         $tmp[$at] = $attrs[$at];
996                                 }
997                         }
998                         if(count($tmp)==0){
999                                 return(false);
1000                         }else{
1002                                 // Add
1003                                 if($id == -1){
1004                                         $str = "INSERT INTO glpi_docs ";
1005                                         $namen = "";
1006                                         $values= "";
1007                                         foreach($tmp as $name => $value){       
1008                                                 $namen .= $name.", ";
1009                                                 if(is_numeric($value)){
1010                                                         $values .= $value.", ";
1011                                                 }else{
1012                                                         $values .= "'".$value."', ";
1013                                                 }
1014                                         }
1015                                         $values = preg_replace("/, $/","",$values);
1016                                         $namen  = preg_replace("/, $/","",$namen);
1017                                         $str .= "(".$namen.") VALUES (".$values.");";
1018                                 }else{
1019                                         $str = "UPDATE glpi_docs SET ";
1020                                         foreach($tmp as $name => $value){       
1021                                                 $str .= $name."= ";
1022                                                 if(is_numeric($value)){
1023                                                         $str .= $value.", ";
1024                                                 }else{
1025                                                         $str .= "'".$value."', ";
1026                                                 }
1027                                         }
1028                                         $str = preg_replace("/, $/","",$str);
1029                                         $str .= " WHERE ID=".$id.";";
1030                                 }
1031                                 $this->query($str);
1032                         }
1033                 }else{
1034                         echo "not connected";
1035                         return(false);
1036                 }
1037         }
1039         
1040         /* Check if given attachment id is used in any Device 
1041                 ( - avoid removing of used attachments)
1042      */
1043         function is_attachmentUsed($id)
1044         {
1045                 if($this->is_connected){
1046                         $ret = array();
1047                         $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;";
1048                         $res = $this->query($qry);
1049                         foreach($res as $val){
1050                                 $ret[$val['name']] = $val['name'];
1051                         }
1052                         return($ret);
1053                 }else{
1054             echo "not connected";
1055             return(false);
1056         }
1057         }
1059                 
1060         /* Monitor handling 
1061      */
1062     function getMonitors()
1063         {
1064                 if($this->is_connected){
1065                         $qry= "SELECT * FROM glpi_monitors;";
1066                         return($this->query($qry));     
1067                 
1068                 }else{
1069             echo "not connected";
1070             return(false);
1071         }
1072         }
1074     function updatedMonitor()
1075         {
1076                 if($this->is_connected){
1077 //                      $qry= "SELECT * FROM glpi_monitors;";
1078 //                      return($this->query($qry));     
1079                 
1080                 }else{
1081             echo "not connected";
1082             return(false);
1083         }
1084         }
1086     function addMonitor()
1087         {
1088                 if($this->is_connected){
1089 //                      $qry= "SELECT * FROM glpi_monitors;";
1090 //                      return($this->query($qry));     
1091                 
1092                 }else{
1093             echo "not connected";
1094             return(false);
1095         }
1096         }
1098     function removeMonitor($id)
1099         {
1100                 if($this->is_connected){
1101                         $qry= "DELETE FROM glpi_monitors WHERE ID=".$id.";";
1102                         $this->query($qry);     
1103                 }else{
1104             echo "not connected";
1105             return(false);
1106         }
1107         }
1109     function getMonitorTypes()
1110         {
1111                 if($this->is_connected){
1112                         $qry= "SELECT * FROM glpi_type_monitors;";
1113                         return($this->query($qry));     
1114                 
1115                 }else{
1116             echo "not connected";
1117             return(false);
1118         }
1119         }
1121     function getLocationTypes()
1122         {
1123                 if($this->is_connected){
1124                         $qry= "SELECT * FROM glpi_dropdown_locations;";
1125                         return($this->query($qry));     
1126                 
1127                 }else{
1128             echo "not connected";
1129             return(false);
1130         }
1131         }
1133     function getStateTypes()
1134         {
1135                 if($this->is_connected){
1136                         $qry= "SELECT * FROM glpi_dropdown_state;";
1137                         return($this->query($qry));     
1138                 }else{
1139             echo "not connected";
1140             return(false);
1141         }
1142         }
1144         
1145         /* Printer functions
1146      */
1149         /* This functions checks if the selected computer/network 
1150            device is already available in the db
1151          */
1152         function is_printer_account($dn)
1153         {
1154                 if(!$this->is_connected){
1155                         $this->lasterror ="Can't query anything, if we aren't connected.";
1156                         return(false);
1157                 }else{
1158                         $qry = "SELECT * FROM glpi_printers WHERE name='".$dn."';";
1159                         $res = $this->query($qry);
1160                         if(count($res)==0){
1161                                 return(false);
1162                         }else{
1163                                 return(true);
1164                         }
1165                 }
1166         }
1168         /* This function returns all available data 
1169        from a specified dn
1170      */
1171         function getPrinterInformations($name)
1172         {
1173                 if($this->is_connected){        
1174                         $ret = $this->query("SELECT * FROM glpi_printers WHERE name='".$name."';");
1175                         return($ret);           
1176                 }else{
1177                         echo "not connected";
1178                         return(false);
1179                 }
1180         }
1182         /* Get Printer attachments
1183      */
1184         function getAssignPrinterAttachments($id)
1185         {
1187                 if($this->is_connected){
1188                         $qry= "SELECT * FROM glpi_doc_device WHERE (device_type=3) AND (FK_device=".$id.");";
1189                         $ret = $this->query($qry);
1190                         return($ret);
1191                 }else{
1192             echo "not connected";
1193             return(false);
1194         }
1195         }
1197         /* Printer types 
1198        Returns all defined printer types 
1199          */
1200         function getPrinterTypes()
1201         {
1202                 if($this->is_connected){
1203                         $ret = array();
1204                         $tmp = ($this->query("SELECT * FROM glpi_type_printers;"));
1205                         foreach($tmp as $t){
1206                                 $ret[$t['ID']]=$t['name'];
1207                         }
1208                         return($ret);
1209                 }else{
1210                         echo "not connected";
1211                         return(false);
1212                 }
1213         }
1215         /* Add pritner types 
1216        Add one entry to the printer types 
1217      */
1218         function addPrinterType($name)
1219         {
1220                 if($this->is_connected){
1221                         $tmp = $this->query("SELECT * FROM glpi_type_printers WHERE name='".$name."';");
1222                         if(isset($tmp[0])){
1223                                 //echo "such an entry already exists";
1224                                 return(false);
1225                         }else{  
1226                                 return($this->query("INSERT INTO glpi_type_printers (name) VALUES ('".$name."');"));
1227                         }
1228                 }else{
1229                         echo "not connected";
1230                         return(false);
1231                 }
1232         }
1234         /* remove printer types 
1235        Remove one entry from the printer types (specified by ID=$id)
1236      */
1237         function removePrinterType($id)
1238         {
1239                 if($this->is_connected){
1240                         $tmp = $this->query("SELECT * FROM glpi_type_printers WHERE ID=".$id.";");
1241                         if(isset($tmp[0])){
1242                                 return($this->query("DELETE FROM glpi_type_printers WHERE ID=".$id.";"));       
1243                         }else{
1244                                 echo "can't remove not existing entry";
1245                                 return(false);  
1246                         }
1247                 }else{
1248                         echo "not connected";
1249                         return(false);
1250                 }
1251         }
1253         /* Update printer types 
1254            Update a printer type        
1255         */
1256         function updatePrinterType($name,$id)
1257         {
1259                 if($this->is_connected){
1260                         $tmp = $this->query("SELECT * FROM glpi_type_printers WHERE ID=".$id.";");
1261                         if(isset($tmp[0])){
1262                                 return($this->query("UPDATE glpi_type_printers SET name='".$name."' WHERE ID=".$id.";"));       
1263                         }else{
1264                                 echo "can't update not existing entry";
1265                                 return(false);  
1266                         }
1267                 }else{
1268                         echo "not connected";
1269                         return(false);
1270                 }
1271         }
1274         /*  This fucntions updates an already existing entry 
1275      */
1276         function updatePrinterInformations($array,$name)
1277         {
1278                 if(!is_array($array)){
1279                         echo "updatePrinterInformations: first paraeter must be an array";
1280                 }elseif($this->is_connected){
1281                         $tmp = $this->query("SELECT * FROM  glpi_printers WHERE name='".$name."';");
1282                         if(isset($tmp[0])){
1284                                 $atr = array(   "ID","name","serial","otherserial","contact","contact_num",
1285                                                 "tech_num","comments","date_mod","location","domain","network","ramSize","flags_serial","flags_par","flags_usb",
1286                                                 "model","type","is_template","tplname","FK_glpi_enterprise","deleted");
1287                                 $v = "";
1288                                 foreach($atr as $at){
1289                                         if(isset($array[$at])){
1290                                                 $v .= " ".$at."='".$array[$at]."', ";
1291                                         }
1292                                 }
1293                                 if(empty($v)){
1294                                         echo "updateSystemInformations: no attributes given ";
1295                                         return(false);
1296                                 }else{
1297                                         $v = preg_replace("/, $/","",$v);
1298                                         return($this->query("UPDATE glpi_printers SET ".$v." WHERE name='".$name."';"));
1299                                 }
1300                         }else{
1301                                 echo "can't update not existing entry";
1302                                 return(false);
1303                         }
1304                 }else{
1305                         echo "not connected";
1306                         return(false);
1307                 }
1309         }
1311         /* This function adds a new inventory settings for printers
1312      */
1313         function addPrinterInformations($array)
1314         {
1315                 if(!is_array($array)){
1316                         echo "updateComputerInformations: first paraeter must be an array";
1317                 }elseif($this->is_connected){
1318                         $atr = array(   "ID","name","serial","otherserial","contact","contact_num",
1319                                         "tech_num","comments","date_mod","os","location","domain","network","ramSize","flags_serial","flags_par","flags_usb",
1320                                         "model","type","is_template","tplname","FK_glpi_enterprise","deleted");
1321                         $v = "";
1322                         $a = "";
1323                         foreach($atr as $at){
1324                                 if(isset($array[$at])){
1325                                         $a .= $at.", ";
1326                                         $v .= "'".$array[$at]."', ";
1327                                 }
1328                         }
1329                         if(empty($v)){
1330                                 echo "updateComputerInformations: no attributes given ";
1331                                 return(false);
1332                         }else{
1333                                 $a = preg_replace("/, $/","",$a);
1334                                 $v = preg_replace("/, $/","",$v);
1335                                 return($this->query("INSERT INTO glpi_printers (".$a.") VALUES (".$v.");"));
1336                         }
1337                 
1338                 }else{
1339                         echo "not connected";
1340                         return(false);
1341                 }
1342         }
1344         /* add atachment to given printer */
1345         function addAttachmentsToPrinter($attr,$id)
1346         {
1347         if(($id == "" )||(!is_numeric($id))){
1348             return (false);
1349         }
1350         if($this->is_connected){
1351             $qry = "DELETE FROM glpi_doc_device WHERE (FK_device=".$id.") AND (device_type=3);";
1352             $this->query($qry);
1353                         
1354                         foreach($attr as $aid => $entry){
1355                                 $str = "INSERT INTO glpi_doc_device (FK_doc,FK_device,device_type,is_template) 
1356                                                                                                 VALUES
1357                                                                                                         ($aid,$id,3,'0');";
1358                                 $this->query($str);
1359                         }
1360         }else{
1361             echo "not connected";
1362             return(false);
1363         }
1364         }
1366         function removePrinterInformations($name)
1367         {
1368                 if($this->is_connected){
1369                         $tmp = $this->query("SELECT * FROM glpi_printers WHERE name='".$name."';");
1370                         if(isset($tmp[0])){
1371                                 $id = $tmp[0]['ID'];
1372 //                              $this->query("DELETE FROM glpi_connect_wire WHERE end2=".$id.";");
1373                                 $this->query("DELETE FROM glpi_doc_device WHERE FK_device=".$id." AND device_type=3;");
1374                                 return($this->query("DELETE FROM glpi_printers WHERE ID=".$id.";"));
1375                         }else{
1376                                 echo "can't remove not existing entry";
1377                                 return(false);
1378                         }
1379                 }else{
1380                         echo "not connected";
1381                         return(false);
1382                 }
1383         }
1386         /* Cartridges 
1387      */
1389         /* return all assigned cartridges */
1390         function getUsedCartridges($printerID)
1391         {
1392                 if($this->is_connected){
1393                         $ret = array();
1394                         $qry = "SELECT 
1395                                                 c.date_use                      as date_use,
1396                                                 c.ID                            as ID, 
1397                                                 t.ID                            as type_ID,
1398                                                 t.name                          as name, 
1399                                                 c.FK_glpi_printers      as FK_glpi_printers, 
1400                                                 d.name                          as type_name 
1401                                         FROM 
1402                                                 glpi_dropdown_cartridge_type as d,
1403                                                 glpi_cartridges as c, 
1404                                                 glpi_cartridges_type as t 
1405                                         WHERE   c.FK_glpi_cartridges_type = t.ID 
1406                                                 AND t.type = d.ID 
1407                                                 AND c.FK_glpi_printers = ".$printerID.";"; 
1408                         $res = $this->query($qry);
1409                         foreach($res as $entry){
1410                                 $ret[$entry['ID']] = $entry;
1411                         }
1412                         return($ret);
1413                 }else{
1414                         echo "not connected";
1415                         return(false);
1416                 }
1417         }
1419         /* return all assigned cartridges */
1420         function getAvailableCartridgeTypes($printerTypeID)
1421         {
1422                 if($this->is_connected){
1423                         $ret = array();
1424                         $qry= "
1425         SELECT  
1426                         ct.ID                   as cartridgeID,
1427                         ct.name                 as cartridgeName,
1428                         pt.ID                   as printerTypeID,
1429                         pt.name                 as printerTypeName,
1430                         ct.type                 as cartridgeTypeID,
1431                         dt.name                 as cartridgeTypeName 
1432         FROM    
1433                         glpi_type_printers                              as pt, 
1434                         glpi_cartridges_type                    as ct, 
1435                         glpi_dropdown_cartridge_type    as dt, 
1436                         glpi_cartridges_assoc                   as ac
1437          WHERE 
1438                                 ac.FK_glpi_type_printer = pt.ID 
1439                         AND ac.FK_glpi_cartridges_type = ct.ID 
1440                         AND ct.type=dt.ID 
1441                         AND pt.ID=".$printerTypeID.";";
1442                         $res = $this->query($qry);
1443                         foreach($res as $entry){
1444                                 $ret[$entry['cartridgeID']] = $entry;
1445                         }
1446                         return($ret);
1447                 }else{
1448                         echo "not connected";
1449                         return(false);
1450                 }
1451         }
1453         function removeCartridgeFromPrinter($cartridgeID)
1454         {
1455                 if($this->is_connected){
1456                         $qry = "DELETE FROM glpi_cartridges WHERE ID=".$cartridgeID.";";
1457                         return($this->query($qry));
1458                 }else{
1459                         echo "not connected";
1460                         return(false);
1461                 }
1462         }
1464         function addCartridgeFromPrinter($printerID,$cartridgeID)
1465         {
1466                 if($this->is_connected){
1467                         $qry ="INSERT INTO 
1468                                                 glpi_cartridges (FK_glpi_cartridges_type,FK_glpi_printers,date_in,date_use) 
1469                                    VALUES 
1470                                                 (".$cartridgeID.",".$printerID.",'".date("Y-m-d")."','".date("Y-m-d")."');";    
1471                         return($this->query($qry));
1472                 }else{
1473                         echo "not connected";
1474                         return(false);
1475                 }
1476         }
1478         function getCartridgeTypeInformations($id = "all"){
1479                 if($this->is_connected){
1480                         $ret = array();                 
1481                         if($id != "all"){
1482                                 $qry = "SELECT * FROM glpi_cartridges_type WHERE ID = ".$id.";";
1483                         }else{
1484                                 $qry = "SELECT * FROM glpi_cartridges_type;";
1485                         }
1486                 
1487                         $res = ($this->query($qry));
1488                         foreach($res as $entry){
1489                                 $ret[$entry['ID']] = $entry;
1490                         }       
1491                         return($ret);
1493                 }else{
1494                         echo "not connected";
1495                         return(false);
1496                 }
1497         }
1499         function getCartridgeTypes(){
1500                 if($this->is_connected){
1501                         $ret = array();                 
1502                         $qry = "SELECT * FROM glpi_dropdown_cartridge_type;";
1503                         $res = ($this->query($qry));
1504                         foreach($res as $entry){
1505                                 $ret[$entry['ID']] = $entry['name'];
1506                         }       
1507                         return($ret);
1509                 }else{
1510                         echo "not connected";
1511                         return(false);
1512                 }
1513         }
1517         /* check if given manufacturer ID ist still in use.
1518            The problem is, that nearly every table uses manufacturers .... 
1519      */
1520         function is_manufacturerUsed($id)
1521         {
1522                 $tables = array();
1523                 foreach($this->deviceMappingGOsaGlpi as $table => $entry){
1524                         $tables[] = $entry;
1525                 }
1526                 $tables[] ="sdf";
1527                 print_a($tables);
1528                 exit();
1530                 if($this->is_connected){
1531                         $ret = array();
1532                 }else{
1533                         echo "not connected";
1534                         return(false);
1535                 }
1536         }
1538         /* Manufacturer 
1539            Updates already existing manufacturer
1540          */
1541         function Add_UpdateCatrigdeType($array,$array_printer_types)
1542         {
1543                 if(!is_array($array)){
1544                         echo "Add_UpdateCatrigdeType: first paraeter must be an array";
1545                 }elseif($this->is_connected){
1548                         $atr = array("name","ref","location","type","FK_glpi_enterprise","tech_num","deleted","comments","alarm");
1550                         /* Entry was edited */
1551                         if($array['ID']>0){
1552                                 $qry = "DELETE FROM glpi_cartridges_assoc WHERE FK_glpi_cartridges_type=".$array['ID'].";";
1554                                 $v = "";
1555                                 foreach($atr as $at){
1556                                         if(isset($array[$at])){
1557                                                 $v .= " ".$at."='".$array[$at]."', ";
1558                                         }
1559                                 }
1560                                 if(empty($v)){
1561                                         echo "Add_UpdateCatrigdeType: no attributes given ";
1562                                         return(false);
1563                                 }else{
1564                                         $v = preg_replace("/, $/","",$v);
1565                                         $qry = "UPDATE glpi_cartridges_type SET ".$v." WHERE ID='".$array['ID']."';";
1566                                         $this->query($qry);     
1567                                 }
1568                         }else{
1570                                 /* skip if name is in use*/
1571                                 $qry = "SELECT * FROM glpi_cartridges_type WHERE name='".$array['name']."';";
1572                                 if(count($this->query($qry))){
1573                                         return;
1574                                 }
1576                                 $str = "INSERT INTO glpi_cartridges_type ";
1577                                 $namen = "";
1578                                 $values= "";
1579                                 foreach($array as $name => $value){     
1580                                         $namen .= $name.", ";
1581                                         if(is_numeric($value)){
1582                                                 $values .= $value.", ";
1583                                         }else{
1584                                                 $values .= "'".$value."', ";
1585                                         }
1586                                 }
1587                                 $values = preg_replace("/, $/","",$values);
1588                                 $namen  = preg_replace("/, $/","",$namen);
1589                                 $str .= "(".$namen.") VALUES (".$values.");";
1590                                 $this->query($str);     
1591                                 $IDs = $this->query("SELECT ID FROM glpi_cartridges_type WHERE name='".$array['name']."';");
1592                                 if(count($IDs) > 1){
1593                                         echo "internal db error";
1594                                         return;
1595                                 }
1596                                 $array['ID'] = $IDs[0]['ID'];
1597                         }
1599                         foreach($array_printer_types as $id){
1600                                 $qry = "INSERT INTO glpi_cartridges_assoc 
1601                                         (FK_glpi_cartridges_type,FK_glpi_type_printer) 
1602                                         VALUES 
1603                                         (".$array['ID'].",".$id.")";
1605                                 $this->query($qry);
1606                         }
1607                 }else{
1608                         echo "not connected";
1609                         return(false);
1610                 }
1611         }
1613         function getSupportedPrinterTypeIDsForCartridge($cid)
1614         {
1615                 if($this->is_connected){
1616                         $ret = array();
1617                         $qry = "SELECT FK_glpi_type_printer FROM glpi_cartridges_assoc WHERE FK_glpi_cartridges_type = ".$cid.";";      
1618                         $res = $this->query($qry);
1619                                 
1620                         foreach($res as $entry => $value){
1621                                 $ret[$value['FK_glpi_type_printer']] = $value['FK_glpi_type_printer'];
1622                         }
1623                         return($ret);
1624                 }else{
1625                         echo "not connected";
1626                         return(false);
1627                 }
1628         }
1630         function removeCartridgeType($id){
1631                 if($this->is_connected){
1632                         $qry = "DELETE FROM glpi_cartridges_assoc WHERE FK_glpi_cartridges_type=".$id.";";
1633                         $this->query($qry);     
1634                         $qry = "DELETE FROM glpi_cartridges_type WHERE ID=".$id.";";
1635                         return($this->query($qry));     
1636                 }else{
1637                         echo "not connected";
1638                         return(false);
1639                 }
1640         }
1642         function getCartridgesWhichUseThisType($id)
1643         {
1644                 if($this->is_connected){
1645                         $qry = "SELECT * FROM glpi_cartridges WHERE FK_glpi_cartridges_type=".$id.";";
1646                         $ret  = $this->query($qry);
1647                         return($ret);
1648                 }else{
1649                         echo "not connected";
1650                         return(false);
1651                 }
1652         }
1655         /* Add pritner types 
1656        Add one entry to the cartridgeType types 
1657      */
1658         function addCartridgeDropdownType($name)
1659         {
1660                 if($this->is_connected){
1661                         $tmp = $this->query("SELECT * FROM glpi_dropdown_cartridge_type WHERE name='".$name."';");
1662                         if(isset($tmp[0])){
1663                                 //echo "such an entry already exists";
1664                                 return(false);
1665                         }else{  
1666                                 return($this->query("INSERT INTO glpi_dropdown_cartridge_type (name) VALUES ('".$name."');"));
1667                         }
1668                 }else{
1669                         echo "not connected";
1670                         return(false);
1671                 }
1672         }
1674         /* remove cartridgeType types 
1675        Remove one entry from the cartridgeType types (specified by ID=$id)
1676      */
1677         function removeCartridgeDropdownType($id)
1678         {
1679                 if($this->is_connected){
1680                         $tmp = $this->query("SELECT * FROM glpi_dropdown_cartridge_type WHERE ID=".$id.";");
1681                         if(isset($tmp[0])){
1682                                 return($this->query("DELETE FROM glpi_dropdown_cartridge_type WHERE ID=".$id.";"));     
1683                         }else{
1684                                 echo "can't remove not existing entry";
1685                                 return(false);  
1686                         }
1687                 }else{
1688                         echo "not connected";
1689                         return(false);
1690                 }
1691         }
1693         /* Update cartridgeType  
1694            Update a cartridgeType       
1695         */
1696         function updateCartridgeDropdownType($name,$id)
1697         {
1699                 if($this->is_connected){
1700                         $tmp = $this->query("SELECT * FROM glpi_dropdown_cartridge_type WHERE ID=".$id.";");
1701                         if(isset($tmp[0])){
1702                                 return($this->query("UPDATE glpi_dropdown_cartridge_type SET name='".$name."' WHERE ID=".$id.";"));     
1703                         }else{
1704                                 echo "can't update not existing entry";
1705                                 return(false);  
1706                         }
1707                 }else{
1708                         echo "not connected";
1709                         return(false);
1710                 }
1711         }
1713         function getUsedDropdownTypes($id=false)
1714         {
1715                 if($this->is_connected){
1716                         if($id){
1717                                 $qry = "SELECT distinct(type) FROM glpi_cartridges_type WHERE type = ".$id.";";
1718                         }else{
1719                                 $qry = "SELECT distinct(type) FROM glpi_cartridges_type;";
1720                         }
1721                         return($this->query($qry));
1722                 }else{
1723                         echo "not connected";
1724                         return(false);
1725                 }
1726         }
1729 //$s = new glpiDB("vserver-01","glpi","tester","glpi");
1730 //print_r($s->query("SELECT * FROM glpi_computers"));
1731 //$s->getComputerInformations("1 OR (c.ID<10000)");
1732 ?>