Code

Removed duplicated post call
[gosa.git] / plugins / admin / systems / class_glpiDeviceManagement.inc
index 5c4f223ea54d78101283e63eb9a47dda8e979fbe..6573cb9a690dbace39bdfa0d2635c451ecad8e99 100644 (file)
@@ -24,6 +24,8 @@ class glpiDeviceManagement extends plugin
 
   var $Selected = array();
 
+  var $delete = array();
+
   function glpiDeviceManagement ($config, $dn= NULL,$used=NULL)
   {
     plugin::plugin ($config, $dn);
@@ -51,8 +53,11 @@ class glpiDeviceManagement extends plugin
     $this->DeviceAttrs['sndcard']   = array("designation","type","comment","FK_glpi_enterprise","specif_default");
     $this->DeviceAttrs['power']     = array("designation","power","comment","FK_glpi_enterprise","atx");
     $this->DeviceAttrs['pci']       = array("designation","comment","FK_glpi_enterprise","specif_default");
+    $this->DeviceAttrs['monitor']   = array("name","comments","serial","otherserial","size",
+                                            "flags_micro","flags_speaker","flags_subd","flags_bnc",
+                                            "location","type","FK_glpi_enterprise","is_global","deleted","is_template","tplname");
 
-    $this->AllowedDevices=array("case","moboard","sndcard","processor","iface","ram","hdd","drive","control","gfxcard","power","pci");
+    $this->AllowedDevices=array("case","moboard","sndcard","processor","iface","ram","hdd","drive","control","gfxcard","power","pci","monitor");
   }
 
   function execute()
@@ -67,14 +72,16 @@ class glpiDeviceManagement extends plugin
     $smarty= get_smarty();
     $display= "";
 
-    /* Check if we should create a new entry .... */
+    /* this var is used to ensure that every post is only performed once */
     $only_once = true;
+
+    /* Check Post for some actions */
     foreach($_POST as $name => $value){
       
-      /* All Post vars starts with new */
+      /* If new_ then we should create an new device */
       if((preg_match("/^new_/",$name))&&($only_once)){
     
-        /* do this twice */
+        /* don't do this twice */
         $only_once = false;
 
         /*extract device device_type */
@@ -95,12 +102,44 @@ class glpiDeviceManagement extends plugin
       }
     }
 
-    /* Open entry for editing if requested */
+    /* delete was requested ... show dialog   */
     if((isset($_GET['act']))&&($_GET['act']=="del_device")){
       $id = base64_decode($_GET['id']);
       $tmp = $this->devices[$id];
-      $this->parent->handle->deleteDevice($tmp);
-      $this->reload();
+
+      $this->delete = $tmp;
+
+      if(isset($tmp['name'])){
+        $smarty->assign("warning", sprintf(_("You're about to delete the glpi device '%s'."), $tmp['name']));
+      }elseif(isset($tmp['designation'])){
+        $smarty->assign("warning", sprintf(_("You're about to delete the glpi device '%s'."), $tmp['designation']));
+      }else{
+        print_red(_("Can't detect object name."));
+      }
+      
+      return($smarty->fetch(get_template_path('remove_glpi.tpl', TRUE)));
+    }
+
+    /* Delete entry, but check if this device is in currently in use */
+    if(isset($_POST['delete_glpi_confirm'])){
+      if(count($this->parent->handle->is_deviceUsed($this->delete))){
+
+        $tmp = $this->parent->handle->is_deviceUsed($this->delete);    
+  
+        $names = "";
+        foreach($tmp as $name){
+          $names .= ", ".$name;
+        }
+        $names = preg_replace("/^, /","",$names);
+        $names = trim($names);
+        if(count($tmp) == 3){
+          $names .= " ...";
+        }
+        print_red(sprintf(_("You can't delete this device, it is still in use by these system(s) '%s'"),$names));
+      }else{
+        $this->parent->handle->deleteDevice($this->delete);
+        $this->reload();
+      }
     }
 
     /* Open entry for editing if requested */
@@ -145,8 +184,8 @@ class glpiDeviceManagement extends plugin
     $divlist->SetEntriesPerPage(0);
     $divlist->SetHeader(array(
           array("string" => " ", "attach" => "style='text-align:center;width:20px;'"),
-          array("string" => _("devices")." / "._("Departments"), "attach" => "style=''"),
-          array("string" => _("Use"), "attach" => "style='width:60px;border-right:0px;text-align:right;'" )));
+          array("string" => _("devices"), "attach" => "style=''"),
+          array("string" => _("Actions"), "attach" => "style='width:60px;border-right:0px;text-align:right;'" )));
 
     $edit = "<a href='?plug=".$_GET['plug']."&amp;act=edit_device&amp;id=%s'>%s</a>";
     $editdel = "<a href='?plug=".$_GET['plug']."&amp;act=edit_device&amp;id=%s'><img src='images/edit.png' alt='"._("Edit")."' border=0></a>";
@@ -156,14 +195,25 @@ class glpiDeviceManagement extends plugin
 
     foreach($this->devices as $key=>$user){
 
-      if(isset($this->Selected[$user['device_type']][$user['designation']])){
+      if(isset($user['designation'])){
+        $str = "designation";
+      }else{
+        $str = "name";
+      }
+
+      if(isset($this->Selected[$user['device_type']][$user[$str]])){
         $use = " checked ";
       }else{
         $use ="";
       }
 
+      /* Dawn databse struckture ....*/
+      if(empty($user['comment']) && isset($user['comments'])) {
+        $user['comment'] = $user['comments'];
+      }
+
       $field1 = array("string" => preg_replace("/%s/",base64_encode($key),preg_replace("/%USE%/",$use,$useDevice)), "attach" => "style='text-align:center;width:20px;'");
-      $field2 = array("string" => sprintf($edit,base64_encode($key),$user['designation']."&nbsp;[".$user['comment']."]"), "attach" => "style=''");
+      $field2 = array("string" => sprintf($edit,base64_encode($key),$user[$str]."&nbsp;[".$user['comment']."]"), "attach" => "style=''");
       $field3 = array("string" => sprintf($editdel,base64_encode($key),base64_encode($key)), 
         "attach" => "style='width:60px;border-right:0px;text-align:right;'");
       $divlist->AddEntry(array($field1,$field2,$field3));
@@ -172,43 +222,45 @@ class glpiDeviceManagement extends plugin
     
   $listhead = "<div style='background:#F0F0F9;padding:5px;'>".
       " <input class='center' type='image' align='middle' 
-      src='images/keyboard.png' title='"._("New mainbord")."' alt='"._("MB")."' name='new_moboard'>&nbsp;".
+      src='images/monitor.png'  title='"._("New monitor")."' alt='"._("M")."' name='new_monitor'>&nbsp;".
+      " <input class='center' type='image' align='middle' 
+      src='images/mainboard.png' title='"._("New mainbord")."' alt='"._("MB")."' name='new_moboard'>&nbsp;".
       " <input class='center' type='image' align='middle' 
-      src='mages/list_back.png' title='"._("New processor")."' alt='"._("P")."' name='new_processor'>&nbsp;".
+      src='images/processor.png' title='"._("New processor")."' alt='"._("P")."' name='new_processor'>&nbsp;".
       " <input class='center' type='image' align='middle' 
-      src='mages/list_back.png' title='"._("New case")."' alt='"._("C")."' name='new_case'>&nbsp;".
+      src='images/server.png' title='"._("New case")."' alt='"._("C")."' name='new_case'>&nbsp;".
       " <input class='center' type='image' align='middle' 
-      src='mages/list_back.png' title='"._("New network interface")."' alt='"._("NI")."' name='new_iface'>&nbsp;".
+      src='images/net_hardware.png' title='"._("New network interface")."' alt='"._("NI")."' name='new_iface'>&nbsp;".
       " <input class='center' type='image' align='middle' 
-      src='mages/list_back.png' title='"._("New ram")."' alt='"._("R")."' name='new_ram'>&nbsp;".
+      src='images/memory.png' title='"._("New ram")."' alt='"._("R")."' name='new_ram'>&nbsp;".
       " <input class='center' type='image' align='middle' 
-      src='mages/list_back.png' title='"._("New hard disk")."' alt='"._("HDD")."' name='new_hdd'>&nbsp;".
+      src='images/fai_partitionTable.png' title='"._("New hard disk")."' alt='"._("HDD")."' name='new_hdd'>&nbsp;".
       " <input class='center' type='image' align='middle' 
-      src='mages/list_back.png' title='"._("New drive")."' alt='"._("D")."' name='new_drive'>&nbsp;".
+      src='images/drives.png' title='"._("New drive")."' alt='"._("D")."' name='new_drive'>&nbsp;".
       " <input class='center' type='image' align='middle' 
-      src='mages/list_back.png' title='"._("New controller")."' alt='"._("CS")."' name='new_control'>&nbsp;".
+      src='images/hardware.png' title='"._("New controller")."' alt='"._("CS")."' name='new_control'>&nbsp;".
       " <input class='center' type='image' align='middle' 
-      src='mages/list_back.png' title='"._("New graphics card")."' alt='"._("GC")."' name='new_gfxcard'>&nbsp;".
+      src='images/gfx_hardware.png' title='"._("New graphics card")."' alt='"._("GC")."' name='new_gfxcard'>&nbsp;".
       " <input class='center' type='image' align='middle' 
-      src='mages/list_back.png' title='"._("New sound card")."' alt='"._("SC")."' name='new_sndcard'>&nbsp;".
+      src='images/snd_hardware.png' title='"._("New sound card")."' alt='"._("SC")."' name='new_sndcard'>&nbsp;".
       " <input class='center' type='image' align='middle' 
-      src='mages/list_back.png' title='"._("New power supply")."' alt='"._("PS")."' name='new_power'>&nbsp;".
+      src='images/power.png' title='"._("New power supply")."' alt='"._("PS")."' name='new_power'>&nbsp;".
       " <input class='center' type='image' align='middle' 
-      src='mages/list_back.png' title='"._("New misc device")."' alt='"._("OC")."' name='new_pci'>&nbsp;".
+      src='images/fai_template.png' title='"._("New misc device")."' alt='"._("OC")."' name='new_pci'>&nbsp;".
       "</div>";
+    
     $filter= $_SESSION['glpiDeviceRegex'];
     $smarty->assign("devicehead", $listhead);
     $smarty->assign("devices", $divlist->DrawList());
     $smarty->assign("search_image", get_template_path('images/search.png'));
     $smarty->assign("searchu_image", get_template_path('images/search_user.png'));
     $smarty->assign("tree_image", get_template_path('images/tree.png'));
-    $smarty->assign("infoimage", get_template_path('images/info.png'));
-    $smarty->assign("launchimage", get_template_path('images/launch.png'));
+    $smarty->assign("infoimage", get_template_path('images/info_small.png'));
+    $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
     $smarty->assign("apply", apply_filter());
     $smarty->assign("alphabet", generate_alphabet());
     $smarty->assign("device_regex", $filter['device_regex']);
 
-
     $display.= $smarty->fetch(get_template_path('glpiDeviceManagement.tpl', TRUE));
     return($display);
   }
@@ -239,18 +291,24 @@ class glpiDeviceManagement extends plugin
   function save_object()
   {
     foreach($_POST as $name => $value){
+
       if(preg_match("/wasOnPage_/",$name)){
         $dN = base64_decode($value);
 
         $device = $this->devices[$dN];
         $type   = $device['device_type'];  
             
+        if(isset($device['designation'])){
+          $str = "designation";
+        }else{
+          $str = "name";
+        }
 
         if(isset($_POST['useDevice_'.$value])){
-          $this->Selected[$type][$device['designation']] = $device;
+          $this->Selected[$type][$device[$str]] = $device;
         }else{
-          if(isset($this->Selected[$type][$device['designation']])){
-            unset($this->Selected[$type][$device['designation']]);
+          if(isset($this->Selected[$type][$device[$str]])){
+            unset($this->Selected[$type][$device[$str]]);
           }
         }
       }
@@ -274,19 +332,35 @@ class glpiDeviceManagement extends plugin
 
   /* This function cehck all created devices if you wan't to create device specific check 
       use >>if($attr['device_type']=="moboard")<< to create a device type depending check
-  */
+   */
   function check($attr)
   {
-    $message = array();
-    if(empty($attr['designation'])){
+    /* Call common method to give check the hook */
+    $message= plugin::check();
+
+    if(isset($attr['designation'])){
+      $str2 = "designation";
+    }else{
+      $str2 = "name";
+    }
+    if(empty($attr[$str2])){
       $message[]=(_("You have to specify a valid name for this device."));
     }
 
     /* Avoid same name twice */
     $devices = ($this->parent->handle->getDevices());
     foreach($devices as $dev){
-      if($dev['designation']==$attr['designation']){
 
+      /* Some devices use designation some name for name
+       */
+      if(isset($dev['designation'])){
+        $str = "designation";
+      }else{
+        $str = "name";
+      }
+
+      if($dev[$str]==$attr[$str2]){
+        
         /* Entries with ['ID'] already exists, and are only edited, if ID is missing we are currently creating a new entry */
         if(isset($attr['ID'])){
           if(!(($dev['ID'] == $attr['ID'])&&($dev['device_type']==$attr['device_type']))){
@@ -301,8 +375,8 @@ class glpiDeviceManagement extends plugin
     return($message);
   }
 
-  /* This funtions displays the tempalte for all available devices 
-   * This funtion is also used if we create a new device
+  /* This functions displays the template for all available devices 
+   * This function is also used if we create a new device
    */
   function editDevice($entry)
   {
@@ -310,7 +384,7 @@ class glpiDeviceManagement extends plugin
 
     /* Transfer given data to smarty */
     foreach($this->EditEntry as $name => $value){
-      $smarty->assign($name,$value);
+      $smarty->assign($name,htmlentities(utf8_decode($value)));
     }
   
     /* Set default select boxes, manufacturers ... */
@@ -337,6 +411,9 @@ class glpiDeviceManagement extends plugin
     $smarty->assign("FK_glpi_enterpriseKeys",array_flip($manufacturer));
     $smarty->assign("FK_glpi_enterprises", $manufacturer);
 
+    $smarty->assign("formats",array("Large","Medium","Micro"));
+    $smarty->assign("formats",array("Large","Medium","Micro"));
+
     $smarty->assign("formats",array("Large","Medium","Micro"));
     $smarty->assign("formatKeys",array('Grand','Moyen','Micro'));
     return($smarty->fetch(get_template_path('glpi_devices.tpl', TRUE)));