Code

Fixed problem with not clickable buttons in fai package error msgs
[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         = "";
23   var $ui;
25   var $view_logged      = FALSE;
27   function faiPartitionTable ($config, $dn= NULL)
28   {
29     /* Load Attributes */
30     plugin::plugin ($config, $dn);
32     $this->acl ="#all#";
34     $this->ui = get_userinfo();    
36     /* If "dn==new" we try to create a new entry
37      * Else we must read all objects from ldap which belong to this entry.
38      * First read disks from ldap ... and then the partition definitions for the disks.
39      */
40     if($dn != "new"){
41       $this->dn =$dn;
43       /* Get FAIstate
44        */
45       if(isset($this->attrs['FAIstate'][0])){
46         $this->FAIstate = $this->attrs['FAIstate'][0];
47       }
49       /* Read all disks from ldap taht are defined fot this partition table 
50        */
51       $ldap = $this->config->get_ldap_link();
52       $ldap->cd ($this->dn);
53       $ldap->search("(&(objectClass=FAIclass)(objectClass=FAIpartitionDisk))",array("*"));
54       while($object = $ldap->fetch()){
56         /* Skip objects, that are tagged as removed */
57         if(isset($object['FAIstate'][0])){
58           if(preg_match("/removed$/",$object['FAIstate'][0])){
59             continue;
60           }
61         }
63         $this->disks[$object['cn'][0]]['status']      = "edited";
64         $this->disks[$object['cn'][0]]['dn']          = $object['dn'];
65         $this->disks[$object['cn'][0]]['cn']          = $object['cn'][0];
66         if(isset($object['description'][0])){
67           $this->disks[$object['cn'][0]]['description'] = $object['description'][0];
68         }else{
69           $this->disks[$object['cn'][0]]['description'] = "";
70         }
71         $this->disks[$object['cn'][0]]['partitions']   = array();
72       }
73   
74       /* read all partitions for each disk 
75        */
76       foreach($this->disks as $name => $disk){
77         $ldap->cd ($disk['dn']);
78         $ldap->search("(&(objectClass=FAIclass)(objectClass=FAIpartitionEntry))",array("*"));
79         while($partition = $ldap->fetch()){
81           /* Skip objects, that are tagged as removed */
82           if(isset($partition['FAIstate'][0])){
83             if(preg_match("/removed$/",$partition['FAIstate'][0])){
84               continue;
85             }
86           }
88           /* remove count ... from ldap result 
89            */
90           foreach($partition as $key=>$val){
91             if((is_numeric($key))||($key=="count")||($key=="dn")){
92               unset($partition[$key]);
93             }else{
94               $partition[$key] = $val[0];
95             }
96           }
98           /* Append fetched partitions
99            */
100           $partition['status']="edited";
101           $this->disks[$name]['partitions'][$partition['FAIpartitionNr']] = $partition; 
102         }  
103       }
104     }
105     ksort($this->disks);
106   }
109   function acl_base_for_current_object($dn)
110   {
111     if($dn == "new"){
112       if($this->dn == "new"){
113         $dn= $_SESSION['CurrentMainBase'];
114       }else{
115         $dn = $this->dn;
116       }
117     }
118     return($dn);
119   }
122   function execute()
123   {
124     /* Call parent execute */
125     plugin::execute();
127     if($this->is_account && !$this->view_logged){
128       $this->view_logged = TRUE;
129       new log("view","fai/".get_class($this),$this->dn);
130     }
132     /* Fill templating stuff */
133     $smarty= get_smarty();
134     $display= "";
135  
136     /* Add Disk to this Partitionset
137      * This code adds a new HDD to the disks 
138      * A new Dialog will be opened 
139      */
140     if(isset($_POST['AddDisk'])){
141       $usedDiskNames =array();
142       foreach($this->disks as $key=>$disk){
143         $usedDiskNames[]= $key;
144       }
145       $this->dialog = new faiPartitionTableEntry($this->config,$this->dn,$usedDiskNames); 
146       $this->dialog->set_acl_base($this->acl_base_for_current_object($this->dn));
147       $this->dialog->set_acl_category("fai");
148       $this->dialog->FAIstate = $this->FAIstate;
149       $this->is_dialog = true;
150     }
152     /* Edit disk.
153      * Open dialog which allows us to edit the selected entry 
154      */    
156     if($this->dn != "new"){
157       $_SESSION['objectinfo']= $this->dn;
158     }
160     if((isset($_POST['EditDisk']))&&(isset($_POST['disks']))){
161       $usedDiskNames =array();
163       $Udisk = $_POST['disks'][0];
164       
165       foreach($this->disks  as $key=>$disk){
166         if($key != $Udisk){
167           $usedDiskNames[]= $key;
168         }
169       }
170     
171       /* Set object info string, which will be displayed in plugin info line */ 
172       if(isset($this->disks[$Udisk]['dn'])){
173         $_SESSION['objectinfo'] = $this->disks[$Udisk]['dn'];
174         $dn = $this->disks[$Udisk]['dn'];
175       }else{
176         $_SESSION['objectinfo'] = "";
177         $dn = "new";
178       }
180       $this->dialog = new faiPartitionTableEntry($this->config,$this->dn,$usedDiskNames,$this->disks[$Udisk]); 
181       $this->dialog->set_acl_base($this->acl_base_for_current_object($dn));
182       $this->dialog->set_acl_category("fai");
183       $this->dialog->FAIstate = $this->FAIstate;
184      
185       $this->is_dialog = true;
186     }
188     /* Edit aborted, close dialog, without saving anything
189      */
190     if(isset($_POST['CancelDisk'])){
191       unset($this->dialog);
192       $this->dialog = NULL;
193       $this->is_dialog=false;
194     }
196     /* Dialog saved
197      * Save given data from Dialog, if no error is occurred
198      */
199     if(isset($_POST['SaveDisk'])){
201       if($this->FAIstate != "freeze"){
202         $this->dialog->save_object();
203         if(count($this->dialog->check())){
204           foreach($this->dialog->check() as $msg){
205             print_red($msg);
206           }
207         }else{
208           $disk = $this->dialog->save();
209           if(isset($disk['rename'])){
210             if($this->disks[$disk['rename']['from']]['status']=="edited"){
211               $this->disks[$disk['rename']['from']]['status']="delete";
212             }else{
213               unset($this->disks[$disk['rename']['from']]);
214             }
216             foreach($disk['partitions'] as $key => $val){
217               if($disk['partitions'][$key]['status']!="delete"){
218                 $disk['partitions'][$key]['status']= "new";
219               }
220             }
222             $disk['status']="new";
223             $disk['cn']= $disk['rename']['to'];
224           }
226           $this->disks[$disk['cn']]=$disk; 
227           unset($this->dialog);
228           $this->dialog = NULL;
229           $this->is_dialog=false;
230           ksort($this->disks);
231         }
232       }else{
233         $this->dialog = NULL;
234         $this->is_dialog=false;
235       }
236     }
238     /* Delete selected disk drive from list
239      * Assign delete status for all its partitions      
240      */
241     if((isset($_POST['DelDisk']))&&(!empty($_POST['disks']))){
242       if($this->FAIstate != "freeze"){
243         foreach($_POST['disks'] as $disk) {
244           if($this->disks[$disk]['status']=="edited"){
245             $this->disks[$disk."-delete"]=$this->disks[$disk];
246             unset($this->disks[$disk]);
247             $disk = $disk."-delete";        
248             $this->disks[$disk]['status']="delete";
249             foreach($this->disks[$disk]['partitions'] as $name => $value ){
250               if($value['status']=="edited"){
251                 $this->disks[$disk]['partitions'][$name]['status']="delete"; 
252               }else{
253                 unset($this->disks[$disk]['partitions'][$name]);
254               }
255             }
256           }else{
257             unset($this->disks[$disk]);
258           }
259         }
260       }
261     }
263     /* Display dialog if one is defined
264      */
265     if(isset($this->dialog)){
266       $this->dialog->save_object();
267       return($this->dialog->execute());
268     }
270     /* Assign all attributes to smarty engine
271      */
272     foreach($this->attributes as $attrs){
273       $smarty->assign($attrs,$this->$attrs);
274       if($this->$attrs){
275         $smarty->assign($attrs."CHK"," ");
276       }else{
277         $smarty->assign($attrs."CHK"," disabled ");
278       }
279     }
280    
281     $dn = $this->acl_base_for_current_object($this->dn);
282     $smarty->assign("sub_object_is_addable",
283         preg_match("/c/",$this->ui->get_permissions($dn,"fai/faiPartitionTableEntry")) &&
284         !preg_match("/freeze/",$this->FAIstate));
286     $tmp = $this->plInfo();
287     foreach($tmp['plProvidedAcls'] as $name => $translated){
288       $smarty->assign($name."ACL",$this->getacl($name));
289     }
290     $disks = $this->getDisks();
291     $smarty->assign("disks"   ,$disks);
292     $display.= $smarty->fetch(get_template_path('faiPartitionTable.tpl', TRUE));
293     return($display);
294   }
296   function getDisks(){
297     /* Return all available disks for this partition table
298      * Return in listBox friendly array
299      */
300     $a_return = array();
301     foreach($this->disks as $key => $disk){
303       $dn = "new";
304       if(isset($obj['dn'])){
305         $dn = $obj['dn'];
306       }
307       $dn = $this->acl_base_for_current_object($dn);
308       $acl = $this->ui->get_permissions($dn,"fai/faiPartitionTableEntry");
309       if(preg_match("/(r|w)/",$acl)) {
311         if($disk['status'] != "delete"){
312           $cnt=0;
313           foreach($disk['partitions'] as $val){
314             if($val['status']!="delete"){
315               $cnt ++;
316             }
317           }
318           if(!empty($disk['description'])){
319             if($cnt == 1){
320               $a_return[$key]=  $disk['cn']." [".$disk['description']."], ".sprintf(_("%s partition"), $cnt);
321             }else{
322               $a_return[$key]=  $disk['cn']." [".$disk['description']."], ".sprintf(_("%s partition(s)"), $cnt);
323             }
324           }else{
325             if($cnt == 1){
326               $a_return[$key]=  $disk['cn'].", ".sprintf(_("%s partition"), $cnt);
327             }else{
328               $a_return[$key]=  $disk['cn'].", ".sprintf(_("%s partition(s)"), $cnt);
329             }
330           }
331         }
332       }
333     }
334     return($a_return);
335   }
338   /* Delete me, and all my subtrees
339    */
340   function remove_from_parent()
341   {
342     $ldap = $this->config->get_ldap_link();
343     $ldap->cd ($this->dn);
345 #    $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $this->dn);
346     $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $this->dn);
347     if($_SESSION['faifilter']['branch'] == "main"){
348       $use_dn = $this->dn;
349     }
351     prepare_to_save_FAI_object($use_dn,array(),true);
353     new log("remove","fai/".get_class($this),$use_dn,$this->attributes);   
354  
355     foreach($this->disks as $disk){
357       $disk_dn = "cn=".$disk['cn'].",".$this->dn;
358 #      $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $disk_dn);
359       $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $disk_dn);
360       if($_SESSION['faifilter']['branch'] == "main"){
361         $use_dn = $disk_dn;
362       }
363       prepare_to_save_FAI_object($use_dn,array(),true);
365       foreach($disk['partitions'] as $key => $partition){    
366      
367         $partition_dn= "FAIpartitionNr=".$partition['FAIpartitionNr'].",".$disk_dn;      
368 #        $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $partition_dn);
369         $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $partition_dn);
370         if($_SESSION['faifilter']['branch'] == "main"){
371           $use_dn = $disk_dn;
372         }
373         prepare_to_save_FAI_object($use_dn,array(),true);
374       }
375     }
376   }
379   /* Save data to object 
380    */
381   function save_object()
382   {
383     if($this->FAIstate == "freeze") return;
384     plugin::save_object();
385     foreach($this->attributes as $attrs){
386       if(isset($_POST[$attrs])){
387         $this->$attrs = $_POST[$attrs];
388       }
389     }
390   }
393   /* Check supplied data */
394   function check()
395   {
396     /* Call common method to give check the hook */
397     $message= plugin::check();
399     return ($message);
400   }
403   /* Save to LDAP */
404   function save()
405   {
407     plugin::save();
408     /* Save current settings.
409      * 1 : We must save the partition table, with its description and cn 
410      * 2 : Append Disk with cn and  description.
411      * 3 : Save partitions for each disk
412      */  
414     $ldap = $this->config->get_ldap_link();
416     prepare_to_save_FAI_object($this->dn,$this->attrs);
417     show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/partition table with dn '%s' failed."),$this->dn));
419     if($this->initially_was_account){
420       new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
421     }else{
422       new log("create","fai/".get_class($this),$this->dn,$this->attributes);
423     }
424  
425     /* Do object tagging */
426     $this->handle_object_tagging();
427   
428     /* Sort entries, because we must delete entries with status="delete" first */
429     $order = array();
430     foreach($this->disks as $key => $disk){
431       if($disk['status'] == "delete"){
432         $order[$key] = $disk;
433       }
434     }
435     foreach($this->disks as $key => $disk){
436       if($disk['status'] != "delete"){
437         $order[$key] = $disk;
438       }
439     }
441     /* Append all disks to ldap */
442     foreach($order as $cn=>$disk){
443       $disk_dn                    = "cn=".$disk['cn'].",".$this->dn;
444       $disk_attrs['cn']           =  $disk['cn'];
445       $disk_attrs['description']  =  $disk['description']; 
446       $disk_attrs['objectClass']  =  array("top","FAIclass","FAIpartitionDisk");
448       if($disk['status']=="new"){
449         $ldap->cat($disk_dn,array("objectClass"));
450         if($ldap->count()){
451           $disk['status']="edited";
452         }
453       }
455       /* Fix problem with missing objectClass "gosaAdministrativeUnitTag" for disks */
456       $ldap->cat($disk_dn,array("objectClass"));
457       $attrs = $ldap->fetch();
458       if(isset($attrs['objectClass']) && in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
459         $disk_attrs['objectClass'][] = "gosaAdministrativeUnitTag";
460       }
462       if($disk['status'] == "delete"){
463         prepare_to_save_FAI_object($disk_dn,array(),true);
464         $this->handle_post_events("remove");
465       }elseif($disk['status'] == "edited"){
466         prepare_to_save_FAI_object($disk_dn,$disk_attrs);
467         $this->handle_post_events("modify");
468       }elseif($disk['status']=="new"){
469         prepare_to_save_FAI_object($disk_dn,$disk_attrs);
470         $this->handle_post_events("add");
471       }
473       $this->handle_object_tagging($disk_dn, $this->gosaUnitTag);
475       if($disk['status']!="delete")
476       /* Add all partitions */
477       foreach($disk['partitions'] as $key => $partition){
478         $partition_attrs = array();
480         foreach($partition as $key => $value){
481           if(!empty($value)){
482             $partition_attrs[$key]=$value;        
483           }else{
484             unset($partition_attrs[$key]);        
485           }
486         }
488         $partition_dn= "FAIpartitionNr=".$partition_attrs['FAIpartitionNr'].",".$disk_dn;      
489         $partition_attrs['objectClass']= array("top","FAIclass","FAIpartitionEntry");
490         $partition_attrs['cn']= $partition_attrs['FAIpartitionNr'];
491         
492         unset($partition_attrs['status']);
493         unset($partition_attrs['old_cn']);
495         if($partition['status']=="new"){
496           $ldap->cat($partition_dn,array("objectClass"));
497           if($ldap->count()){
498             $partition['status']="edited";
499           }
500         }
502         if((!isset($partition['FAImountPoint']))||(empty($partition['FAImountPoint']))){
503           $partition_attrs['FAImountPoint']="swap";
504         }
506         /* Fix problem with missing objectClass "gosaAdministrativeUnitTag" for partitions */
507         $ldap->cat($partition_dn,array("objectClass"));
508         $attrs = $ldap->fetch();
509         if(isset($attrs['objectClass']) && in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
510           $partition_attrs['objectClass'][] = "gosaAdministrativeUnitTag";
511         }
513         if($partition['status'] == "delete"){
514           prepare_to_save_FAI_object($partition_dn,array(),true);
515           $this->handle_post_events("remove");
516         }elseif($partition['status'] == "edited"){
517           prepare_to_save_FAI_object($partition_dn,$partition_attrs);
518           $this->handle_post_events("modify");
519         }elseif($partition['status']=="new"){
520           prepare_to_save_FAI_object($partition_dn,$partition_attrs);
521           $this->handle_post_events("add");
522         }
524         $this->handle_object_tagging($partition_dn, $this->gosaUnitTag);
525       }
526     }
527     $this->handle_post_events("add");
528   }
531   /* Return plugin informations for acl handling */ 
532   function plInfo()
533   {
534     return (array( 
535           "plShortName" => _("Partition table"),
536           "plDescription" => _("FAI partition table"),
537           "plSelfModify"  => FALSE,
538           "plDepends"     => array(),
539           "plPriority"    => 26,
540           "plSection"     => array("administration"),
541           "plCategory"    => array("fai"),
542           "plProvidedAcls" => array(
543             "cn"                => _("Name")."&nbsp;("._("Read only").")",
544             "description"       => _("Description"))
545           ));
546   }
549 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
550 ?>