Code

Udpated Summary tab
[gosa.git] / plugins / admin / fai / class_faiSummaryTab.inc
1 <?php
2 /*
3    This code is part of GOsa (https://gosa.gonicus.de)
4    Copyright (C) Fabian Hickert 
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
21 class faiSummaryTab extends plugin{
23   var $Classes        = array();
24   var $Release        = "";
25   var $Releases       = array();
26   var $Result         = array();
27   var $objs               = array();
28   var $ObjectList     = "";
29   var $is_dialog      = false;
30   var $InitCalled     = false;
31   var $usedClasses    = array();
32   
33   function faiSummaryTab($config,$dn)
34   {
35     plugin::plugin($config,$dn);
37     /* Base object configuration */
38     $this->objs = array(        
39         "FAIscript"                 => array(   "Image" =>"images/fai_script.png",        "Name"=>_("Scripts")      ,"Tree"     =>"ou=scripts,") ,
40         "FAIhook"                           => array(   "Image" =>"images/fai_hook.png" ,         "Name"=>_("Hooks")        ,"Tree"     =>"ou=hooks,"),
41         "FAIvariable"             => array(     "Image" =>"images/fai_variable.png",      "Name"=>_("Variables")    ,"Tree"     =>"ou=variables,") ,
42         "FAItemplate"             => array(     "Image" =>"images/fai_template.png",      "Name"=>_("Templates")    ,"Tree"     =>"ou=templates,") ,
43         "FAIpartitionTable" => array(   "Image" =>"images/fai_partitionTable.png","Name"=>_("Partition table")  ,"Tree" =>"ou=disk,") ,
44         "FAIpackageList"          => array(     "Image" =>"images/fai_packages.png",      "Name"=>_("Package list") ,"Tree"     =>"ou=packages,") ,
45         "FAIprofile"                => array(   "Image" =>"images/fai_profile.png",       "Name"=>_("Profiles")     ,"Tree"     =>"ou=profiles,"));
46   }
49   /* Init the fai tree with objects.
50      If this method is called, all assigned classes and the release name 
51       will be read from parent object. */
52   function init()
53   {
54     $this->InitCalled = true;
55     $this->Result = array();
56     $_SESSION['faiSummaryTab']['UniqueID'] = 0; 
57     $tmp = $this->getBranches();
58     
59     /* Get classes & release name 
60        There are 2 different tabs which use the summary tab
61        faiProfile / Workstations */
62     
63     /* Check if given parent object is fai profile tabs */
64     if(isset($this->parent->by_name['faiProfile'])){
65       
66       /* Get Classes and release */
67       $this->Classes = $this->parent->by_object['faiProfile']->FAIclasses;
68       $str = preg_replace("/^.*ou=profiles,/","",$this->dn);
69       if(isset($tmp[$str])){
70         $this->Release = $tmp[$str];
71       }
72   
73     /* Check if parent tab is work tab */
74     }elseif(isset($this->parent->by_name['workstartup'])){
75       $this->Classes = $this->parent->by_object['workstartup']->FAIclass;
76       $this->Release = $this->parent->by_object['workstartup']->FAIrelease;
77       $tmp = array_flip($tmp);
78       if(!isset($tmp[$this->Release])) {
79         $this->Release = "";
80       }
81     }
83     /* Set all available releases */
84     $this->Releases = array_flip($this->getBranches());
85     $this->usedClasses = array();
87     /* Check the configuration */
88     if(count($this->Classes) && (!empty($this->Release))){
89       foreach($this->Classes as $class){
90         $this->resolveObject($class);
91       }
92     }
93   }
96   /* Capture posts, and call opnen close tag */
97   function save_object()
98   {
99     $once = true;
100     foreach($_POST as $name => $value ){
102       /* check for open tag request */
103       if(preg_match("/^open_/",$name) && $once){
104         $once  = false;
105         $value = preg_replace("/^open_/","",$name);
106         $value = preg_replace("/_.*$/","",$value); 
107         $this->Result[$value]['Open'] =true  ;
108       }
109       /* Check for close tag request */
110       if(preg_match("/^close_/",$name) && $once){
111         $once  = false;
112         $value = preg_replace("/^close_/","",$name);
113         $value = preg_replace("/_.*$/","",$value); 
114         unset($this->Result[$value]['Open']);
115       }
116       if(preg_match("/^reloadList/",$name)){
117         $this->InitCalled = false;
118       }
119     }
120   }
123   /* returns an array with all possibly release types */
124   function getBranches($base = false,$prefix = "")
125   {
126     $ret = array("main"=>"/");
127     $ldap = $this->config->get_ldap_link();
128     $ldap->cd($this->config->current['BASE']);
129     if(!$base){
130       $base= "ou=fai,ou=configs,ou=systems,".$_SESSION['CurrentMainBase'];
131     }
132     $ldap->cd($base);
133     $ldap->ls("(objectClass=FAIbranch)",$base);
134     while($attrs = $ldap->fetch()){
135       if((isset($attrs['ou']))&&($attrs['dn']!=$base)){
136         $ret[$attrs['dn']]= $prefix.$attrs['ou'][0];
137         $ret = array_merge($ret,$this->getBranches($attrs['dn'],"".$prefix.$attrs['ou'][0]."/"));
138       }
139     }
140     return($ret);
141   }
144   /* This function calls all nescessary functions to generate the fai class summary tree */
145   function execute() 
146   {
147     /* Check if we must (re)init the this tab */
148     if(!$this->InitCalled){
149       $this->init();
150     }
152     /* Get smarty class & assign created summary results */
153     $smarty = get_smarty();
154     $this->ObjectList = $this->createSummary($this->Result);
155     $smarty->assign("objectList",$this->ObjectList);
156     return($smarty->fetch (get_template_path('faiSummary.tpl', TRUE, dirname(__FILE__))));
157   }
160   /* Create output of from resolved fai objects */
161   function createSummary($data)
162   {
163     /* Don't generate any output, if there is no result */ 
164     $str ="<table style='width:100%;background:#F5F5F5;'> ";
165     if(!count($this->Result)){
166       $str.="<tr><td>"; 
167       $str.= (_("This object has no FAI classes assigned."));
168       $str.= "</tr></td></table>";
169       return($str);
170     }
172     /* walk through all classes */      
173     foreach($data as $key => $entry){
174       
175       $image = "<img src='".$this->objs[$key]['Image']."' class='center' alt='".$this->objs[$key]['Name']."' border=0>";
177       $str.="<tr><td>";
178       if(!isset($entry['Open'])){
179         $str .= "<input type='image' class='center' src='images/expand.png' name='open_".$key."'>&nbsp;";
180         $str .= $image."&nbsp;";
181         $str .= "<b>".$this->objs[$key]['Name']."</b>";
182       }else{
183         $str .= "<input type='image' class='center' src='images/sort_down.png' name='close_".$key."'>&nbsp;";
184         $str .= $image."&nbsp;";
185         $str .= "<b>".$this->objs[$key]['Name']."</b>";
186    
187         /* Display FAItemplate FAIhook FAIscript entries */ 
188         if(in_array($key,array("FAItemplate","FAIhook","FAIscript"))){
190           if($key == "FAIscript"){
191             krsort($entry['Entries']);
192             foreach($entry['Entries'] as $scripts){
193               ksort($scripts);
194               foreach($scripts as $script){
195                 $script['cn'][0] = "(".$script['FAIpriority'][0].") ".$script['cn'][0];
196                 $tmp[$script['cn'][0]] = $script;
197               }
198             }
199             $entry['Entries'] = $tmp;
200           }
202           foreach($entry['Entries'] as $cn => $data){
203             $str .="<td></tr><tr><td style='padding-left:20px;'>";
204             $str .= "<a href='?plug=".$_GET['plug']."&show".$key."&id=".base64_encode($data['dn'])."'>";
205             $str .= $image."&nbsp;".$data['cn'][0];
206             if(isset($data['description'][0])){
207               $str .= " [".$data['description'][0]."]";
208             }
209             $str .="</a>";
210           }
211         }
213         /* Create variable entries */
214         if(in_array($key,array("FAIvariable"))) {
215           foreach($entry['Entries'] as $cn => $data){
216             $str .="<td></tr><tr><td style='padding-left:20px;'>";
217             $str .= $image."&nbsp;".$data['cn'][0];
218             if(isset($data['description'][0])){
219               $str .= " [".$data['description'][0]."] ";
220             }
221             $str .=" = '".$data['FAIvariableContent'][0]."'";
222           }
223         }
225         /* Create packagelist entries */ 
226         if(in_array($key,array("FAIpackageList"))) {
227           ksort($entry['Entries']);
228           foreach($entry['Entries'] as $cn => $data){
229             $str .="<td></tr><tr><td style='padding-left:20px;'>";
230             $str .= $image."&nbsp;".$data;
231           }
232         }
234         /* Create partition table entries */
235         if(in_array($key,array("FAIpartitionTable"))) {
236           foreach($entry['Entries'] as $cn => $data){
237             $str .="<td></tr><tr><td style='padding-left:20px;'>";
238             $str .= $image."&nbsp;".$data['name'];
239             ksort($data['partitions']);
240             $str .= "<table cellspacing=1 style='background:#FFFFFF;'>
241                       <tr style='background-color:#BBBBBB;'>
242                        <td>"._("No.")."</td>
243                        <td>"._("Name")."</td>
244                        <td>"._("FS options")."</td>
245                        <td>"._("Mount options")."</td>
246                        <td>"._("Size in MB")."</td>
247                        <td>"._("Mount point")."</td>
248                        <td>"._("Type")."</td>
249                       </tr>";
251             $i = 0;
252             foreach($data['partitions'] as $key => $part){
253               $i ++;
255               if($i%2 == 1){
256                 $c = " class='rowxp1'";
257                 $d = " class='list1' style='border:0px;'";
258               }else{
259                 $c = " class='rowxp0'";
260                 $d = " class='list0' style='border:0px;'";
261               }
263               $str.="<tr $c style='height:10px;'>
264                       <td $d>".$i."</td> 
265                       <td $d>".$part['cn'][0]."</td> 
266                       <td $d>".$part['FAIfsType'][0]."</td> 
267                       <td $d>".$part['FAIfsOptions'][0]."</td> 
268                       <td $d>".$part['FAIpartitionSize'][0]."</td> 
269                       <td $d>".$part['FAImountPoint'][0]."</td> 
270                       <td $d>".$part['FAIpartitionType'][0]."</td> 
271                     </tr>";
272             }
273             $str .="</table>";
274           }
275         }
276       }
277       $str.="</td></tr>"  ;
278     }
279     $str .="</table>";  
280     return($str);
281   }
284   /* resolve specified object to append it to our object tree */
285   function resolveObject($class)
286   {
287     $ldap       = $this->config->get_ldap_link();
288     $dn           = $this->Releases [$this->Release];
289     $res    = array();
290  
291     if(in_array($class,$this->usedClasses)) return;
292   
293     $this->usedClasses[] = $class;
294  
295     /* Try to fetch all types of fai objects with the given cn  */
296     foreach($this->objs as $key =>  $obj){
297       
298       /* Check for objects with cn=$class 
299           and add them to our list */
300       $ldap->ls("(&(objectClass=".$key.")(cn=".$class."))",$obj['Tree'].$dn,array("*"));
301       while($attrs = $ldap->fetch()){
302         switch($key){
303           case "FAIprofile":                    $this->prepare_FAIprofile($attrs);break;
304           case "FAIscript":                     $this->prepare_FAIscript($attrs);break;
305           case "FAIhook":                         $this->prepare_FAIhook($attrs);break;
306           case "FAIvariable":                   $this->prepare_FAIvariable($attrs);break;
307           case "FAItemplate":                   $this->prepare_FAItemplate($attrs);break;
308           case "FAIpackageList":              $this->prepare_FAIpackageList($attrs);break;
309           case "FAIpartitionTable":         $this->prepare_FAIpartitionTable($attrs);break;
310         }
311       }
312     }
313     return($res);
314   }
317   /* Prepare fai script */
318   function prepare_FAIscript($data)
319   {
320     if(isset($this->Result['FAIscript']['Entries'])){
321       $current = $this->Result['FAIscript']['Entries'];
322     }else{
323       $current = array();
324     }
325     $ldap = $this->config->get_ldap_link();
326     $ldap->cd($data['dn']);
327     $ldap->search("(objectClass=FAIscriptEntry)",array("cn","description","FAIpriority"));
328     while($attrs = $ldap->fetch()){
329       $current[$attrs['FAIpriority'][0]][$attrs['cn'][0]] = $attrs;
330     }
331     $this->Result['FAIscript']['Entries'] = $current ;
332   }
335   /* Prepare fai script */
336   function prepare_FAIpartitionTable($data)
337   {
338     if(isset($this->Result['FAIpartitionTable']['Entries'])){
339       $current = $this->Result['FAIpartitionTable']['Entries'];
340     }else{
341       $current = array();
342     }
344     /* get subentries */
345     $ldap = $this->config->get_ldap_link();
346     $ldap->cd($data['dn']);
347     $ldap->search("(objectClass=FAIpartitionDisk)",array("cn","description"));
348     $disks = array();
350     /* Create an array which is sortable by priority */
351     while($attrs = $ldap->fetch()){
352       if(isset($attrs['description'][0])){
353         $disks[$attrs['cn'][0]]['name'] = $attrs['cn'][0]." [".$attrs['description'][0]."]";
354       }else{
355         $disks[$attrs['cn'][0]]['name'] = $attrs['cn'][0];
356       }
357       $disks[$attrs['cn'][0]]['dn'] = $attrs['dn'];
358       $disks[$attrs['cn'][0]]['partitions'] = array();
359     }
361     /* Sort by priority */
362     foreach($disks as $key => $disk){
363       $ldap->cd($disk['dn']);
364       $ldap->search("(objectClass=FAIpartitionEntry)",array("*"));
365       while($attrs = $ldap->fetch()){
366         if(!isset($attrs['FAIfsOptions'][0])){
367           $attrs['FAIfsOptions'][0] = "";
368         }
369         $disks[$key]['partitions'][$attrs['cn'][0]] = $attrs;
370       }
371     }
372     $this->Result['FAIpartitionTable']['Entries'] = $disks;
373   }
376   /* Create template entry */   
377   function prepare_FAItemplate($data)
378   {
379     $current = array();
380     if(isset($this->Result['FAItemplate']['Entries'])){
381       $current = $this->Result['FAItemplate']['Entries'];
382     }
384     $ldap = $this->config->get_ldap_link();
385     $ldap->cd($data['dn']);
386     $ldap->search("(objectClass=FAItemplateEntry)",array("cn","description"));
387     while($attrs = $ldap->fetch()){
388       $current[$attrs['cn'][0]] = $attrs;
389     }
390     $this->Result['FAItemplate']['Entries'] = $current ;
391   }
394   /* Create a package list  */  
395   function prepare_FAIpackageList($data)
396   {
397     $current = array();
398     if(isset($this->Result['FAIpackageList']['Entries'])){
399       $current = $this->Result['FAIpackageList']['Entries'];
400     }
401     if(isset($data['FAIpackage'])){
402       unset($data['FAIpackage']['count']);
403       foreach($data['FAIpackage'] as $pkg){
404         $current[$pkg] = $pkg;
405       }
406     }
407     $this->Result['FAIpackageList']['Entries'] = $current ;
408   }
411   /* Create a variable entry */ 
412   function prepare_FAIvariable($data)
413   {
414     $current = array();
415     if(isset($this->Result['FAIvariable']['Entries'])){
416       $current = $this->Result['FAIvariable']['Entries'];
417     }
418     $ldap = $this->config->get_ldap_link();
419     $ldap->cd($data['dn']);
420     $ldap->search("(objectClass=FAIvariableEntry)",array("cn","description","FAIvariableContent"));
421     while($attrs = $ldap->fetch()){
422       $current[$attrs['cn'][0]] = $attrs;
423     }
424     $this->Result['FAIvariable']['Entries'] = $current ;
425   }
427   /* Create a hook entry */     
428   function prepare_FAIhook($data)
429   {
430     $current = array();
431     if(isset($this->Result['FAIhook']['Entries'])){
432       $current = $this->Result['FAIhook']['Entries'];
433     }
434     $ldap = $this->config->get_ldap_link();
435     $ldap->cd($data['dn']);
436     $ldap->search("(objectClass=FAIhookEntry)",array("cn","description"));
437     while($attrs = $ldap->fetch()){
438       $current[$attrs['cn'][0]] = $attrs;
439     }
440     $this->Result['FAIhook']['Entries'] = $current ;
441   }
444   /* Create a new Profile entry */
445   function prepare_FAIprofile($data)
446   {
447     $classes = split("\ ",$data['FAIclass'][0]);
448     foreach($classes as $class){
449       $class = trim($class);
450       $this->resolveObject($class);
451     }
452   }
454 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
455 ?>