Code

Added support for general check hooks
[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)){
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"){
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'])){
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     foreach($this->UsedAttrs as $attr){
157       if(($this->FAIstate == "freeze") || (chkacl($this->acl,$attr)!= "")){
158         $smarty->assign($attr."ACL"," disabled ");
159       }else{
160         $smarty->assign($attr."ACL","  ");
161       }
162     }
165     /* The user can't change a diskname 
166      *  if we are currently in edit mode for the specified disk 
167      */
168 //    if($this->is_edit){
169 //      $smarty->assign("DISK_cnACL"," disabled ");
170 //    }else{
171       $smarty->assign("DISK_cnACL","");
172 //    }
174   
176     /* Fetch template and show the result
177      */
178     $display.= $smarty->fetch(get_template_path('faiPartitionTableEntry.tpl', TRUE));
179     return($display);
180   }
182   function generateParts()
183   {
184     /* Define Arrays with allowed syntax */
185     $PartitionTypes   = array("primary","logical");
186     $FAIfsTypes       = array("ext2","ext3","xfs","swap","reiserfs","dosfat16","winfat32");
188     /* Display Header */
189     $str = "<table summary='' style='width:100%'>";
190     if (count($this->partitions)){
191       $str .= "<tr>";
192       $str .= "<td>"._("Type")."</td>";
193       $str .= "<td>"._("FS type")."</td>";
194       $str .= "<td>"._("Mount point")."</td>";
195       $str .= "<td>"._("Size in MB")."</td>";
196       $str .= "<td>"._("Mount options")."</td>";
197       $str .= "<td>"._("FS option")."</td>";
198       $str .= "<td>"._("Preserve")."</td>";
199       $str .= "<td>&nbsp;</td>";
200       $str .= "</tr>";
201     }
202     
203     /* Walk through all defined partitions.
204      * Create a new row for each partition and append it to 
205      *  the header defined above.
206      * To be able to check the posts later, we append a key to each single postfield. like cn_1 ... cn_2
207      */
208     foreach($this->partitions as $key => $part){
209       
210       $dis = "";
211       if($part['FAIpartitionFlags'] == "preserve"){
212         $dis = " disabled ";
213       }
215       $disableALL = "";
216       if($this->FAIstate =="freeze"){
217         $disableALL = " disabled ";
218       }
220       if($part['status']!="delete"){
221         /* Generate Partition select box  
222          */
223         $PartitionType = "<select name='FAIpartitionType_".$key."' id='FAIpartitionType_".$key."' ".$disableALL.">";
224         foreach($PartitionTypes as $type){
225           if($part['FAIpartitionType'] == $type){
226             $PartitionType .="<option value='".$type."' selected >".$type."</option>";
227           }else{
228             $PartitionType .="<option value='".$type."'>".$type."</option>";
229           }
230         }        
231         $PartitionType.="</select>";   
234         /* Generate fsType select box  
235          */
236         $FAIfsType= "<select name='FAIfsType_".$key."' id='FAIfsType_".$key."' ".$dis." ".$disableALL.">";
237         foreach($FAIfsTypes as $type){
238           if($part['FAIfsType'] == $type){
239             $FAIfsType  .="<option value='".$type."' selected >".$type."</option>";
240           }else{
241             $FAIfsType .="<option value='".$type."'>".$type."</option>";
242           }
243         }        
244         $FAIfsType.="</select>";   
246         $str .= "\n<tr>";
247         $str .= "\n<td>".$PartitionType."</td>";
248         $str .= "\n<td>".$FAIfsType."</td>";
249         $str .= "\n<td><input name='FAImountPoint_".$key."'  ".$disableALL."  ".$dis."  value='".$part['FAImountPoint']."'    id='FAImountPoint_".$key."'></td>";
250         $str .= "\n<td><input name='FAIpartitionSize_".$key."' ".$disableALL." ".$dis." value='".$part['FAIpartitionSize']."' id='FAIpartitionSize_".$key."' size=12></td>";
251         $str .= "\n<td><input name='FAImountOptions_".$key."' ".$disableALL."  ".$dis."  value='".$part['FAImountOptions']."'  id='FAImountOptions_".$key."' style='width:100px;'></td>";
252         $str .= "\n<td><input name='FAIfsOptions_".$key."'   ".$disableALL."  ".$dis."  value='".$part['FAIfsOptions']."'     id='FAIfsOptions_".$key."' style='width:100px;'></td>";
254         $changeState = "onClick=\"changeState('FAImountPoint_".$key."') ; ".
255                                  "changeState('FAIpartitionSize_".$key."') ; ".
256                                  "changeState('FAImountOptions_".$key."') ; ".
257                                  "changeState('FAIfsType_".$key."') ; ".
258                                  "changeState('FAIfsOptions_".$key."') ; \"";
259   
261         if($part['FAIpartitionFlags']!=false){
262           $str .= "\n<td><input type='checkbox' name='FAIpartitionFlags_".$key."' value='preserve' checked ".$changeState." ".$disableALL."></td>";
263         }else{
264           $str .= "\n<td><input type='checkbox' name='FAIpartitionFlags_".$key."' value='preserve' ".$changeState." ".$disableALL."></td>";
265         }
266         $str .= "\n<td><input type='submit' name='Delete_".$key."' value='"._("Remove")."' ".$disableALL."></td>";
267         $str .= "\n</tr>";    
268       }
269     }
270     $str.="</table>";
271     return($str);
273   }
275   function save()
276   {
277     $tmp = array();
278     $tmp['cn']          = $this->DISK_cn;
280     /* Attach partitions */
281     foreach($this->partitions as $key=>$val) {
282       $this->partitions[$key]['FAIpartitionNr']=$key;
283     }
285     /* Attach deleted */
286     foreach($this->deletePartitions as $key=>$val) {
287       $this->partitions[$key."-delete"]=$val;
288       $this->partitions[$key."-delete"]['status']="delete";
289     }
291     $tmp['description'] = $this->DISK_description;
292     $tmp['partitions']  = $this->partitions;
293     $tmp['status']      = $this->status;
295     /* If hdd name has changed, tell partitionTable to rename it */
296     if(($this->is_edit)&&($this->old_cn != $this->DISK_cn)){
297       $tmp['rename']['from']  = $this->old_cn;
298       $tmp['rename']['to']    = $this->DISK_cn;
299     }
300     return($tmp);
301   }
304   /* Save data to object */
305   function save_object()
306   {
307     if((isset($_POST['TableEntryFrameSubmitted'])) && ($this->FAIstate != "freeze") ){
308       plugin::save_object();
310       /* Check base attributes */
311       foreach($this->attributes as $attrs){
312         if(isset($_POST[$attrs])){
313           $this->$attrs = $_POST[$attrs];
314         }
315       }
316      
317       foreach($this->partitions as $key => $part){
318         foreach($this->UsedAttrs as $attrs){
319           if(isset($_POST[$attrs."_".$key])){
320             $this->partitions[$key][$attrs] = $_POST[$attrs."_".$key];
321           }else{
322             $this->partitions[$key][$attrs] = false;
323           }
324         }
325       }
326     }
327   }
330   /* Check supplied data */
331   function check()
332   {
333     /* Call common method to give check the hook */
334     $message= plugin::check();
335    
336     /* check every partition.
337      * if there is an invalid value defined, append an errorstr to message
338      */
340     /* Array that contain every partitionname mountpoint etc already assigned */
341     $alreadyUsed    = array();
342     foreach($this->UsedAttrs as $attrs){
343       $alreadyUsed[$attrs] = array();
344     }      
346     foreach($this->partitions as $key => $part){
347   
348       /* Skip all checks, if preserve is set */ 
349       if($part['FAIpartitionFlags'] == "preserve"){
350         $this->partitions[$key]['FAIfsType']        = "preserve";
351         $this->partitions[$key]['FAIpartitionSize'] = "preserve";
352         continue;
353       }
354  
355       if((in_array($part['FAImountPoint'],$alreadyUsed['FAImountPoint']))&&($part['FAIfsType']!="swap")){
356         $message[]=sprintf(_("please enter a unique mount point for partition %s"),($key));
357       }
359       if($part['FAIfsType']!="swap"){
360         if((empty($part['FAImountPoint']))||(!((preg_match("/^\/.*/",$part['FAImountPoint']))||(preg_match("/^swap$/",$part['FAImountPoint']))))){
361           $message[]=sprintf(_("Please enter a valid mount point for partition %s." ),($key));
362         }
363       }
364       if($part['FAIfsType'] == "swap"){
365         if(in_array($part['FAIfsType'],$alreadyUsed['FAIfsType'])){
366           $message[]=sprintf(_("File system type 'swap' is already used, change file system type for partition %s."),$key);
367         }
368       }
369       if(($part['FAIfsType'] == "swap")&&(!empty($part['FAImountPoint']))&&($part['FAImountPoint']!="swap")){
370         $message[]=_("Please use 'swap' as mount point, if 'swap' is used as fs-type.");
371       }
373       $tmp = split("-",$part['FAIpartitionSize']);
374       switch (count($tmp)){
375         case 0:
376                 $message[]= sprintf(_("Please enter a valid partition size for partition %s."),($key));
377                 break;
378         case 1:
379                 if (!is_id(is_id($tmp[0]))){
380                   $message[]= sprintf(_("Please enter a valid partition size for partition %s."),($key));
381                 }
382                 break;
383                 
384         case 2:
385                 if((!is_id($tmp[0]))||(!is_id($tmp[1]))){
386                   $message[]=sprintf(_("Please enter a valid range for partition %s."),($key)); 
387                 }elseif($tmp[0]>=$tmp[1]){
388                   $message[]=sprintf(_("Please enter a valid range for partition %s."),($key));
389                 }
390                 break;
392         default:
393                 $message[]=sprintf(_("Please enter a range for partition size for partition %s."),($key));
394       }
396       foreach($this->UsedAttrs as $attrs){
397         $alreadyUsed[$attrs][$key] = $part[$attrs];
398       }      
399     }
401     $cnt = 0;
402     foreach($this->partitions as $key => $part){
403       if($part['FAIpartitionType'] == "primary"){
404         $cnt ++ ; 
405       }
406     }
407     if($cnt > 3){
408       $message[] = _("You have more than 3 primary partition table entries in your configuration, please check your configuration twice.");
409     }
411     return ($message);
412   }
413  
416 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
417 ?>