Code

Added some comments and checks
[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         print_red(_("Can't delete this attachment, it is still in use."));
102       }else{
103         $this->parent->handle->deleteAttachment($this->delAttach);
104         $attach     = $this->parent->handle->getAttachments();
105       }
106     }
108     /* Someone tries to upload a file */
109     if(($this->edit == true)&&(isset($_POST['upload']))){
110       if(!isset($_FILES['filename'])){
111         print_red(_("There is no valid file uploaded."));
112       }else{
113         $FILE = $_FILES['filename'];  
114         if(!isset($FILE['name'])){
115           print_red(_("There is no valid file uploaded."));
116         }else{
117           if($FILE['error']!=0) {
118             print_red(_("Upload wasn't successfull."));
119           }else{
120             if(!is_dir("/etc/gosa/glpi/")){
121               print_red(_("Missing directory '/etc/gosa/glpi/' to store glpi uploads."));
122             }else{
123               $filen = "/etc/gosa/glpi/".$FILE['name'];
124               $fh = fopen($filen,"w");
125               if(!$fh){
126                 print_red(sprintf(_("Can't create file '%s'."),$filen));
127               }else{
128                 $str = file_get_contents($FILE['tmp_name']);
129                 fwrite($fh,$str,strlen($str));
130                 fclose($fh);
131                 $this->mime     = $FILE['type'];   
132                 $this->filename = $FILE['name'];   
133               }
134             }
135           } // Check if any error occured
136         } // check if valid filename was uploaded
137       } // ende check if file was uploaded 
138     }// upload post
140     /* save attachment*/
141     if(($this->edit == true)&&(isset($_POST['SaveAttachment']))){
142       if(count($this->check())==0){
143         $this->date_mod = date("Y-m-d H:i:s");
144         $this->save_entry();
145         $this->edit= false;
146         $this->entry = array();
147         $attach = $this->parent->handle->getAttachments();
148       }else{
149         foreach($this->check() as $msg){
150           print_red($msg);
151         }
152       }
153     }
155     /* Abort editing/adding  attachment*/
156     if(($this->edit == true)&&(isset($_POST['CancelAttachment']))){
157       $this->edit  = false;
158       $this->entry = array();
159     }
161     /* If edit == true, we have to show a dialog to edit or add an attach 
162      */
163     if($this->edit == true){
164       foreach($this->attributes as $attr){
165         $smarty->assign($attr,htmlentities(utf8_decode($this->$attr)));
166       }
167       if(!empty($this->filename)){
168         if(is_readable("/etc/gosa/glpi/".$this->filename)){
169           $status =_("File is available.");
170         }else{
171           $status =_("File is not readable, possibly the file is missing.");
172         }
173       }else{
174         $status = _("Currently no file uploaded.");
175       }
176       $smarty->assign("status",$status); 
177       return($smarty->fetch(get_template_path('glpiAttachmentEdit.tpl', TRUE)));
178     }
180     /* Create list with checkboxes to select / deselect some attachents */
181     $divlist = new divlist("Attachment");  
182     $divlist->SetHeader(array(
183           array("string" => "&nbsp;", "attach" => "style='text-align:center;width:20px;'"),
184           array("string" => _("Name")),
185           array("string" => _("Mime"),"attach"=>"style='width:200px;'"),
186           array("string" => _("Actions"), "attach" => "style='width:60px;border-right:0px;text-align:right;'" )));
188     $divlist->SetSummary(_("This table displays all available attachments."));
189     $divlist->SetEntriesPerPage(0);
191     $editdel = "<input type='image' name='editAttach_%s' src='images/edit.png'>";
192     $editdel.= "<input type='image' name='delAttach_%s'  src='images/edittrash.png'>";
194     /* Add all given attachments to divlist 
195      */ 
196     foreach($attach as $entry){
198       /* Create display name*/
199       if((empty($entry['name']))||(empty($entry['comment']))){
201         /* In glpi it is possible to add entries without name 
202            so i've added this block 
203          */ 
204         if(empty($entry['name'])){
205           $str1 = "<i>"._("empty")."</i>";
206         }else{ 
207           $str1 = $entry['name'];
208         }
210         if(!empty($entry['comment'])){
211           $str1.= " [".$entry['comment']."]";
212         }
213       }else{
214         /* Both attributes given */
215         $str1 = $entry['name']." [".$entry['comment']."]";
216       }
218       $edit = str_replace("%s",base64_encode($entry['ID']),$editdel);
219       $str2 = $entry['mime']."&nbsp;";      
221       $chkbox = "<input type='hidden' name='wasOnPage_%s'>".
222         "<input type='checkbox' name='useMe_%s' value='%s' %CHECKED%>";
224       if(in_array($entry['ID'],$this->Selected)){
225         $chkbox = preg_replace("/%CHECKED%/"," checked ",$chkbox);
226       }else {
227         $chkbox = preg_replace("/%CHECKED%/"," ",$chkbox);
228       }
229       $chkbox = preg_replace("/%s/",$entry['ID'],$chkbox);
230       $divlist->AddEntry(array(
231             array("string" => $chkbox, 
232               "attach" => "style='text-align:center;width:20px;'"),
233             array("string"=> $str1),
234             array("string"=> $str2,"attach"=>"style='width:200px;'"),
235             array("string"=> $edit ,"attach" => "style='width:60px;border-right:0px;text-align:right;'")
236             ));
238     }
240     $listhead = "<div style='background:#F0F0F9;padding:5px;'>".
241       "   <input class='center' type='image' 
242       src='images/new.png' align='middle' title='"._("Create new attachment")."' 
243       name='new_attach' alt='"._("New Attachment"). "'>&nbsp;".
244       "</div>";
246     $smarty->assign("attachments",      $divlist->DrawList());
247     $smarty->assign("attachmenthead",   $listhead);
248     $smarty->assign("search_image",     get_template_path('images/search.png'));
249     $smarty->assign("searchu_image",    get_template_path('images/search_user.png'));
250     $smarty->assign("tree_image",       get_template_path('images/tree.png'));
251     $smarty->assign("infoimage",        get_template_path('images/info.png'));
252     $smarty->assign("launchimage",      get_template_path('images/launch.png'));
253     $smarty->assign("apply",            apply_filter());
254     $smarty->assign("alphabet",         generate_alphabet());
255     $smarty->assign("attachment_regex", $_SESSION['GlpiAttachmentFilter']['filter']);
257     $display.= $smarty->fetch(get_template_path('glpiAttachmentPool.tpl', TRUE));
258     return($display);
259   }
261   function save()
262   {
263     return($this->Selected);
264   }
266   /* Adds new or saves edited entries */
267   function save_entry()
268   {
269     if($this->edit){
270       $tmp = array();
271       foreach($this->attributes as $attr){
272         $tmp[$attr] = $this->$attr;
273       } 
274       $id = -1;
275       if(isset($this->entry['ID'])){
276         $id = $this->entry['ID'];
277       }
278       $this->parent->handle->saveAttachments($tmp,$id);
279     }
280   }
282   function save_object()
283   {
284     foreach($this->attributes as $attr){
285       if(isset($_POST[$attr])){
286         $this->$attr = $_POST[$attr];
287       }
288     }
290     /* Checkboxes are only posted, if they are checked
291      * To check if it was deselected, we check if wasOnPage 
292      * was posted with given name, so we can deselect this entry
293      */  
295     foreach($_POST as $name => $value){
296       if(preg_match("/wasOnPage_/",$name)){
297         $id=preg_replace("/wasOnPage_/","",$name);
298         if(isset($_POST["useMe_".$id])){
299           $this->Selected[$id]=$id;
300         }else{
301           if(isset($this->Selected[$id])){
302             unset($this->Selected[$id]);
303           }
304         }
305       }
306     }
307   }
309   /* Simple check */
310   function check()
311   {
312     $message = array();
313     if($this->edit){
314       $this->name=trim($this->name);
315       if(empty($this->name)){
316         $message[] = _("Please specify a valid name for this attachment.");
317       }
318     }
319     return($message);
320   }
322 }// vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
323 ?>