Code

Added several comments
[gosa.git] / plugins / admin / fai / class_faiPartitionTableEntry.inc
1 <?php
3 class faiPartitionTableEntry 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("DISK_cn","DISK_description");
13   var $objectclasses= array();
15   var $DISK_cn              = "";
16   var $DISK_description     = "";
17   
18   var $partitions           = array();
19   var $UsedAttrs            = array();
20   var $is_edit              = false;
21   var $old_cn               = "";
22   var $status               = "new";
23   var $deletePartitions     = array();
25   var $FAIstate             = "";
27   function faiPartitionTableEntry ($config, $dn= NULL,$usedDiskNames=array(),$disk=false)
28   {
29     plugin::plugin ($config, $dn);
30     $this->UsedAttrs  = array("cn","description","FAIpartitionType","FAIpartitionNr","FAIfsType","FAImountPoint","FAIpartitionSize",
31                          "FAImountOptions","FAIfsOptions","FAIpartitionFlags");
33     /* Default status is new */
34     $this->status = "new";    
35  
36     /* We want to edit an entry */
37     if($disk){
39       /* Set disk status */      
40       $this->status = $disk['status'];
42       /* Walk through partitions */
43       foreach($disk['partitions'] as $name => $values){
45         /* If a partition is already marked as delete, attach it to deletePartitions only. */
46         if($values['status'] == "delete"){
47           unset($disk['partitions'][$name]);
48           $this->deletePartitions[]=$values;
49         }else{
51           /* Set status, to know which partition must be deleted from ldap
52              new    : Neu partition entry // save
53              edited : Update partition entry in ldap
54              deleted: Remove partition from ldap
55            */
56     
57           /* If status is not new, set to edit mode. 
58            * New means that this partition currently wasn't saved to ldap.
59            */
60           if($disk['partitions'][$name]['status']!="new"){
61             $disk['partitions'][$name]['status']="edited";
62           }
63      
64           $disk['partitions'][$name]['old_cn']= $disk['partitions'][$name]['cn'];
65  
66           /* Assign empty attributes, if attribute is missing */
67           foreach($this->UsedAttrs as $attr){
68             if(!isset($values[$attr])){
69               $disk['partitions'][$name][$attr]="";  
70             }
71           }
72         }
73       }
75       /* Set default attributes */
76       $this->DISK_cn          = $disk['cn'];
77       $this->DISK_description = $disk['description'];
78       $this->partitions       = $disk['partitions'];
79       $this->is_edit          = true;
80       $this->old_cn           = $disk['cn'];
81     }
82   }
84   function execute()
85   {
86         /* Call parent execute */
87         plugin::execute();
89     /* Fill templating stuff */
90     $smarty     = get_smarty();
91     $s_action   = "";
92     $s_entry    = "";
93     $display    = "";
94     
95     /* Assign values 
96      * And Checkbox selection
97      */
98     foreach($this->attributes as $attrs){
99       $smarty->assign($attrs,$this->$attrs);
100       if($this->$attrs){
101         $smarty->assign($attrs."CHK"," ");
102       }else{
103         $smarty->assign($attrs."CHK"," disabled ");
104       }
105     }
107     /* Check all Posts if there is something usefull for us,
108      * For example : Delete is posted as Delete_1 
109      * The number specifies the index we want to delete
110      */
111     foreach($_POST as $name => $value){
112       if((preg_match("/Delete_.*/",$name)) && $this->acl_is_removeable()){
113         $tmp = split("_",$name);
114         $s_action = "remove";
115         $s_entry  = $tmp[1]; 
116       }
117     }
119     /* To remove a partition we unset the index posted.
120      * We must sort the index again, else we possibly got problems 
121      * with partitions order.
122      */
123     if($s_action == "remove" && $this->acl_is_removeable()){
124       if($this->partitions[$s_entry]['status'] == "edited"){
125         $this->deletePartitions[]= $this->partitions[$s_entry];
126         unset($this->partitions[$s_entry]);
127       }else{
128         unset($this->partitions[$s_entry]);
129       }
130       $tmp= array();
131       foreach($this->partitions as $part){
132         $tmp[count($tmp)+1]=$part;
133       }
134       $this->partitions = $tmp;
135     }
137     /* To add a partitions we only append an empty 
138      *  array to the already defined partitions.
139      */
140     if(isset($_POST['AddPartition']) && $this->acl_is_createable()){
141       foreach($this->UsedAttrs as $attr){
142         $tmp[$attr]                = "";     
143       }
144       $tmp["old_cn"]               = "";     
145       $tmp['status']="new";
146       $this->partitions[count($this->partitions)+1]=$tmp;
147     }
149     /* $setup contains a table with the partitions. 
150      */
151     $smarty->assign("setup", $this->generateParts());
152     foreach($this->attributes as $attr){
153       $smarty->assign($attr,$this->$attr);
154     }
156     $tmp = $this->plInfo();
157     $sacl = "";
158     foreach($tmp['plProvidedAcls'] as $name => $translated){
160       $acl = $this->getacl($name);
161       $sacl .= $acl;
162       if($this->FAIstate == "freezed"){
163         $acl = preg_replace("/w/","",$acl);
164       }
165       $smarty->assign($name."ACL",$acl);
166     }
167     $smarty->assign("allowSave",$sacl);
168    
169     $smarty->assign("sub_object_is_createable",$this->acl_is_createable());
170  
171     /* Fetch template and show the result
172      */
173     $display.= $smarty->fetch(get_template_path('faiPartitionTableEntry.tpl', TRUE));
174     return($display);
175   }
177   function generateParts()
178   {
179     /* Define Arrays with allowed syntax */
180     $PartitionTypes   = array("primary"=>_("primary"),"logical"=>_("logical"));
181     $FAIfsTypes       = array("ext2","ext3","xfs","swap","reiserfs","dosfat16","winfat32");
183     /* Display Header */
184     $str = "<table summary='' style='width:100%'>";
185     if (count($this->partitions)){
186       $str .= "<tr>";
187       $str .= "<td><b>"._("Type")."</b></td>";
188       $str .= "<td><b>"._("FS type")."</b></td>";
189       $str .= "<td><b>"._("Mount point")."</b></td>";
190       $str .= "<td><b>"._("Size in MB")."</b></td>";
191       $str .= "<td><b>"._("Mount options")."</b></td>";
192       $str .= "<td><b>"._("FS option")."</b></td>";
193       $str .= "<td><b>"._("Preserve")."</b></td>";
194       $str .= "<td>&nbsp;</td>";
195       $str .= "</tr>";
196     }
197     
198     /* Walk through all defined partitions.
199      * Create a new row for each partition and append it to 
200      *  the header defined above.
201      * To be able to check the posts later, we append a key to each single postfield. like cn_1 ... cn_2
202      */
203     foreach($this->partitions as $key => $part){
204       
205       $dis = "";
206       if($part['FAIpartitionFlags'] == "preserve"){
207         $dis = " disabled ";
208       }
210       $disableALL = "";
211       if($this->FAIstate =="freeze"){
212         $disableALL = " disabled ";
213       }
215       if($part['status']!="delete"){
217         /* Generate Partition select box  
218          */
219         $PartitionType = "";
220         if($this->acl_is_writeable("FAIpartitionType")){
221           $PartitionType = "<select name='FAIpartitionType_".$key."' id='FAIpartitionType_".$key."' ".$disableALL.">";
222           foreach($PartitionTypes as $type => $PTname){
223             if($part['FAIpartitionType'] == $type){
224               $PartitionType .="<option value='".$type."' selected >".$PTname."</option>";
225             }else{
226               $PartitionType .="<option value='".$type."'>".$PTname."</option>";
227             }
228           }        
229           $PartitionType.="</select>";   
230         }elseif($this->acl_is_readable("FAIpartitionType")){
231           $PartitionType = $part['FAIpartitionType'];
232         }
235         /* Generate fsType select box  
236          */
237         $FAIfsType= "";
238         if($this->acl_is_writeable("FAIfsType")){
239           $FAIfsType= "<select name='FAIfsType_".$key."' id='FAIfsType_".$key."' ".$dis." ".$disableALL.">";
240           foreach($FAIfsTypes as $type){
241             if($part['FAIfsType'] == $type){
242               $FAIfsType  .="<option value='".$type."' selected >".$type."</option>";
243             }else{
244               $FAIfsType .="<option value='".$type."'>".$type."</option>";
245             }
246           }        
247           $FAIfsType.="</select>";   
248         }elseif($this->acl_is_readable("FAIfsType")){
249           $FAIfsType = $part['FAIfsType'];
250         }
252         foreach(array("FAImountPoint","FAIpartitionSize","FAImountOptions","FAIfsOptions") as $attr){
253           $$attr  = "";
254           if($this->acl_is_writeable($attr)){
255             $$attr = "<input name='".$attr."_".$key."'  ".$disableALL."  ".$dis."
256                         value='".$part[$attr]."'    id='".$attr."_".$key."'>";
258           }elseif($this->acl_is_readable($attr)){
259             $$attr = $part[$attr];
260           }
261         }
264         $str .= "\n<tr>";
265         $str .= "\n<td>".$PartitionType."</td>";
266         $str .= "\n<td>".$FAIfsType."</td>";
267         $str .= "\n<td>".$FAImountPoint."</td>";
268         $str .= "\n<td>".$FAIpartitionSize."</td>";
269         $str .= "\n<td>".$FAImountOptions."</td>";
270         $str .= "\n<td>".$FAIfsOptions."</td>";
272         $changeState = "onClick=\"changeState('FAImountPoint_".$key."') ; ".
273                                  "changeState('FAIpartitionSize_".$key."') ; ".
274                                  "changeState('FAImountOptions_".$key."') ; ".
275                                  "changeState('FAIfsType_".$key."') ; ".
276                                  "changeState('FAIfsOptions_".$key."') ; \"";
277   
278         if($this->acl_is_writeable("FAIpartitionFlags")){
279           if($part['FAIpartitionFlags']!=false){
280             $str .= "\n<td><input type='checkbox' name='FAIpartitionFlags_".$key."' value='preserve' checked ".$changeState." ".$disableALL."></td>";
281           }else{
282             $str .= "\n<td><input type='checkbox' name='FAIpartitionFlags_".$key."' value='preserve' ".$changeState." ".$disableALL."></td>";
283           }
284         }else{
285           if($part['FAIpartitionFlags']!=false){
286             $str .= "<td>"._("False")."</td>";
287           }else{
288             $str .= "<td>"._("True")."</td>";
289           }
290         }
291         if($this->acl_is_removeable()){
292           $str .= "\n<td><input type='submit' name='Delete_".$key."' value='"._("Remove")."' ".$disableALL."></td>";    
293         }else{
294           $str .= "\n<td><input type='submit' name='restricted' disabled value='"._("Remove")."'></td>";    
295         }
296         $str .= "\n</tr>";    
297       }
298     }
299     $str.="</table>";
300     return($str);
302   }
304   function save()
305   {
306     $tmp = array();
307     $tmp['cn']          = $this->DISK_cn;
309     /* Attach partitions */
310     foreach($this->partitions as $key=>$val) {
311       $this->partitions[$key]['FAIpartitionNr']=$key;
312     }
314     /* Attach deleted */
315     foreach($this->deletePartitions as $key=>$val) {
316       $this->partitions[$key."-delete"]=$val;
317       $this->partitions[$key."-delete"]['status']="delete";
318     }
320     $tmp['description'] = $this->DISK_description;
321     $tmp['partitions']  = $this->partitions;
322     $tmp['status']      = $this->status;
324     /* If hdd name has changed, tell partitionTable to rename it */
325     if(($this->is_edit)&&($this->old_cn != $this->DISK_cn)){
326       $tmp['rename']['from']  = $this->old_cn;
327       $tmp['rename']['to']    = $this->DISK_cn;
328     }
329     return($tmp);
330   }
333   /* Save data to object */
334   function save_object()
335   {
336     if((isset($_POST['TableEntryFrameSubmitted'])) && ($this->FAIstate != "freeze") ){
337       plugin::save_object();
339       foreach($this->partitions as $key => $part){
340         foreach($this->UsedAttrs as $attrs){
342           if($this->acl_is_writeable($attrs)){
344             if(isset($_POST[$attrs."_".$key])){
345               $this->partitions[$key][$attrs] = $_POST[$attrs."_".$key];
346             }else{
347               $this->partitions[$key][$attrs] = false;
348             }
349           }
350         }
351       }
352     }
353   }
356   /* Check supplied data */
357   function check()
358   {
359     /* Call common method to give check the hook */
360     $message= plugin::check();
361    
362     /* check every partition.
363      * if there is an invalid value defined, append an errorstr to message
364      */
366     /* Array that contain every partitionname mountpoint etc already assigned */
367     $alreadyUsed    = array();
368     foreach($this->UsedAttrs as $attrs){
369       $alreadyUsed[$attrs] = array();
370     }      
372     foreach($this->partitions as $key => $part){
373   
374       /* Skip all checks, if preserve is set */ 
375       if($part['FAIpartitionFlags'] == "preserve"){
376         $this->partitions[$key]['FAIfsType']        = "preserve";
377         $this->partitions[$key]['FAIpartitionSize'] = "preserve";
378         continue;
379       }
380  
381       if((in_array($part['FAImountPoint'],$alreadyUsed['FAImountPoint']))&&($part['FAIfsType']!="swap")){
382         $message[]=sprintf(_("please enter a unique mount point for partition %s"),($key));
383       }
385       if($part['FAIfsType']!="swap"){
386         if((empty($part['FAImountPoint']))||(!((preg_match("/^\/.*/",$part['FAImountPoint']))||(preg_match("/^swap$/",$part['FAImountPoint']))))){
387           $message[]=sprintf(_("Please enter a valid mount point for partition %s." ),($key));
388         }
389       }
390       if($part['FAIfsType'] == "swap"){
391         if(in_array($part['FAIfsType'],$alreadyUsed['FAIfsType'])){
392           $message[]=sprintf(_("File system type 'swap' is already used, change file system type for partition %s."),$key);
393         }
394       }
395       if(($part['FAIfsType'] == "swap")&&(!empty($part['FAImountPoint']))&&($part['FAImountPoint']!="swap")){
396         $message[]=_("Please use 'swap' as mount point, if 'swap' is used as fs-type.");
397       }
399       $tmp = split("-",$part['FAIpartitionSize']);
400       switch (count($tmp)){
401         case 0:
402                 $message[]= sprintf(_("Please enter a valid partition size for partition %s."),($key));
403                 break;
404         case 1:
405                 if (!is_id(is_id($tmp[0]))){
406                   $message[]= sprintf(_("Please enter a valid partition size for partition %s."),($key));
407                 }
408                 break;
409                 
410         case 2:
411                 if((!is_id($tmp[0]))||(!is_id($tmp[1]))){
412                   $message[]=sprintf(_("Please enter a valid range for partition %s."),($key)); 
413                 }elseif($tmp[0]>=$tmp[1]){
414                   $message[]=sprintf(_("Please enter a valid range for partition %s."),($key));
415                 }
416                 break;
418         default:
419                 $message[]=sprintf(_("Please enter a range for partition size for partition %s."),($key));
420       }
422       foreach($this->UsedAttrs as $attrs){
423         $alreadyUsed[$attrs][$key] = $part[$attrs];
424       }      
425     }
427     $cnt = 0;
428     foreach($this->partitions as $key => $part){
429       if($part['FAIpartitionType'] == "primary"){
430         $cnt ++ ; 
431       }
432     }
433     if($cnt > 3){
434       $message[] = _("You have more than 3 primary partition table entries in your configuration, please check your configuration twice.");
435     }
437     return ($message);
438   }
440   
442   /* Return plugin informations for acl handling */
443   function plInfo()
444   {
445     return (array(
446           "plShortName" => _("Partition table entry"),
447           "plDescription" => _("FAI partition table entry"),
448           "plSelfModify"  => FALSE,
449           "plDepends"     => array(),
450           "plPriority"    => 27,
451           "plSection"     => array("administration"),
452           "plCategory"    => array("fai"),
453           "plProvidedAcls" => array(
454             "cn"                => _("Name"),
455             "description"       => _("Description"),
456             "FAIpartitionType"  => _("Partition type"),
457             "FAIpartitionNr"    => _("Partition no."),
458             "FAIfsType"         => _("File system type"),
459             "FAImountPoint"     => _("Mount point"),
460             "FAIpartitionSize"  => _("Partition size"),
461             "FAImountOptions"   => _("Mount options"),
462             "FAIfsOptions"      => _("File system options"),
463             "FAIpartitionFlags" => _("Partition flags"))
464           ));
465   }
467  
470 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
471 ?>