Code

Cutted objects will be displayed in light grey in the management lists now.
[gosa.git] / plugins / admin / systems / class_glpiPrinterAccount.inc
index 1e8aac795b74059abace4b9e453b721857953ac4..65f090b1146f89b15857334ae3709c1c6a293f0d 100644 (file)
@@ -2,11 +2,6 @@
 
 class glpiPrinterAccount extends plugin
 {
-  /* CLI vars */
-  var $cli_summary= "Manage server basic objects";
-  var $cli_description= "Some longer text\nfor help";
-  var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
-
   /* attribute list for save action */
   var $ignore_account= FALSE;
   var $attributes= array("ID","name","contact","ramSize","flags_serial","flags_par","flags_usb",
@@ -21,7 +16,7 @@ class glpiPrinterAccount extends plugin
   
   var $comments           = "";    // Comment
   
-  var $type               = 0;     // System type id
+  var $type               = 0;     // printer type id
   var $serial             = "";   
   var $otherserial        = "";  
   var $ramSize            = 0;
@@ -39,6 +34,11 @@ class glpiPrinterAccount extends plugin
   var $contact            = "";    // Empty
   var $deleted            = "N";   // Deleted entries should have this set to Y
 
+  var $rename             = false;
+  var $select_type        ;
+
+  var $editManufacturer   = false;
+
   /* Not necessary, cause we use mysql databse */
   var $objectclasses= array("whatever");
 
@@ -59,15 +59,18 @@ class glpiPrinterAccount extends plugin
   
   var $usedDevices      = array();  // Which devices are currently selected 
   var $usedAttachments  = array();  // Used Attachments 
+  var $usedCartridges   = array();  // Used Cartridges
 
   /* Contructor 
      Sets default values and checks if we already have an existing glpi account
    */
-  function glpiPrinterAccount ($config, $dn= NULL)
+  function glpiPrinterAccount ($config, $dn= NULL, $parent= NULL)
   {
-    plugin::plugin ($config, $dn);
+    plugin::plugin ($config, $dn, $parent);
     $this->ui= get_userinfo();
 
+    $this->is_account = false;
+
     /* Abort class construction, if no db is defined */
     if(!isset($this->config->data['SERVERS']['GLPI'])){
       return;
@@ -101,6 +104,14 @@ class glpiPrinterAccount extends plugin
       foreach($atts as $attachment){
         $this->usedAttachments[$attachment['FK_doc']]=$attachment['FK_doc']; 
       }
+
+      $cart= $this->handle->getUsedCartridges($tmp[0]['ID']);
+      foreach($cart as $key => $cartridge){
+        $this->usedCartridges[$key]=$cartridge; 
+        $this->usedCartridges[$key]['status']="exists"; 
+      }
+
+
     }else{
       $this->is_account = false;
     }
@@ -120,14 +131,16 @@ class glpiPrinterAccount extends plugin
     $smarty= get_smarty();
     $display= "";
 
+    $smarty->assign("CartridgesACL",chkacl($this->acl,"Cartridges"));
+
     /*  Assign smarty defaults 
         To avoid undefined indexes, if there is an error with the glpi db
      */ 
-    foreach(array("PrinterTypeKeys","PrinterTypes") 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") 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 ");
     }
@@ -173,15 +186,24 @@ class glpiPrinterAccount extends plugin
 
     /* Do we need to flip is_account state? */
     if (isset($_POST['modify_state'])){
-      $this->is_account= !$this->is_account;
+
+      /* Only change account state if allowed */
+      if($this->is_account && $this->acl == "#all#"){
+        $this->is_account= !$this->is_account;
+        $this->is_modified = true;
+      }elseif(!$this->is_account && chkacl($this->acl,"create") == ""){
+        $this->is_account= !$this->is_account;
+        $this->is_modified = true;
+      }
     }
 
+
     /* Show tab dialog headers */
     if ($this->is_account){
-      $display= $this->show_header(_("Remove inventory service"),
+      $display= $this->show_header(_("Remove inventory"),
           _("This server has inventory features enabled. You can disable them by clicking below."));
     } else {
-      $display= $this->show_header(_("Add inventory service"),
+      $display= $this->show_header(_("Add inventory"),
           _("This server has inventory features disabled. You can enable them by clicking below."));
       return ($display);
     }
@@ -192,9 +214,32 @@ class glpiPrinterAccount extends plugin
      *  Dialog 
      */
 
+    /* Rename was requested */
+    if(isset($_POST['Rename_PType_OK']) && chkacl($this->acl,"type") == ""){
+      $tmp = $this->handle->getPrinterTypes();
+      $allok = true;
+      foreach($tmp as $id => $name){
+        if(trim($name) == trim($_POST['string'])){
+          $allok = false;
+        }
+      }
+      if($allok){
+        $this->handle->updatePrinterType($_POST['string'],$this->select_type);
+        $this->rename = false;
+      }else{
+        print_red(sprintf(_("Can't rename given printer type to '%s', because this type name already exists."),$_POST['string']));
+      }
+    }
+
+    /* abort rename 
+     */
+    if(isset($_POST['Rename_Cancel'])){
+      $this->rename = false;
+    }
+
     /* Printer type management
      */
-    if(isset($_POST['edit_type'])){
+    if(isset($_POST['edit_type']) && chkacl($this->acl,"type") == ""){
       $this->dialog = true;
       $this->edit_type=true;
     }
@@ -208,26 +253,64 @@ 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']);  
+    if((isset($_POST['add_type']))&&(!empty($_POST['type_string'])) && chkacl($this->acl,"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->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']);  
+    if((isset($_POST['del_type']))&&(!empty($_POST['select_type'])) && chkacl($this->acl,"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']);
-    }
+    if((isset($_POST['rename_type']))&&(!empty($_POST['select_type']))||($this->rename) && chkacl($this->acl,"type") == ""){
+      $this->rename = true;
+
+      $smarty->assign("Method","rename");
+
+      $tmp = $this->handle->getPrinterTypes();
+
+      if(isset($_POST['select_type'])){
+        $this->select_type = $_POST['select_type'];
+      }  
+      $smarty->assign("string",$tmp[$this->select_type]);
+      if(isset($_POST['string'])){
+        $smarty->assign("string",$_POST['string']);
+      }
+
+      $display= $smarty->fetch(get_template_path('glpi_edit_printer_type.tpl', TRUE));
+      return($display);
+    }  
 
     /* Someone wants to edit the printer types ... 
        So, lets open a new dialog which provides some buttons to edit the types
      */
     if($this->edit_type){
+      $smarty->assign("Method","edit");
       $smarty->assign("PrinterTypes",            $this->handle->getPrinterTypes());
       $smarty->assign("PrinterTypeKeys",         array_flip($this->handle->getPrinterTypes()));
       $display= $smarty->fetch(get_template_path('glpi_edit_printer_type.tpl', TRUE));
@@ -242,16 +325,18 @@ class glpiPrinterAccount extends plugin
 
     /* Open dialog which allows to edit the manufacturers
      */
-    if(isset($_POST['edit_manufacturer'])){
+    if(isset($_POST['edit_manufacturer']) && chkacl($this->acl,"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) && chkacl($this->acl,"manufacturer") == ""){
       $this->dialog = false;
       $this->cur_dialog = false;
+      $this->editManufacturer=false;
     }
 
 
@@ -264,20 +349,21 @@ class glpiPrinterAccount extends plugin
     /* Show dialog to select a new contact person
      * Select a contact person
      */
-    if(isset($_POST['SelectContactPerson'])){
+    if(isset($_POST['SelectContactPerson']) && chkacl($this->acl,"user_tech_num") == ""){
       $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'])){
+    if(isset($_POST['SelectTechPerson']) && chkacl($this->acl,"user_tech_num") == ""){
       $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 ="";
@@ -285,7 +371,7 @@ class glpiPrinterAccount extends plugin
     }
 
     /* Technical responsible/contact person selected */
-    if(isset($_GET['act'])&&($_GET['act']=="user_tech_num")){
+    if(isset($_GET['act'])&&($_GET['act']=="user_tech_num") && chkacl($this->acl,"user_tech_num") == ""){
 
       /* Get posted id */
       $id = base64_decode($_GET['id']);
@@ -294,7 +380,8 @@ class glpiPrinterAccount extends plugin
       if(!in_array($id,$users)){
 
         /* If this user doesn't exists in glpi db, we must create him */
-        $atr = $ldap->fetch($ldap->cat($id));
+        $ldap->cat($id, array('cn', 'mail', 'telephoneNumber'));
+        $atr = $ldap->fetch();
         $tmp = array();
         $use = array( "cn"              =>"name",
             "mail"            =>"email",
@@ -335,7 +422,7 @@ class glpiPrinterAccount extends plugin
     
     /* Attachment pool was closed with use
      */
-    if(isset($_POST['UseAttachment'])){
+    if(isset($_POST['UseAttachment']) && chkacl($this->acl,"useAttachments") == ""){
       if(count($this->cur_dialog->check())){
         foreach($this->cur_dialog->check() as $msg){
           print_red($msg);
@@ -357,19 +444,78 @@ class glpiPrinterAccount extends plugin
 
     /* Open Attachment pool to add/edit Attachments
      */
-    if(isset($_POST['AddAttachment'])){
+    if(isset($_POST['AddAttachment']) && chkacl($this->acl,"useAttachments") == ""){
       $this->cur_dialog = new glpiAttachmentPool($this->config,$this->dn,$this->usedAttachments);
       $this->dialog = true;
     }
     
     /* Remove Attachment from this tab 
      */
-    if((isset($_POST['RemoveAttachment']))&&(isset($_POST['Attachments']))){
+    $once = true;
+    foreach($_POST as $name => $value){
+      if((preg_match("/^delAttachment_/",$name))&&($once) && chkacl($this->acl,"useAttachments") == ""){
+        $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'])) && chkacl($this->acl,"useAttachments") == ""){
       if(isset($this->usedAttachments[$_POST['Attachments']])){
         unset($this->usedAttachments[$_POST['Attachments']]);
       }
     }
 
+    /*  ##########################################################################
+     *  Printer Cartridge handling 
+     */
+
+    /* Abort cartridge select dialog
+     */
+    if(isset($_POST['SelectCartridgeCancel'])){
+      $this->cur_dialog = false;
+      $this->edit_type = false; 
+    }
+
+    /* Get selected cartridges and add them to our list 
+     */ 
+    if(isset($_POST['SelectCartridgeSave'])){
+      $this->cur_dialog->save_object();
+      $carts = $this->cur_dialog->save();
+      foreach($carts as $cart){
+        $cart['status'] = "new";
+        $this->usedCartridges[] = $cart;
+      }
+      $this->cur_dialog = false;
+      $this->edit_type = false; 
+    }
+
+    /* Remove cartridge  
+     */
+    if((isset($_POST['RemoveCartridge']))&&(isset($_POST['Cartridges'])) && chkacl($this->acl,"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]);
+          }
+        }
+      }
+    }
+
+    /* Open Attachment pool to add/edit Attachments
+     */
+    if(isset($_POST['AddCartridge']) && chkacl($this->acl,"cartridges") == ""){
+      $this->cur_dialog = new glpiPrinterCartridges($this->config,$this->dn,$this->type);
+      $this->dialog = true;
+    }
+
+
     /*  ##########################################################################
      *  Draw Dialogs
      */
@@ -390,7 +536,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));
     }
@@ -427,15 +573,39 @@ 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()));
 
+    /* Assign Cartridges 
+     */
+    $smarty->assign("Cartridges",            $this->getUsedCartridges());
+    $smarty->assign("CartridgeKeys",         $this->getUsedCartridges(true));
 
     /*  ##########################################################################
      *  Assign contact and technical responsible person 
      */
     if(isset($users[$this->contact_num])){
-      $tr = $ldap->fetch($ldap->cat($users[$this->contact_num]));
+      $ldap->cat($users[$this->contact_num], array('givenName', 'sn', 'uid'));
+      $tr = $ldap->fetch();
       $str = "";
       if(isset($tr['givenName'][0])){   $str .= $tr['givenName'][0]." ";      }
       if(isset($tr['sn'][0])) {         $str .= $tr['sn'][0]." ";             }
@@ -449,7 +619,8 @@ class glpiPrinterAccount extends plugin
        Assign name ... to smarty, if set
      */ 
     if(isset($users[$this->tech_num])){
-      $tr = $ldap->fetch($ldap->cat($users[$this->tech_num]));
+      $ldap->cat($users[$this->tech_num], array('givenName', 'sn', 'uid'));
+      $tr = $ldap->fetch();
       $str = "";
       if(isset($tr['givenName'][0])){   $str .= $tr['givenName'][0]." ";      }
       if(isset($tr['sn'][0])) {         $str .= $tr['sn'][0]." ";             }
@@ -459,6 +630,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);
   }
@@ -475,18 +659,17 @@ 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];
-      }
-    }
-
-    foreach(array("flags_serial","flags_par","flags_usb") as $checkboxes){
-      if(isset($_POST[$checkboxes])){
-        $this->$checkboxes = 1;
-      }else{
-        $this->$checkboxes = 0;
+    if(isset($_POST['glpiPrinterFlagsPosted'])){
+      plugin::save_object();
+
+      foreach(array("flags_serial","flags_par","flags_usb") as $checkboxes){
+        if(chkacl($this->acl,$checkboxes) == ""){
+          if(isset($_POST[$checkboxes])){
+            $this->$checkboxes = 1;
+          }else{
+            $this->$checkboxes = 0;
+          }
+        }
       }
     }
 
@@ -496,7 +679,8 @@ class glpiPrinterAccount extends plugin
   /* Check supplied data */
   function check()
   {
-    $message= array();
+    /* Call common method to give check the hook */
+    $message= plugin::check();
 
     //    if($this->TechnicalResponsible == ""){
     //      $message[] = _("Please select a technical responsible person for this entry.");
@@ -524,31 +708,55 @@ class glpiPrinterAccount extends plugin
       }
       $tmp = $this->handle->getPrinterInformations($this->dn);
       $this->handle->addAttachmentsToPrinter($this->usedAttachments,$tmp[0]['ID']);
+
+      foreach($this->usedCartridges as $cart){
+        if($cart['status'] == "deleted"){
+          $this->handle->removeCartridgeFromPrinter($cart['ID']);
+        }elseif($cart['status'] == "new"){
+          $this->handle->addCartridgeFromPrinter($tmp[0]['ID'],$cart['type_ID']);
+        }
+      }
     }
   }
 
   /* 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']."]";
-        }
-        if(isset($entry['mime'])){
-          $cm.=" -".$entry['mime']."";
+          $ret[$entry['ID']]= $entry['name'].$cm;
         }
-
-        $ret[$entry['ID']]= $entry['name'].$cm;
       }
     }
     return($ret);
   }
-  
+
+  function getUsedCartridges($flip = false)
+  {
+    $ret = array();
+    foreach($this->usedCartridges as $key => $value){
+      if($value['status'] == "deleted") continue;
+      if($flip){
+        $ret[$key] = $key;    
+      }else{
+        $ret[$key] = $value['name']." [".$value['type_name']."] "._("since")." :".$value['date_use'];
+      }
+    }
+    return($ret);
+  }  
 
 }