Code

Added \$this->cleanup before all calls of $ldap->modify
[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   function faiPartitionTable ($config, $dn= NULL)
23   {
24     /* Load Attributes */
25     plugin::plugin ($config, $dn);
27     /* If "dn==new" we try to create a new entry
28      * Else we must read all objects from ldap which belong to this entry.
29      * First read disks from ldap ... and then the partition definitions for the disks.
30      */
31     if($dn != "new"){
32       $this->dn =$dn;
34       /* Read all disks from ldap taht are defined fot this partition table 
35        */
36       $ldap = $this->config->get_ldap_link();
37       $ldap->cd ($this->dn);
38       $ldap->search("(&(objectClass=FAIclass)(objectClass=FAIpartitionDisk))",array("*"));
39       while($object = $ldap->fetch()){
40         $this->disks[$object['cn'][0]]['status']      = "edited";
41         $this->disks[$object['cn'][0]]['dn']          = $object['dn'];
42         $this->disks[$object['cn'][0]]['cn']          = $object['cn'][0];
43         if(isset($object['description'][0])){
44           $this->disks[$object['cn'][0]]['description'] = $object['description'][0];
45         }else{
46           $this->disks[$object['cn'][0]]['description'] = "";
47         }
48         $this->disks[$object['cn'][0]]['partitions']   = array();
49       }
50   
51       /* read all partitions for each disk 
52        */
53       foreach($this->disks as $name => $disk){
54         $ldap->cd ($disk['dn']);
55         $ldap->search("(&(objectClass=FAIclass)(objectClass=FAIpartitionEntry))",array("*"));
56         while($partition = $ldap->fetch()){
58           /* remove count ... from ldap result 
59            */
60           foreach($partition as $key=>$val){
61             if((is_numeric($key))||($key=="count")||($key=="dn")){
62               unset($partition[$key]);
63             }else{
64               $partition[$key] = $val[0];
65             }
66           }
68           /* Append fetched partitions
69            */
70           $partition['status']="edited";
71           $this->disks[$name]['partitions'][$partition['FAIpartitionNr']] = $partition; 
72         }  
73       }
74     }
75     ksort($this->disks);
76   }
78   function execute()
79   {
80         /* Call parent execute */
81         plugin::execute();
83     /* Fill templating stuff */
84     $smarty= get_smarty();
85     $display= "";
86  
87     /* Add Disk to this Partitionset
88      * This code adds a new HDD to the disks 
89      * A new Dialog will be opened 
90      */
91     if(isset($_POST['AddDisk'])){
92       $usedDiskNames =array();
93       foreach($this->disks as $key=>$disk){
94         $usedDiskNames[]= $key;
95       }
96       $this->dialog = new faiPartitionTableEntry($this->config,$this->dn,$usedDiskNames); 
97       $this->is_dialog = true;
98     }
100     /* Edit disk.
101      * Open dialog which allows us to edit the selected entry 
102      */    
104     if($this->dn != "new"){
105       $_SESSION['objectinfo']= $this->dn;
106     }
108     if((isset($_POST['EditDisk']))&&(isset($_POST['disks']))){
109       $usedDiskNames =array();
111       $Udisk = $_POST['disks'][0];
112       
113       foreach($this->disks  as $key=>$disk){
114         if($key != $Udisk){
115           $usedDiskNames[]= $key;
116         }
117       }
118       $this->dialog = new faiPartitionTableEntry($this->config,$this->dn,$usedDiskNames,$this->disks[$Udisk]); 
119       $_SESSION['objectinfo'] = $this->disks[$Udisk]['dn'];
120       $this->is_dialog = true;
121     }
123     /* Edit aborted, close dialog, without saving anything
124      */
125     if(isset($_POST['CancelDisk'])){
126       unset($this->dialog);
127       $this->dialog = NULL;
128       $this->is_dialog=false;
129     }
130   
131     /* Dialog saved
132      * Save given data from Dialog, if no error is occured
133      */
134     if(isset($_POST['SaveDisk'])){
135       $this->dialog->save_object();
136       if(count($this->dialog->check())){
137         foreach($this->dialog->check() as $msg){
138           print_red($msg);
139         }
140       }else{
141         $disk = $this->dialog->save();
142         if(isset($disk['rename'])){
143           if($this->disks[$disk['rename']['from']]['status']=="edited"){
144             $this->disks[$disk['rename']['from']]['status']="delete";
145           }else{
146             unset($this->disks[$disk['rename']['from']]);
147           }
148   
149           foreach($disk['partitions'] as $key => $val){
150             if($disk['partitions'][$key]['status']!="delete"){
151               $disk['partitions'][$key]['status']= "new";
152             }
153           }
155           $disk['status']="new";
156           $disk['cn']= $disk['rename']['to'];
157         }
158       
159         $this->disks[$disk['cn']]=$disk; 
160         unset($this->dialog);
161         $this->dialog = NULL;
162         $this->is_dialog=false;
163         ksort($this->disks);
164       }
165     }
167     /* Delete selected disk drive from list
168      * Assign delete status for all its partitions      
169      */
170     if((isset($_POST['DelDisk']))&&(!empty($_POST['disks']))){
172       foreach($_POST['disks'] as $disk) {
173         if($this->disks[$disk]['status']=="edited"){
174           $this->disks[$disk."-delete"]=$this->disks[$disk];
175           unset($this->disks[$disk]);
176           $disk = $disk."-delete";        
177           $this->disks[$disk]['status']="delete";
178           foreach($this->disks[$disk]['partitions'] as $name => $value ){
179             if($value['status']=="edited"){
180               $this->disks[$disk]['partitions'][$name]['status']="delete"; 
181             }else{
182               unset($this->disks[$disk]['partitions'][$name]);
183             }
184           }
185         }else{
186           unset($this->disks[$disk]);
187         }
188       }
189     }
191     /* Display dialog if one is defined
192      */
193     if(isset($this->dialog)){
194       $this->dialog->save_object();
195       return($this->dialog->execute());
196     }
198     /* Assign all attributes to smarty engine
199      */
200     foreach($this->attributes as $attrs){
201       $smarty->assign($attrs,$this->$attrs);
202       if($this->$attrs){
203         $smarty->assign($attrs."CHK"," ");
204       }else{
205         $smarty->assign($attrs."CHK"," disabled ");
206       }
207     }
208     
209     $disks = $this->getDisks();
210     $smarty->assign("disks"   ,$disks);
211     $smarty->assign("diskKeys",array_flip($disks));
212     $display.= $smarty->fetch(get_template_path('faiPartitionTable.tpl', TRUE));
213     return($display);
214   }
216   function getDisks(){
217     /* Return all available disks for this partition table
218      * Return in listBox friendly array
219      */
220     $a_return = array();
221     foreach($this->disks as $key => $disk){
222       if($disk['status'] != "delete"){
223         $cnt=0;
224         foreach($disk['partitions'] as $val){
225           if($val['status']!="delete"){
226             $cnt ++;
227           }
228         }
229         if(!empty($disk['description'])){
230           if($cnt == 1){
231             $a_return[$key]=  $disk['cn']." [".$disk['description']."], ".sprintf(_("%s partition"), $cnt);
232           }else{
233             $a_return[$key]=  $disk['cn']." [".$disk['description']."], ".sprintf(_("%s partition(s)"), $cnt);
234           }
235         }else{
236           if($cnt == 1){
237             $a_return[$key]=  $disk['cn'].", ".sprintf(_("%s partition"), $cnt);
238           }else{
239             $a_return[$key]=  $disk['cn'].", ".sprintf(_("%s partition(s)"), $cnt);
240           }
241         }
242       }
243     }
244     return($a_return);
245   }
248   /* Delete me, and all my subtrees
249    */
250   function remove_from_parent()
251   {
252     $ldap = $this->config->get_ldap_link();
253     $ldap->cd ($this->dn);
254     $ldap->rmdir_recursive($this->dn);
255     $this->handle_post_events("remove");    
256   
257     /* This cannot be removed... */
258   }
261   /* Save data to object 
262    */
263   function save_object()
264   {
265     plugin::save_object();
266     foreach($this->attributes as $attrs){
267       if(isset($_POST[$attrs])){
268         $this->$attrs = $_POST[$attrs];
269       }
270     }
271   }
274   /* Check supplied data */
275   function check()
276   {
278     $message= array();
279     return ($message);
280   }
283   /* Save to LDAP */
284   function save()
285   {
287     plugin::save();
288     /* Save current settings.
289      * 1 : We must save the partition table, with its description and cn 
290      * 2 : Append Disk with cn and  description.
291      * 3 : Save partitions for each disk
292      */  
294     $ldap = $this->config->get_ldap_link();
296     if($this->new){
297       $ldap->cd($this->config->current['BASE']);
298       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
299       $ldap->cd($this->dn);
300       $ldap->add($this->attrs);
301       show_ldap_error($ldap->get_error());
302     }else{
303       /* Add partition table to ldap
304        */
305       $ldap->cd($this->dn);
306       $this->cleanup();
307 $ldap->modify ($this->attrs); 
309       show_ldap_error($ldap->get_error());
310     }
311   
312   
313     /* Sort entries, because we must delete entries with status="delete" first */
314     $order = array();
315     foreach($this->disks as $key => $disk){
316       if($disk['status'] == "delete"){
317         $order[$key] = $disk;
318       }
319     }
320     foreach($this->disks as $key => $disk){
321       if($disk['status'] != "delete"){
322         $order[$key] = $disk;
323       }
324     }
326     /* Append all disks to ldap */
327     foreach($order as $cn=>$disk){
328       $disk_dn                    = "cn=".$disk['cn'].",".$this->dn;
329       $disk_attrs['cn']           =  $disk['cn'];
330       $disk_attrs['description']  =  $disk['description']; 
331       $disk_attrs['objectClass']  =  array("top","FAIclass","FAIpartitionDisk");
333       if($disk['status']=="new"){
334         $ldap->cat($disk_dn);
335         if($ldap->count()){
336           $disk['status']="edited";
337         }
338       }
339  
340       if($disk['status'] == "delete"){
341         $ldap->cd($disk_dn);
342         $ldap->rmdir_recursive($disk_dn);
343       }elseif($disk['status']== "edited"){
344         if(empty($disk_attrs['description'])){
345           $disk_attrs['description']=array();
346         }
347         $ldap->cd($disk_dn);
348         $this->cleanup();
349 $ldap->modify ($disk_attrs); 
351       }elseif($disk['status']== "new"){
352         if(empty($disk_attrs['description'])){
353           unset($disk_attrs['description']);
354         }
355         $ldap->cd($this->config->current['BASE']);
356         $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $disk_dn));
357         $ldap->cd($disk_dn);
358         $ldap->add($disk_attrs);
359       }else{
360         print_red("unknown status while saving disks");
361       }
363       show_ldap_error($ldap->get_error());
364       if($disk['status']!="delete")
365       /* Add all partitions */
366       foreach($disk['partitions'] as $key => $partition){
367         $partition_attrs = array();
369         foreach($partition as $key => $value){
370           if(!empty($value)){
371             $partition_attrs[$key]=$value;        
372           }else{
373             unset($partition_attrs[$key]);        
374           }
375         }
377         $partition_dn= "FAIpartitionNr=".$partition_attrs['FAIpartitionNr'].",".$disk_dn;      
378         $partition_attrs['objectClass']= array("top","FAIclass","FAIpartitionEntry");
379         $partition_attrs['cn']= $partition_attrs['FAIpartitionNr'];
380         
381         unset($partition_attrs['status']);
382         unset($partition_attrs['old_cn']);
384         if($partition['status']=="new"){
385           $ldap->cat($partition_dn);
386           if($ldap->count()){
387             $partition['status']="edited";
388           }
389         }
391         if((!isset($partition['FAImountPoint']))||(empty($partition['FAImountPoint']))){
392           $partition_attrs['FAImountPoint']="swap";
393         }
395         if(($partition['status'] == "delete")&&($disk['status']!="new")){
396           $ldap->cd($partition_dn);
397           $ldap->rmdir_recursive($partition_dn);
398         }elseif($partition['status'] == "new"){
399           if(empty($partition_attrs['description'])){
400             unset($partition_attrs['description']);
401           }
402           $ldap->cd($this->config->current['BASE']);
403           $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $partition_dn));
404           $ldap->cd($partition_dn);
405           $ldap->add($partition_attrs);
406         }elseif($partition['status'] == "edited"){
407           if(empty($partition_attrs['description'])){
408             $partition_attrs['description']=array();
409           }
410           $ldap->cd($partition_dn);
411           $this->cleanup();
412 $ldap->modify ($partition_attrs); 
414         } 
415       show_ldap_error($ldap->get_error());
416       }
417     }
418     $this->handle_post_events("add");
419   }
422 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
423 ?>