Code

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