Code

Added some comments
[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");
13   var $objectclasses  = array("whatever");
15   var $parent;
16   var $edit = false;
17   var $entry = false;
19   var $name     ="";
20   var $comment  ="";
21   var $mime     ="";
22   var $filename ="";  
24   var $Selected = array();
26   function glpiAttachmentPool ($config, $dn= NULL,$used=NULL)
27   {
28     plugin::plugin ($config, $dn);
29     if(!isset($_SESSION['GlpiAttachmentFilter'])){
30       $_SESSION['GlpiAttachmentFilter'] = array("filter"=>"*");
31     }
33     /* Assign used attributes */
34     if($used != NULL){
35       $this->Selected = $used;
36     }
37   }
39   function execute()
40   {
41     plugin::execute();
42     $attach     = $this->parent->handle->getAttachments();
43     /* Fill templating stuff */
44     $smarty     = get_smarty();
45     $display    = "";
46     $only_once  = true; 
48     /* walk through all posted objects */
49     foreach($_POST as $name => $value){
51       /* Open dialog to create a new entry */
52       if(preg_match("/new_attach/",$name)){
53         $this->edit = true;
54         $this->entry=array();
56         /* Set default values */
57         foreach($this->attributes as $attr) {
58           $this->$attr = "";
59           $this->entry[$attr]="";
60         }
61       }
62   
63       /* Remove attach*/ 
64       if((preg_match("/^delAttach_/",$name))&&($only_once)){
65         $only_once = false;
66         $str = preg_replace("/^delAttach_/","",$name);
67         $str = base64_decode(preg_replace("/_.*$/","",$str));
69         /* remove attach from db */
70         $this->parent->handle->deleteAttachment($str);
71       }
72      
73       /* Start editing entry */ 
74       if((preg_match("/^editAttach_/",$name))&&($only_once)){
75         $only_once = false;
76         $str = preg_replace("/^editAttach_/","",$name);
77         $str = base64_decode(preg_replace("/_.*$/","",$str));
78         
79         /* Check if we have an attachment with given ID */ 
80         foreach($attach as $att ){
81           if($att['ID'] == $str ){
83             /* Entry was found, show dialog */          
84             $this->edit = true;
85             $this->entry  = $att;       
87             foreach($att as $name => $value){
88               $this->$name = $value;
89             } 
90           } 
91         }
92       }
93     }
95     /* Someone tries to upload a file */
96     if(($this->edit == true)&&(isset($_POST['upload']))){
97       if(!isset($_FILES['filename'])){
98         print_red(_("There is no valid file uploaded."));
99       }else{
100         $FILE = $_FILES['filename'];  
101         if(!isset($FILE['name'])){
102           print_red(_("There is no valid file uploaded."));
103         }else{
104           if($FILE['error']!=0) {
105             print_red(_("Upload wasn't successfull."));
106           }else{
107             if(!is_dir("/etc/gosa/glpi/")){
108               print_red(_("Missing directory '/etc/gosa/glpi/' to store glpi uploads."));
109             }else{
110               $filen = "/etc/gosa/glpi/".$FILE['name'];
111               $fh = fopen($filen,"w");
112               if(!$fh){
113                 print_red(sprintf(_("Can't create file '%s'."),$filen));
114               }else{
115                 $str = file_get_contents($FILE['tmp_name']);
116                 fwrite($fh,$str,strlen($str));
117                 fclose($fh);
118                 $this->mime     = $FILE['type'];   
119                 $this->filename = $FILE['name'];   
120               }
121             }
122           } // Check if any error occured
123         } // check if valid filename was uploaded
124       } // ende check if file was uploaded 
125     }// upload post
127     /* save attachment*/
128     if(($this->edit == true)&&(isset($_POST['SaveAttachment']))){
129       if(count($this->check())==0){
130         $this->save_entry();
131         $this->edit= false;
132         $this->entry = array();
133         $attach = $this->parent->handle->getAttachments();
134       }else{
135         foreach($this->check() as $msg){
136           print_red($msg);
137         }
138       }
139     }
141     /* Abort editing/adding  attachment*/
142     if(($this->edit == true)&&(isset($_POST['CancelAttachment']))){
143       $this->edit  = false;
144       $this->entry = array();
145     }
147     /* If edit == true, we have to show a dialog to edit or add an attach 
148      */
149     if($this->edit == true){
150       foreach($this->attributes as $attr){
151         $smarty->assign($attr,$this->$attr);
152       }
153       if(!empty($this->filename)){
154         if(is_readable("/etc/gosa/glpi/".$this->filename)){
155           $status =_("File is available.");
156         }else{
157           $status =_("File is not readable, possibly the file is missing.");
158         }
159       }else{
160         $status = _("Currently no file uploaded.");
161       }
162       $smarty->assign("status",$status); 
163       return($smarty->fetch(get_template_path('glpiAttachmentEdit.tpl', TRUE)));
164     }
166     /* Create list with checkboxes to select / deselect some attachents */
167     $divlist = new divlist("Attachment");  
168     $divlist->SetHeader(array(
169           array("string" => "&nbsp;", "attach" => "style='text-align:center;width:20px;'"),
170           array("string" => _("Name")),
171           array("string" => _("Mime"),"attach"=>"style='width:200px;'"),
172           array("string" => _("Actions"), "attach" => "style='width:60px;border-right:0px;text-align:right;'" )));
174     $divlist->SetSummary(_("This table displays all available attachments."));
175     $divlist->SetEntriesPerPage(0);
177     $editdel = "<input type='image' name='editAttach_%s' src='images/edit.png'>";
178     $editdel.= "<input type='image' name='delAttach_%s'  src='images/edittrash.png'>";
180     /* Add all given attachments to divlist 
181      */ 
182     foreach($attach as $entry){
184       /* Create display name*/
185       if((empty($entry['name']))||(empty($entry['comment']))){
187         /* In glpi it is possible to add entries without name 
188            so i've added this block 
189          */ 
190         if(empty($entry['name'])){
191           $str1 = "<i>"._("empty")."</i>";
192         }else{ 
193           $str1 = $entry['name'];
194         }
196         if(!empty($entry['comment'])){
197           $str1.= " [".$entry['comment']."]";
198         }
199       }else{
200         /* Both attributes given */
201         $str1 = $entry['name']." [".$entry['comment']."]";
202       }
204       $edit = str_replace("%s",base64_encode($entry['ID']),$editdel);
205       $str2 = $entry['mime']."&nbsp;";      
207       $chkbox = "<input type='hidden' name='wasOnPage_%s'>".
208         "<input type='checkbox' name='useMe_%s' value='%s' %CHECKED%>";
210       if(in_array($entry['ID'],$this->Selected)){
211         $chkbox = preg_replace("/%CHECKED%/"," checked ",$chkbox);
212       }else {
213         $chkbox = preg_replace("/%CHECKED%/"," ",$chkbox);
214       }
215       $chkbox = preg_replace("/%s/",$entry['ID'],$chkbox);
216       $divlist->AddEntry(array(
217             array("string" => $chkbox, 
218               "attach" => "style='text-align:center;width:20px;'"),
219             array("string"=> $str1),
220             array("string"=> $str2,"attach"=>"style='width:200px;'"),
221             array("string"=> $edit ,"attach" => "style='width:60px;border-right:0px;text-align:right;'")
222             ));
224     }
226     $listhead = "<div style='background:#F0F0F9;padding:5px;'>".
227       "   <input class='center' type='image' 
228       src='images/new.png' align='middle' title='"._("Create new attachment")."' 
229       name='new_attach' alt='"._("New Attachment"). "'>&nbsp;".
230       "</div>";
232     $smarty->assign("attachments",      $divlist->DrawList());
233     $smarty->assign("attachmenthead",   $listhead);
234     $smarty->assign("search_image",     get_template_path('images/search.png'));
235     $smarty->assign("searchu_image",    get_template_path('images/search_user.png'));
236     $smarty->assign("tree_image",       get_template_path('images/tree.png'));
237     $smarty->assign("infoimage",        get_template_path('images/info.png'));
238     $smarty->assign("launchimage",      get_template_path('images/launch.png'));
239     $smarty->assign("apply",            apply_filter());
240     $smarty->assign("alphabet",         generate_alphabet());
241     $smarty->assign("attachment_regex", $_SESSION['GlpiAttachmentFilter']['filter']);
243     $display.= $smarty->fetch(get_template_path('glpiAttachmentPool.tpl', TRUE));
244     return($display);
245   }
247   function save()
248   {
249     return($this->Selected);
250   }
252   /* Adds new or saves edited entries */
253   function save_entry()
254   {
255     if($this->edit){
256       $tmp = array();
257       foreach($this->attributes as $attr){
258         $tmp[$attr] = $this->$attr;
259       } 
260       $id = -1;
261       if(isset($this->entry['ID'])){
262         $id = $this->entry['ID'];
263       }
264       $this->parent->handle->saveAttachments($tmp,$id);
265     }
266   }
268   function save_object()
269   {
270     foreach($this->attributes as $attr){
271       if(isset($_POST[$attr])){
272         $this->$attr = $_POST[$attr];
273       }
274     }
276     /* Checkboxes are only posted, if they are checked
277      * To check if it was deselected, we check if wasOnPage 
278      * was posted with given name, so we can deselect this entry
279      */  
281     foreach($_POST as $name => $value){
282       if(preg_match("/wasOnPage_/",$name)){
283         $id=preg_replace("/wasOnPage_/","",$name);
284         if(isset($_POST["useMe_".$id])){
285           $this->Selected[$id]=$id;
286         }else{
287           if(isset($this->Selected[$id])){
288             unset($this->Selected[$id]);
289           }
290         }
291       }
292     }
293   }
295   /* Simple check */
296   function check()
297   {
298     $message = array();
299     if($this->edit){
300       if(empty($this->name)){
301         $message[] = _("Please specify a valid name for this attachment.");
302       }
303     }
304     return($message);
305   }
307 }// vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
308 ?>