Code

76d2fcb5864987b2524379856fcebef0cd51a77e
[gosa.git] / plugins / admin / fai / class_faiPartitionTableEntry.inc
1 <?php
3 class faiPartitionTableEntry extends plugin
4 {
5   /* attribute list for save action */
6   var $ignore_account= TRUE;
7   var $attributes   = array("DISK_cn","DISK_description");
8   var $objectclasses= array();
10   var $DISK_cn              = "";
11   var $DISK_description     = "";
12   
13   var $partitions           = array();
14   var $UsedAttrs            = array();
15   var $is_edit              = false;
16   var $old_cn               = "";
17   var $status               = "new";
18   var $deletePartitions     = array();
20   var $FAIstate             = "";
22   function faiPartitionTableEntry ($config, $dn= NULL,$usedDiskNames=array(),$disk=false)
23   {
24     plugin::plugin ($config, $dn);
25     $this->UsedAttrs  = array("cn","description","FAIpartitionType","FAIpartitionNr","FAIfsType","FAImountPoint","FAIpartitionSize",
26                          "FAImountOptions","FAIfsOptions","FAIpartitionFlags");
28     /* Default status is new */
29     $this->status = "new";    
30  
31     /* We want to edit an entry */
32     if($disk){
34       /* Set disk status */      
35       $this->status = $disk['status'];
37       /* Walk through partitions */
38       foreach($disk['partitions'] as $name => $values){
40         /* If a partition is already marked as delete, attach it to deletePartitions only. */
41         if($values['status'] == "delete"){
42           unset($disk['partitions'][$name]);
43           $this->deletePartitions[]=$values;
44         }else{
46           /* Set status, to know which partition must be deleted from ldap
47              new    : Neu partition entry // save
48              edited : Update partition entry in ldap
49              deleted: Remove partition from ldap
50            */
51     
52           /* If status is not new, set to edit mode. 
53            * New means that this partition currently wasn't saved to ldap.
54            */
55           if($disk['partitions'][$name]['status']!="new"){
56             $disk['partitions'][$name]['status']="edited";
57           }
58      
59           $disk['partitions'][$name]['old_cn']= $disk['partitions'][$name]['cn'];
60  
61           /* Assign empty attributes, if attribute is missing */
62           foreach($this->UsedAttrs as $attr){
63             if(!isset($values[$attr])){
64               $disk['partitions'][$name][$attr]="";  
65             }
66           }
67         }
68       }
70       /* Set default attributes */
71       $this->DISK_cn          = $disk['cn'];
72       $this->DISK_description = $disk['description'];
73       $this->partitions       = $disk['partitions'];
74       $this->is_edit          = true;
75       $this->old_cn           = $disk['cn'];
76     }
77   }
79   function execute()
80   {
81         /* Call parent execute */
82         plugin::execute();
84     /* Fill templating stuff */
85     $smarty     = get_smarty();
86     $s_action   = "";
87     $s_entry    = "";
88     $display    = "";
89     
90     /* Assign values 
91      * And Checkbox selection
92      */
93     foreach($this->attributes as $attrs){
94       $smarty->assign($attrs,$this->$attrs);
95       if($this->$attrs){
96         $smarty->assign($attrs."CHK"," ");
97       }else{
98         $smarty->assign($attrs."CHK"," disabled ");
99       }
100     }
102     /* Check all Posts if there is something usefull for us,
103      * For example : Delete is posted as Delete_1 
104      * The number specifies the index we want to delete
105      */
106     foreach($_POST as $name => $value){
107       if(preg_match("/Delete_.*/",$name)){
108         $tmp = split("_",$name);
109         $s_action = "remove";
110         $s_entry  = $tmp[1]; 
111       }
112     }
114     /* To remove a partition we unset the index posted.
115      * We must sort the index again, else we possibly got problems 
116      * with partitions order.
117      */
118     if($s_action == "remove"){
119       if($this->partitions[$s_entry]['status'] == "edited"){
120         $this->deletePartitions[] = $this->partitions[$s_entry];
121         unset($this->partitions[$s_entry]);
122       }else{
123         unset($this->partitions[$s_entry]);
124       }
125       $tmp= array();
126       foreach($this->partitions as $part){
127         $tmp[count($tmp)+1]=$part;
128       }
129       $this->partitions = $tmp;
130     }
132     /* To add a partitions we only append an empty 
133      *  array to the already defined partitions.
134      */
135     if(isset($_POST['AddPartition'])){
136   
137       /* FAIpartitionNr is used to indentify an entry.
138            (The index of an entry may vary)
139          Walk through the given entries and check for a free FAIpartitionNr. */
140       $ids = array();
141       foreach($this->partitions as $part){
142         $ids[$part['FAIpartitionNr']] = $part['FAIpartitionNr'];
143       }
144       $new_nr = 1 ; 
145       while(isset($ids[$new_nr]) && $new_nr < 100){
146         $new_nr ++ ;
147       }      
149       /* Add empty attributes */
150       foreach($this->UsedAttrs as $attr){
151         $tmp[$attr]                = "";     
152       }
153       $tmp["old_cn"]               = "";     
154       $tmp['status']               ="new";
155       $tmp['FAIpartitionNr']       = $new_nr;
156       $this->partitions[]=$tmp;
157     }
159     /* $setup contains a table with the partitions. 
160      */
161     $smarty->assign("setup", $this->generateParts());
162     foreach($this->attributes as $attr){
163       $smarty->assign($attr,$this->$attr);
164     }
166     foreach($this->UsedAttrs as $attr){
167       if(($this->FAIstate == "freeze") || (chkacl($this->acl,$attr)!= "")){
168         $smarty->assign($attr."ACL"," disabled ");
169       }else{
170         $smarty->assign($attr."ACL","  ");
171       }
172     }
175     /* The user can't change a diskname 
176      *  if we are currently in edit mode for the specified disk 
177      */
178 //    if($this->is_edit){
179 //      $smarty->assign("DISK_cnACL"," disabled ");
180 //    }else{
181       $smarty->assign("DISK_cnACL","");
182 //    }
184     /* Fetch template and show the result
185      */
186     $display.= $smarty->fetch(get_template_path('faiPartitionTableEntry.tpl', TRUE));
187     return($display);
188   }
190   function generateParts()
191   {
192     /* Define Arrays with allowed syntax */
193     $PartitionTypes   = array("primary"=>_("primary"),"logical"=>_("logical"));
194     $FAIfsTypes       = array("ext2","ext3","xfs","swap","reiser","dosfat16","winfat32");
196     /* Display Header */
197     $str = "<table summary='' style='width:100%'>";
198     if (count($this->partitions)){
199       $str .= "<tr>";
200       $str .= "<td>"._("Type")."</td>";
201       $str .= "<td>"._("FS type")."</td>";
202       $str .= "<td>"._("Mount point")."</td>";
203       $str .= "<td>"._("Size in MB")."</td>";
204       $str .= "<td>"._("Mount options")."</td>";
205       $str .= "<td>"._("FS option")."</td>";
206       $str .= "<td>"._("Preserve")."</td>";
207       $str .= "<td>&nbsp;</td>";
208       $str .= "</tr>";
209     }
210     
211     /* Walk through all defined partitions.
212      * Create a new row for each partition and append it to 
213      *  the header defined above.
214      * To be able to check the posts later, we append a key to each single postfield. like cn_1 ... cn_2
215      */
216     foreach($this->partitions as $key => $part){
217       
218       $dis = "";
219       if($part['FAIpartitionFlags'] == "preserve"){
220         $dis = " disabled ";
221       }
223       $disableALL = "";
224       if($this->FAIstate =="freeze"){
225         $disableALL = " disabled ";
226       }
228       if($part['status']!="delete"){
229         /* Generate Partition select box  
230          */
231         $PartitionType = "<select name='FAIpartitionType_".$key."' id='FAIpartitionType_".$key."' ".$disableALL.">";
232         foreach($PartitionTypes as $type => $PTname){
233           if($part['FAIpartitionType'] == $type){
234             $PartitionType .="<option value='".$type."' selected >".$PTname."</option>";
235           }else{
236             $PartitionType .="<option value='".$type."'>".$PTname."</option>";
237           }
238         }        
239         $PartitionType.="</select>";   
242         /* Generate fsType select box  
243          */
244         $FAIfsType= "<select name='FAIfsType_".$key."' id='FAIfsType_".$key."' ".$dis." ".$disableALL.">";
245         foreach($FAIfsTypes as $type){
246           if($part['FAIfsType'] == $type){
247             $FAIfsType  .="<option value='".$type."' selected >".$type."</option>";
248           }else{
249             $FAIfsType .="<option value='".$type."'>".$type."</option>";
250           }
251         }        
252         $FAIfsType.="</select>";   
254         $str .= "\n<tr>";
255         $str .= "\n<td>".$PartitionType."</td>";
256         $str .= "\n<td>".$FAIfsType."</td>";
257         $str .= "\n<td><input name='FAImountPoint_".$key."'  ".$disableALL."  ".$dis."  value='".$part['FAImountPoint']."'    id='FAImountPoint_".$key."'></td>";
258         $str .= "\n<td><input name='FAIpartitionSize_".$key."' ".$disableALL." ".$dis." value='".$part['FAIpartitionSize']."' id='FAIpartitionSize_".$key."' size=12></td>";
259         $str .= "\n<td><input name='FAImountOptions_".$key."' ".$disableALL."  ".$dis."  value='".$part['FAImountOptions']."'  id='FAImountOptions_".$key."' style='width:100px;'></td>";
260         $str .= "\n<td><input name='FAIfsOptions_".$key."'   ".$disableALL."  ".$dis."  value='".$part['FAIfsOptions']."'     id='FAIfsOptions_".$key."' style='width:100px;'></td>";
262         $changeState = "onClick=\"changeState('FAImountPoint_".$key."') ; ".
263                                  "changeState('FAIpartitionSize_".$key."') ; ".
264                                  "changeState('FAImountOptions_".$key."') ; ".
265                                  "changeState('FAIfsType_".$key."') ; ".
266                                  "changeState('FAIfsOptions_".$key."') ; \"";
267   
269         if($part['FAIpartitionFlags']!=false){
270           $str .= "\n<td><input type='checkbox' name='FAIpartitionFlags_".$key."' value='preserve' checked ".$changeState." ".$disableALL."></td>";
271         }else{
272           $str .= "\n<td><input type='checkbox' name='FAIpartitionFlags_".$key."' value='preserve' ".$changeState." ".$disableALL."></td>";
273         }
274         $str .= "\n<td><input type='submit' name='Delete_".$key."' value='"._("Remove")."' ".$disableALL."></td>";
275         $str .= "\n</tr>";    
276       }
277     }
278     $str.="</table>";
279     return($str);
281   }
283   function save()
284   {
285     $tmp = array();
286     $tmp['cn']          = $this->DISK_cn;
288     /* Attach deleted */
289     foreach($this->deletePartitions as $key=>$val) {
290       $this->partitions[$val['FAIpartitionNr']."-delete"]=$val;
291       $this->partitions[$val['FAIpartitionNr']."-delete"]['status']="delete";
292     }
294     $tmp['description'] = $this->DISK_description;
295     $tmp['partitions']  = $this->partitions;
296     $tmp['status']      = $this->status;
298     /* If hdd name has changed, tell partitionTable to rename it */
299     if(($this->is_edit)&&($this->old_cn != $this->DISK_cn)){
300       $tmp['rename']['from']  = $this->old_cn;
301       $tmp['rename']['to']    = $this->DISK_cn;
302     }
303     return($tmp);
304   }
307   /* Save data to object */
308   function save_object()
309   {
310     if((isset($_POST['TableEntryFrameSubmitted'])) && ($this->FAIstate != "freeze") ){
311       plugin::save_object();
313       /* Check base attributes */
314       foreach($this->attributes as $attrs){
315         if(isset($_POST[$attrs])){
316           $this->$attrs = $_POST[$attrs];
317         }
318       }
319      
320       foreach($this->partitions as $key => $part){
321         foreach($this->UsedAttrs as $attrs){
323           if(!in_array_ics($attrs,array("FAIpartitionNr"))){
324             if(isset($_POST[$attrs."_".$key])){ 
325               $this->partitions[$key][$attrs] = $_POST[$attrs."_".$key];
326             }else{
327               $this->partitions[$key][$attrs] = false;
328             }
329           }
330         }
331       }
332     }
333   }
336   /* Check supplied data */
337   function check()
338   {
339     /* Call common method to give check the hook */
340     $message= plugin::check();
341    
342     /* check every partition.
343      * if there is an invalid value defined, append an errorstr to message
344      */
346     /* Array that contain every partitionname mountpoint etc already assigned */
347     $alreadyUsed    = array();
348     foreach($this->UsedAttrs as $attrs){
349       $alreadyUsed[$attrs] = array();
350     }      
352     foreach($this->partitions as $key => $part){
353   
354       /* Skip all checks, if preserve is set */ 
355       if($part['FAIpartitionFlags'] == "preserve"){
356         $this->partitions[$key]['FAIfsType']        = "preserve";
357         $this->partitions[$key]['FAIpartitionSize'] = "preserve";
358         continue;
359       }
360  
361       if((in_array($part['FAImountPoint'],$alreadyUsed['FAImountPoint']))&&($part['FAIfsType']!="swap")){
362         $message[]=sprintf(_("please enter a unique mount point for partition %s"),($key));
363       }
365       if($part['FAIfsType']!="swap"){
366         if((empty($part['FAImountPoint']))||(!((preg_match("/^\/.*/",$part['FAImountPoint']))||(preg_match("/^swap$/",$part['FAImountPoint']))))){
367           $message[]=sprintf(_("Please enter a valid mount point for partition %s." ),($key));
368         }
369       }
370       if($part['FAIfsType'] == "swap"){
371         if(in_array($part['FAIfsType'],$alreadyUsed['FAIfsType'])){
372           $message[]=sprintf(_("File system type 'swap' is already used, change file system type for partition %s."),$key);
373         }
374       }
375       if(($part['FAIfsType'] == "swap")&&(!empty($part['FAImountPoint']))&&($part['FAImountPoint']!="swap")){
376         $message[]=_("Please use 'swap' as mount point, if 'swap' is used as fs-type.");
377       }
379       $tmp = split("-",$part['FAIpartitionSize']);
380       switch (count($tmp)){
381         case 0:
382                 $message[]= sprintf(_("Please enter a valid partition size for partition %s."),($key));
383                 break;
384         case 1:
385                 if (!is_id(is_id($tmp[0]))){
386                   $message[]= sprintf(_("Please enter a valid partition size for partition %s."),($key));
387                 }
388                 break;
389                 
390         case 2:
391                 if((!is_id($tmp[0]))&&(!is_id($tmp[1]))&&(!empty($tmp[1]))){
392                   $message[]=sprintf(_("Please enter a valid range for partition %s."),($key)); 
393                 }elseif($tmp[0]>=$tmp[1] &&(!empty($tmp[1]))){
394                   $message[]=sprintf(_("Please enter a valid range for partition %s."),($key));
395                 }
396                 break;
398         default:
399                 $message[]=sprintf(_("Please enter a range for partition size for partition %s."),($key));
400       }
402       foreach($this->UsedAttrs as $attrs){
403         $alreadyUsed[$attrs][$key] = $part[$attrs];
404       }      
405     }
407     $cnt = 0;
408     foreach($this->partitions as $key => $part){
409       if($part['FAIpartitionType'] == "primary"){
410         $cnt ++ ; 
411       }
412     }
413     if($cnt > 3){
414       $message[] = _("You have more than 3 primary partition table entries in your configuration, please check your configuration twice.");
415     }
417     return ($message);
418   }
419  
422 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
423 ?>