Code

f5ca9f7b1ec3568fbdfc860c86543c8293db9232
[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
20   var $dialog           = NULL;     // a dialog, e.g. new disk dialog
22   var $FAIstate         = "";
23   var $base             = "";
24   var $release          = "";
25   var $copy_paste_mode  = false;
27   function faiPartitionTable ($config, $dn= NULL)
28   {
29     /* Load Attributes */
30     plugin::plugin ($config, $dn);
32     $this->acl ="#all#";
34     /* If "dn==new" we try to create a new entry
35      * Else we must read all objects from ldap which belong to this entry.
36      * First read disks from ldap ... and then the partition definitions for the disks.
37      */
38     if($dn != "new"){
39       $this->dn =$dn;
41       /* Set acls
42        */
43       $ui   = get_userinfo();
44       $acl  = get_permissions ($this->dn, $ui->subtreeACL);
45       $acli = get_module_permission($acl, "FAIclass", $this->dn);
46       $this->acl=$acli;
48       /* Get FAIstate
49        */
50       if(isset($this->attrs['FAIstate'][0])){
51         $this->FAIstate = $this->attrs['FAIstate'][0];
52       }
54       /* Read all disks from ldap taht are defined fot this partition table 
55        */
56       $ldap = $this->config->get_ldap_link();
57       $ldap->cd ($this->dn);
58       $ldap->search("(&(objectClass=FAIclass)(objectClass=FAIpartitionDisk))",array("*"));
59       while($object = $ldap->fetch()){
60         $this->disks[$object['cn'][0]]['status']      = "edited";
61         $this->disks[$object['cn'][0]]['dn']          = $object['dn'];
62         $this->disks[$object['cn'][0]]['cn']          = $object['cn'][0];
63         if(isset($object['description'][0])){
64           $this->disks[$object['cn'][0]]['description'] = $object['description'][0];
65         }else{
66           $this->disks[$object['cn'][0]]['description'] = "";
67         }
68         $this->disks[$object['cn'][0]]['partitions']   = array();
69       }
70   
71       /* read all partitions for each disk 
72        */
73       foreach($this->disks as $name => $disk){
74         $ldap->cd ($disk['dn']);
75         $ldap->search("(&(objectClass=FAIclass)(objectClass=FAIpartitionEntry))",array("*"));
76         while($partition = $ldap->fetch()){
78           /* remove count ... from ldap result 
79            */
80           foreach($partition as $key=>$val){
81             if((is_numeric($key))||($key=="count")||($key=="dn")){
82               unset($partition[$key]);
83             }else{
84               $partition[$key] = $val[0];
85             }
86           }
88           /* Append fetched partitions
89            */
90           $partition['status']="edited";
91           $this->disks[$name]['partitions'][$partition['FAIpartitionNr']] = $partition; 
92         }  
93       }
94     }
95     ksort($this->disks);
96   }
98   function execute()
99   {
100         /* Call parent execute */
101         plugin::execute();
103     /* Fill templating stuff */
104     $smarty= get_smarty();
105     $display= "";
106  
107     /* Add Disk to this Partitionset
108      * This code adds a new HDD to the disks 
109      * A new Dialog will be opened 
110      */
111     if(isset($_POST['AddDisk'])){
112       $usedDiskNames =array();
113       foreach($this->disks as $key=>$disk){
114         $usedDiskNames[]= $key;
115       }
116       $this->dialog = new faiPartitionTableEntry($this->config,$this->dn,$usedDiskNames); 
117       $this->dialog->acl = $this->acl;
118       $this->dialog->FAIstate = $this->FAIstate;
119       $this->is_dialog = true;
120     }
122     /* Edit disk.
123      * Open dialog which allows us to edit the selected entry 
124      */    
126     if($this->dn != "new"){
127       $_SESSION['objectinfo']= $this->dn;
128     }
130     if((isset($_POST['EditDisk']))&&(isset($_POST['disks']))){
131       $usedDiskNames =array();
133       $Udisk = $_POST['disks'][0];
134       
135       foreach($this->disks  as $key=>$disk){
136         if($key != $Udisk){
137           $usedDiskNames[]= $key;
138         }
139       }
140       $this->dialog = new faiPartitionTableEntry($this->config,$this->dn,$usedDiskNames,$this->disks[$Udisk]); 
141       $this->dialog->acl = $this->acl;
142       $this->dialog->FAIstate = $this->FAIstate;
143      
144       /* Set object info string, which will be displayed in plugin info line */ 
145       if(isset($this->disks[$Udisk]['dn'])){
146         $_SESSION['objectinfo'] = $this->disks[$Udisk]['dn'];
147       }else{
148         $_SESSION['objectinfo'] = "";
149       }
150     
151       $this->is_dialog = true;
152     }
154     /* Edit aborted, close dialog, without saving anything
155      */
156     if(isset($_POST['CancelDisk'])){
157       unset($this->dialog);
158       $this->dialog = NULL;
159       $this->is_dialog=false;
160     }
162     /* Dialog saved
163      * Save given data from Dialog, if no error is occured
164      */
165     if(isset($_POST['SaveDisk'])){
167       if($this->FAIstate != "freeze"){
168         $this->dialog->save_object();
169         if(count($this->dialog->check())){
170           foreach($this->dialog->check() as $msg){
171             print_red($msg);
172           }
173         }else{
174           $disk = $this->dialog->save();
175           if(isset($disk['rename'])){
176             if($this->disks[$disk['rename']['from']]['status']=="edited"){
177               $this->disks[$disk['rename']['from']]['status']="delete";
178             }else{
179               unset($this->disks[$disk['rename']['from']]);
180             }
182             foreach($disk['partitions'] as $key => $val){
183               if($disk['partitions'][$key]['status']!="delete"){
184                 $disk['partitions'][$key]['status']= "new";
185               }
186             }
188             $disk['status']="new";
189             $disk['cn']= $disk['rename']['to'];
190           }
192           $this->disks[$disk['cn']]=$disk; 
193           unset($this->dialog);
194           $this->dialog = NULL;
195           $this->is_dialog=false;
196           ksort($this->disks);
197         }
198       }else{
199         $this->dialog = NULL;
200         $this->is_dialog=false;
201       }
202     }
204     /* Delete selected disk drive from list
205      * Assign delete status for all its partitions      
206      */
207     if((isset($_POST['DelDisk']))&&(!empty($_POST['disks']))){
208       if($this->FAIstate != "freeze"){
209         foreach($_POST['disks'] as $disk) {
210           if($this->disks[$disk]['status']=="edited"){
211             $this->disks[$disk."-delete"]=$this->disks[$disk];
212             unset($this->disks[$disk]);
213             $disk = $disk."-delete";        
214             $this->disks[$disk]['status']="delete";
215             foreach($this->disks[$disk]['partitions'] as $name => $value ){
216               if($value['status']=="edited"){
217                 $this->disks[$disk]['partitions'][$name]['status']="delete"; 
218               }else{
219                 unset($this->disks[$disk]['partitions'][$name]);
220               }
221             }
222           }else{
223             unset($this->disks[$disk]);
224           }
225         }
226       }
227     }
229     /* Display dialog if one is defined
230      */
231     if(isset($this->dialog)){
232       $this->dialog->save_object();
233       return($this->dialog->execute());
234     }
236     /* Assign all attributes to smarty engine
237      */
238     foreach($this->attributes as $attrs){
239       $smarty->assign($attrs,$this->$attrs);
240       if($this->$attrs){
241         $smarty->assign($attrs."CHK"," ");
242       }else{
243         $smarty->assign($attrs."CHK"," disabled ");
244       }
245     }
246    
247     foreach($this->attributes as $attr){
248       $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
249     }
251     $disks = $this->getDisks();
252     $smarty->assign("disks"   ,$disks);
253     $display.= $smarty->fetch(get_template_path('faiPartitionTable.tpl', TRUE));
254     return($display);
255   }
257   function getDisks(){
258     /* Return all available disks for this partition table
259      * Return in listBox friendly array
260      */
261     $a_return = array();
262     foreach($this->disks as $key => $disk){
263       if($disk['status'] != "delete"){
264         $cnt=0;
265         foreach($disk['partitions'] as $val){
266           if($val['status']!="delete"){
267             $cnt ++;
268           }
269         }
270         if(!empty($disk['description'])){
271           if($cnt == 1){
272             $a_return[$key]=  $disk['cn']." [".$disk['description']."], ".sprintf(_("%s partition"), $cnt);
273           }else{
274             $a_return[$key]=  $disk['cn']." [".$disk['description']."], ".sprintf(_("%s partition(s)"), $cnt);
275           }
276         }else{
277           if($cnt == 1){
278             $a_return[$key]=  $disk['cn'].", ".sprintf(_("%s partition"), $cnt);
279           }else{
280             $a_return[$key]=  $disk['cn'].", ".sprintf(_("%s partition(s)"), $cnt);
281           }
282         }
283       }
284     }
285     return($a_return);
286   }
289   /* Delete me, and all my subtrees
290    */
291   function remove_from_parent()
292   {
293     $ldap = $this->config->get_ldap_link();
294     $ldap->cd ($this->dn);
295     $ldap->rmdir_recursive($this->dn);
296     $this->handle_post_events("remove");    
297   
298     /* This cannot be removed... */
299   }
302   /* Save data to object 
303    */
304   function save_object()
305   {
306     if($this->FAIstate == "freeze") return;
307     plugin::save_object();
308     foreach($this->attributes as $attrs){
309       if(isset($_POST[$attrs])){
310         $this->$attrs = $_POST[$attrs];
311       }
312     }
313   }
316   /* Check supplied data */
317   function check()
318   {
319     /* Call common method to give check the hook */
320     $message= plugin::check();
322     /* If this is a new script, check if a script with this name already exists */
323     if(!empty($this->release) && $this->copy_paste_mode){
325       /* Check if current name is already used for fai scripts in selected release */
326       $dn = 'cn='.$this->cn.",ou=disk,".$this->release;
327       $ldap = $this->config->get_ldap_link();
328       $ldap->cat($dn);
329       if($ldap->count()){
331         $r =convert_department_dn($this->release);;
332         $message[] = sprintf(_("Can't insert a new fai partition table named '%s' in '%s' there is already one defined with the given name."),$this->cn,$r);
333       }
334     }
335     return ($message);
336   }
339   /* Save to LDAP */
340   function save()
341   {
343     plugin::save();
344     /* Save current settings.
345      * 1 : We must save the partition table, with its description and cn 
346      * 2 : Append Disk with cn and  description.
347      * 3 : Save partitions for each disk
348      */  
350     $ldap = $this->config->get_ldap_link();
352     if($this->new){
353       $ldap->cd($this->config->current['BASE']);
354       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
355       $ldap->cd($this->dn);
356       $ldap->add($this->attrs);
357     }else{
358       /* Add partition table to ldap
359        */
360       $ldap->cd($this->dn);
361       $this->cleanup();
362       $ldap->modify ($this->attrs); 
363     }
364     show_ldap_error($ldap->get_error(), _("Saving FAI partition table base failed"));
365   
366     /* Do object tagging */
367     $this->handle_object_tagging();
368   
369     /* Sort entries, because we must delete entries with status="delete" first */
370     $order = array();
371     foreach($this->disks as $key => $disk){
372       if($disk['status'] == "delete"){
373         $order[$key] = $disk;
374       }
375     }
376     foreach($this->disks as $key => $disk){
377       if($disk['status'] != "delete"){
378         $order[$key] = $disk;
379       }
380     }
382     /* Append all disks to ldap */
383     foreach($order as $cn=>$disk){
384       $disk_dn                    = "cn=".$disk['cn'].",".$this->dn;
385       $disk_attrs['cn']           =  $disk['cn'];
386       $disk_attrs['description']  =  $disk['description']; 
387       $disk_attrs['objectClass']  =  array("top","FAIclass","FAIpartitionDisk");
389       if($disk['status']=="new"){
390         $ldap->cat($disk_dn,array("objectClass"));
391         if($ldap->count()){
392           $disk['status']="edited";
393         }
394       }
396       /* Fix problem with missing objectClass "gosaAdministrativeUnitTag" for disks */
397       $ldap->cat($disk_dn,array("objectClass"));
398       $attrs = $ldap->fetch();
399       if(isset($attrs['objectClass']) && in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
400         $disk_attrs['objectClass'][] = "gosaAdministrativeUnitTag";
401       }
402  
403       if($disk['status'] == "delete"){
404         $ldap->cd($disk_dn);
405         $ldap->rmdir_recursive($disk_dn);
406         show_ldap_error($ldap->get_error(), _("Removing FAI partition table failed"));
407       }elseif($disk['status']== "edited"){
408         if(empty($disk_attrs['description'])){
409           $disk_attrs['description']=array();
410         }
411         $ldap->cd($disk_dn);
412         $this->cleanup();
413         $ldap->modify ($disk_attrs); 
414         show_ldap_error($ldap->get_error(), _("Saving FAI partition table failed"));
415       }elseif($disk['status']== "new"){
416         if(empty($disk_attrs['description'])){
417           unset($disk_attrs['description']);
418         }
419         $ldap->cd($this->config->current['BASE']);
420         $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $disk_dn));
421         $ldap->cd($disk_dn);
422         $ldap->add($disk_attrs);
423         show_ldap_error($ldap->get_error(), _("Saving FAI partition table failed"));
424       }else{
425         print_red("unknown status while saving disks");
426       }
428       $this->handle_object_tagging($disk_dn, $this->gosaUnitTag);
430       /* Collect all opperations. Delete first than add new entries .*/
431       $Todo['delete'] = array();
432       $Todo['rest'] = array();
434       /* Skip partition handling if current disk is marked as deleted */
435       if($disk['status']!="delete"){
437         /* Add all partitions */
438         foreach($disk['partitions'] as $key => $partition){
440           /* Create attribute array */
441           $partition_attrs = array();
442           foreach($partition as $key => $value){
443             if(!empty($value)){
444               $partition_attrs[$key]=$value;        
445             }else{
446               unset($partition_attrs[$key]);        
447             }
448           }
450           /* Create partition dn and add necessary attributes */
451           $partition_dn= "FAIpartitionNr=".$partition_attrs['FAIpartitionNr'].",".$disk_dn;      
452           $partition_attrs['objectClass']= array("top","FAIclass","FAIpartitionEntry");
453           $partition_attrs['cn']= $partition_attrs['FAIpartitionNr'];
455           /* Unset status attributes */
456           unset($partition_attrs['status']);
457           unset($partition_attrs['old_cn']);
459           /* Check if this partition is realy new .. */
460           if($partition['status']=="new"){
461             $ldap->cat($partition_dn,array("objectClass"));
462             if($ldap->count()){
463               $partition['status']="edited";
464             }
465           }
467           /* Set mount point top swap if it is emtpy */
468           if((!isset($partition['FAImountPoint']))||(empty($partition['FAImountPoint']))){
469             $partition_attrs['FAImountPoint']="swap";
470           }
472           /* Collect all operations */
473           if(($partition['status'] == "delete")&&($disk['status']!="new")){
474             $Todo['delete'][$partition_dn] = $partition_attrs;
475           }elseif($partition['status'] == "new"){
476             $Todo['rest'][$partition_dn] = $partition_attrs;
477           }elseif($partition['status'] == "edited"){
478             $Todo['rest'][$partition_dn] = $partition_attrs;
479           } 
480         }
481       }
483       /* First of all, delete old entries */
484       foreach($Todo['delete'] as $partition_dn => $attrs){
485           $ldap->cd($partition_dn);
486           $ldap->rmdir_recursive($partition_dn);
487           show_ldap_error($ldap->get_error(), _("Removing FAI partition table entry failed"));
488       }
489     
490       /* Add/edit entries */
491       foreach($Todo['rest'] as $partition_dn => $partition_attrs){
493         /* Check if entry exists */
494         $ldap->cat($partition_dn);
495         if($ldap->count()){
497           /* Fix problem with missing objectClass "gosaAdministrativeUnitTag" for partitions */
498           $attrs = $ldap->fetch();
499           if(isset($attrs['objectClass']) && in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
500             $partition_attrs['objectClass'][] = "gosaAdministrativeUnitTag";
501           }
502           if(empty($partition_attrs['description'])){
503             $partition_attrs['description']=array();
504           }
505           $ldap->cd($partition_dn);
506           $this->cleanup();
507           $ldap->modify ($partition_attrs); 
508           show_ldap_error($ldap->get_error(), _("Saving FAI partition table entry failed"));
509         }else{
511           if(empty($partition_attrs['description'])){
512             unset($partition_attrs['description']);
513           }
515           $ldap->cd($this->config->current['BASE']);
516           $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $partition_dn));
517           $ldap->cd($partition_dn);
518           $ldap->add($partition_attrs);
519           show_ldap_error($ldap->get_error(), _("Saving FAI partition table entry failed"));
520         }
521       }
522       $this->handle_object_tagging($partition_dn, $this->gosaUnitTag);
523     }
524     $this->handle_post_events("add");
525   }
528 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
529 ?>