Code

587f8379ab409f82914753644f3db928a20fb74e
[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     /* 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       foreach($this->UsedAttrs as $attr){
137         $tmp[$attr]                = "";     
138       }
139       $tmp["old_cn"]               = "";     
140       $tmp['status']="new";
141       $this->partitions[count($this->partitions)+1]=$tmp;
142     }
144     /* $setup contains a table with the partitions. 
145      */
146     $smarty->assign("setup", $this->generateParts());
147     foreach($this->attributes as $attr){
148       $smarty->assign($attr,$this->$attr);
149     }
151     /* The user can't change a diskname 
152      *  if we are currently in edit mode for the specified disk 
153      */
154 //    if($this->is_edit){
155 //      $smarty->assign("DISK_cnACL"," disabled ");
156 //    }else{
157       $smarty->assign("DISK_cnACL","");
158 //    }
160     /* Fetch template and show the result
161      */
162     $display.= $smarty->fetch(get_template_path('faiPartitionTableEntry.tpl', TRUE));
163     return($display);
164   }
166   function generateParts()
167   {
168     /* Define Arrays with allowed syntax */
169     $PartitionTypes   = array("primary","logical");
170     $FAIfsTypes       = array("ext2","ext3","xfs","swap","reiserfs","dosfat16","winfat32");
172     /* Display Header */
173     $str = "<table style='width:100%'>";
174     if (count($this->partitions)){
175       $str .= "<tr>";
176       $str .= "<td>"._("Type")."</td>";
177       $str .= "<td>"._("FS type")."</td>";
178       $str .= "<td>"._("Mount point")."</td>";
179       $str .= "<td>"._("Size in MB")."</td>";
180       $str .= "<td>"._("Mount options")."</td>";
181       $str .= "<td>"._("FS option")."</td>";
182       $str .= "<td>"._("Preserve")."</td>";
183       $str .= "<td>&nbsp;</td>";
184       $str .= "</tr>";
185     }
186     
187     /* Walk through all defined partitions.
188      * Create a new row for each partition and append it to 
189      *  the header defined above.
190      * To be able to check the posts later, we append a key to each single postfield. like cn_1 ... cn_2
191      */
192     foreach($this->partitions as $key => $part){
193       
194       $dis = "";
195       if($part['FAIpartitionFlags'] == "preserve"){
196         $dis = " disabled ";
197       }
199       if($part['status']!="delete"){
200         /* Generate Partition select box  
201          */
202         $PartitionType = "<select name='FAIpartitionType_".$key."' id='FAIpartitionType_".$key."' >";
203         foreach($PartitionTypes as $type){
204           if($part['FAIpartitionType'] == $type){
205             $PartitionType .="<option value='".$type."' selected >".$type."</option>";
206           }else{
207             $PartitionType .="<option value='".$type."'>".$type."</option>";
208           }
209         }        
210         $PartitionType.="</select>";   
213         /* Generate fsType select box  
214          */
215         $FAIfsType= "<select name='FAIfsType_".$key."' id='FAIfsType_".$key."' ".$dis.">";
216         foreach($FAIfsTypes as $type){
217           if($part['FAIfsType'] == $type){
218             $FAIfsType  .="<option value='".$type."' selected >".$type."</option>";
219           }else{
220             $FAIfsType .="<option value='".$type."'>".$type."</option>";
221           }
222         }        
223         $FAIfsType.="</select>";   
225         $str .= "\n<tr>";
226         $str .= "\n<td>".$PartitionType."</td>";
227         $str .= "\n<td>".$FAIfsType."</td>";
228         $str .= "\n<td><input name='FAImountPoint_".$key."'    ".$dis."  value='".$part['FAImountPoint']."'    id='FAImountPoint_".$key."'></td>";
229         $str .= "\n<td><input name='FAIpartitionSize_".$key."'  ".$dis." value='".$part['FAIpartitionSize']."' id='FAIpartitionSize_".$key."' size=12></td>";
230         $str .= "\n<td><input name='FAImountOptions_".$key."'  ".$dis."  value='".$part['FAImountOptions']."'  id='FAImountOptions_".$key."' style='width:100px;'></td>";
231         $str .= "\n<td><input name='FAIfsOptions_".$key."'     ".$dis."  value='".$part['FAIfsOptions']."'     id='FAIfsOptions_".$key."' style='width:100px;'></td>";
233         $changeState = "onClick=\"changeState('FAImountPoint_".$key."') ; ".
234                                  "changeState('FAIpartitionSize_".$key."') ; ".
235                                  "changeState('FAImountOptions_".$key."') ; ".
236                                  "changeState('FAIfsType_".$key."') ; ".
237                                  "changeState('FAIfsOptions_".$key."') ; \"";
238   
240         if($part['FAIpartitionFlags']!=false){
241           $str .= "\n<td><input type='checkbox' name='FAIpartitionFlags_".$key."' value='preserve' checked ".$changeState."></td>";
242         }else{
243           $str .= "\n<td><input type='checkbox' name='FAIpartitionFlags_".$key."' value='preserve' ".$changeState."></td>";
244         }
245         $str .= "\n<td><input type='submit' name='Delete_".$key."' value='"._("Remove")."'></td>";
246         $str .= "\n</tr>";    
247       }
248     }
249     $str.="</table>";
250     return($str);
252   }
254   function save()
255   {
256     $tmp = array();
257     $tmp['cn']          = $this->DISK_cn;
259     /* Attach partitions */
260     foreach($this->partitions as $key=>$val) {
261       $this->partitions[$key]['FAIpartitionNr']=$key;
262     }
264     /* Attach deleted */
265     foreach($this->deletePartitions as $key=>$val) {
266       $this->partitions[$key."-delete"]=$val;
267       $this->partitions[$key."-delete"]['status']="delete";
268     }
270     $tmp['description'] = $this->DISK_description;
271     $tmp['partitions']  = $this->partitions;
272     $tmp['status']      = $this->status;
274     /* If hdd name has changed, tell partitionTable to rename it */
275     if(($this->is_edit)&&($this->old_cn != $this->DISK_cn)){
276       $tmp['rename']['from']  = $this->old_cn;
277       $tmp['rename']['to']    = $this->DISK_cn;
278     }
279     return($tmp);
280   }
283   /* Save data to object */
284   function save_object()
285   {
286     if(isset($_POST['TableEntryFrameSubmitted'])){
287       plugin::save_object();
289       /* Check base attributes */
290       foreach($this->attributes as $attrs){
291         if(isset($_POST[$attrs])){
292           $this->$attrs = $_POST[$attrs];
293         }
294       }
295      
296       foreach($this->partitions as $key => $part){
297         foreach($this->UsedAttrs as $attrs){
298           if(isset($_POST[$attrs."_".$key])){
299             $this->partitions[$key][$attrs] = $_POST[$attrs."_".$key];
300           }else{
301             $this->partitions[$key][$attrs] = false;
302           }
303         }
304       }
305     }
306   }
309   /* Check supplied data */
310   function check()
311   {
312     $message= array();
313    
314     /* check every partition.
315      * if there is an invalid value defined, append an errorstr to message
316      */
318     /* Array that contain every partitionname mountpoint etc already assigned */
319     $alreadyUsed    = array();
320     foreach($this->UsedAttrs as $attrs){
321       $alreadyUsed[$attrs] = array();
322     }      
324     foreach($this->partitions as $key => $part){
325   
326       /* Skip all checks, if preserve is set */ 
327       if($part['FAIpartitionFlags'] == "preserve"){
328         $this->partitions[$key]['FAIfsType']        = "preserve";
329         $this->partitions[$key]['FAIpartitionSize'] = "preserve";
330         continue;
331       }
332  
333       if((in_array($part['FAImountPoint'],$alreadyUsed['FAImountPoint']))&&($part['FAIfsType']!="swap")){
334         $message[]=sprintf(_("please enter a unique mount point for partition %s"),($key));
335       }
337       if($part['FAIfsType']!="swap"){
338         if((empty($part['FAImountPoint']))||(!((preg_match("/^\/.*/",$part['FAImountPoint']))||(preg_match("/^swap$/",$part['FAImountPoint']))))){
339           $message[]=sprintf(_("Please enter a valid mount point for partition %s." ),($key));
340         }
341       }
342       if($part['FAIfsType'] == "swap"){
343         if(in_array($part['FAIfsType'],$alreadyUsed['FAIfsType'])){
344           $message[]=sprintf(_("File system type 'swap' is already used, change file system type for partition %s."),$key);
345         }
346       }
347       if(($part['FAIfsType'] == "swap")&&(!empty($part['FAImountPoint']))&&($part['FAImountPoint']!="swap")){
348         $message[]=_("Please use 'swap' as mount point, if 'swap' is used as fs-type.");
349       }
351       $tmp = split("-",$part['FAIpartitionSize']);
352       switch (count($tmp)){
353         case 0:
354                 $message[]= sprintf(_("Please enter a valid partition size for partition %s."),($key));
355                 break;
356         case 1:
357                 if (!is_id(is_id($tmp[0]))){
358                   $message[]= sprintf(_("Please enter a valid partition size for partition %s."),($key));
359                 }
360                 break;
361                 
362         case 2:
363                 if((!is_id($tmp[0]))||(!is_id($tmp[1]))){
364                   $message[]=sprintf(_("Please enter a valid range for partition %s."),($key)); 
365                 }elseif($tmp[0]>=$tmp[1]){
366                   $message[]=sprintf(_("Please enter a valid range for partition %s."),($key));
367                 }
368                 break;
370         default:
371                 $message[]=sprintf(_("Please enter a range for partition size for partition %s."),($key));
372       }
374       foreach($this->UsedAttrs as $attrs){
375         $alreadyUsed[$attrs][$key] = $part[$attrs];
376       }      
377     }
379     return ($message);
380   }
381  
384 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
385 ?>