Code

Fixed some typos
[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();
110       foreach($this->disks  as $key=>$disk){
111         if($key != $_POST['disks']){
112           $usedDiskNames[]= $key;
113         }
114       }
115       $this->dialog = new faiPartitionTableEntry($this->config,$this->dn,$usedDiskNames,$this->disks[$_POST['disks']]); 
116       $_SESSION['objectinfo'] = $this->disks[$_POST['disks']]['dn'];
117       $this->is_dialog = true;
118     }
120     /* Edit aborted, close dialog, without saving anything
121      */
122     if(isset($_POST['CancelDisk'])){
123       unset($this->dialog);
124       $this->dialog = NULL;
125       $this->is_dialog=false;
126     }
127   
128     /* Dialog saved
129      * Save given data from Dialog, if no error is occured
130      */
131     if(isset($_POST['SaveDisk'])){
132       $this->dialog->save_object();
133       if(count($this->dialog->check())){
134         foreach($this->dialog->check() as $msg){
135           print_red($msg);
136         }
137       }else{
138         $disk = $this->dialog->save();
139         if(isset($disk['rename'])){
140           if($this->disks[$disk['rename']['from']]['status']=="edited"){
141             $this->disks[$disk['rename']['from']]['status']="delete";
142           }else{
143             unset($this->disks[$disk['rename']['from']]);
144           }
145   
146           foreach($disk['partitions'] as $key => $val){
147             if($disk['partitions'][$key]['status']!="delete"){
148               $disk['partitions'][$key]['status']= "new";
149             }
150           }
152           $disk['status']="new";
153           $disk['cn']= $disk['rename']['to'];
154         }
155       
156         $this->disks[$disk['cn']]=$disk; 
157         unset($this->dialog);
158         $this->dialog = NULL;
159         $this->is_dialog=false;
160         ksort($this->disks);
161       }
162     }
164     /* Delete selected disk drive from list
165      * Assign delete status for all its partitions      
166      */
167     if((isset($_POST['DelDisk']))&&(!empty($_POST['disks']))){
168       $disk = $_POST['disks'];
169       if($this->disks[$disk]['status']=="edited"){
170         $this->disks[$disk."-delete"]=$this->disks[$disk];
171         unset($this->disks[$disk]);
172         $disk = $disk."-delete";        
173         $this->disks[$disk]['status']="delete";
174         foreach($this->disks[$disk]['partitions'] as $name => $value ){
175           if($value['status']=="edited"){
176             $this->disks[$disk]['partitions'][$name]['status']="delete"; 
177           }else{
178             unset($this->disks[$disk]['partitions'][$name]);
179           }
180         }
181       }else{
182         unset($this->disks[$disk]);
183       }
184     }
186     /* Display dialog if one is defined
187      */
188     if(isset($this->dialog)){
189       $this->dialog->save_object();
190       return($this->dialog->execute());
191     }
193     /* Assign all attributes to smarty engine
194      */
195     foreach($this->attributes as $attrs){
196       $smarty->assign($attrs,$this->$attrs);
197       if($this->$attrs){
198         $smarty->assign($attrs."CHK"," ");
199       }else{
200         $smarty->assign($attrs."CHK"," disabled ");
201       }
202     }
203     
204     $disks = $this->getDisks();
205     $smarty->assign("disks"   ,$disks);
206     $smarty->assign("diskKeys",array_flip($disks));
207     $display.= $smarty->fetch(get_template_path('faiPartitionTable.tpl', TRUE));
208     return($display);
209   }
211   function getDisks(){
212     /* Return all available disks for this partition table
213      * Return in listBox friendly array
214      */
215     $a_return = array();
216     foreach($this->disks as $key => $disk){
217       if($disk['status'] != "delete"){
218         $cnt=0;
219         foreach($disk['partitions'] as $val){
220           if($val['status']!="delete"){
221             $cnt ++;
222           }
223         }
224         if(!empty($disk['description'])){
225           if($cnt == 1){
226             $a_return[$key]=  $disk['cn']." [".$disk['description']."], ".sprintf(_("%s partition"), $cnt);
227           }else{
228             $a_return[$key]=  $disk['cn']." [".$disk['description']."], ".sprintf(_("%s partition(s)"), $cnt);
229           }
230         }else{
231           if($cnt == 1){
232             $a_return[$key]=  $disk['cn'].", ".sprintf(_("%s partition"), $cnt);
233           }else{
234             $a_return[$key]=  $disk['cn'].", ".sprintf(_("%s partition(s)"), $cnt);
235           }
236         }
237       }
238     }
239     return($a_return);
240   }
243   /* Delete me, and all my subtrees
244    */
245   function remove_from_parent()
246   {
247     $ldap = $this->config->get_ldap_link();
248     $ldap->cd ($this->dn);
249     $ldap->rmdir_recursive($this->dn);
250     $this->handle_post_events("remove");    
251   
252     /* This cannot be removed... */
253   }
256   /* Save data to object 
257    */
258   function save_object()
259   {
260     plugin::save_object();
261     foreach($this->attributes as $attrs){
262       if(isset($_POST[$attrs])){
263         $this->$attrs = $_POST[$attrs];
264       }
265     }
266   }
269   /* Check supplied data */
270   function check()
271   {
273     $message= array();
274     return ($message);
275   }
278   /* Save to LDAP */
279   function save()
280   {
282     plugin::save();
283     /* Save current settings.
284      * 1 : We must save the partition table, with its description and cn 
285      * 2 : Append Disk with cn and  description.
286      * 3 : Save partitions for each disk
287      */  
289     $ldap = $this->config->get_ldap_link();
291     if($this->new){
292       $ldap->cd($this->config->current['BASE']);
293       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
294       $ldap->cd($this->dn);
295       $ldap->add($this->attrs);
296       show_ldap_error($ldap->get_error());
297     }else{
298       /* Add partition table to ldap
299        */
300       $ldap->cd($this->dn);
301       $ldap->modify($this->attrs);
302       show_ldap_error($ldap->get_error());
303     }
304   
305   
306     /* Sort entries, because we must delete entries with status="delete" first */
307     $order = array();
308     foreach($this->disks as $key => $disk){
309       if($disk['status'] == "delete"){
310         $order[$key] = $disk;
311       }
312     }
313     foreach($this->disks as $key => $disk){
314       if($disk['status'] != "delete"){
315         $order[$key] = $disk;
316       }
317     }
319     /* Append all disks to ldap */
320     foreach($order as $cn=>$disk){
321       $disk_dn                    = "cn=".$disk['cn'].",".$this->dn;
322       $disk_attrs['cn']           =  $disk['cn'];
323       $disk_attrs['description']  =  $disk['description']; 
324       $disk_attrs['objectClass']  =  array("top","FAIclass","FAIpartitionDisk");
326       if($disk['status']=="new"){
327         $ldap->cat($disk_dn);
328         if($ldap->count()){
329           $disk['status']="edited";
330         }
331       }
332  
333       if($disk['status'] == "delete"){
334         $ldap->cd($disk_dn);
335         $ldap->rmdir_recursive($disk_dn);
336       }elseif($disk['status']== "edited"){
337         if(empty($disk_attrs['description'])){
338           $disk_attrs['description']=array();
339         }
340         $ldap->cd($disk_dn);
341         $ldap->modify($disk_attrs);
342       }elseif($disk['status']== "new"){
343         if(empty($disk_attrs['description'])){
344           unset($disk_attrs['description']);
345         }
346         $ldap->cd($this->config->current['BASE']);
347         $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $disk_dn));
348         $ldap->cd($disk_dn);
349         $ldap->add($disk_attrs);
350       }else{
351         print_red("unknown status while saving disks");
352       }
354       show_ldap_error($ldap->get_error());
355       if($disk['status']!="delete")
356       /* Add all partitions */
357       foreach($disk['partitions'] as $key => $partition){
358         $partition_attrs = array();
360         foreach($partition as $key => $value){
361           if(!empty($value)){
362             $partition_attrs[$key]=$value;        
363           }else{
364             unset($partition_attrs[$key]);        
365           }
366         }
368         $partition_dn= "FAIpartitionNr=".$partition_attrs['FAIpartitionNr'].",".$disk_dn;      
369         $partition_attrs['objectClass']= array("top","FAIclass","FAIpartitionEntry");
370         $partition_attrs['cn']= $partition_attrs['FAIpartitionNr'];
371         
372         unset($partition_attrs['status']);
373         unset($partition_attrs['old_cn']);
375         if($partition['status']=="new"){
376           $ldap->cat($partition_dn);
377           if($ldap->count()){
378             $partition['status']="edited";
379           }
380         }
382         if((!isset($partition['FAImountPoint']))||(empty($partition['FAImountPoint']))){
383           $partition_attrs['FAImountPoint']="swap";
384         }
386         if(($partition['status'] == "delete")&&($disk['status']!="new")){
387           $ldap->cd($partition_dn);
388           $ldap->rmdir_recursive($partition_dn);
389         }elseif($partition['status'] == "new"){
390           if(empty($partition_attrs['description'])){
391             unset($partition_attrs['description']);
392           }
393           $ldap->cd($this->config->current['BASE']);
394           $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $partition_dn));
395           $ldap->cd($partition_dn);
396           $ldap->add($partition_attrs);
397         }elseif($partition['status'] == "edited"){
398           if(empty($partition_attrs['description'])){
399             $partition_attrs['description']=array();
400           }
401           $ldap->cd($partition_dn);
402           $ldap->modify($partition_attrs);
403         } 
404       show_ldap_error($ldap->get_error());
405       }
406     }
407     $this->handle_post_events("add");
408   }
411 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
412 ?>