Code

Removed base64_encoding, from all classes
[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."' ".$dis.">";
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('FAIpartitionType_".$key."') ; ".
237                                  "changeState('FAIfsType_".$key."') ; ".
238                                  "changeState('FAIfsOptions_".$key."') ; \"";
239   
241         if($part['FAIpartitionFlags']!=false){
242           $str .= "\n<td><input type='checkbox' name='FAIpartitionFlags_".$key."' value='preserve' checked ".$changeState."></td>";
243         }else{
244           $str .= "\n<td><input type='checkbox' name='FAIpartitionFlags_".$key."' value='preserve' ".$changeState."></td>";
245         }
246         $str .= "\n<td><input type='submit' name='Delete_".$key."' value='"._("Remove")."'></td>";
247         $str .= "\n</tr>";    
248       }
249     }
250     $str.="</table>";
251     return($str);
253   }
255   function save()
256   {
257     $tmp = array();
258     $tmp['cn']          = $this->DISK_cn;
260     /* Attach partitions */
261     foreach($this->partitions as $key=>$val) {
262       $this->partitions[$key]['FAIpartitionNr']=$key;
263     }
265     /* Attach deleted */
266     foreach($this->deletePartitions as $key=>$val) {
267       $this->partitions[$key."-delete"]=$val;
268       $this->partitions[$key."-delete"]['status']="delete";
269     }
271     $tmp['description'] = $this->DISK_description;
272     $tmp['partitions']  = $this->partitions;
273     $tmp['status']      = $this->status;
275     /* If hdd name has changed, tell partitionTable to rename it */
276     if(($this->is_edit)&&($this->old_cn != $this->DISK_cn)){
277       $tmp['rename']['from']  = $this->old_cn;
278       $tmp['rename']['to']    = $this->DISK_cn;
279     }
281     return($tmp);
282   }
285   /* Save data to object */
286   function save_object()
287   {
288     if(isset($_POST['TableEntryFrameSubmitted'])){
289       plugin::save_object();
291       /* Check base attributes */
292       foreach($this->attributes as $attrs){
293         if(isset($_POST[$attrs])){
294           $this->$attrs = $_POST[$attrs];
295         }
296       }
297      
298       foreach($this->partitions as $key => $part){
299         foreach($this->UsedAttrs as $attrs){
300           if(isset($_POST[$attrs."_".$key])){
301             $this->partitions[$key][$attrs] = $_POST[$attrs."_".$key];
302           }else{
303             $this->partitions[$key][$attrs] = false;
304           }
305         }
306       }
307     }
308   }
311   /* Check supplied data */
312   function check()
313   {
314     $message= array();
315    
316     /* check every partition.
317      * if there is an invalid value defined, append an errorstr to message
318      */
320     /* Array that contain every partitionname mountpoint etc already assigned */
321     $alreadyUsed    = array();
322     foreach($this->UsedAttrs as $attrs){
323       $alreadyUsed[$attrs] = array();
324     }      
326     foreach($this->partitions as $key => $part){
327   
328       /* Skip all checks, if preserve is set */ 
329       if($part['FAIpartitionFlags'] == "preserve"){
330         $this->partitions[$key]['FAIpartitionType'] = "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     return ($message);
383   }
384  
387 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
388 ?>