Code

Added check to avoid removing of used attachments
[gosa.git] / include / class_glpi.inc
index 2170e0089aee68e6ce833d7c31918d6ba9cb1ee1..fc8dd37f64f254ae84703c3da90d846da9fcbec3 100644 (file)
@@ -40,6 +40,7 @@ class glpiDB{
                                "glpi_device_power"     => "power",
                                "glpi_device_processor" => "processor",
                                "glpi_device_ram"       => "ram",
+                               "glpi_monitors"       => "monitor",
                                "glpi_device_sndcard"   => "sndcard");
 
                $this->deviceMappingTableNameID = array(                "moboard"       => 1,                                                                           
@@ -726,7 +727,12 @@ class glpiDB{
                                $ret = $this->query($qry);
                                foreach($ret as $id => $entry){
                                        $entry['device_type'] = $gosa;
-                                       $res[$entry['designation']."-".$gosa] = $entry;
+       
+                                       if(isset($entry['designation'])){
+                                               $res[$entry['designation']."-".$gosa] = $entry;
+                                       }else{
+                                               $res[$entry['name']."-".$gosa] = $entry;
+                                       }
                                }
                        }
                        return($res);
@@ -762,6 +768,19 @@ class glpiDB{
                                if(count($res2)!=0){
                                        $ret[$devtype][$res2[0]['designation']]=$res2[0];
                                }
+
+                       $qry = "SELECT * FROM glpi_connect_wire WHERE type=4 AND end2=".$computerID.";";
+                       $res2 = $this->query($qry);
+                       foreach($res2 as $monitor){
+                               $qry = "SELECT * FROM glpi_monitors WHERE ID=".$monitor['end1'].";";
+                               $res3 = $this->query($qry);
+                               foreach($res3 as $moni){
+                                       $ret['monitor'][$moni['name']]=$moni;
+                               }
+                       }
+
+
+
                        }
                        return($ret);
                }else{
@@ -787,8 +806,13 @@ class glpiDB{
                
                        foreach($devices as $type => $entries){
                                foreach($entries as $entry){
-                                       $str = "INSERT INTO glpi_computer_device (device_type,FK_device,FK_computers) 
-                                       VALUES (".$deviceMappingTableNameID[$type].",".$entry['ID'].",".$id.");";
+                                       if($type=="monitor"){
+                                               $str = "INSERT INTO glpi_connect_wire (end1,end2,type) 
+                                               VALUES (".$entry['ID'].",".$id.",4);";
+                                       }else{
+                                               $str = "INSERT INTO glpi_computer_device (device_type,FK_device,FK_computers) 
+                                               VALUES (".$deviceMappingTableNameID[$type].",".$entry['ID'].",".$id.");";
+                                       }
                                        $this->query($str);
                                }
                        }
@@ -801,12 +825,14 @@ class glpiDB{
 
        }
 
-       function removeComputerInformations($id)
+       function removeComputerInformations($name)
        {
-/*             
                if($this->is_connected){
-                       $tmp = $this->query("SELECT * FROM glpi_computers WHERE ID=".$id.";");
+                       $tmp = $this->query("SELECT * FROM glpi_computers WHERE name='".$name."';");
                        if(isset($tmp[0])){
+                               $id = $tmp[0]['ID'];
+                               $this->query("DELETE FROM glpi_connect_wire WHERE end2=".$id.";");
+                               $this->query("DELETE FROM glpi_computer_device WHERE FK_computers=".$id.";");
                                return($this->query("DELETE FROM glpi_computers WHERE ID=".$id.";"));
                        }else{
                                echo "can't remove not existing entry";
@@ -816,7 +842,6 @@ class glpiDB{
                        echo "not connected";
                        return(false);
                }
-*/
        }
 
        function is_connected()
@@ -824,6 +849,40 @@ class glpiDB{
                return($this->is_connected);
        }
 
+       function addAttachmentsToComputer($attr,$id)
+       {
+        if(($id == "" )||(!is_numeric($id))){
+            return (false);
+        }
+        if($this->is_connected){
+            $qry = "DELETE FROM glpi_doc_device WHERE (FK_device=".$id.") AND (device_type=1);";
+            $this->query($qry);
+                       
+                       foreach($attr as $aid => $entry){
+                               $str = "INSERT INTO glpi_doc_device (FK_doc,FK_device,device_type,is_template) 
+                                                                                               VALUES
+                                                                                                       ($aid,$id,1,'0');";
+                               $this->query($str);
+                       }
+        }else{
+            echo "not connected";
+            return(false);
+        }
+       }
+
+       function getAssignAttachments($id)
+       {
+
+               if($this->is_connected){
+                       $qry= "SELECT * FROM glpi_doc_device WHERE (device_type=1) AND (FK_device=".$id.");";
+                       $ret = $this->query($qry);
+                       return($ret);
+               }else{
+            echo "not connected";
+            return(false);
+        }
+       }
+
        function deleteAttachment($id)
        {
                if($this->is_connected){
@@ -837,9 +896,15 @@ class glpiDB{
        
        function getAttachments()
        {
+               $ret = array();
                if($this->is_connected){
                        $qry = "SELECT * FROM glpi_docs WHERE name!='';";
-                       $ret = $this->query($qry);
+                       $re = $this->query($qry);
+
+                       foreach($re as $entry){
+                               $ret[$entry['ID']]=$entry;
+                       }
+
                        return($ret);
                }else{
             echo "not connected";
@@ -847,6 +912,397 @@ class glpiDB{
         }
        }
        
+       function saveAttachments($attrs,$id = -1)
+       {
+               if($this->is_connected){
+                       $atr = array("name","filename","rubrique","mime","date_mod","comment","deleted","link");
+                       $tmp = array();
+                       foreach($atr as $at){
+                               if(isset($attrs[$at])){
+                                       $tmp[$at] = $attrs[$at];
+                               }
+                       }
+                       if(count($tmp)==0){
+                               return(false);
+                       }else{
+
+                               // Add
+                               if($id == -1){
+                                       $str = "INSERT INTO glpi_docs ";
+                                       $namen = "";
+                                       $values= "";
+                                       foreach($tmp as $name => $value){       
+                                               $namen .= $name.", ";
+                                               if(is_numeric($value)){
+                                                       $values .= $value.", ";
+                                               }else{
+                                                       $values .= "'".$value."', ";
+                                               }
+                                       }
+                                       $values = preg_replace("/, $/","",$values);
+                                       $namen  = preg_replace("/, $/","",$namen);
+                                       $str .= "(".$namen.") VALUES (".$values.");";
+                               }else{
+                                       $str = "UPDATE glpi_docs SET ";
+                                       foreach($tmp as $name => $value){       
+                                               $str .= $name."= ";
+                                               if(is_numeric($value)){
+                                                       $str .= $value.", ";
+                                               }else{
+                                                       $str .= "'".$value."', ";
+                                               }
+                                       }
+                                       $str = preg_replace("/, $/","",$str);
+                                       $str .= " WHERE ID=".$id.";";
+                               }
+                               $this->query($str);
+                       }
+               }else{
+                       echo "not connected";
+                       return(false);
+               }
+       }
+
+       
+       /* Check if given attachment id is used in any Device 
+               ( - avoid removing of used attachments)
+     */
+       function is_attachmentUsed($id)
+       {
+               if($this->is_connected){
+                       $qry = "SELECT * FROM glpi_doc_device WHERE FK_doc =".$id." LIMIT 1; ";
+                       return(count($this->query($qry)));
+               }else{
+            echo "not connected";
+            return(false);
+        }
+       }
+
+
+       /* Monitor handling 
+     */
+    function getMonitors()
+       {
+               if($this->is_connected){
+                       $qry= "SELECT * FROM glpi_monitors;";
+                       return($this->query($qry));     
+               
+               }else{
+            echo "not connected";
+            return(false);
+        }
+       }
+
+    function updatedMonitor()
+       {
+               if($this->is_connected){
+//                     $qry= "SELECT * FROM glpi_monitors;";
+//                     return($this->query($qry));     
+               
+               }else{
+            echo "not connected";
+            return(false);
+        }
+       }
+
+    function addMonitor()
+       {
+               if($this->is_connected){
+//                     $qry= "SELECT * FROM glpi_monitors;";
+//                     return($this->query($qry));     
+               
+               }else{
+            echo "not connected";
+            return(false);
+        }
+       }
+
+    function removeMonitor($id)
+       {
+               if($this->is_connected){
+                       $qry= "DELETE FROM glpi_monitors WHERE ID=".$id.";";
+                       $this->query($qry);     
+               }else{
+            echo "not connected";
+            return(false);
+        }
+       }
+
+    function getMonitorTypes()
+       {
+               if($this->is_connected){
+                       $qry= "SELECT * FROM glpi_type_monitors;";
+                       return($this->query($qry));     
+               
+               }else{
+            echo "not connected";
+            return(false);
+        }
+       }
+
+    function getLocationTypes()
+       {
+               if($this->is_connected){
+                       $qry= "SELECT * FROM glpi_dropdown_locations;";
+                       return($this->query($qry));     
+               
+               }else{
+            echo "not connected";
+            return(false);
+        }
+       }
+
+    function getStateTypes()
+       {
+               if($this->is_connected){
+                       $qry= "SELECT * FROM glpi_dropdown_state;";
+                       return($this->query($qry));     
+               }else{
+            echo "not connected";
+            return(false);
+        }
+       }
+
+       
+       /* Printer functions
+     */
+
+
+       /* This functions checks if the selected computer/network 
+          device is already available in the db
+        */
+       function is_printer_account($dn)
+       {
+               if(!$this->is_connected){
+                       $this->lasterror ="Can't query anything, if we aren't connected.";
+                       return(false);
+               }else{
+                       $qry = "SELECT * FROM glpi_printers WHERE name='".$dn."';";
+                       $res = $this->query($qry);
+                       if(count($res)==0){
+                               return(false);
+                       }else{
+                               return(true);
+                       }
+               }
+       }
+
+       /* This function returns all available data 
+       from a specified dn
+     */
+       function getPrinterInformations($name)
+       {
+               if($this->is_connected){        
+                       $ret = $this->query("SELECT * FROM glpi_printers WHERE name='".$name."';");
+                       return($ret);           
+               }else{
+                       echo "not connected";
+                       return(false);
+               }
+       }
+
+       /* Get Printer attachments
+     */
+       function getAssignPrinterAttachments($id)
+       {
+
+               if($this->is_connected){
+                       $qry= "SELECT * FROM glpi_doc_device WHERE (device_type=3) AND (FK_device=".$id.");";
+                       $ret = $this->query($qry);
+                       return($ret);
+               }else{
+            echo "not connected";
+            return(false);
+        }
+       }
+
+       /* Printer types 
+       Returns all defined printer types 
+        */
+       function getPrinterTypes()
+       {
+               if($this->is_connected){
+                       $ret = array();
+                       $tmp = ($this->query("SELECT * FROM glpi_type_printers;"));
+                       foreach($tmp as $t){
+                               $ret[$t['ID']]=$t['name'];
+                       }
+                       return($ret);
+               }else{
+                       echo "not connected";
+                       return(false);
+               }
+       }
+
+       /* Add pritner types 
+       Add one entry to the printer types 
+     */
+       function addPrinterType($name)
+       {
+               if($this->is_connected){
+                       $tmp = $this->query("SELECT * FROM glpi_type_printers WHERE name='".$name."';");
+                       if(isset($tmp[0])){
+                               //echo "such an entry already exists";
+                               return(false);
+                       }else{  
+                               return($this->query("INSERT INTO glpi_type_printers (name) VALUES ('".$name."');"));
+                       }
+               }else{
+                       echo "not connected";
+                       return(false);
+               }
+       }
+
+       /* remove printer types 
+       Remove one entry from the printer types (specified by ID=$id)
+     */
+       function removePrinterType($id)
+       {
+               if($this->is_connected){
+                       $tmp = $this->query("SELECT * FROM glpi_type_printers WHERE ID=".$id.";");
+                       if(isset($tmp[0])){
+                               return($this->query("DELETE FROM glpi_type_printers WHERE ID=".$id.";"));       
+                       }else{
+                               echo "can't remove not existing entry";
+                               return(false);  
+                       }
+               }else{
+                       echo "not connected";
+                       return(false);
+               }
+       }
+
+       /* Update printer types 
+          Update a printer type        
+       */
+       function updatePrinterType($name,$id)
+       {
+
+               if($this->is_connected){
+                       $tmp = $this->query("SELECT * FROM glpi_type_printers WHERE ID=".$id.";");
+                       if(isset($tmp[0])){
+                               return($this->query("UPDATE glpi_type_printers SET name='".$name."' WHERE ID=".$id.";"));       
+                       }else{
+                               echo "can't update not existing entry";
+                               return(false);  
+                       }
+               }else{
+                       echo "not connected";
+                       return(false);
+               }
+       }
+
+
+       /*  This fucntions updates an already existing entry 
+     */
+       function updatePrinterInformations($array,$name)
+       {
+               if(!is_array($array)){
+                       echo "updatePrinterInformations: first paraeter must be an array";
+               }elseif($this->is_connected){
+                       $tmp = $this->query("SELECT * FROM  glpi_printers WHERE name='".$name."';");
+                       if(isset($tmp[0])){
+
+                               $atr = array(   "ID","name","serial","otherserial","contact","contact_num",
+                                               "tech_num","comments","date_mod","location","domain","network","ramSize","flags_serial","flags_par","flags_usb",
+                                               "model","type","is_template","tplname","FK_glpi_enterprise","deleted");
+                               $v = "";
+                               foreach($atr as $at){
+                                       if(isset($array[$at])){
+                                               $v .= " ".$at."='".$array[$at]."', ";
+                                       }
+                               }
+                               if(empty($v)){
+                                       echo "updateSystemInformations: no attributes given ";
+                                       return(false);
+                               }else{
+                                       $v = preg_replace("/, $/","",$v);
+                                       return($this->query("UPDATE glpi_printers SET ".$v." WHERE name='".$name."';"));
+                               }
+                       }else{
+                               echo "can't update not existing entry";
+                               return(false);
+                       }
+               }else{
+                       echo "not connected";
+                       return(false);
+               }
+
+       }
+
+       /* This function adds a new inventory settings for printers
+     */
+       function addPrinterInformations($array)
+       {
+               if(!is_array($array)){
+                       echo "updateComputerInformations: first paraeter must be an array";
+               }elseif($this->is_connected){
+                       $atr = array(   "ID","name","serial","otherserial","contact","contact_num",
+                                       "tech_num","comments","date_mod","os","location","domain","network","ramSize","flags_serial","flags_par","flags_usb",
+                                       "model","type","is_template","tplname","FK_glpi_enterprise","deleted");
+                       $v = "";
+                       $a = "";
+                       foreach($atr as $at){
+                               if(isset($array[$at])){
+                                       $a .= $at.", ";
+                                       $v .= "'".$array[$at]."', ";
+                               }
+                       }
+                       if(empty($v)){
+                               echo "updateComputerInformations: no attributes given ";
+                               return(false);
+                       }else{
+                               $a = preg_replace("/, $/","",$a);
+                               $v = preg_replace("/, $/","",$v);
+                               return($this->query("INSERT INTO glpi_printers (".$a.") VALUES (".$v.");"));
+                       }
+               
+               }else{
+                       echo "not connected";
+                       return(false);
+               }
+       }
+
+       /* add atachment to given printer */
+       function addAttachmentsToPrinter($attr,$id)
+       {
+        if(($id == "" )||(!is_numeric($id))){
+            return (false);
+        }
+        if($this->is_connected){
+            $qry = "DELETE FROM glpi_doc_device WHERE (FK_device=".$id.") AND (device_type=3);";
+            $this->query($qry);
+                       
+                       foreach($attr as $aid => $entry){
+                               $str = "INSERT INTO glpi_doc_device (FK_doc,FK_device,device_type,is_template) 
+                                                                                               VALUES
+                                                                                                       ($aid,$id,3,'0');";
+                               $this->query($str);
+                       }
+        }else{
+            echo "not connected";
+            return(false);
+        }
+       }
+
+       function removePrinterInformations($name)
+       {
+               if($this->is_connected){
+                       $tmp = $this->query("SELECT * FROM glpi_printers WHERE name='".$name."';");
+                       if(isset($tmp[0])){
+                               $id = $tmp[0]['ID'];
+//                             $this->query("DELETE FROM glpi_connect_wire WHERE end2=".$id.";");
+                               $this->query("DELETE FROM glpi_doc_device WHERE FK_device=".$id." AND device_type=3;");
+                               return($this->query("DELETE FROM glpi_printers WHERE ID=".$id.";"));
+                       }else{
+                               echo "can't remove not existing entry";
+                               return(false);
+                       }
+               }else{
+                       echo "not connected";
+                       return(false);
+               }
+       }
 
 }
 //$s = new glpiDB("vserver-01","glpi","tester","glpi");