Code

Updated FAI handling for Templates && Partitions.
[gosa.git] / gosa-plugins / fai / admin / fai / class_faiPartitionTable.inc
1 <?php
3 class faiPartitionTable extends plugin
4 {
5   /* attribute list for save action */
6   var $ignore_account   = TRUE;
7   var $attributes       = array("cn","description");
8   var $objectclasses    = array("top","FAIclass","FAIpartitionTable");
10   var $subAttributes = array("cn","description","FAIpartitionNr");
11   var $sub64coded = array();
12   var $subBinary = array();
14   /* Specific attributes */
15   var $cn               = "";       // The class name for this object
16   var $description      = "";       // The description for this set of partitions
17   var $disks            = array();  // All defined Disks 
18   var $is_dialog        = false;    // specifies which buttons will be shown to save or abort
20   var $FAIstate         = "";
21   var $ui;
23   var $view_logged      = FALSE;
25   function faiPartitionTable (&$config, $dn= NULL)
26   {
27     /* Load Attributes */
28     plugin::plugin ($config, $dn);
30     /* If "dn==new" we try to create a new entry
31      * Else we must read all objects from ldap which belong to this entry.
32      */
33     $this->ui = get_userinfo();
34     if($dn != "new"){
35       $this->dn =$dn;
37       /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
38        */
39       $res = FAI::get_all_objects_for_given_base($this->dn,"(&(objectClass=FAIclass)(objectClass=FAIpartitionTable))");
40       foreach($res as $obj){
41         $objects = array();
42         $objects['status']      = "edited";
43         $objects['dn']          = $obj['dn'];
44         $objects                = $this->get_object_attributes($objects,$this->subAttributes);
45         $this->disks[$objects['cn']] = $objects;
46       }
48       /* read all partitions for each disk 
49        */
50       foreach($this->disks as $name => $disk){
52         $res = FAI::get_all_objects_for_given_base($disk['dn'],"(&(objectClass=FAIclass)(objectClass=FAIpartitionEntry))");
53         foreach($res as $obj){
54           $objects = array();
55           $objects['status']      = "edited";
56           $objects['dn']          = $obj['dn'];
57           $objects                = $this->get_object_attributes($objects,$this->subAttributes);
58           $this->disks[$name]['partitions'][$objects['cn']] = $objects;
59         }
60       }
61     }
62     $this->is_new = FALSE;
63     if($this->dn == "new"){
64       $this->is_new =TRUE;
65     }
66     ksort($this->disks);
67   }
70   function acl_base_for_current_object($dn)
71   {
72     if($dn == "new"){
73       if($this->dn == "new"){
74         $dn = session::get('CurrentMainBase');
75       }else{
76         $dn = $this->dn;
77       }
78     }
79     return($dn);
80   }
83   function execute()
84   {
85     /* Call parent execute */
86     plugin::execute();
88     print_a($this->disks);
90     if($this->is_account && !$this->view_logged){
91       $this->view_logged = TRUE;
92       new log("view","fai/".get_class($this),$this->dn);
93     }
95     /* Fill templating stuff */
96     $smarty= get_smarty();
97     $display= "";
98  
99     /* Add Disk to this Partitionset
100      * This code adds a new HDD to the disks 
101      * A new Dialog will be opened 
102      */
103     if(isset($_POST['AddDisk']) && !preg_match("/freeze/i",$this->FAIstate)){
104       $usedDiskNames =array();
105       foreach($this->disks as $key=>$disk){
106         $usedDiskNames[]= $key;
107       }
108       $this->dialog = new faiPartitionTableEntry($this->config,$this->dn,$usedDiskNames); 
109       $this->dialog->set_acl_base($this->acl_base_for_current_object($this->dn));
110       $this->dialog->set_acl_category("fai");
111       $this->dialog->FAIstate = $this->FAIstate;
112       $this->is_dialog = true;
113     }
115     /* Edit disk.
116      * Open dialog which allows us to edit the selected entry 
117      */    
119     if($this->dn != "new"){
120       session::set('objectinfo',$this->dn);
121     }
123     if((isset($_POST['EditDisk']))&&(isset($_POST['disks']))){
124       $usedDiskNames =array();
125       $Udisk = $_POST['disks'][0];
126       if(isset($this->disks[$Udisk])){
128         foreach($this->disks  as $key=>$disk){
129           if($key != $Udisk){
130             $usedDiskNames[]= $key;
131           }
132         }
134         /* Set object info string, which will be displayed in plugin info line */ 
135         if(isset($this->disks[$Udisk]['dn'])){
136           session::set('objectinfo',$this->disks[$Udisk]['dn']);
137           $dn = $this->disks[$Udisk]['dn'];
138         }else{
139           session::set('objectinfo',"");
140           $dn = "new";
141         }
143         $this->dialog = new faiPartitionTableEntry($this->config,$this->dn,$usedDiskNames,$this->disks[$Udisk]); 
144         $this->dialog->set_acl_base($this->acl_base_for_current_object($dn));
145         $this->dialog->set_acl_category("fai");
146         $this->dialog->FAIstate = $this->FAIstate;
148         $this->is_dialog = true;
149       }
150     }
152     /* Edit aborted, close dialog, without saving anything
153      */
154     if(isset($_POST['CancelDisk'])){
155       unset($this->dialog);
156       $this->dialog = FALSE;
157       $this->is_dialog=false;
158     }
160     /* Dialog saved
161      * Save given data from Dialog, if no error is occurred
162      */
163     if(isset($_POST['SaveDisk'])){
165       if (!preg_match("/freeze/i", $this->FAIstate)){
166         $this->dialog->save_object();
167         if(count($this->dialog->check())){
168           foreach($this->dialog->check() as $msg){
169             msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
170           }
171         }else{
172           $disk = $this->dialog->save();
173           if(isset($disk['rename'])){
174             if($this->disks[$disk['rename']['from']]['status']=="edited"){
175               $this->disks[$disk['rename']['from']]['status']="delete";
176             }else{
177               unset($this->disks[$disk['rename']['from']]);
178             }
180             foreach($disk['partitions'] as $key => $val){
181               if($disk['partitions'][$key]['status']!="delete"){
182                 $disk['partitions'][$key]['status']= "new";
183               }
184             }
186             $disk['status']="new";
187             $disk['cn']= $disk['rename']['to'];
188           }
190           $this->disks[$disk['cn']]=$disk; 
191           unset($this->dialog);
192           $this->dialog = FALSE;
193           $this->is_dialog=false;
194           ksort($this->disks);
195         }
196       }else{
197         $this->dialog = FALSE;
198         $this->is_dialog=false;
199       }
200     }
202     /* Delete selected disk drive from list
203      * Assign delete status for all its partitions      
204      */
205     if((isset($_POST['DelDisk']))&&(!empty($_POST['disks']))){
206       if (!preg_match("/freeze/i", $this->FAIstate)){
207         foreach($_POST['disks'] as $disk) {
209           if(isset($this->disks[$disk])){
210             if($this->disks[$disk]['status']=="edited"){
211               $this->disks[$disk."-delete"]=$this->disks[$disk];
212               unset($this->disks[$disk]);
213               $disk = $disk."-delete";        
214               $this->disks[$disk]['status']="delete";
215               foreach($this->disks[$disk]['partitions'] as $name => $value ){
216                 if($value['status']=="edited"){
217                   $this->disks[$disk]['partitions'][$name]['status']="delete"; 
218                 }else{
219                   unset($this->disks[$disk]['partitions'][$name]);
220                 }
221               }
222             }else{
223               unset($this->disks[$disk]);
224             }
225           }
226         }
227       }
228     }
230     /* Display dialog if one is defined
231      */
232     if(is_object($this->dialog)){
233       $this->dialog->save_object();
234       return($this->dialog->execute());
235     }
237     /* Assign all attributes to smarty engine
238      */
239     foreach($this->attributes as $attrs){
240       $smarty->assign($attrs,$this->$attrs);
241       if($this->$attrs){
242         $smarty->assign($attrs."CHK"," ");
243       }else{
244         $smarty->assign($attrs."CHK"," disabled ");
245       }
246     }
247    
248     $dn = $this->acl_base_for_current_object($this->dn);
249     $smarty->assign("sub_object_is_addable",
250         preg_match("/c/",$this->ui->get_permissions($dn,"fai/faiPartitionTableEntry")) &&
251         !preg_match("/freeze/",$this->FAIstate));
252     $smarty->assign("sub_object_is_removeable",
253         preg_match("/d/",$this->ui->get_permissions($dn,"fai/faiPartitionTableEntry")) &&
254         !preg_match("/freeze/",$this->FAIstate));
256     $tmp = $this->plInfo();
257     foreach($tmp['plProvidedAcls'] as $name => $translated){
258       $smarty->assign($name."ACL",$this->getacl($name,preg_match("/freeze/i",$this->FAIstate)));
259     }
260     $disks = $this->getDisks();
261     $smarty->assign("disks"   ,$disks);
262     $display.= $smarty->fetch(get_template_path('faiPartitionTable.tpl', TRUE));
263     return($display);
264   }
266   function getDisks(){
267     /* Return all available disks for this partition table
268      * Return in listBox friendly array
269      */
270     $a_return = array();
271     foreach($this->disks as $key => $disk){
273       $dn = "new";
274       if(isset($obj['dn'])){
275         $dn = $obj['dn'];
276       }
277       $dn = $this->acl_base_for_current_object($dn);
278       $acl = $this->ui->get_permissions($dn,"fai/faiPartitionTableEntry");
279       if(preg_match("/(r|w)/",$acl)) {
281         if($disk['status'] != "delete"){
282           $cnt=0;
283           foreach($disk['partitions'] as $val){
284             if($val['status']!="delete"){
285               $cnt ++;
286             }
287           }
288           if(!empty($disk['description'])){
289             if($cnt == 1){
290               $a_return[$key]=  $disk['cn']." [".$disk['description']."], ".sprintf(_("%s partition"), $cnt);
291             }else{
292               $a_return[$key]=  $disk['cn']." [".$disk['description']."], ".sprintf(_("%s partition(s)"), $cnt);
293             }
294           }else{
295             if($cnt == 1){
296               $a_return[$key]=  $disk['cn'].", ".sprintf(_("%s partition"), $cnt);
297             }else{
298               $a_return[$key]=  $disk['cn'].", ".sprintf(_("%s partition(s)"), $cnt);
299             }
300           }
301         }
302       }
303     }
304     return($a_return);
305   }
308   /* Delete me, and all my subtrees
309    */
310   function remove_from_parent()
311   {
312     $ldap = $this->config->get_ldap_link();
313     $ldap->cd ($this->dn);
315     $release = $this->parent->parent->fai_release;
316     $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $release, $this->dn);
318     FAI::prepare_to_save_FAI_object($use_dn,array(),true);
319     new log("remove","fai/".get_class($this),$use_dn,$this->attributes);   
320     foreach($this->disks as $disk){
321       $disk_dn = "cn=".$disk['cn'].",".$this->dn;
322       $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i",$release, $disk_dn);
323       FAI::prepare_to_save_FAI_object($use_dn,array(),true);
324       foreach($disk['partitions'] as $key => $partition){    
325         $partition_dn= "FAIpartitionNr=".$partition['FAIpartitionNr'].",".$disk_dn;      
326         $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $release, $partition_dn);
327         FAI::prepare_to_save_FAI_object($use_dn,array(),true);
328       }
329     }
330   }
333   /* Save data to object 
334    */
335   function save_object()
336   {
337     if (preg_match("/freeze/", $this->FAIstate)) return;
338     plugin::save_object();
339     foreach($this->attributes as $attrs){
340       if(isset($_POST[$attrs])){
341         $this->$attrs = $_POST[$attrs];
342       }
343     }
344   }
347   /* Check supplied data */
348   function check()
349   {
350     /* Call common method to give check the hook */
351     $message= plugin::check();
353     /* Ensure that we do not overwrite an allready existing entry 
354      */
355     if($this->is_new){
356       $release = $this->parent->parent->fai_release;
357       $new_dn= 'cn='.$this->cn.",".get_ou('faipartitionou').get_ou('faiou').$release;
358       $res = faiManagement::check_class_name("FAIpartitionTable",$this->cn,$new_dn);
359       if(isset($res[$this->cn])){
360         $message[] = msgPool::duplicated(_("Name"));
361       }
362     }
363     return ($message);
364   }
367   /* Save to LDAP */
368   function save()
369   {
371     plugin::save();
372     /* Save current settings.
373      * 1 : We must save the partition table, with its description and cn 
374      * 2 : Append Disk with cn and  description.
375      * 3 : Save partitions for each disk
376      */  
378     $ldap = $this->config->get_ldap_link();
380     FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
382     if($this->initially_was_account){
383       new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
384     }else{
385       new log("create","fai/".get_class($this),$this->dn,$this->attributes);
386     }
387  
388     /* Sort entries, because we must delete entries with status="delete" first */
389     $order = array();
390     foreach($this->disks as $key => $disk){
391       if($disk['status'] == "delete"){
392         $order[$key] = $disk;
393       }
394     }
395     foreach($this->disks as $key => $disk){
396       if($disk['status'] != "delete"){
397         $order[$key] = $disk;
398       }
399     }
401     /* Append all disks to ldap */
402     foreach($order as $cn=>$disk){
403       $disk_dn                    = "cn=".$disk['cn'].",".$this->dn;
404       $disk_attrs['cn']           =  $disk['cn'];
405       $disk_attrs['description']  =  $disk['description']; 
406       $disk_attrs['objectClass']  =  array("top","FAIclass","FAIpartitionDisk");
408       if($disk['status']=="new"){
409         $ldap->cat($disk_dn,array("objectClass"));
410         if($ldap->count()){
411           $disk['status']="edited";
412         }
413       }
415       /* Tag object */
416       $this->tag_attrs($disk_attrs, $disk_dn, $this->gosaUnitTag);
418       if($disk['status'] == "delete"){
419         FAI::prepare_to_save_FAI_object($disk_dn,array(),true);
420         $this->handle_post_events("remove");
421       }elseif($disk['status'] == "edited"){
422         FAI::prepare_to_save_FAI_object($disk_dn,$disk_attrs);
423         $this->handle_post_events("modify");
424       }elseif($disk['status']=="new"){
425         FAI::prepare_to_save_FAI_object($disk_dn,$disk_attrs);
426         $this->handle_post_events("add");
427       }
429       if($disk['status']!="delete")
430       /* Add all partitions */
431       foreach($disk['partitions'] as $key => $partition){
432         $partition_attrs = array();
434         foreach($partition as $key => $value){
435           if(!empty($value)){
436             $partition_attrs[$key]=$value;        
437           }else{
438             unset($partition_attrs[$key]);        
439           }
440         }
442         $partition_dn= "FAIpartitionNr=".$partition_attrs['FAIpartitionNr'].",".$disk_dn;      
443         $partition_attrs['objectClass']= array("top","FAIclass","FAIpartitionEntry");
444         $partition_attrs['cn']= $partition_attrs['FAIpartitionNr'];
445         
446         unset($partition_attrs['status']);
447         unset($partition_attrs['old_cn']);
449         if($partition['status']=="new"){
450           $ldap->cat($partition_dn,array("objectClass"));
451           if($ldap->count()){
452             $partition['status']="edited";
453           }
454         }
456         if((!isset($partition['FAImountPoint']))||(empty($partition['FAImountPoint']))){
457           $partition_attrs['FAImountPoint']="swap";
458         }
460         /* Tag object */
461         $this->tag_attrs($partition_attrs, $partition_dn, $this->gosaUnitTag);
463         if($partition['status'] == "delete"){
464           FAI::prepare_to_save_FAI_object($partition_dn,array(),true);
465           $this->handle_post_events("remove");
466         }elseif($partition['status'] == "edited"){
467           FAI::prepare_to_save_FAI_object($partition_dn,$partition_attrs);
468           $this->handle_post_events("modify");
469         }elseif($partition['status']=="new"){
470           FAI::prepare_to_save_FAI_object($partition_dn,$partition_attrs);
471           $this->handle_post_events("add");
472         }
473       }
474     }
475     $this->handle_post_events("add");
476   }
479   function PrepareForCopyPaste($source)
480   {
481     plugin::PrepareForCopyPaste($source);
482     /* Get FAIstate
483      */
484     if(isset($source['FAIstate'][0])){
485       $this->FAIstate = $source['FAIstate'][0];
486     }
488     /* Read all disks from ldap taht are defined fot this partition table 
489      */
490     $ldap = $this->config->get_ldap_link();
491     $ldap->cd ($source['dn']);
492     $ldap->search("(&(objectClass=FAIclass)(objectClass=FAIpartitionDisk))",array("*"));
493     while($object = $ldap->fetch()){
495       /* Skip objects, that are tagged as removed */
496       if(isset($object['FAIstate'][0])){
497         if(preg_match("/removed$/",$object['FAIstate'][0])){
498           continue;
499         }
500       }
502       $this->disks[$object['cn'][0]]['status']      = "edited";
503       $this->disks[$object['cn'][0]]['dn']          = $object['dn'];
504       $this->disks[$object['cn'][0]]['cn']          = $object['cn'][0];
505       if(isset($object['description'][0])){
506         $this->disks[$object['cn'][0]]['description'] = $object['description'][0];
507       }else{
508         $this->disks[$object['cn'][0]]['description'] = "";
509       }
510       $this->disks[$object['cn'][0]]['partitions']   = array();
511     }
513     /* read all partitions for each disk 
514      */
515     foreach($this->disks as $name => $disk){
516       $ldap->cd ($disk['dn']);
517       $ldap->search("(&(objectClass=FAIclass)(objectClass=FAIpartitionEntry))",array("*"));
518       while($partition = $ldap->fetch()){
520         /* Skip objects, that are tagged as removed */
521         if(isset($partition['FAIstate'][0])){
522           if(preg_match("/removed$/",$partition['FAIstate'][0])){
523             continue;
524           }
525         }
527         /* remove count ... from ldap result 
528          */
529         foreach($partition as $key=>$val){
530           if((is_numeric($key))||($key=="count")||($key=="dn")){
531             unset($partition[$key]);
532           }else{
533             $partition[$key] = $val[0];
534           }
535         }
537         /* Append fetched partitions
538          */
539         $partition['status']="edited";
540         $this->disks[$name]['partitions'][$partition['FAIpartitionNr']] = $partition; 
541       }  
542     }
543     ksort($this->disks);
544   }
547   /* Return plugin informations for acl handling */ 
548   static function plInfo()
549   {
550     return (array( 
551           "plShortName" => _("Partition table"),
552           "plDescription" => _("FAI partition table"),
553           "plSelfModify"  => FALSE,
554           "plDepends"     => array(),
555           "plPriority"    => 26,
556           "plSection"     => array("administration"),
557           "plCategory"    => array("fai"),
558           "plProvidedAcls" => array(
559             "cn"                => _("Name")."&nbsp;("._("Read only").")",
560             "description"       => _("Description"))
561           ));
562   }
565   /*! \brief  Used for copy & paste.
566     Returns a HTML input mask, which allows to change the cn of this entry.
567     @param  Array   Array containing current status && a HTML template.
568    */
569   function getCopyDialog()
570   {
571     $vars = array("cn");
572     $smarty = get_smarty();
573     $smarty->assign("cn", htmlentities($this->cn));
574     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
575     $ret = array();
576     $ret['string'] = $str;
577     $ret['status'] = "";
578     return($ret);
579   }
582   /*! \brief  Used for copy & paste.
583     Some entries must be renamed to avaoid duplicate entries.
584    */
585   function saveCopyDialog()
586   {
587     if(isset($_POST['cn'])){
588       $this->cn = get_post('cn');
589     }
590   }
592   /* Reload some attributes */
593   function get_object_attributes($object,$attributes)
594   {
595     $ldap = $this->config->get_ldap_link();
596     $ldap->cd($this->config->current['BASE']);
597     $ldap->cat($object['dn'],$attributes);
598     $tmp  = $ldap->fetch();
600     foreach($attributes as $attrs){
601       if(isset($tmp[$attrs][0])){
602         $var = $tmp[$attrs][0];
604         /* Check if we must decode some attributes */
605         if(in_array_ics($attrs,$this->sub64coded)){
606           $var = base64_decode($var);
607         }
609         /*  check if this is a binary entry */
610         if(in_array_ics($attrs,$this->subBinary)){
611           $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
612         }
614         /* Fix slashes */
615         $var = addslashes($var);
616         $object[$attrs] = $var;
617       }
618     }
619     return($object);
620   }
624 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
625 ?>