Code

Fixed a couple of static/non-static error messages
[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("DISKcn","DISKdescription");
13   var $objectclasses= array();
15   var $DISKcn              = "";
16   var $DISKdescription     = "";
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   var $FAIstate             = "";
27   function faiPartitionTableEntry (&$config, $dn= NULL,$usedDiskNames=array(),$disk=false)
28   {
29     plugin::plugin ($config, $dn);
30     $this->UsedAttrs  = array("cn","description","FAIpartitionType","FAIpartitionNr","FAIfsType","FAImountPoint","FAIpartitionSize",
31                          "FAImountOptions","FAIfsOptions","FAIpartitionFlags");
33     /* Default status is new */
34     $this->status = "new";    
35  
36     /* We want to edit an entry */
37     if($disk){
39       /* Set disk status */      
40       $this->status = $disk['status'];
42       /* Walk through partitions */
43       foreach($disk['partitions'] as $name => $values){
45         /* If a partition is already marked as delete, attach it to deletePartitions only. */
46         if($values['status'] == "delete"){
47           unset($disk['partitions'][$name]);
48           $this->deletePartitions[]=$values;
49         }else{
51           /* Set status, to know which partition must be deleted from ldap
52              new    : Neu partition entry // save
53              edited : Update partition entry in ldap
54              deleted: Remove partition from ldap
55            */
56     
57           /* If status is not new, set to edit mode. 
58            * New means that this partition currently wasn't saved to ldap.
59            */
60           if($disk['partitions'][$name]['status']!="new"){
61             $disk['partitions'][$name]['status']="edited";
62           }
63      
64           $disk['partitions'][$name]['old_cn']= $disk['partitions'][$name]['cn'];
65  
66           /* Assign empty attributes, if attribute is missing */
67           foreach($this->UsedAttrs as $attr){
68             if(!isset($values[$attr])){
69               $disk['partitions'][$name][$attr]="";  
70             }
71           }
72         }
73       }
75       /* Set default attributes */
76       $this->DISKcn          = $disk['cn'];
77       $this->DISKdescription = $disk['description'];
78       $this->partitions       = $disk['partitions'];
79       $this->is_edit          = true;
80       $this->old_cn           = $disk['cn'];
81     }
82   }
85   function execute()
86   {
87     /* Call parent execute */
88     plugin::execute();
90     /* Fill templating stuff */
91     $smarty     = get_smarty();
92     $s_action   = "";
93     $s_entry    = "";
94     $display    = "";
95     
96     /* Assign values 
97      * And Checkbox selection
98      */
99     foreach($this->attributes as $attrs){
100       $smarty->assign($attrs,$this->$attrs);
101       if($this->$attrs){
102         $smarty->assign($attrs."CHK"," ");
103       }else{
104         $smarty->assign($attrs."CHK"," disabled ");
105       }
106     }
108     /* Check all Posts if there is something usefull for us,
109      * For example : Delete is posted as Delete_1 
110      * The number specifies the index we want to delete
111      */
112     foreach($_POST as $name => $value){
113       if((preg_match("/Delete_.*/",$name)) && $this->acl_is_removeable()){
114         $tmp = split("_",$name);
115         $s_action = "remove";
116         $s_entry  = $tmp[1]; 
117       }
118     }
120     /* To remove a partition we unset the index posted.
121      * We must sort the index again, else we possibly got problems 
122      * with partitions order.
123      */
124     if($s_action == "remove" && $this->acl_is_removeable()){
125       if($this->partitions[$s_entry]['status'] == "edited"){
126         $this->deletePartitions[$s_entry]= $this->partitions[$s_entry];
127         $this->deletePartitions[$s_entry]['FAIpartitionNr']=$s_entry;
128         unset($this->partitions[$s_entry]);
129       }else{
130         unset($this->partitions[$s_entry]);
131       }
132       $tmp= array();
133       foreach($this->partitions as $part){
134         $tmp[count($tmp)+1]=$part;
135       }
136       $this->partitions = $tmp;
137     }
139     /* To add a partitions we only append an empty 
140      *  array to the already defined partitions.
141      */
142     if(isset($_POST['AddPartition']) && $this->acl_is_createable()){
143       foreach($this->UsedAttrs as $attr){
144         $tmp[$attr]                = "";     
145       }
146       $tmp["old_cn"]               = "";     
147       $tmp['status']="new";
148       $this->partitions[count($this->partitions)+1]=$tmp;
149     }
151     /* $setup contains a table with the partitions. 
152      */
153     $smarty->assign("setup", $this->generateParts());
154     foreach($this->attributes as $attr){
155       $smarty->assign($attr,$this->$attr);
156     }
158     $tmp = $this->plInfo();
159     $sacl = "";
160     foreach($tmp['plProvidedAcls'] as $name => $translated){
162       $acl = $this->getacl($name);
163       $sacl .= $acl;
164       if($this->FAIstate == "freezed"){
165         $acl = preg_replace("/w/","",$acl);
166       }
167       $smarty->assign($name."ACL",$acl);
168     }
169     $smarty->assign("allowSave",$sacl);
170    
171     $smarty->assign("sub_object_is_createable",$this->acl_is_createable());
172  
173     /* Fetch template and show the result
174      */
175     $display.= $smarty->fetch(get_template_path('faiPartitionTableEntry.tpl', TRUE));
176     return($display);
177   }
179   function generateParts()
180   {
181     /* Define Arrays with allowed syntax */
182     $PartitionTypes   = array("primary"=>_("primary"),"logical"=>_("logical"));
183     $FAIfsTypes       = array("ext2","ext3","xfs","swap","reiser","dosfat16","winfat32");
185     /* Display Header */
186     $str = "";
187     if (count($this->partitions)){
188       $str .= "<tr>";
189       $str .= "<td><b>"._("Type")."</b></td>";
190       $str .= "<td><b>"._("FS type")."</b></td>";
191       $str .= "<td><b>"._("Mount point")."</b></td>";
192       $str .= "<td><b>"._("Size in MB")."</b></td>";
193       $str .= "<td><b>"._("Mount options")."</b></td>";
194       $str .= "<td><b>"._("FS option")."</b></td>";
195       $str .= "<td><b>"._("Preserve")."</b></td>";
196       $str .= "<td>&nbsp;</td>";
197       $str .= "</tr>";
198     }
199     
200     /* Walk through all defined partitions.
201      * Create a new row for each partition and append it to 
202      *  the header defined above.
203      * To be able to check the posts later, we append a key to each single postfield. like cn_1 ... cn_2
204      */
205     foreach($this->partitions as $key => $part){
206       
207       $dis = "";
208       if($part['FAIpartitionFlags'] == "preserve"){
209         $dis = " disabled ";
210       }
212       $disableALL = "";
213       if($this->FAIstate =="freeze"){
214         $disableALL = " disabled ";
215       }
217       if($part['status']!="delete"){
219         /* Generate Partition select box  
220          */
221         $PartitionType = "";
222         if($this->acl_is_writeable("FAIpartitionType")){
223           $PartitionType = "<select name='FAIpartitionType_".$key."' id='FAIpartitionType_".$key."' ".$disableALL.">";
224           foreach($PartitionTypes as $type => $PTname){
225             if($part['FAIpartitionType'] == $type){
226               $PartitionType .="<option value='".$type."' selected >".$PTname."</option>";
227             }else{
228               $PartitionType .="<option value='".$type."'>".$PTname."</option>";
229             }
230           }        
231           $PartitionType.="</select>";   
232         }elseif($this->acl_is_readable("FAIpartitionType")){
233           $PartitionType = $part['FAIpartitionType'];
234         }
237         /* Generate fsType select box  
238          */
239         $FAIfsType= "";
240         if($this->acl_is_writeable("FAIfsType")){
241           $FAIfsType= "<select name='FAIfsType_".$key."' id='FAIfsType_".$key."' ".$dis." ".$disableALL.">";
242           foreach($FAIfsTypes as $type){
243             if($part['FAIfsType'] == $type){
244               $FAIfsType  .="<option value='".$type."' selected >".$type."</option>";
245             }else{
246               $FAIfsType .="<option value='".$type."'>".$type."</option>";
247             }
248           }        
249           $FAIfsType.="</select>";   
250         }elseif($this->acl_is_readable("FAIfsType")){
251           $FAIfsType = $part['FAIfsType'];
252         }
254         foreach(array("FAImountPoint","FAIpartitionSize","FAImountOptions","FAIfsOptions") as $attr){
255           $$attr  = "";
256           if($this->acl_is_writeable($attr)){
257             $$attr = "<input name='".$attr."_".$key."'  ".$disableALL."  ".$dis."
258                         value='".$part[$attr]."'    id='".$attr."_".$key."'>";
260           }elseif($this->acl_is_readable($attr)){
261             $$attr = $part[$attr];
262           }
263         }
266         $str .= "\n<tr>";
267         $str .= "\n<td>".$PartitionType."</td>";
268         $str .= "\n<td>".$FAIfsType."</td>";
269         $str .= "\n<td>".$FAImountPoint."</td>";
270         $str .= "\n<td>".$FAIpartitionSize."</td>";
271         $str .= "\n<td>".$FAImountOptions."</td>";
272         $str .= "\n<td>".$FAIfsOptions."</td>";
274         $changeState = "onClick=\"changeState('FAImountPoint_".$key."') ; ".
275                                  "changeState('FAIpartitionSize_".$key."') ; ".
276                                  "changeState('FAImountOptions_".$key."') ; ".
277                                  "changeState('FAIfsType_".$key."') ; ".
278                                  "changeState('FAIfsOptions_".$key."') ; \"";
279   
280         if($this->acl_is_writeable("FAIpartitionFlags")){
281           if($part['FAIpartitionFlags']!=false){
282             $str .= "\n<td><input type='checkbox' name='FAIpartitionFlags_".$key."' value='preserve' checked ".$changeState." ".$disableALL."></td>";
283           }else{
284             $str .= "\n<td><input type='checkbox' name='FAIpartitionFlags_".$key."' value='preserve' ".$changeState." ".$disableALL."></td>";
285           }
286         }else{
287           if($part['FAIpartitionFlags']!=false){
288             $str .= "<td>"._("False")."</td>";
289           }else{
290             $str .= "<td>"._("True")."</td>";
291           }
292         }
293         if($this->acl_is_removeable()){
294           $str .= "\n<td><input type='submit' name='Delete_".$key."' value='"._("Remove")."' ".$disableALL."></td>";    
295         }else{
296           $str .= "\n<td><input type='submit' name='restricted' disabled value='"._("Remove")."'></td>";    
297         }
298         $str .= "\n</tr>";    
299       }
300     }
301     
302     if(!empty($str)){
303       $str = "<table summary='' style='width:100%'>".$str."</table>";
304     }
305     return($str);
307   }
309   function save()
310   {
311     $tmp = array();
312     $tmp['cn']          = $this->DISKcn;
314     /* Attach partitions */
315     foreach($this->partitions as $key=>$val) {
316       $this->partitions[$key]['FAIpartitionNr']=$key;
317     }
319     /* Attach deleted */
320     foreach($this->deletePartitions as $key=>$val) {
321       $this->partitions[$key."-delete"]=$val;
322       $this->partitions[$key."-delete"]['status']="delete";
323     }
325     $tmp['description'] = $this->DISKdescription;
326     $tmp['partitions']  = $this->partitions;
327     $tmp['status']      = $this->status;
329     /* If hdd name has changed, tell partitionTable to rename it */
330     if(($this->is_edit)&&($this->old_cn != $this->DISKcn)){
331       $tmp['rename']['from']  = $this->old_cn;
332       $tmp['rename']['to']    = $this->DISKcn;
333     }
334     return($tmp);
335   }
338   /* Save data to object */
339   function save_object()
340   {
343     if((isset($_POST['TableEntryFrameSubmitted'])) && ($this->FAIstate != "freeze") ){
344       plugin::save_object();
346       foreach($this->partitions as $key => $part){
347         foreach($this->UsedAttrs as $attrs){
349           if($this->acl_is_writeable($attrs)){
351             if(isset($_POST[$attrs."_".$key])){
352               $this->partitions[$key][$attrs] = $_POST[$attrs."_".$key];
353             }else{
354               $this->partitions[$key][$attrs] = false;
355             }
356           }
357         }
358       }
359     }
360   }
363   /* Check supplied data */
364   function check()
365   {
366     /* Call common method to give check the hook */
367     $message= plugin::check();
368   
369     /* Check for an empty disk name */
370     if(empty($this->DISKcn)){
371       $message[] = _("Please specify a valid disc name.");
372     }
373        
374     /* check every partition.
375      * if there is an invalid value defined, append an errorstr to message
376      */
378     /* Array that contain every partitionname mountpoint etc already assigned */
379     $alreadyUsed    = array();
380     foreach($this->UsedAttrs as $attrs){
381       $alreadyUsed[$attrs] = array();
382     }      
384     foreach($this->partitions as $key => $part){
385   
386       /* Skip all checks, if preserve is set */ 
387       if($part['FAIpartitionFlags'] == "preserve"){
388         $this->partitions[$key]['FAIfsType']        = "preserve";
389         $this->partitions[$key]['FAIpartitionSize'] = "preserve";
390         continue;
391       }
392  
393       if((in_array($part['FAImountPoint'],$alreadyUsed['FAImountPoint']))&&($part['FAIfsType']!="swap")){
394         $message[]=sprintf(_("please enter a unique mount point for partition %s"),($key));
395       }
397       if($part['FAIfsType']!="swap" && $part['FAImountPoint'] != "-"){
398         if((empty($part['FAImountPoint']))||(!((preg_match("/^\/.*/",$part['FAImountPoint']))||(preg_match("/^swap$/",$part['FAImountPoint']))))){
399           $message[]=sprintf(_("Please enter a valid mount point for partition %s." ),($key));
400         }
401       }
402       if($part['FAIfsType'] == "swap"){
403         if(in_array($part['FAIfsType'],$alreadyUsed['FAIfsType'])){
404           $message[]=sprintf(_("File system type 'swap' is already used, change file system type for partition %s."),$key);
405         }
406       }
407       if(($part['FAIfsType'] == "swap")&&(!empty($part['FAImountPoint']))&&($part['FAImountPoint']!="swap")){
408         $message[]=_("Please use 'swap' as mount point, if 'swap' is used as fs-type.");
409       }
411       $tmp = split("-",$part['FAIpartitionSize']);
412       switch (count($tmp)){
413         case 0:
414                 $message[]= sprintf(_("Please enter a valid partition size for partition %s."),($key));
415                 break;
416         case 1:
417                 if (!is_id(is_id($tmp[0])) &&(!empty($tmp[1]))){
418                   $message[]= sprintf(_("Please enter a valid partition size for partition %s."),($key));
419                 }
420                 break;
421                 
422         case 2:
423                 if((!is_id($tmp[0]))&&(!is_id($tmp[1])) &&(!empty($tmp[1]))){
424                   $message[]=sprintf(_("Please enter a valid range for partition %s."),($key)); 
425                 }elseif($tmp[0]>=$tmp[1]){
426                   $message[]=sprintf(_("Please enter a valid range for partition %s."),($key));
427                 }
428                 break;
430         default:
431                 $message[]=sprintf(_("Please enter a range for partition size for partition %s."),($key));
432       }
434       foreach($this->UsedAttrs as $attrs){
435         $alreadyUsed[$attrs][$key] = $part[$attrs];
436       }      
437     }
439     $cnt = 0;
440     foreach($this->partitions as $key => $part){
441       if($part['FAIpartitionType'] == "primary"){
442         $cnt ++ ; 
443       }
444     }
445     if($cnt > 3){
446       $message[] = _("You have more than 3 primary partition table entries in your configuration, please check your configuration twice.");
447     }
449     return ($message);
450   }
452   
454   /* Return plugin informations for acl handling */
455   static function plInfo()
456   {
457     return (array(
458           "plShortName" => _("Partition table entry"),
459           "plDescription" => _("FAI partition table entry"),
460           "plSelfModify"  => FALSE,
461           "plDepends"     => array(),
462           "plPriority"    => 27,
463           "plSection"     => array("administration"),
464           "plCategory"    => array("fai"),
465           "plProvidedAcls" => array(
466             "DISKcn"           => _("Name"),
467             "DISKdescription"  => _("Description"),
468             "FAIpartitionType"  => _("Partition type"),
469             "FAIpartitionNr"    => _("Partition no."),
470             "FAIfsType"         => _("File system type"),
471             "FAImountPoint"     => _("Mount point"),
472             "FAIpartitionSize"  => _("Partition size"),
473             "FAImountOptions"   => _("Mount options"),
474             "FAIfsOptions"      => _("File system options"),
475             "FAIpartitionFlags" => _("Partition flags"))
476           ));
477   }
479  
482 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
483 ?>