Code

Some updates for glpi
[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     }
32     if($used != NULL){
33       $this->Selected = $used;
34     }
35   }
37   function execute()
38   {
39     plugin::execute();
40     $attach = $this->parent->handle->getAttachments();
41     /* Fill templating stuff */
42     $smarty= get_smarty();
43     $display= "";
44     $only_once = true; 
45     foreach($_POST as $name => $value){
47       if(preg_match("/new_attach/",$name)){
48         $this->edit = true;
49         $this->entry=array();
50         foreach($this->attributes as $attr) {
51           $this->$attr = "";
52           $this->entry[$attr]="";
53         }
54       }
55   
56       if((preg_match("/^delAttach_/",$name))&&($only_once)){
57         $only_once = false;
58         $str = preg_replace("/^delAttach_/","",$name);
59         $str = base64_decode(preg_replace("/_.*$/","",$str));
60         $this->parent->handle->deleteAttachment($str);
61       }
62       
63       if((preg_match("/^editAttach_/",$name))&&($only_once)){
64         $only_once = false;
65         $str = preg_replace("/^editAttach_/","",$name);
66         $str = base64_decode(preg_replace("/_.*$/","",$str));
67           
68         $this->edit = true;
69         foreach($attach as $att ){
70       
71           if($att['ID'] == $str ){
72             $this->entry  = $att;        
73           } 
74         }
75       }
76     }
78     if(($this->edit == true)&&(isset($_POST['upload']))){
79       if(!isset($_FILES['filename'])){
80         print_red(_("There is no valid file uploaded."));
81       }else{
82         $FILE = $_FILES['filename'];  
83         if(!isset($FILE['name'])){
84           print_red(_("There is no valid file uploaded."));
85         }else{
86           if($FILE['error']!=0) {
87             print_red(_("Upload wasn't successfull."));
88           }else{
89             if(!is_dir("/etc/gosa/glpi/")){
90               print_red(_("Missing directory '/etc/gosa/glpi/' to store glpi uploads."));
91             }else{
92               $filen = "/etc/gosa/glpi/".$FILE['name'];
93               $fh = fopen($filen,"w");
94               if(!$fh){
95                 print_red(sprintf(_("Can't create file '%s'."),$filen));
96               }else{
97                 $str = file_get_contents($FILE['tmp_name']);
98                 fwrite($fh,$str,strlen($str));
99                 fclose($fh);
100                 $this->mime     = $FILE['type'];   
101                 $this->filename = $FILE['name'];   
102               }
103             }
104           } // Check if any error occured
105         } // check if valid filename was uploaded
106       } // ende check if file was uploaded 
107     }// upload post
109     if(($this->edit == true)&&(isset($_POST['SaveAttachment']))){
110       if(count($this->check())==0){
111         $this->save_entry();
112         $this->edit= false;
113         $this->entry = array();
114         $attach = $this->parent->handle->getAttachments();
115       }else{
116         foreach($this->check() as $msg){
117           print_red($msg);
118         }
119       }
120     }
122     if(($this->edit == true)&&(isset($_POST['CancelAttachment']))){
123       $this->edit  = false;
124       $this->entry = array();
125     }
127     if($this->edit == true){
128       foreach($this->attributes as $attr){
129         $smarty->assign($attr,$this->$attr);
130       }
131       if(!empty($this->filename)){
132         if(is_readable("/etc/gosa/glpi/".$this->filename)){
133           $status =_("File is available.");
134         }else{
135           $status =_("File is not readable, possibly the file is missing.");
136         }
137       }else{
138         $status = _("Currently no file uploaded.");
139       }
140       $smarty->assign("status",$status); 
141       return($smarty->fetch(get_template_path('glpiAttachmentEdit.tpl', TRUE)));
142     }
148     $divlist = new divlist("Attachment");  
149     $divlist->SetHeader(array(
150           array("string" => "&nbsp;", "attach" => "style='text-align:center;width:20px;'"),
151           array("string" => _("Name")),
152           array("string" => _("Mime"),"attach"=>"style='width:200px;'"),
153           array("string" => _("Actions"), "attach" => "style='width:60px;border-right:0px;text-align:right;'" )));
155     $divlist->SetSummary(_("This table displays all available attachments."));
156     $divlist->SetEntriesPerPage(0);
158     $editdel = "<input type='image' name='editAttach_%s' src='images/edit.png'>";
159     $editdel.= "<input type='image' name='delAttach_%s'  src='images/edittrash.png'>";
161     foreach($attach as $entry){
162       
163         if((empty($entry['name']))||(empty($entry['comment']))){
164           
165           if(empty($entry['name'])){
166             $str1 = "<i>"._("empty")."</i>";
167           }else{ 
168             $str1 = $entry['name'];
169           }
170           
171           if(!empty($entry['comment'])){
172             $str1.= " [".$entry['comment']."]";
173           }
174         }else{
175           $str1 = $entry['name']." [".$entry['comment']."]";
176         }
178         $edit = str_replace("%s",base64_encode($entry['ID']),$editdel);
179         $str2 = $entry['mime']."&nbsp;";      
181         $chkbox = "<input type='hidden' name='wasOnPage_%s'>".
182                   "<input type='checkbox' name='useMe_%s' value='%s' %CHECKED%>";
183         
184         if(in_array($entry['ID'],$this->Selected)){
185           $chkbox = preg_replace("/%CHECKED%/"," checked ",$chkbox);
186         }else {
187           $chkbox = preg_replace("/%CHECKED%/"," ",$chkbox);
188         }
189         $chkbox = preg_replace("/%s/",$entry['ID'],$chkbox);
190         $divlist->AddEntry(array(
191                                   array("string" => $chkbox, 
192                                         "attach" => "style='text-align:center;width:20px;'"),
193                                   array("string"=> $str1),
194                                   array("string"=> $str2,"attach"=>"style='width:200px;'"),
195                                   array("string"=> $edit ,"attach" => "style='width:60px;border-right:0px;text-align:right;'")
196                                 ));
197  
198     }
200     $listhead = "<div style='background:#F0F0F9;padding:5px;'>".
201               "   <input class='center' type='image' 
202                     src='images/new.png' align='middle' title='"._("Create new attachment")."' 
203                     name='new_attach' alt='"._("New Attachment"). "'>&nbsp;".
204               "</div>";
207     $smarty->assign("attachments",      $divlist->DrawList());
208     $smarty->assign("attachmenthead",   $listhead);
209     $smarty->assign("search_image",     get_template_path('images/search.png'));
210     $smarty->assign("searchu_image",    get_template_path('images/search_user.png'));
211     $smarty->assign("tree_image",       get_template_path('images/tree.png'));
212     $smarty->assign("infoimage",        get_template_path('images/info.png'));
213     $smarty->assign("launchimage",      get_template_path('images/launch.png'));
214     $smarty->assign("apply",            apply_filter());
215     $smarty->assign("alphabet",         generate_alphabet());
216     $smarty->assign("attachment_regex", $_SESSION['GlpiAttachmentFilter']['filter']);
219     $display.= $smarty->fetch(get_template_path('glpiAttachmentPool.tpl', TRUE));
220     return($display);
221   }
223   function save()
224   {
225     return($this->Selected);
226   }
228   function save_entry()
229   {
230     if($this->edit){
231       $tmp = array();
232       foreach($this->attributes as $attr){
233         $tmp[$attr] = $this->$attr;
234       } 
235       $id = -1;
236       if(isset($this->entry['ID'])){
237         $id = $this->entry['ID'];
238       }
239       $this->parent->handle->saveAttachments($tmp,$id);
240     }
241   }
243   function save_object()
244   {
245     foreach($this->attributes as $attr){
246       if(isset($_POST[$attr])){
247         $this->$attr = $_POST[$attr];
248       }
249     }
251     foreach($_POST as $name => $value){
252       if(preg_match("/wasOnPage_/",$name)){
253         $id=preg_replace("/wasOnPage_/","",$name);
254         if(isset($_POST["useMe_".$id])){
255           $this->Selected[$id]=$id;
256         }else{
257           if(isset($this->Selected[$id])){
258             unset($this->Selected[$id]);
259           }
260         }
261       }
262     }
263   }
265   function check()
266   {
267     $message = array();
268     if($this->edit){
269       if(empty($this->name)){
270         $message[] = _("Please specify a valid name for this attachment.");
271       }
272     }
273     return($message);
274   }
276 }// vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
277 ?>