Code

Renamed add/remove buttons
[gosa.git] / plugins / admin / systems / class_glpiPrinterAccount.inc
index 7c86816893efac3be2284de5e8a6efe33a4732b8..59da4baf45669e3bba068a8ff830f3f5ebe79627 100644 (file)
@@ -39,6 +39,8 @@ class glpiPrinterAccount extends plugin
   var $contact            = "";    // Empty
   var $deleted            = "N";   // Deleted entries should have this set to Y
 
+  var $editManufacturer   = false;
+
   /* Not necessary, cause we use mysql databse */
   var $objectclasses= array("whatever");
 
@@ -136,11 +138,11 @@ class glpiPrinterAccount extends plugin
     /*  Assign smarty defaults 
         To avoid undefined indexes, if there is an error with the glpi db
      */ 
-    foreach(array("PrinterTypeKeys","PrinterTypes","ManufacturerKeys","Manufacturers","Attachments","AttachmentKeys") as $attr){
+    foreach(array("PrinterTypeKeys","PrinterTypes","ManufacturerKeys","Manufacturers","Attachments","AttachmentKeys","CartridgeKeys","Cartridges") as $attr){
       $smarty->assign($attr,array());
       $smarty->assign($attr."ACL"," disabled ");
     }
-    foreach(array("type","FK_glpi_enterprise","tech_num","contact_num","comments","flags_serial","flags_par","flags_usb") as $attr){
+    foreach(array("type","FK_glpi_enterprise","tech_num","contact_num","comments","flags_serial","flags_par","flags_usb","AttachmentsDiv") as $attr){
       $smarty->assign($attr,"");
       $smarty->assign($attr."ACL"," disabled ");
     }
@@ -222,19 +224,52 @@ class glpiPrinterAccount extends plugin
     /* This appends a new printer to our sytem types
      */
     if((isset($_POST['add_type']))&&(!empty($_POST['type_string']))){
-      $this->handle->addPrinterType($_POST['type_string']);  
+    
+      $tmp = $this->handle->getPrinterTypes();
+      $allok = true;
+      foreach($tmp as $id => $name){
+        if(trim($name) == trim($_POST['type_string'])){
+          $allok = false;
+        }
+      }
+      if($allok){
+        $this->handle->addPrinterType($_POST['type_string']);  
+      }else{
+        print_red(sprintf(_("Can't rename given printer type to '%s', because this type name already exists."),$_POST['type_string']));
+      }
     }
 
     /* Remove selected type from our printer types list
      */
     if((isset($_POST['del_type']))&&(!empty($_POST['select_type']))){
-      $this->handle->removePrinterType($_POST['select_type']);  
+      $tmp = $this->handle->is_printerTypeUsed($_POST['select_type']);
+      if(count($tmp)){
+        $str = "";
+        foreach($tmp as $id => $name){
+          $str .= $name.", ";
+        }
+        $str = preg_replace("/, $/","",$str); 
+        print_red(sprintf(_("Can't delete printer type, it is still in use by '%s'."),$str));
+      }else{
+        $this->handle->removePrinterType($_POST['select_type']);  
+      }
     }
 
     /* Rename selected printer type to given string
      */
     if((isset($_POST['rename_type']))&&(!empty($_POST['select_type']))&&(!empty($_POST['type_string']))){
-      $this->handle->updatePrinterType($_POST['type_string'],$_POST['select_type']);
+      $tmp = $this->handle->getPrinterTypes();
+      $allok = true;
+      foreach($tmp as $id => $name){
+        if(trim($name) == trim($_POST['type_string'])){
+          $allok = false;
+        }
+      }
+      if($allok){
+        $this->handle->updatePrinterType($_POST['type_string'],$_POST['select_type']);
+      }else{
+        print_red(sprintf(_("Can't rename given printer type to '%s', because this type name already exists."),$_POST['type_string']));
+      }
     }
 
     /* Someone wants to edit the printer types ... 
@@ -258,13 +293,15 @@ class glpiPrinterAccount extends plugin
     if(isset($_POST['edit_manufacturer'])){
       $this->cur_dialog = new glpiManufacturer($this->config,$this->dn);
       $this->dialog = true;
+      $this->editManufacturer =true;
     }
 
     /* Close manufacturer editing dialog
      */
-    if(isset($_POST['close_edit_manufacturer'])){
+    if((isset($_POST['close_edit_manufacturer']))&&($this->editManufacturer)){
       $this->dialog = false;
       $this->cur_dialog = false;
+      $this->editManufacturer=false;
     }
 
 
@@ -279,18 +316,19 @@ class glpiPrinterAccount extends plugin
      */
     if(isset($_POST['SelectContactPerson'])){
       $this->addUser = "contact";
-      $this->cur_dialog= new glpiSelectUser($this->config,$this->dn);
+      $this->cur_dialog= new glpiSelectUser($this->config,$this->dn,"user_tech_num");
     }
 
     /* Selecte technical responsible person
      */
     if(isset($_POST['SelectTechPerson'])){
       $this->addUser ="tech";
-      $this->cur_dialog= new glpiSelectUser($this->config,$this->dn);
+      $this->cur_dialog= new glpiSelectUser($this->config,$this->dn,"user_tech_num");
     }
 
     /* Abort user selection
      */
+    $smarty->assign("AbortSelectUser","SelectUserCancel");
     if(isset($_POST['SelectUserCancel'])){
       $this->dialog = false;
       $this->addUser ="";
@@ -377,6 +415,17 @@ class glpiPrinterAccount extends plugin
     
     /* Remove Attachment from this tab 
      */
+    $once = true;
+    foreach($_POST as $name => $value){
+      if((preg_match("/^delAttachment_/",$name))&&($once)){
+        $once= false;
+        $name = preg_replace("/^delAttachment_/","",$name);
+        $entry = preg_replace("/_.*$/","",$name);
+        if(isset($this->usedAttachments[$entry])){
+          unset($this->usedAttachments[$entry]);
+        }
+      }
+    }
     if((isset($_POST['RemoveAttachment']))&&(isset($_POST['Attachments']))){
       if(isset($this->usedAttachments[$_POST['Attachments']])){
         unset($this->usedAttachments[$_POST['Attachments']]);
@@ -410,11 +459,15 @@ class glpiPrinterAccount extends plugin
     /* Remove cartridge  
      */
     if((isset($_POST['RemoveCartridge']))&&(isset($_POST['Cartridges']))){
-      if(isset($this->usedCartridges[$_POST['Cartridges']])){
-        if($this->usedCartridges[$_POST['Cartridges']]['status'] == "exists"){
-          $this->usedCartridges[$_POST['Cartridges']]['status'] = "deleted";
-        }else{
-          unset($this->usedCartridges[$_POST['Cartridges']]);
+
+      foreach($_POST['Cartridges'] as $cartID){
+
+        if(isset($this->usedCartridges[$cartID])){
+          if($this->usedCartridges[$cartID]['status'] == "exists"){
+            $this->usedCartridges[$cartID]['status'] = "deleted";
+          }else{
+            unset($this->usedCartridges[$cartID]);
+          }
         }
       }
     }
@@ -447,7 +500,7 @@ class glpiPrinterAccount extends plugin
      *  Assign listbox / checkbox .... values to smarty  
      */
     /* Assign smarty defaults */ 
-    foreach(array("PrinterTypes","PrinterTypeKeys","Manufacturers","TechnicalResponsibles","Attachments") as $attr){
+    foreach(array("PrinterTypes","PrinterTypeKeys","Manufacturers","TechnicalResponsibles","Attachments","Cartridges") as $attr){
       $smarty->assign($attr,array());
       $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
     }
@@ -484,6 +537,25 @@ class glpiPrinterAccount extends plugin
 
     /* Assign used Attachments
     */
+
+    $divlist = new divSelectBox("glpiAttachmentsList");
+    $divlist-> SetHeight(120);
+    $atts = $this->getUsedAttachments(true);
+    $downlink = "<a href='get_attachment.php?id=%s' target='_blank'>%s</a>";
+    $del_link = "<input type='image' src='images/edittrash.png' name='delAttachment_%s'>";
+    foreach($atts as $id => $attachment){
+      $divlist->AddEntry
+          (
+        array(
+            array("string"=>$attachment['name']),
+            array("string"=>$attachment['mime']),
+            array("string"=>sprintf($downlink,$id,$attachment['filename'])),
+            array("string"=>sprintf($del_link,$attachment['ID']),"attach"=>"style='border-right:0px;'"),
+             )
+          );
+    }
+
+    $smarty->assign("AttachmentsDiv"          ,$divlist->DrawList());
     $smarty->assign("Attachments",            $this->getUsedAttachments());
     $smarty->assign("AttachmentKeys",         array_flip($this->getUsedAttachments()));
 
@@ -520,6 +592,19 @@ class glpiPrinterAccount extends plugin
       $smarty->assign("tech_num",               _("N/A"));
     }
 
+    /* If theres a cartridge selected, you can't change the printer type.
+     */ 
+    $disp = true;
+
+    foreach($this->usedCartridges as $cart){
+      if($cart['status'] != "deleted"){
+        $disp = false;
+      }
+    }
+    if($disp==false){
+      $smarty->assign("typeACL","disabled");
+    }
+
     $display.= $smarty->fetch(get_template_path('glpiPrinter.tpl', TRUE));
     return($display);
   }
@@ -536,14 +621,14 @@ class glpiPrinterAccount extends plugin
   /* Save data to object */
   function save_object()
   {
-    plugin::save_object();
-    foreach($this->attributes as $attrs){
-      if(isset($_POST[$attrs])){
-        $this->$attrs = $_POST[$attrs];
+    if(isset($_POST['glpiPrinterFlagsPosted'])){
+      plugin::save_object();
+      foreach($this->attributes as $attrs){
+        if(isset($_POST[$attrs])){
+          $this->$attrs = $_POST[$attrs];
+        }
       }
-    }
 
-    if(isset($_POST['glpiPrinterFlagsPosted'])){
       foreach(array("flags_serial","flags_par","flags_usb") as $checkboxes){
         if(isset($_POST[$checkboxes])){
           $this->$checkboxes = 1;
@@ -599,22 +684,25 @@ class glpiPrinterAccount extends plugin
   }
 
   /* Return used attachments */
-  function getUsedAttachments()
+  function getUsedAttachments($divlist = false)
   {
     $atts =$this->handle->getAttachments();
     $ret = array();
     foreach($atts as $entry){
       if(in_array($entry['ID'],$this->usedAttachments)){
+        if($divlist){
+          $ret[$entry['ID']] = $entry;
+        }else{
+          $cm ="";
+          if(isset($entry['comment'])){
+            $cm=" [".$entry['comment']."]";
+          }
+          if(isset($entry['mime'])){
+            $cm.=" -".$entry['mime']."";
+          }
 
-        $cm ="";
-        if(isset($entry['comment'])){
-          $cm=" [".$entry['comment']."]";
+          $ret[$entry['ID']]= $entry['name'].$cm;
         }
-        if(isset($entry['mime'])){
-          $cm.=" -".$entry['mime']."";
-        }
-
-        $ret[$entry['ID']]= $entry['name'].$cm;
       }
     }
     return($ret);