Code

Adapted path and image
[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");
14   var $base64_encode    = array("FAImountPoint","FAImountOptions","FAIfsOptions","description");
16   /* Specific attributes */
17   var $cn               = "";       // The class name for this object
18   var $description      = "";       // The description for this set of partitions
19   var $disks            = array();  // All defined Disks 
20   var $is_dialog        = false;    // specifies which buttons will be shown to save or abort
21   var $dialog           = NULL;     // a dialog, e.g. new disk dialog
23   function faiPartitionTable ($config, $dn= NULL)
24   {
25     /* Load Attributes */
26     plugin::plugin ($config, $dn);
28     /* If "dn==new" we try to create a new entry
29      * Else we must read all objects from ldap which belong to this entry.
30      * First read disks from ldap ... and then the partition definitions for the disks.
31      */
32     if($dn != "new"){
33       $this->dn =$dn;
35       /* Read all disks from ldap taht are defined fot this partition table 
36        */
37       $ldap = $this->config->get_ldap_link();
38       $ldap->cd ($this->dn);
39       $ldap->search("(&(objectClass=FAIclass)(objectClass=FAIpartitionDisk))",array("*"));
40       while($object = $ldap->fetch()){
41         $this->disks[$object['cn'][0]]['status']      = "edited";
42         $this->disks[$object['cn'][0]]['dn']          = $object['dn'];
43         $this->disks[$object['cn'][0]]['cn']          = $object['cn'][0];
44         if(isset($object['description'][0])){
45           $this->disks[$object['cn'][0]]['description'] = $object['description'][0];
46         }else{
47           $this->disks[$object['cn'][0]]['description'] = "";
48         }
49         $this->disks[$object['cn'][0]]['partitions']   = array();
50       }
51   
52       /* read all partitions for each disk 
53        */
54       foreach($this->disks as $name => $disk){
55         $ldap->cd ($disk['dn']);
56         $ldap->search("(&(objectClass=FAIclass)(objectClass=FAIpartitionEntry))",array("*"));
57         while($partition = $ldap->fetch()){
59           /* remove count ... from ldap result 
60            */
61           foreach($partition as $key=>$val){
62             if((is_numeric($key))||($key=="count")||($key=="dn")){
63               unset($partition[$key]);
64             }else{
65               $partition[$key] = $val[0];
66             }
67           }
68           /* Append fetched partitions
69            */
70           $partition['status']="edited";
71     
72           foreach($this->base64_encode as $attrs){
73             $partition[$attrs] = base64_decode($partition[$attrs]);
74           }
75           $this->disks[$name]['partitions'][$partition['FAIpartitionNr']] = $partition; 
76         }  
77       }
78     }
79   }
81   function execute()
82   {
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     if((isset($_POST['EditDisk']))&&(isset($_POST['disks']))){
104       $usedDiskNames =array();
105       foreach($this->disks  as $key=>$disk){
106         if($key != $_POST['disks']){
107           $usedDiskNames[]= $key;
108         }
109       }
110       $this->dialog = new faiPartitionTableEntry($this->config,$this->dn,$usedDiskNames,$this->disks[$_POST['disks']]); 
111       $this->is_dialog = true;
112     }
114     /* Edit aborted, close dialog, without saving anything
115      */
116     if(isset($_POST['CancelDisk'])){
117       unset($this->dialog);
118       $this->dialog = NULL;
119       $this->is_dialog=false;
120     }
121   
122     /* Dialog saved
123      * Save given data from Dialog, if no error is occured
124      */
125     if(isset($_POST['SaveDisk'])){
126       $this->dialog->save_object();
127       if(count($this->dialog->check())){
128         foreach($this->dialog->check() as $msg){
129           print_red($msg);
130         }
131       }else{
132         $disk = $this->dialog->save();
133         if(isset($disk['rename'])){
134           if($this->disks[$disk['rename']['from']]['status']=="edited"){
135             $this->disks[$disk['rename']['from']]['status']="delete";
136           }else{
137             unset($this->disks[$disk['rename']['from']]);
138           }
139   
140           foreach($disk['partitions'] as $key => $val){
141             if($disk['partitions'][$key]['status']!="delete"){
142               $disk['partitions'][$key]['status']= "new";
143             }
144           }
146           $disk['status']="new";
147           $disk['cn']= $disk['rename']['to'];
148         }
149       
150         $this->disks[$disk['cn']]=$disk; 
151         unset($this->dialog);
152         $this->dialog = NULL;
153         $this->is_dialog=false;
154       }
155     }
157     /* Delete selected disk drive from list
158      * Assign delete status for all its partitions      
159      */
160     if((isset($_POST['DelDisk']))&&(!empty($_POST['disks']))){
161       $disk = $_POST['disks'];
162       if($this->disks[$disk]['status']=="edited"){
163         $this->disks[$disk."-delete"]=$this->disks[$disk];
164         unset($this->disks[$disk]);
165         $disk = $disk."-delete";        
166         $this->disks[$disk]['status']="delete";
167         foreach($this->disks[$disk]['partitions'] as $name => $value ){
168           if($value['status']=="edited"){
169             $this->disks[$disk]['partitions'][$name]['status']="delete"; 
170           }else{
171             unset($this->disks[$disk]['partitions'][$name]);
172           }
173         }
174       }else{
175         unset($this->disks[$disk]);
176       }
177     }
179     /* Display dialog if one is defined
180      */
181     if(isset($this->dialog)){
182       $this->dialog->save_object();
183       return($this->dialog->execute());
184     }
186     /* Assign all attributes to smarty engine
187      */
188     foreach($this->attributes as $attrs){
189       $smarty->assign($attrs,$this->$attrs);
190       if($this->$attrs){
191         $smarty->assign($attrs."CHK"," ");
192       }else{
193         $smarty->assign($attrs."CHK"," disabled ");
194       }
195     }
196     
197     $disks = $this->getDisks();
198     $smarty->assign("disks"   ,$disks);
199     $smarty->assign("diskKeys",array_flip($disks));
200     $display.= $smarty->fetch(get_template_path('faiPartitionTable.tpl', TRUE));
201     return($display);
202   }
204   function getDisks(){
205     /* Return all available disks for this partition table
206      * Return in listBox friendly array
207      */
208     $a_return = array();
209     foreach($this->disks as $key => $disk){
210       if($disk['status'] != "delete"){
211         $cnt=0;
212         foreach($disk['partitions'] as $val){
213           if($val['status']!="delete"){
214             $cnt ++;
215           }
216         }
217         if(!empty($disk['description'])){
218           $a_return[$key]=  $disk['cn']." [".$disk['description']."] - "._("Number of Partitions")." :".$cnt;
219         }else{
220           $a_return[$key]=  $disk['cn']." - "._("Number of Partitions")." :".$cnt;
221         }
222       }
223     }
224     return($a_return);
225   }
228   /* Delete me, and all my subtrees
229    */
230   function remove_from_parent()
231   {
232     $ldap = $this->config->get_ldap_link();
233     $ldap->cd ($this->dn);
234     $ldap->rmdir_recursive($this->dn);
235     $this->handle_post_events("remove");    
236   
237     /* This cannot be removed... */
238   }
241   /* Save data to object 
242    */
243   function save_object()
244   {
245     plugin::save_object();
246     foreach($this->attributes as $attrs){
247       if(isset($_POST[$attrs])){
248         $this->$attrs = $_POST[$attrs];
249       }
250     }
251   }
254   /* Check supplied data */
255   function check()
256   {
258     $message= array();
260     $str = utf8_encode("üöä");
261     if((empty($this->description))||(preg_match("/[^a-z0-9".$str."\.,;:\-_\? ]/i",$this->description))){
262       $message[]=_("Please enter a valid description.");
263     }
265     if((empty($this->cn))||(preg_match("/[^a-z0-9]/i",$this->cn))){
266       $message[]=_("Please enter a valid name.");
267     }
269     return ($message);
270   }
273   /* Save to LDAP */
274   function save()
275   {
277     plugin::save();
278     /* Save current settings.
279      * 1 : We must save the partition table, with its description and cn 
280      * 2 : Append Disk with cn and  description.
281      * 3 : Save partitions for each disk
282      */  
284     $ldap = $this->config->get_ldap_link();
286     if($this->new){
287       $ldap->cd($this->dn);
288       $ldap->create_missing_trees($this->dn);
289       $ldap->cd($this->dn);
290       $ldap->add($this->attrs);
291       show_ldap_error($ldap->get_error());
292     }else{
293       /* Add partition table to ldap
294        */
295       $ldap->cd($this->dn);
296       $ldap->modify($this->attrs);
297       show_ldap_error($ldap->get_error());
298     }
299   
300   
301     /* Sort entries, because we must delete entries with status="delete" first */
302     $order = array();
303     foreach($this->disks as $key => $disk){
304       if($disk['status'] == "delete"){
305         $order[$key] = $disk;
306       }
307     }
308     foreach($this->disks as $key => $disk){
309       if($disk['status'] != "delete"){
310         $order[$key] = $disk;
311       }
312     }
314     /* Append all disks to ldap */
315     foreach($order as $cn=>$disk){
316       $disk_dn                    = "cn=".$disk['cn'].",".$this->dn;
317       $disk_attrs['cn']           =  $disk['cn'];
318       $disk_attrs['description']  =  $disk['description']; 
319       $disk_attrs['objectClass']  =  array("top","FAIclass","FAIpartitionDisk");
320   
321       if($disk['status'] == "delete"){
322         $ldap->cd($disk_dn);
323         $ldap->rmdir_recursive($disk_dn);
324       }elseif($disk['status']== "edited"){
325         $ldap->cd($disk_dn);
326         $ldap->modify($disk_attrs);
327       }elseif($disk['status']== "new"){
328         $ldap->cd($disk_dn);
329         $ldap->create_missing_trees($disk_dn);
330         $ldap->cd($disk_dn);
331         $ldap->add($disk_attrs);
332       }else{
333         print_red("unknown status while saving disks");
334       }
336       show_ldap_error($ldap->get_error());
337       if($disk['status']!="delete")
338       /* Add all partitions */
339       foreach($disk['partitions'] as $key => $partition){
340         $partition_attrs = array();
342         foreach($this->base64_encode as $attrs){
343           $partition[$attrs] = base64_encode($partition[$attrs]);          
344         }
346         foreach($partition as $key => $value){
347           if(!empty($value)){
348             $partition_attrs[$key]=$value;        
349           }else{
350             unset($partition_attrs[$key]);        
351           }
352         }
354         $partition_dn                       = "cn=".$partition_attrs['cn'].",".$disk_dn;      
355         $partition_attrs['objectClass']     = array("top","FAIclass","FAIpartitionEntry");
356         
357         unset($partition_attrs['status']);
358         unset($partition_attrs['old_cn']);
359       
360         if(($partition['status'] == "delete")&&($disk['status']!="new")){
361           $ldap->cd($partition_dn);
362           $ldap->rmdir_recursive($partition_dn);
363         }elseif($partition['status'] == "new"){
364           $ldap->cd($partition_dn);
365           $ldap->create_missing_trees($partition_dn);
366           $ldap->cd($partition_dn);
367           $ldap->add($partition_attrs);
368         }elseif($partition['status'] == "edited"){
369           $ldap->cd($partition_dn);
370           $ldap->modify($partition_attrs);
371         } 
372       show_ldap_error($ldap->get_error());
373       }
374     }
375     $this->handle_post_events("add");
376   }
379 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
380 ?>