Code

Attachmentpool dialog fixed
[gosa.git] / plugins / admin / systems / class_glpiAttachmentPool.inc
1 <?php
3 class glpiAttachmentPool extends plugin
4 {
5   /* CLI vars */
6   var $cli_summary= "Manage server basic objects";
7   var $cli_description= "Some longer text\nfor help";
8   var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
10   /* attribute list for save action */
11   var $ignore_account = TRUE;
12   var $attributes     = array("name","comment","mime","filename","date_mod");
13   var $objectclasses  = array("whatever");
15   var $parent;
16   var $edit = false;
17   var $entry = false;
18   var $date_mod ="";
19   var $name     ="";
20   var $comment  ="";
21   var $mime     ="";
22   var $filename ="";  
24   var $Selected = array();
26   var $delAttach= "";
28   function glpiAttachmentPool ($config, $dn= NULL,$used=NULL)
29   {
30     plugin::plugin ($config, $dn);
31     if(!isset($_SESSION['GlpiAttachmentFilter'])){
32       $_SESSION['GlpiAttachmentFilter'] = array("filter"=>"*");
33     }
35     /* Assign used attributes */
36     if($used != NULL){
37       $this->Selected = $used;
38     }
39   }
41   function execute()
42   {
43     plugin::execute();
44     $attach     = $this->parent->handle->getAttachments();
45     /* Fill templating stuff */
46     $smarty     = get_smarty();
47     $display    = "";
48     $only_once  = true; 
50     /* walk through all posted objects */
51     foreach($_POST as $name => $value){
53       /* Open dialog to create a new entry */
54       if(preg_match("/new_attach/",$name)){
55         $this->edit = true;
56         $this->entry=array();
58         /* Set default values */
59         foreach($this->attributes as $attr) {
60           $this->$attr = "";
61           $this->entry[$attr]="";
62         }
63       }
64   
65       /* Remove attach*/ 
66       if((preg_match("/^delAttach_/",$name))&&($only_once)){
67         $only_once = false;
68         $str = preg_replace("/^delAttach_/","",$name);
69         $str = base64_decode(preg_replace("/_.*$/","",$str));
71         /* remove attach from db */
72         $this->delAttach = $str;
73         $smarty->assign("warning", sprintf(_("You're about to delete the glpi attachment component '%s'."), $attach[$str]['name']));
74         return($smarty->fetch(get_template_path('remove_glpi.tpl', TRUE)));
75       }
76  
77       /* Start editing entry */ 
78       if((preg_match("/^editAttach_/",$name))&&($only_once)){
79         $only_once = false;
80         $str = preg_replace("/^editAttach_/","",$name);
81         $str = base64_decode(preg_replace("/_.*$/","",$str));
82         
83         /* Check if we have an attachment with given ID */ 
84         foreach($attach as $att ){
85           if($att['ID'] == $str ){
87             /* Entry was found, show dialog */          
88             $this->edit = true;
89             $this->entry  = $att;       
91             foreach($att as $name => $value){
92               $this->$name = $value;
93             } 
94           } 
95         }
96       }
97     }
99     if((isset($_POST['delete_glpi_confirm']))&&(isset($attach[$this->delAttach]))){
100       if($this->parent->handle->is_attachmentUsed($this->delAttach)){
101         $tmp = $this->parent->handle->is_attachmentUsed($this->delAttach);
102         
103         $names = "";
104         foreach($tmp as $name){
105           $names .= ", ".$name;
106         }
107         $names = preg_replace("/^, /","",$names);
108         $names = trim($names);
109         if(count($tmp) == 3){
110           $names .= " ...";
111         }
112         print_red(sprintf(_("You can't delete this attachment, it is still in use by these system(s) '%s'"),$names));
114       }else{
115         $this->parent->handle->deleteAttachment($this->delAttach);
116         $attach     = $this->parent->handle->getAttachments();
117       }
118     }
120     /* Someone tries to upload a file */
121     if(($this->edit == true)&&(isset($_POST['upload']))){
122       if(!isset($_FILES['filename'])){
123         print_red(_("There is no valid file uploaded."));
124       }else{
125         $FILE = $_FILES['filename'];  
126         if(!isset($FILE['name'])){
127           print_red(_("There is no valid file uploaded."));
128         }else{
129           if($FILE['error']!=0) {
130             print_red(_("Upload wasn't successfull."));
131           }else{
132             if(!is_dir("/etc/gosa/glpi/")){
133               print_red(_("Missing directory '/etc/gosa/glpi/' to store glpi uploads."));
134             }else{
135               $filen = "/etc/gosa/glpi/".$FILE['name'];
136               $fh = fopen($filen,"w");
137               if(!$fh){
138                 print_red(sprintf(_("Can't create file '%s'."),$filen));
139               }else{
140                 $str = file_get_contents($FILE['tmp_name']);
141                 fwrite($fh,$str,strlen($str));
142                 fclose($fh);
143                 $this->mime     = $FILE['type'];   
144                 $this->filename = $FILE['name'];   
145               }
146             }
147           } // Check if any error occured
148         } // check if valid filename was uploaded
149       } // ende check if file was uploaded 
150     }// upload post
152     /* save attachment*/
153     if(($this->edit == true)&&(isset($_POST['SaveAttachment']))){
154       if(count($this->check())==0){
155         $this->date_mod = date("Y-m-d H:i:s");
156         $this->save_entry();
157         $this->edit= false;
158         $this->entry = array();
159         $attach = $this->parent->handle->getAttachments();
160       }else{
161         foreach($this->check() as $msg){
162           print_red($msg);
163         }
164       }
165     }
167     /* Abort editing/adding  attachment*/
168     if(($this->edit == true)&&(isset($_POST['CancelAttachment']))){
169       $this->edit  = false;
170       $this->entry = array();
171     }
173     /* If edit == true, we have to show a dialog to edit or add an attach 
174      */
175     if($this->edit == true){
176       foreach($this->attributes as $attr){
177         $smarty->assign($attr,htmlentities(utf8_decode($this->$attr)));
178       }
179       if(!empty($this->filename)){
180         if(is_readable("/etc/gosa/glpi/".$this->filename)){
181           $status =_("File is available.");
182         }else{
183           $status =_("File is not readable, possibly the file is missing.");
184         }
185       }else{
186         $status = _("Currently no file uploaded.");
187       }
188       $smarty->assign("status",$status); 
189       return($smarty->fetch(get_template_path('glpiAttachmentEdit.tpl', TRUE)));
190     }
192     /* Create list with checkboxes to select / deselect some attachents */
193     $divlist = new divlist("Attachment");  
194     $divlist->SetHeader(array(
195           array("string" => "&nbsp;", "attach" => "style='text-align:center;width:20px;'"),
196           array("string" => _("Name")),
197           array("string" => _("Mime"),"attach"=>"style='width:200px;'"),
198           array("string" => _("Actions"), "attach" => "style='width:60px;border-right:0px;text-align:right;'" )));
200     $divlist->SetSummary(_("This table displays all available attachments."));
201     $divlist->SetEntriesPerPage(0);
203     $editdel = "<input type='image' name='editAttach_%s' src='images/edit.png'>";
204     $editdel.= "<input type='image' name='delAttach_%s'  src='images/edittrash.png'>";
206     /* Add all given attachments to divlist 
207      */ 
208     foreach($attach as $entry){
210       /* Create display name*/
211       if((empty($entry['name']))||(empty($entry['comment']))){
213         /* In glpi it is possible to add entries without name 
214            so i've added this block 
215          */ 
216         if(empty($entry['name'])){
217           $str1 = "<i>"._("empty")."</i>";
218         }else{ 
219           $str1 = $entry['name'];
220         }
222         if(!empty($entry['comment'])){
223           $str1.= " [".$entry['comment']."]";
224         }
225       }else{
226         /* Both attributes given */
227         $str1 = $entry['name']." [".$entry['comment']."]";
228       }
230       $edit = str_replace("%s",base64_encode($entry['ID']),$editdel);
231       $str2 = $entry['mime']."&nbsp;";      
233       $chkbox = "<input type='hidden' name='wasOnPage_%s'>".
234         "<input type='checkbox' name='useMe_%s' value='%s' %CHECKED%>";
236       if(in_array($entry['ID'],$this->Selected)){
237         $chkbox = preg_replace("/%CHECKED%/"," checked ",$chkbox);
238       }else {
239         $chkbox = preg_replace("/%CHECKED%/"," ",$chkbox);
240       }
241       $chkbox = preg_replace("/%s/",$entry['ID'],$chkbox);
242       $divlist->AddEntry(array(
243             array("string" => $chkbox, 
244               "attach" => "style='text-align:center;width:20px;'"),
245             array("string"=> $str1),
246             array("string"=> $str2,"attach"=>"style='width:200px;'"),
247             array("string"=> $edit ,"attach" => "style='width:60px;border-right:0px;text-align:right;'")
248             ));
250     }
252     $listhead = "<div style='background:#F0F0F9;padding:5px;'>".
253       "   <input class='center' type='image' 
254       src='images/new.png' align='middle' title='"._("Create new attachment")."' 
255       name='new_attach' alt='"._("New Attachment"). "'>&nbsp;".
256       "</div>";
258     $smarty->assign("attachments",      $divlist->DrawList());
259     $smarty->assign("attachmenthead",   $listhead);
260     $smarty->assign("search_image",     get_template_path('images/search.png'));
261     $smarty->assign("searchu_image",    get_template_path('images/search_user.png'));
262     $smarty->assign("tree_image",       get_template_path('images/tree.png'));
263     $smarty->assign("infoimage",        get_template_path('images/info_small.png'));
264     $smarty->assign("launchimage",      get_template_path('images/small_filter.png'));
265     $smarty->assign("apply",            apply_filter());
266     $smarty->assign("alphabet",         generate_alphabet());
267     $smarty->assign("attachment_regex", $_SESSION['GlpiAttachmentFilter']['filter']);
269     $display.= $smarty->fetch(get_template_path('glpiAttachmentPool.tpl', TRUE));
270     return($display);
271   }
273   function save()
274   {
275     return($this->Selected);
276   }
278   /* Adds new or saves edited entries */
279   function save_entry()
280   {
281     if($this->edit){
282       $tmp = array();
283       foreach($this->attributes as $attr){
284         $tmp[$attr] = $this->$attr;
285       } 
286       $id = -1;
287       if(isset($this->entry['ID'])){
288         $id = $this->entry['ID'];
289       }
290       $this->parent->handle->saveAttachments($tmp,$id);
291     }
292   }
294   function save_object()
295   {
296     foreach($this->attributes as $attr){
297       if(isset($_POST[$attr])){
298         $this->$attr = $_POST[$attr];
299       }
300     }
302     /* Checkboxes are only posted, if they are checked
303      * To check if it was deselected, we check if wasOnPage 
304      * was posted with given name, so we can deselect this entry
305      */  
307     foreach($_POST as $name => $value){
308       if(preg_match("/wasOnPage_/",$name)){
309         $id=preg_replace("/wasOnPage_/","",$name);
310         if(isset($_POST["useMe_".$id])){
311           $this->Selected[$id]=$id;
312         }else{
313           if(isset($this->Selected[$id])){
314             unset($this->Selected[$id]);
315           }
316         }
317       }
318     }
319   }
321   /* Simple check */
322   function check()
323   {
324     $message = array();
325     if($this->edit){
326     
327       /* check if given name is already in use */
328       $att = $this->parent->handle->getAttachments();
329       $ok = true;
330       $this->name=trim($this->name);
332       foreach($att as $val){
333         if(!isset($this->entry['ID'])){
334           if($val['name'] == $this->name){
335             $ok = false;
336           }
337         }else{
338           if(($val['name'] == $this->name)&&($this->entry['ID'] != $val['ID'])){
339             $ok = false;
340           }
341         }
342       }
343       if(!$ok){
344         $message[] = _("This name is already in use.");
345       }
346       if(empty($this->name)){
347         $message[] = _("Please specify a valid name for this attachment.");
348       }
349     }
350     return($message);
351   }
353 }// vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
354 ?>