Code

Fixed a couple of static/non-static error messages
[gosa.git] / plugins / admin / fai / class_faiPartitionTable.inc
1 <?php
3 class faiPartitionTable 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("cn","description");
13   var $objectclasses    = array("top","FAIclass","FAIpartitionTable");
15   /* Specific attributes */
16   var $cn               = "";       // The class name for this object
17   var $description      = "";       // The description for this set of partitions
18   var $disks            = array();  // All defined Disks 
19   var $is_dialog        = false;    // specifies which buttons will be shown to save or abort
21   var $FAIstate         = "";
22   var $ui;
24   var $view_logged      = FALSE;
26   function faiPartitionTable (&$config, $dn= NULL)
27   {
28     /* Load Attributes */
29     plugin::plugin ($config, $dn);
31     $this->acl ="#all#";
33     $this->ui = get_userinfo();    
35     /* If "dn==new" we try to create a new entry
36      * Else we must read all objects from ldap which belong to this entry.
37      * First read disks from ldap ... and then the partition definitions for the disks.
38      */
39     if($dn != "new"){
40       $this->dn =$dn;
42       /* Get FAIstate
43        */
44       if(isset($this->attrs['FAIstate'][0])){
45         $this->FAIstate = $this->attrs['FAIstate'][0];
46       }
48       /* Read all disks from ldap taht are defined fot this partition table 
49        */
50       $ldap = $this->config->get_ldap_link();
51       $ldap->cd ($this->dn);
52       $ldap->search("(&(objectClass=FAIclass)(objectClass=FAIpartitionDisk))",array("*"));
53       while($object = $ldap->fetch()){
55         /* Skip objects, that are tagged as removed */
56         if(isset($object['FAIstate'][0])){
57           if(preg_match("/removed$/",$object['FAIstate'][0])){
58             continue;
59           }
60         }
62         $this->disks[$object['cn'][0]]['status']      = "edited";
63         $this->disks[$object['cn'][0]]['dn']          = $object['dn'];
64         $this->disks[$object['cn'][0]]['cn']          = $object['cn'][0];
65         if(isset($object['description'][0])){
66           $this->disks[$object['cn'][0]]['description'] = $object['description'][0];
67         }else{
68           $this->disks[$object['cn'][0]]['description'] = "";
69         }
70         $this->disks[$object['cn'][0]]['partitions']   = array();
71       }
72   
73       /* read all partitions for each disk 
74        */
75       foreach($this->disks as $name => $disk){
76         $ldap->cd ($disk['dn']);
77         $ldap->search("(&(objectClass=FAIclass)(objectClass=FAIpartitionEntry))",array("*"));
78         while($partition = $ldap->fetch()){
80           /* Skip objects, that are tagged as removed */
81           if(isset($partition['FAIstate'][0])){
82             if(preg_match("/removed$/",$partition['FAIstate'][0])){
83               continue;
84             }
85           }
87           /* remove count ... from ldap result 
88            */
89           foreach($partition as $key=>$val){
90             if((is_numeric($key))||($key=="count")||($key=="dn")){
91               unset($partition[$key]);
92             }else{
93               $partition[$key] = $val[0];
94             }
95           }
97           /* Append fetched partitions
98            */
99           $partition['status']="edited";
100           $this->disks[$name]['partitions'][$partition['FAIpartitionNr']] = $partition; 
101         }  
102       }
103     }
104     ksort($this->disks);
105   }
108   function acl_base_for_current_object($dn)
109   {
110     if($dn == "new"){
111       if($this->dn == "new"){
112         $dn= $_SESSION['CurrentMainBase'];
113       }else{
114         $dn = $this->dn;
115       }
116     }
117     return($dn);
118   }
121   function execute()
122   {
123     /* Call parent execute */
124     plugin::execute();
126     if($this->is_account && !$this->view_logged){
127       $this->view_logged = TRUE;
128       new log("view","fai/".get_class($this),$this->dn);
129     }
131     /* Fill templating stuff */
132     $smarty= get_smarty();
133     $display= "";
134  
135     /* Add Disk to this Partitionset
136      * This code adds a new HDD to the disks 
137      * A new Dialog will be opened 
138      */
139     if(isset($_POST['AddDisk'])){
140       $usedDiskNames =array();
141       foreach($this->disks as $key=>$disk){
142         $usedDiskNames[]= $key;
143       }
144       $this->dialog = new faiPartitionTableEntry($this->config,$this->dn,$usedDiskNames); 
145       $this->dialog->set_acl_base($this->acl_base_for_current_object($this->dn));
146       $this->dialog->set_acl_category("fai");
147       $this->dialog->FAIstate = $this->FAIstate;
148       $this->is_dialog = true;
149     }
151     /* Edit disk.
152      * Open dialog which allows us to edit the selected entry 
153      */    
155     if($this->dn != "new"){
156       $_SESSION['objectinfo']= $this->dn;
157     }
159     if((isset($_POST['EditDisk']))&&(isset($_POST['disks']))){
160       $usedDiskNames =array();
161       $Udisk = $_POST['disks'][0];
162       if(isset($this->disks[$Udisk])){
164         foreach($this->disks  as $key=>$disk){
165           if($key != $Udisk){
166             $usedDiskNames[]= $key;
167           }
168         }
170         /* Set object info string, which will be displayed in plugin info line */ 
171         if(isset($this->disks[$Udisk]['dn'])){
172           $_SESSION['objectinfo'] = $this->disks[$Udisk]['dn'];
173           $dn = $this->disks[$Udisk]['dn'];
174         }else{
175           $_SESSION['objectinfo'] = "";
176           $dn = "new";
177         }
179         $this->dialog = new faiPartitionTableEntry($this->config,$this->dn,$usedDiskNames,$this->disks[$Udisk]); 
180         $this->dialog->set_acl_base($this->acl_base_for_current_object($dn));
181         $this->dialog->set_acl_category("fai");
182         $this->dialog->FAIstate = $this->FAIstate;
184         $this->is_dialog = true;
185       }
186     }
188     /* Edit aborted, close dialog, without saving anything
189      */
190     if(isset($_POST['CancelDisk'])){
191       unset($this->dialog);
192       $this->dialog = FALSE;
193       $this->is_dialog=false;
194     }
196     /* Dialog saved
197      * Save given data from Dialog, if no error is occurred
198      */
199     if(isset($_POST['SaveDisk'])){
201       if($this->FAIstate != "freeze"){
202         $this->dialog->save_object();
203         if(count($this->dialog->check())){
204           foreach($this->dialog->check() as $msg){
205             print_red($msg);
206           }
207         }else{
208           $disk = $this->dialog->save();
209           if(isset($disk['rename'])){
210             if($this->disks[$disk['rename']['from']]['status']=="edited"){
211               $this->disks[$disk['rename']['from']]['status']="delete";
212             }else{
213               unset($this->disks[$disk['rename']['from']]);
214             }
216             foreach($disk['partitions'] as $key => $val){
217               if($disk['partitions'][$key]['status']!="delete"){
218                 $disk['partitions'][$key]['status']= "new";
219               }
220             }
222             $disk['status']="new";
223             $disk['cn']= $disk['rename']['to'];
224           }
226           $this->disks[$disk['cn']]=$disk; 
227           unset($this->dialog);
228           $this->dialog = FALSE;
229           $this->is_dialog=false;
230           ksort($this->disks);
231         }
232       }else{
233         $this->dialog = FALSE;
234         $this->is_dialog=false;
235       }
236     }
238     /* Delete selected disk drive from list
239      * Assign delete status for all its partitions      
240      */
241     if((isset($_POST['DelDisk']))&&(!empty($_POST['disks']))){
242       if($this->FAIstate != "freeze"){
243         foreach($_POST['disks'] as $disk) {
245           if(isset($this->disks[$disk])){
246             if($this->disks[$disk]['status']=="edited"){
247               $this->disks[$disk."-delete"]=$this->disks[$disk];
248               unset($this->disks[$disk]);
249               $disk = $disk."-delete";        
250               $this->disks[$disk]['status']="delete";
251               foreach($this->disks[$disk]['partitions'] as $name => $value ){
252                 if($value['status']=="edited"){
253                   $this->disks[$disk]['partitions'][$name]['status']="delete"; 
254                 }else{
255                   unset($this->disks[$disk]['partitions'][$name]);
256                 }
257               }
258             }else{
259               unset($this->disks[$disk]);
260             }
261           }
262         }
263       }
264     }
266     /* Display dialog if one is defined
267      */
268     if(is_object($this->dialog)){
269       $this->dialog->save_object();
270       return($this->dialog->execute());
271     }
273     /* Assign all attributes to smarty engine
274      */
275     foreach($this->attributes as $attrs){
276       $smarty->assign($attrs,$this->$attrs);
277       if($this->$attrs){
278         $smarty->assign($attrs."CHK"," ");
279       }else{
280         $smarty->assign($attrs."CHK"," disabled ");
281       }
282     }
283    
284     $dn = $this->acl_base_for_current_object($this->dn);
285     $smarty->assign("sub_object_is_addable",
286         preg_match("/c/",$this->ui->get_permissions($dn,"fai/faiPartitionTableEntry")) &&
287         !preg_match("/freeze/",$this->FAIstate));
289     $tmp = $this->plInfo();
290     foreach($tmp['plProvidedAcls'] as $name => $translated){
291       $smarty->assign($name."ACL",$this->getacl($name));
292     }
293     $disks = $this->getDisks();
294     $smarty->assign("disks"   ,$disks);
295     $display.= $smarty->fetch(get_template_path('faiPartitionTable.tpl', TRUE));
296     return($display);
297   }
299   function getDisks(){
300     /* Return all available disks for this partition table
301      * Return in listBox friendly array
302      */
303     $a_return = array();
304     foreach($this->disks as $key => $disk){
306       $dn = "new";
307       if(isset($obj['dn'])){
308         $dn = $obj['dn'];
309       }
310       $dn = $this->acl_base_for_current_object($dn);
311       $acl = $this->ui->get_permissions($dn,"fai/faiPartitionTableEntry");
312       if(preg_match("/(r|w)/",$acl)) {
314         if($disk['status'] != "delete"){
315           $cnt=0;
316           foreach($disk['partitions'] as $val){
317             if($val['status']!="delete"){
318               $cnt ++;
319             }
320           }
321           if(!empty($disk['description'])){
322             if($cnt == 1){
323               $a_return[$key]=  $disk['cn']." [".$disk['description']."], ".sprintf(_("%s partition"), $cnt);
324             }else{
325               $a_return[$key]=  $disk['cn']." [".$disk['description']."], ".sprintf(_("%s partition(s)"), $cnt);
326             }
327           }else{
328             if($cnt == 1){
329               $a_return[$key]=  $disk['cn'].", ".sprintf(_("%s partition"), $cnt);
330             }else{
331               $a_return[$key]=  $disk['cn'].", ".sprintf(_("%s partition(s)"), $cnt);
332             }
333           }
334         }
335       }
336     }
337     return($a_return);
338   }
341   /* Delete me, and all my subtrees
342    */
343   function remove_from_parent()
344   {
345     $ldap = $this->config->get_ldap_link();
346     $ldap->cd ($this->dn);
348 #    $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $this->dn);
349     $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $this->dn);
350     if($_SESSION['faifilter']['branch'] == "main"){
351       $use_dn = $this->dn;
352     }
354     prepare_to_save_FAI_object($use_dn,array(),true);
356     new log("remove","fai/".get_class($this),$use_dn,$this->attributes);   
357  
358     foreach($this->disks as $disk){
360       $disk_dn = "cn=".$disk['cn'].",".$this->dn;
361 #      $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $disk_dn);
362       $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $disk_dn);
363       if($_SESSION['faifilter']['branch'] == "main"){
364         $use_dn = $disk_dn;
365       }
366       prepare_to_save_FAI_object($use_dn,array(),true);
368       foreach($disk['partitions'] as $key => $partition){    
369      
370         $partition_dn= "FAIpartitionNr=".$partition['FAIpartitionNr'].",".$disk_dn;      
371 #        $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $partition_dn);
372         $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $partition_dn);
373         if($_SESSION['faifilter']['branch'] == "main"){
374           $use_dn = $disk_dn;
375         }
376         prepare_to_save_FAI_object($use_dn,array(),true);
377       }
378     }
379   }
382   /* Save data to object 
383    */
384   function save_object()
385   {
386     if($this->FAIstate == "freeze") return;
387     plugin::save_object();
388     foreach($this->attributes as $attrs){
389       if(isset($_POST[$attrs])){
390         $this->$attrs = $_POST[$attrs];
391       }
392     }
393   }
396   /* Check supplied data */
397   function check()
398   {
399     /* Call common method to give check the hook */
400     $message= plugin::check();
402     return ($message);
403   }
406   /* Save to LDAP */
407   function save()
408   {
410     plugin::save();
411     /* Save current settings.
412      * 1 : We must save the partition table, with its description and cn 
413      * 2 : Append Disk with cn and  description.
414      * 3 : Save partitions for each disk
415      */  
417     $ldap = $this->config->get_ldap_link();
419     prepare_to_save_FAI_object($this->dn,$this->attrs);
420     show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/partition table with dn '%s' failed."),$this->dn));
422     if($this->initially_was_account){
423       new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
424     }else{
425       new log("create","fai/".get_class($this),$this->dn,$this->attributes);
426     }
427  
428     /* Do object tagging */
429     $this->handle_object_tagging();
430   
431     /* Sort entries, because we must delete entries with status="delete" first */
432     $order = array();
433     foreach($this->disks as $key => $disk){
434       if($disk['status'] == "delete"){
435         $order[$key] = $disk;
436       }
437     }
438     foreach($this->disks as $key => $disk){
439       if($disk['status'] != "delete"){
440         $order[$key] = $disk;
441       }
442     }
444     /* Append all disks to ldap */
445     foreach($order as $cn=>$disk){
446       $disk_dn                    = "cn=".$disk['cn'].",".$this->dn;
447       $disk_attrs['cn']           =  $disk['cn'];
448       $disk_attrs['description']  =  $disk['description']; 
449       $disk_attrs['objectClass']  =  array("top","FAIclass","FAIpartitionDisk");
451       if($disk['status']=="new"){
452         $ldap->cat($disk_dn,array("objectClass"));
453         if($ldap->count()){
454           $disk['status']="edited";
455         }
456       }
458       /* Fix problem with missing objectClass "gosaAdministrativeUnitTag" for disks */
459       $ldap->cat($disk_dn,array("objectClass"));
460       $attrs = $ldap->fetch();
461       if(isset($attrs['objectClass']) && in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
462         $disk_attrs['objectClass'][] = "gosaAdministrativeUnitTag";
463       }
465       if($disk['status'] == "delete"){
466         prepare_to_save_FAI_object($disk_dn,array(),true);
467         $this->handle_post_events("remove");
468       }elseif($disk['status'] == "edited"){
469         prepare_to_save_FAI_object($disk_dn,$disk_attrs);
470         $this->handle_post_events("modify");
471       }elseif($disk['status']=="new"){
472         prepare_to_save_FAI_object($disk_dn,$disk_attrs);
473         $this->handle_post_events("add");
474       }
476       $this->handle_object_tagging($disk_dn, $this->gosaUnitTag);
478       if($disk['status']!="delete")
479       /* Add all partitions */
480       foreach($disk['partitions'] as $key => $partition){
481         $partition_attrs = array();
483         foreach($partition as $key => $value){
484           if(!empty($value)){
485             $partition_attrs[$key]=$value;        
486           }else{
487             unset($partition_attrs[$key]);        
488           }
489         }
491         $partition_dn= "FAIpartitionNr=".$partition_attrs['FAIpartitionNr'].",".$disk_dn;      
492         $partition_attrs['objectClass']= array("top","FAIclass","FAIpartitionEntry");
493         $partition_attrs['cn']= $partition_attrs['FAIpartitionNr'];
494         
495         unset($partition_attrs['status']);
496         unset($partition_attrs['old_cn']);
498         if($partition['status']=="new"){
499           $ldap->cat($partition_dn,array("objectClass"));
500           if($ldap->count()){
501             $partition['status']="edited";
502           }
503         }
505         if((!isset($partition['FAImountPoint']))||(empty($partition['FAImountPoint']))){
506           $partition_attrs['FAImountPoint']="swap";
507         }
509         /* Fix problem with missing objectClass "gosaAdministrativeUnitTag" for partitions */
510         $ldap->cat($partition_dn,array("objectClass"));
511         $attrs = $ldap->fetch();
512         if(isset($attrs['objectClass']) && in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
513           $partition_attrs['objectClass'][] = "gosaAdministrativeUnitTag";
514         }
516         if($partition['status'] == "delete"){
517           prepare_to_save_FAI_object($partition_dn,array(),true);
518           $this->handle_post_events("remove");
519         }elseif($partition['status'] == "edited"){
520           prepare_to_save_FAI_object($partition_dn,$partition_attrs);
521           $this->handle_post_events("modify");
522         }elseif($partition['status']=="new"){
523           prepare_to_save_FAI_object($partition_dn,$partition_attrs);
524           $this->handle_post_events("add");
525         }
527         $this->handle_object_tagging($partition_dn, $this->gosaUnitTag);
528       }
529     }
530     $this->handle_post_events("add");
531   }
534   /* Return plugin informations for acl handling */ 
535   static function plInfo()
536   {
537     return (array( 
538           "plShortName" => _("Partition table"),
539           "plDescription" => _("FAI partition table"),
540           "plSelfModify"  => FALSE,
541           "plDepends"     => array(),
542           "plPriority"    => 26,
543           "plSection"     => array("administration"),
544           "plCategory"    => array("fai"),
545           "plProvidedAcls" => array(
546             "cn"                => _("Name")."&nbsp;("._("Read only").")",
547             "description"       => _("Description"))
548           ));
549   }
552 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
553 ?>