Code

default is now visible as _("inherited")
[gosa.git] / plugins / admin / systems / class_glpiAttachmentPool.inc
index 4cfd7f46e470d4887c2d0ef3f93316e0dcc177bf..8b0300d6f65669bbe48af73d96e160f2e94938d8 100644 (file)
@@ -9,13 +9,13 @@ class glpiAttachmentPool extends plugin
 
   /* attribute list for save action */
   var $ignore_account = TRUE;
-  var $attributes     = array("name","comment","mime","filename");
+  var $attributes     = array("name","comment","mime","filename","date_mod");
   var $objectclasses  = array("whatever");
 
   var $parent;
   var $edit = false;
   var $entry = false;
-
+  var $date_mod ="";
   var $name     ="";
   var $comment  ="";
   var $mime     ="";
@@ -23,6 +23,8 @@ class glpiAttachmentPool extends plugin
 
   var $Selected = array();
 
+  var $delAttach= "";
+
   function glpiAttachmentPool ($config, $dn= NULL,$used=NULL)
   {
     plugin::plugin ($config, $dn);
@@ -67,9 +69,11 @@ class glpiAttachmentPool extends plugin
         $str = base64_decode(preg_replace("/_.*$/","",$str));
 
         /* remove attach from db */
-        $this->parent->handle->deleteAttachment($str);
+        $this->delAttach = $str;
+        $smarty->assign("warning", sprintf(_("You're about to delete the glpi attachment component '%s'."), $attach[$str]['name']));
+        return($smarty->fetch(get_template_path('remove_glpi.tpl', TRUE)));
       }
-     
       /* Start editing entry */ 
       if((preg_match("/^editAttach_/",$name))&&($only_once)){
         $only_once = false;
@@ -92,6 +96,28 @@ class glpiAttachmentPool extends plugin
       }
     }
 
+    if((isset($_POST['delete_glpi_confirm']))&&(isset($attach[$this->delAttach]))){
+      if($this->parent->handle->is_attachmentUsed($this->delAttach)){
+        $tmp = $this->parent->handle->is_attachmentUsed($this->delAttach);
+        
+        $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 attachment, it is still in use by these system(s) '%s'"),$names));
+
+      }else{
+        $this->parent->handle->deleteAttachment($this->delAttach);
+        @unlink("/etc/gosa/glpi/".$this->filename);
+        $attach     = $this->parent->handle->getAttachments();
+      }
+    }
+
     /* Someone tries to upload a file */
     if(($this->edit == true)&&(isset($_POST['upload']))){
       if(!isset($_FILES['filename'])){
@@ -108,16 +134,20 @@ class glpiAttachmentPool extends plugin
               print_red(_("Missing directory '/etc/gosa/glpi/' to store glpi uploads."));
             }else{
               $filen = "/etc/gosa/glpi/".$FILE['name'];
-              $fh = fopen($filen,"w");
-              if(!$fh){
-                print_red(sprintf(_("Can't create file '%s'."),$filen));
+              if(file_exists($filen)){
+                print_red(_("There is already a file with the same name uploaded."));
               }else{
-                $str = file_get_contents($FILE['tmp_name']);
-                fwrite($fh,$str,strlen($str));
-                fclose($fh);
-                $this->mime     = $FILE['type'];   
-                $this->filename = $FILE['name'];   
-              }
+                $fh = fopen($filen,"w");
+                if(!$fh){
+                  print_red(sprintf(_("Can't create file '%s'."),$filen));
+                }else{
+                  $str = file_get_contents($FILE['tmp_name']);
+                  fwrite($fh,$str,strlen($str));
+                  fclose($fh);
+                  $this->mime     = $FILE['type'];   
+                  $this->filename = $FILE['name'];   
+                }
+              } // File already exists
             }
           } // Check if any error occured
         } // check if valid filename was uploaded
@@ -127,6 +157,7 @@ class glpiAttachmentPool extends plugin
     /* save attachment*/
     if(($this->edit == true)&&(isset($_POST['SaveAttachment']))){
       if(count($this->check())==0){
+        $this->date_mod = date("Y-m-d H:i:s");
         $this->save_entry();
         $this->edit= false;
         $this->entry = array();
@@ -148,7 +179,7 @@ class glpiAttachmentPool extends plugin
      */
     if($this->edit == true){
       foreach($this->attributes as $attr){
-        $smarty->assign($attr,$this->$attr);
+        $smarty->assign($attr,htmlentities(utf8_decode($this->$attr)));
       }
       if(!empty($this->filename)){
         if(is_readable("/etc/gosa/glpi/".$this->filename)){
@@ -225,7 +256,7 @@ class glpiAttachmentPool extends plugin
 
     $listhead = "<div style='background:#F0F0F9;padding:5px;'>".
       "   <input class='center' type='image' 
-      src='images/new.png' align='middle' title='"._("Create new attachment")."' 
+      src='images/fai_new_template.png' align='middle' title='"._("Create new attachment")."' 
       name='new_attach' alt='"._("New Attachment"). "'>&nbsp;".
       "</div>";
 
@@ -234,8 +265,8 @@ class glpiAttachmentPool extends plugin
     $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("attachment_regex", $_SESSION['GlpiAttachmentFilter']['filter']);
@@ -295,8 +326,30 @@ class glpiAttachmentPool extends plugin
   /* Simple check */
   function check()
   {
-    $message = array();
+    /* Call common method to give check the hook */
+    $message= plugin::check();
+
     if($this->edit){
+    
+      /* check if given name is already in use */
+      $att = $this->parent->handle->getAttachments();
+      $ok = true;
+      $this->name=trim($this->name);
+
+      foreach($att as $val){
+        if(!isset($this->entry['ID'])){
+          if($val['name'] == $this->name){
+            $ok = false;
+          }
+        }else{
+          if(($val['name'] == $this->name)&&($this->entry['ID'] != $val['ID'])){
+            $ok = false;
+          }
+        }
+      }
+      if(!$ok){
+        $message[] = _("This name is already in use.");
+      }
       if(empty($this->name)){
         $message[] = _("Please specify a valid name for this attachment.");
       }