Code

get rid of the div stuff
[gosa.git] / gosa-plugins / fai / admin / fai / class_faiPartitionTableEntry.inc
1 <?php
3 class faiPartitionTableEntry extends plugin
4 {
5   /* attribute list for save action */
6   var $ignore_account= TRUE;
7   var $attributes   = array("DISKcn","DISKdescription");
8   var $objectclasses= array();
10   var $DISKcn              = "";
11   var $DISKdescription     = "";
12   
13   var $partitions           = array();
14   var $UsedAttrs            = array();
15   var $is_edit              = false;
16   var $old_cn               = "";
17   var $parent               = null;
19   function faiPartitionTableEntry (&$config, $dn= NULL,&$parent,$disk=false)
20   {
21     plugin::plugin ($config, $dn);
22     $this->UsedAttrs  = array("cn","description","FAIpartitionType","FAIpartitionNr","FAIfsType","FAImountPoint","FAIpartitionSize",
23                          "FAImountOptions","FAIfsOptions","FAIpartitionFlags");
25     $this->parent = $parent;
27     /* We want to edit an entry */
28     if($disk){
30       /* Walk through partitions */
31       foreach($disk['partitions'] as $name => $values){
33         $disk['partitions'][$name]['old_cn']= $disk['partitions'][$name]['cn'];
35         /* Assign empty attributes, if attribute is missing */
36         foreach($this->UsedAttrs as $attr){
37           if(!isset($values[$attr])){
38             $disk['partitions'][$name][$attr]="";  
39           }
40         }
42         if (preg_match('/^_/', $disk['partitions'][$name]['FAIfsType'])){
43           $disk['partitions'][$name]['FAIfsType']= preg_replace('/^_/', '', $disk['partitions'][$name]['FAIfsType']);
44         }
45       }
47       /* Set default attributes */
48       $this->DISKcn          = $disk['cn'];
49       $this->DISKdescription = $disk['description'];
50       $this->partitions       = $disk['partitions'];
51       $this->is_edit          = true;
52       $this->old_cn           = $disk['cn'];
53     }
54   }
57   function execute()
58   {
59     /* Call parent execute */
60     plugin::execute();
62     /* Fill templating stuff */
63     $smarty     = get_smarty();
64     $s_action   = "";
65     $s_entry    = "";
66     $display    = "";
67     
68     /* Assign values 
69      * And Checkbox selection
70      */
71     foreach($this->attributes as $attrs){
72       $smarty->assign($attrs,$this->$attrs);
73       if($this->$attrs){
74         $smarty->assign($attrs."CHK"," ");
75       }else{
76         $smarty->assign($attrs."CHK"," disabled ");
77       }
78     }
80     /* Check all Posts if there is something usefull for us,
81      * For example : Delete is posted as Delete_1 
82      * The number specifies the index we want to delete
83      */
84     foreach($_POST as $name => $value){
85       if((preg_match("/Delete_.*/",$name)) && $this->acl_is_removeable() && !preg_match("/freeze/i",$this->FAIstate)){
86         $tmp = explode("_",$name);
87         $s_action = "remove";
88         $s_entry  = $tmp[1]; 
89       }
90     }
92     /* To remove a partition we unset the index posted.
93      * We must sort the index again, else we possibly got problems 
94      * with partitions order.
95      */
96     if($s_action == "remove" && $this->acl_is_removeable() && !preg_match("/freeze/i",$this->FAIstate)){
97       unset($this->partitions[$s_entry]);
98       $tmp= array();
99       foreach($this->partitions as $part){
100         $tmp[count($tmp)+1]=$part;
101       }
102       $this->partitions = $tmp;
103     }
105     /* To add a partitions we only append an empty 
106      *  array to the already defined partitions.
107      */
108     if(isset($_POST['AddPartition']) && $this->acl_is_createable() && !preg_match("/freeze/i",$this->FAIstate)){
109       foreach($this->UsedAttrs as $attr){
110         $tmp[$attr]                = "";     
111       }
112       $tmp["old_cn"]               = "";     
113       $this->partitions[count($this->partitions)+1]=$tmp;
114     }
116     /* $setup contains a table with the partitions. 
117      */
118     $smarty->assign("setup", $this->generateParts());
119     foreach($this->attributes as $attr){
120       $smarty->assign($attr,$this->$attr);
121     }
123     $tmp = $this->plInfo();
124     $sacl = "";
125     foreach($tmp['plProvidedAcls'] as $name => $translated){
126       $acl = $this->getacl($name, preg_match("/freeze/i",$this->FAIstate));
127       $smarty->assign($name."ACL",$acl);
128     }
129    
130     $smarty->assign("sub_object_is_createable",$this->acl_is_createable());
131     $smarty->assign("freeze",preg_match("/freeze/i",$this->FAIstate));
132  
133     /* Fetch template and show the result
134      */
135     $display.= $smarty->fetch(get_template_path('faiPartitionTableEntry.tpl', TRUE));
136     return($display);
137   }
139   function generateParts()
140   {
141     /* Define Arrays with allowed syntax */
142     $PartitionTypes   = array("primary"=>_("primary"),"logical"=>_("logical"));
143     $FAIfsTypes       = array("ext2","ext3","xfs","swap","reiser","dosfat16","winfat32");
145     /* Display Header */
146     $str = "";
147     if (count($this->partitions)){
148       $str .= "<tr>";
149       $str .= "<td><b>"._("Type")."</b></td>";
150       $str .= "<td><b>"._("FS type")."</b></td>";
151       $str .= "<td><b>"._("Mount point")."</b></td>";
152       $str .= "<td><b>"._("Size in MB")."</b></td>";
153       $str .= "<td><b>"._("Mount options")."</b></td>";
154       $str .= "<td><b>"._("FS option")."</b></td>";
155       $str .= "<td><b>"._("Preserve")."</b></td>";
156       $str .= "<td>&nbsp;</td>";
157       $str .= "</tr>";
158     }
159     
160     /* Walk through all defined partitions.
161      * Create a new row for each partition and append it to 
162      *  the header defined above.
163      * To be able to check the posts later, we append a key to each single postfield. like cn_1 ... cn_2
164      */
165     foreach($this->partitions as $key => $part){
167       $dis = "";
168       if($part['FAIpartitionFlags'] == "preserve"){
169         $dis = " disabled ";
170       }
172       $disableALL = "";
173       if (preg_match("/freeze/", $this->FAIstate)){
174         $disableALL = " disabled ";
175       }
177       /* Generate Partition select box  
178        */
179       $PartitionType = "";
180       if($this->acl_is_writeable("FAIpartitionType")){
181         $PartitionType = "<select name='FAIpartitionType_".$key."' id='FAIpartitionType_".$key."' ".$disableALL.">";
182         foreach($PartitionTypes as $type => $PTname){
183           if($part['FAIpartitionType'] == $type){
184             $PartitionType .="<option value='".$type."' selected >".$PTname."</option>";
185           }else{
186             $PartitionType .="<option value='".$type."'>".$PTname."</option>";
187           }
188         }        
189         $PartitionType.="</select>";   
190       }elseif($this->acl_is_readable("FAIpartitionType")){
191         $PartitionType = $part['FAIpartitionType'];
192       }
195       /* Generate fsType select box  
196        */
197       $FAIfsType= "";
198       if($this->acl_is_writeable("FAIfsType")){
199         $FAIfsType= "<select size='1' name='FAIfsType_".$key."' id='FAIfsType_".$key."' ".$dis." ".$disableALL.">";
200         foreach($FAIfsTypes as $type){
201           if($part['FAIfsType'] == $type){
202             $FAIfsType  .="<option value='".$type."' selected >".$type."</option>";
203           }else{
204             $FAIfsType .="<option value='".$type."'>".$type."</option>";
205           }
206         }        
207         $FAIfsType.="</select>";   
208       }elseif($this->acl_is_readable("FAIfsType")){
209         $FAIfsType = $part['FAIfsType'];
210       }
212       foreach(array("FAImountPoint","FAIpartitionSize","FAImountOptions","FAIfsOptions") as $attr){
213         $$attr  = "";
214         if($this->acl_is_writeable($attr)){
215           $$attr = "<input type='text' name='".$attr."_".$key."'  ".$disableALL."  ".$dis."
216             value='".$part[$attr]."'    id='".$attr."_".$key."'>";
218         }elseif($this->acl_is_readable($attr)){
219           $$attr = $part[$attr];
220         }
221       }
224       $str .= "\n<tr>";
225       $str .= "\n<td>".$PartitionType."</td>";
226       $str .= "\n<td>".$FAIfsType."</td>";
227       $str .= "\n<td>".$FAImountPoint."</td>";
228       $str .= "\n<td>".$FAIpartitionSize."</td>";
229       $str .= "\n<td>".$FAImountOptions."</td>";
230       $str .= "\n<td>".$FAIfsOptions."</td>";
232       $changeState = "onClick=\"changeState('FAImountPoint_".$key."') ; ".
233         "changeState('FAIpartitionSize_".$key."') ; ".
234         "changeState('FAImountOptions_".$key."') ; ".
235         "changeState('FAIfsType_".$key."') ; ".
236         "changeState('FAIfsOptions_".$key."') ; \"";
238       if($this->acl_is_writeable("FAIpartitionFlags")){
239         if($part['FAIpartitionFlags']!=false){
240           $str .= "\n<td><input type='checkbox' name='FAIpartitionFlags_".$key."' value='preserve' checked ".$changeState." ".$disableALL."></td>";
241         }else{
242           $str .= "\n<td><input type='checkbox' name='FAIpartitionFlags_".$key."' value='preserve' ".$changeState." ".$disableALL."></td>";
243         }
244       }else{
245         if($part['FAIpartitionFlags']!=false){
246           $str .= "<td>"._("False")."</td>";
247         }else{
248           $str .= "<td>"._("True")."</td>";
249         }
250       }
251       if($this->acl_is_removeable()){
252         $str .= "\n<td><button type='submit' {$disableALL} name='Delete_".$key."'>"._("Remove")."</button></td>";    
253       }else{
254         $str .= "\n<td><button type='button' disabled name='restricted'>"._("Remove")."</button></td>";    
255       }
256       $str .= "\n</tr>";    
257     }
259     if(!empty($str)){
260       $str = "<table summary='' style='width:100%'>".$str."</table>";
261     }
262     return($str);
264   }
266   function save()
267   {
268     $tmp = array();
269     $tmp['cn']          = $this->DISKcn;
271     /* Attach partitions */
272     foreach($this->partitions as $key=>$val) {
273       $this->partitions[$key]['FAIpartitionNr']=$key;
274       $this->partitions[$key]['cn']=$key;
275     }
277     $tmp['description'] = $this->DISKdescription;
278     $tmp['partitions']  = $this->partitions;
280     /* If hdd name has changed, tell partitionTable to rename it */
281     if(($this->is_edit)&&($this->old_cn != $this->DISKcn)){
282       $tmp['rename']['from']  = $this->old_cn;
283       $tmp['rename']['to']    = $this->DISKcn;
284     }
285     $tmp['FAIdiskType'] = "old";
286     return($tmp);
287   }
290   /* Save data to object */
291   function save_object()
292   {
293     if((isset($_POST['TableEntryFrameSubmitted'])) && !preg_match("/freeze/", $this->FAIstate) ){
294       plugin::save_object();
296       foreach($this->partitions as $key => $part){
297         foreach($this->UsedAttrs as $attrs){
298           if(in_array($attrs,array('cn','description'))) continue;
299           
300           if($this->acl_is_writeable($attrs)){
302             if(isset($_POST[$attrs."_".$key])){
303               $this->partitions[$key][$attrs] = $_POST[$attrs."_".$key];
304             }else{
305               $this->partitions[$key][$attrs] = false;
306             }
307           }
308         }
310         /* Adapt ext3 to have -j option */
311         if ($this->partitions[$key]["FAIfsType"] == "ext3") {
312           $this->partitions[$key]["FAIfsOptions"]= preg_replace('/\s*-j\s*/', '', $this->partitions[$key]["FAIfsOptions"]);
313           $this->partitions[$key]["FAIfsOptions"]= "-j ".$this->partitions[$key]["FAIfsOptions"];
314         }
315       }
317     }
318   }
321   /* Check supplied data */
322   function check()
323   {
324     /* Call common method to give check the hook */
325     $message= plugin::check();
326   
327     /* Check for an empty disk name */
328     $d = trim($this->DISKcn);
329     if($d == "" ){
330       $message[] = msgPool::required(_("Name"));
331     }
332     if(preg_match("/[^a-z0-9_\-]/i",$d)){
333       $message[] = msgPool::invalid(_("Name"),$d,"/[a-z0-9_\-]/i");
334     }
336     if($this->old_cn != $this->DISKcn && in_array($this->DISKcn,$this->parent->getUsedDiskNames())){
337       $message[] = msgPool::duplicated(_("Name"));
338     }
339        
340     /* check every partition.
341      * if there is an invalid value defined, append an errorstr to message
342      */
344     /* Array that contain every partitionname mountpoint etc already assigned */
345     $alreadyUsed    = array();
346     foreach($this->UsedAttrs as $attrs){
347       $alreadyUsed[$attrs] = array();
348     }      
350     foreach($this->partitions as $key => $part){
351   
352       /* Skip all checks, if preserve is set */ 
353       if($part['FAIpartitionFlags'] == "preserve"){
354         $this->partitions[$key]['FAIfsType']        = "preserve";
355         $this->partitions[$key]['FAIpartitionSize'] = "preserve";
356         continue;
357       }
358  
359       if((in_array($part['FAImountPoint'],$alreadyUsed['FAImountPoint']))&&($part['FAIfsType']!="swap")){
360         $message[]=sprintf(_("please enter a unique mount point for partition %s"),($key));
361       }
363       if($part['FAIfsType']!="swap" && $part['FAImountPoint'] != "-"){
364         if((empty($part['FAImountPoint']))||(!((preg_match("/^\/.*/",$part['FAImountPoint']))||(preg_match("/^swap$/",$part['FAImountPoint']))))){
365           $message[]= msgPool::invalid(sprintf(_("partition %s mount point"),$key));
366         }
367       }
368       if($part['FAIfsType'] == "swap"){
369         if(in_array($part['FAIfsType'],$alreadyUsed['FAIfsType'])){
370           $message[]=sprintf(_("File system type 'swap' is already used, change file system type for partition %s."),$key);
371         }
372       }
373       if(($part['FAIfsType'] == "swap")&&(!empty($part['FAImountPoint']))&&($part['FAImountPoint']!="swap")){
374         $message[]=_("Please use 'swap' as mount point, if 'swap' is used as fs-type.");
375       }
377       $tmp = explode("-",$part['FAIpartitionSize']);
379       switch (count($tmp)){
380         case 0:
381                 $message[]= msgPool::invalid(sprintf(_("partition %s size"),$key));
382                 break;
383         case 1:
384                 if (!tests::is_id($tmp[0]) || empty($tmp[0])){
385                   $message[]=  msgPool::invalid(sprintf(_("partition %s size"),$key));
386                 }
387                 break;
388                 
389         case 2:
390                 if( !tests::is_id($tmp[0]) && !tests::is_id($tmp[1]) && !empty($tmp[1]) ){
391                   $message[]= msgPool::invalid(sprintf(_("partition %s size"),$key));
392                 }elseif(!empty($tmp[1]) && $tmp[0]>=$tmp[1]){
393                   $message[]= msgPool::invalid(sprintf(_("partition %s size"),$key));
394                 }
395                 break;
397         default:
398                 $message[]= msgPool::invalid(sprintf(_("partition %s size"),$key));
399       }
401       foreach($this->UsedAttrs as $attrs){
402         $alreadyUsed[$attrs][$key] = $part[$attrs];
403       }      
404     }
406     $cnt = 0;
407     foreach($this->partitions as $key => $part){
408       if($part['FAIpartitionType'] == "primary"){
409         $cnt ++ ; 
410       }
411     }
412     if($cnt > 3){
413       $message[] = _("You have more than 3 primary partition table entries in your configuration, please check your configuration twice.");
414     }
416     return ($message);
417   }
419   
421   /* Return plugin informations for acl handling */
422   static function plInfo()
423   {
424     return (array(
425           "plShortName" => _("Partition table entry"),
426           "plDescription" => _("FAI partition table entry"),
427           "plSelfModify"  => FALSE,
428           "plDepends"     => array(),
429           "plPriority"    => 27,
430           "plSection"     => array("administration"),
431           "plCategory"    => array("fai"),
432           "plProvidedAcls" => array(
433             "DISKcn"           => _("Name"),
434             "DISKdescription"  => _("Description"),
435             "FAIpartitionType"  => _("Partition type"),
436             "FAIpartitionNr"    => _("Partition no."),
437             "FAIfsType"         => _("File system type"),
438             "FAImountPoint"     => _("Mount point"),
439             "FAIpartitionSize"  => _("Partition size"),
440             "FAImountOptions"   => _("Mount options"),
441             "FAIfsOptions"      => _("File system options"),
442             "FAIpartitionFlags" => _("Partition flags"))
443           ));
444   }
446  
449 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
450 ?>