Code

Avoid initial reset of ocMapping. It is array() anyway.
[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 = array_flip($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   
70     /* Check if parent tab is work tab */
71     }elseif(isset($this->parent->by_name['workstartup'])){
72       $this->Classes = $this->parent->by_object['workstartup']->FAIclass;
73       $this->Release = $this->parent->by_object['workstartup']->FAIrelease;
75       /* Append workstation class && LAST */
76       if(isset($this->parent->by_object['workgeneric']->cn)){
77         $this->Classes[] = $this->parent->by_object['workgeneric']->cn;
78       }
79     }
81     $this->Classes[] = "LAST";
83     /* Set all available releases */
84     $this->Releases = $tmp;
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;
101     if(isset($_GET['expand']) && isset($_GET['id'])){
102       if(isset($this->Result[$_GET['id']])){
103         $this->Result[$_GET['id']]['Open'] =true  ;
104       }
105     }
107     if(isset($_GET['compress']) && isset($_GET['id'])){
108       if(isset($this->Result[$_GET['id']])){
109         unset($this->Result[$_GET['id']]['Open']);
110       }
111     }
113     foreach($_POST as $name => $value ){
115       /* check for open tag request */
116       if(preg_match("/^open_/",$name) && $once){
117         $once  = false;
118         $value = preg_replace("/^open_/","",$name);
119         $value = preg_replace("/_.*$/","",$value); 
120         $this->Result[$value]['Open'] =true  ;
121       }
122       /* Check for close tag request */
123       if(preg_match("/^close_/",$name) && $once){
124         $once  = false;
125         $value = preg_replace("/^close_/","",$name);
126         $value = preg_replace("/_.*$/","",$value); 
127         unset($this->Result[$value]['Open']);
128       }
129       if(preg_match("/^reloadList/",$name)){
130         $this->InitCalled = false;
131       }
132     }
133   }
136   /* returns an array with all possibly release types */
137   function getBranches($base = false,$prefix = "")
138   {
139     if(!$base){
140       $base = $this->config->current['BASE'];
141     }
142     return(get_all_releases_from_base($base,true));
143   }
146   /* This function calls all nescessary functions to generate the fai class summary tree */
147   function execute() 
148   {
149     /* Check if we must (re)init the this tab */
150     if(!$this->InitCalled){
151       $this->init();
152     }
154     if(isset($_GET['show'])){
155       $ldap = $this->config->get_ldap_link();
156       $ldap->cat(base64_decode($_GET['id']), array('FAIscript', 'FAIhook', 'FAItemplate'));
157       $attrs = $ldap->fetch();
158       if($_GET['type'] == "FAIscript"){
159         $str = $attrs['FAIscript'][0];
160       }elseif($_GET['type'] == "FAIhook"){
161         $str = $attrs['FAIscript'][0];
162       }elseif($_GET['type'] == "FAItemplate"){
163         $str = $attrs['FAItemplateFile'][0];
164       }
165       echo nl2br($str);
166       exit();
167     }
169     /* Get smarty class & assign created summary results */
170     $smarty = get_smarty();
171     $this->ObjectList = $this->createSummary($this->Result);
172     $smarty->assign("objectList",$this->ObjectList);
173     return($smarty->fetch (get_template_path('faiSummary.tpl', TRUE, dirname(__FILE__))));
174   }
177   /* Create output of from resolved fai objects */
178   function createSummary($data)
179   {
180     /* Don't generate any output, if there is no result */ 
181     $str ="<table cellspacing=0 cellpadding=2 style='width:100%;background:#F5F5F5;'> ";
182     if(!count($this->Result)){
183       $str.="<tr><td>"; 
184       $str.= (_("This object has no FAI classes assigned."));
185       $str.= "</tr></td></table>";
186       return($str);
187     }
189     /* walk through all classes */      
190     foreach($data as $key => $entry){
191       
192       $image = "<img src='".$this->objs[$key]['Image']."' class='center' alt='".$this->objs[$key]['Name']."' border=0>";
193       if(!isset($entry['Open'])){
194         $str .= "<tr><td colspan=2>
195                     <a href='?plug=".$_GET['plug']."&expand&id=".$key."'>
196                   <img border=0 class='center' src='images/expand.png' alt='"._("Open")."'>&nbsp;";
197         $str .= $image."&nbsp;";
198         $str .= "<b>".$this->objs[$key]['Name']."</b></b></td></tr>";
199       }else{
200         $str .= "<tr><td colspan=2>
201                     <a href='?plug=".$_GET['plug']."&compress&id=".$key."'>
202                    <img border=0 class='center' src='images/sort_down.png' alt='"._("Close")."'>&nbsp;";
203         $str .= $image."&nbsp;";
204         $str .= "<b>".$this->objs[$key]['Name']."</b></a></td></tr>";
205    
206         /* Display FAItemplate FAIhook FAIscript entries */ 
207         if(in_array($key,array("FAItemplate","FAIhook","FAIscript"))){
209           $nums = array();          
211           if($key == "FAIscript"){
212             foreach($entry['Entries'] as $scripts){
213               foreach($scripts as $script){
214                 
215                 if(!isset($nums[$script['cn'][0]])){
216                   $nums[$script['cn'][0]]= 0;
217                 } 
218                 $nums[$script['cn'][0]] ++;
219             
220                 $tmp[$script['FAIpriority'][0].$script['cn'][0].$script['CLASS']] = $script;
221               }
222             }
223             krsort($tmp);;
224             
225             $entry['Entries'] = $tmp;
226           }else{
227             $tmp = array();
228             foreach($entry['Entries'] as $script){
229               $tmp[$script['cn'][0].$script['CLASS']] = $script;
230               if(!isset($nums[$script['cn'][0]])){
231                 $nums[$script['cn'][0]]= 0;
232               } 
233               $nums[$script['cn'][0]] ++;
234             }
235              ksort($tmp);
236             $entry['Entries'] = $tmp;
237           }
239           foreach($entry['Entries'] as $cn => $data){
241             if(isset($nums[$data['cn'][0]]) && ($nums[$data['cn'][0]] > 1)){
242               $str .="<tr style='background: #EE3434;'><td style='padding-left:20px;'>";
243             }else{
244               $str .="<tr><td style='padding-left:20px;'>";
245             }
247             $str .= "<a target='_blank' href='?plug=".$_GET['plug']."&show&type=".$key."&id=".base64_encode($data['dn'])."'>";
248             $str .= $image."&nbsp;";
249             if(isset($data['FAIpriority'][0])){
250               $str .= "(".$data['FAIpriority'][0].")";
251             }
253             $str .= "&nbsp;".$data['cn'][0];
254             if(isset($data['description'][0])){
255               $str .= " [".$data['description'][0]."]";
256             }
257             $str .= "</a>";
258             $str .= "</td><td><i>".$data['CLASS']."</i>";
259             $str .= "</td></tr>";
260           }
261         }
263         /* Create variable entries */
264         if(in_array($key,array("FAIvariable"))) {
265           foreach($entry['Entries'] as $cn => $data){
266             $str .="<tr><td style='padding-left:20px;'>";
267             $str .= $image."&nbsp;".$data['cn'][0];
268             if(isset($data['description'][0])){
269               $str .= " [".$data['description'][0]."] ";
270             }
271             /* Only display FAIvariableContent if description dosn't contain [*] */ 
272             if(isset($data['description'][0]) && !preg_match("#\[\*\]#",$data['description'][0])){
273               $str .=" = '".$data['FAIvariableContent'][0]."'";
274             }
275             $str .= "</td><td><i>".$data['CLASS']."</i>";
276             $str .="</td></tr>";
277           }
278         }
280         /* Create packagelist entries */ 
281         if(in_array($key,array("FAIpackageList"))) {
282           ksort($entry['Entries']);
283           foreach($entry['Entries'] as $data){
284             $str .="<tr><td style='padding-left:20px;'>";
285             $str .= $image."&nbsp;".$data['cn'][0];
286             $str .= "</td><td><i>".$data['CLASS']."</i>";
287             $str .= "</td></tr>";;
288           }
289         }
291         /* Create partition table entries */
292         if(in_array($key,array("FAIpartitionTable"))) {
293           foreach($entry['Entries'] as $cn => $data){
294             $str .= "<tr><td style='padding-left:20px;'>";
295             $str .= $image."&nbsp;".$data['name'];
296             $str .= "</td><td><i>".$data['CLASS']."</i>";
297             $str .= "</td></tr>";
298             ksort($data['partitions']);
300             $str .= "<tr><td colspan=2  style='padding-left:20px;'>";
301             $str .= "<table cellspacing=0 cellpadding=2 style='background:#FFFFFF;border:1px solid #B0B0B0'>
302                       <tr style='background-color:#DBDBDB;'>
303                        <td class='tbhead'>"._("No.")."</td>
304                        <td class='tbhead'>"._("Name")."</td>
305                        <td class='tbhead'>"._("FS options")."</td>
306                        <td class='tbhead'>"._("Mount options")."</td>
307                        <td class='tbhead'>"._("Size in MB")."</td>
308                        <td class='tbhead'>"._("Mount point")."</td>
309                        <td class='tbrhead'>"._("Type")."</td>
310                       </tr>";
312             $i = 0;
313             foreach($data['partitions'] as $key => $part){
314               $i ++;
316               if($i%2 == 1){
317                 $c = " class='rowxp1'";
318                 $d = " class='list1'";
319               }else{
320                 $c = " class='rowxp0'";
321                 $d = " class='list0'";
322               }
324               $str.="<tr $c style='height:10px;'>
325                       <td $d>".$i."</td> 
326                       <td $d>".$part['cn'][0]."</td> 
327                       <td $d>".$part['FAIfsType'][0]."</td> 
328                       <td $d>".$part['FAIfsOptions'][0]."</td> 
329                       <td $d>".$part['FAIpartitionSize'][0]."</td> 
330                       <td $d>".$part['FAImountPoint'][0]."</td> 
331                       <td $d style='border:none'>".$part['FAIpartitionType'][0]."</td> 
332                     </tr>";
333             }
334             $str .="</table>";
335           }
336         }
337       }
338     }
339     $str .="</table>";  
340     return($str);
341   }
344   /* resolve specified object to append it to our object tree */
345   function resolveObject($class)
346   {
347     $ldap                 = $this->config->get_ldap_link();
348     $dn                     = $this->Releases [$this->Release];
349     $resolvedClasses  = get_all_objects_for_given_base($dn,"(&(objectClass=FAIclass)(cn=".$class."))");
351     /* Try to fetch all types of fai objects with the given cn  */
352     foreach($resolvedClasses as $obj){
354       $dn = $obj['dn'];
355       $ldap->cat($dn);
357       while($attrs = $ldap->fetch()){
359         foreach($this-> objs as $key => $rest){
360           if(in_array($key,$attrs['objectClass'])){
362             if(!isset($this->usedClasses[$key][$class])){
363               $this->usedClasses[$key][$class] = true;
364               switch($key){
365                 case "FAIprofile":                      $this->prepare_FAIprofile($attrs);break;
366                 case "FAIscript":                       $this->prepare_FAIscript($attrs);break;
367                 case "FAIhook":                           $this->prepare_FAIhook($attrs);break;
368                 case "FAIvariable":                     $this->prepare_FAIvariable($attrs);break;
369                 case "FAItemplate":                     $this->prepare_FAItemplate($attrs);break;
370                 case "FAIpackageList":        $this->prepare_FAIpackageList($attrs);break;
371                 case "FAIpartitionTable":           $this->prepare_FAIpartitionTable($attrs);break;
372               }
373             }
375           }
376         }
377       }
378     }
379   }
381   /* Prepare fai script */
382   function prepare_FAIscript($data)
383   {
384     if(isset($this->Result['FAIscript']['Entries'])){
385       $current = $this->Result['FAIscript']['Entries'];
386     }else{
387       $current = array();
388     }
389     $ldap = $this->config->get_ldap_link();
390     $ldap->cd($data['dn']);
391     $ldap->search("(objectClass=FAIscriptEntry)",array("cn","description","FAIpriority"));
392     while($attrs = $ldap->fetch()){
393       $attrs['CLASS'] = $data['cn'][0];
394       $current[$attrs['FAIpriority'][0]][] = $attrs;
395     }
396     $this->Result['FAIscript']['Entries'] = $current ;
397   }
400   /* Prepare fai script */
401   function prepare_FAIpartitionTable($data)
402   {
403     if(isset($this->Result['FAIpartitionTable']['Entries'])){
404       $current = $this->Result['FAIpartitionTable']['Entries'];
405     }else{
406       $current = array();
407     }
409     /* get subentries */
410     $ldap = $this->config->get_ldap_link();
411     $ldap->cd($data['dn']);
412     $ldap->search("(objectClass=FAIpartitionDisk)",array("cn","description"));
413     $disks = array();
415     /* Create an array which is sortable by priority */
416     while($attrs = $ldap->fetch()){
417       $attrs['CLASS'] = $data['cn'][0];
418       $disks[$attrs['cn'][0]]= $attrs;
419       if(isset($attrs['description'][0])){
420         $disks[$attrs['cn'][0]]['name'] = $attrs['cn'][0]." [".$attrs['description'][0]."]";
421       }else{
422         $disks[$attrs['cn'][0]]['name'] = $attrs['cn'][0];
423       }
424       $disks[$attrs['cn'][0]]['dn'] = $attrs['dn'];
425       $disks[$attrs['cn'][0]]['partitions'] = array();
426     }
428     /* Sort by priority */
429     foreach($disks as $key => $disk){
430       $ldap->cd($disk['dn']);
431       $ldap->search("(objectClass=FAIpartitionEntry)",array("*"));
432       while($attrs = $ldap->fetch()){
433         if(!isset($attrs['FAIfsOptions'][0])){
434           $attrs['FAIfsOptions'][0] = "";
435         }
436         $attrs['CLASS'] = $data['cn'][0];
437         $disks[$key]['partitions'][$attrs['cn'][0]] = $attrs;
438       }
439     }
440     $this->Result['FAIpartitionTable']['Entries'] = $disks;
441   }
444   /* Create template entry */   
445   function prepare_FAItemplate($data)
446   {
447     $current = array();
448     if(isset($this->Result['FAItemplate']['Entries'])){
449       $current = $this->Result['FAItemplate']['Entries'];
450     }
452     $ldap = $this->config->get_ldap_link();
453     $ldap->cd($data['dn']);
454     $ldap->search("(objectClass=FAItemplateEntry)",array("cn","description"));
455     while($attrs = $ldap->fetch()){
456       $attrs['CLASS'] = $data['cn'][0];
457       $current[] = $attrs;
458     }
459     $this->Result['FAItemplate']['Entries'] = $current ;
460   }
463   /* Create a package list  */  
464   function prepare_FAIpackageList($data)
465   {
466     $current = array();
467     if(isset($this->Result['FAIpackageList']['Entries'])){
468       $current = $this->Result['FAIpackageList']['Entries'];
469     }
470     if(isset($data['FAIpackage'])){
471       unset($data['FAIpackage']['count']);
472       foreach($data['FAIpackage'] as $pkg){
473         $attrs['CLASS'] = $data['cn'][0];
474         $attrs['cn'][0] = $pkg;
475         $current[$pkg] = $attrs;
476       }
477     }
478     $this->Result['FAIpackageList']['Entries'] = $current ;
479   }
482   /* Create a variable entry */ 
483   function prepare_FAIvariable($data)
484   {
485     $current = array();
486     if(isset($this->Result['FAIvariable']['Entries'])){
487       $current = $this->Result['FAIvariable']['Entries'];
488     }
489     $ldap = $this->config->get_ldap_link();
490     $ldap->cd($data['dn']);
491     $ldap->search("(objectClass=FAIvariableEntry)",array("cn","description","FAIvariableContent"));
492     while($attrs = $ldap->fetch()){
493       $attrs['CLASS'] = $data['cn'][0];
494       $current[] = $attrs;
495     }
496     $this->Result['FAIvariable']['Entries'] = $current ;
497   }
499   /* Create a hook entry */     
500   function prepare_FAIhook($data)
501   {
502     $current = array();
503     if(isset($this->Result['FAIhook']['Entries'])){
504       $current = $this->Result['FAIhook']['Entries'];
505     }
506     $ldap = $this->config->get_ldap_link();
507     $ldap->cd($data['dn']);
508     $ldap->search("(objectClass=FAIhookEntry)",array("cn","description"));
509     while($attrs = $ldap->fetch()){
510       $attrs['CLASS'] = $data['cn'][0];
511       $current[$attrs['cn'][0]] = $attrs;
512     }
513     $this->Result['FAIhook']['Entries'] = $current ;
514   }
517   /* Create a new Profile entry */
518   function prepare_FAIprofile($data)
519   {
520     $classes = split("\ ",$data['FAIclass'][0]);
521     foreach($classes as $class){
522       $class = trim($class);
523       $this->resolveObject($class);
524     }
525   }
527 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
528 ?>