Code

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