Code

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