Code

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