Code

Added printer patch
[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  
60     /* Get classes & release name 
61        There are 2 different tabs which use the summary tab
62        faiProfile / Workstations */
63     
64     /* Check if given parent object is fai profile tabs */
65     if(isset($this->parent->by_name['faiProfile'])){
66       
67       /* Get Classes and release */
68       $this->Classes = $this->parent->by_object['faiProfile']->FAIclasses;
69       $str = preg_replace("/^.*ou=profiles,/","",$this->dn);
71       if(isset($tmp[$str])){
72         $this->Release = $tmp[$str];
73       }
74   
75     /* Check if parent tab is work tab */
76     }elseif(isset($this->parent->by_name['workstartup'])){
77       $this->Classes = $this->parent->by_object['workstartup']->FAIclass;
78       $this->Release = $this->parent->by_object['workstartup']->FAIrelease;
80       /* Append workstation class && LAST */
81       if(isset($this->parent->by_object['workgeneric']->cn)){
82         $this->Classes[] = $this->parent->by_object['workgeneric']->cn;
83       }
84  
85       $tmp = array_flip($tmp);
86       if(!isset($tmp[$this->Release])) {
87         $this->Release = "";
88       }
89     }
91     $this->Classes[] = "LAST";
93     /* Set all available releases */
94     $this->Releases = array_flip($this->getBranches());
95     $this->usedClasses = array();
97     /* Check the configuration */
98     if(count($this->Classes) && (!empty($this->Release))){
99       foreach($this->Classes as $class){
100         $this->resolveObject($class);
101       }
102     }
103   }
106   /* Capture posts, and call opnen close tag */
107   function save_object()
108   {
109     $once = true;
111     if(isset($_GET['expand']) && isset($_GET['id'])){
112       if(isset($this->Result[$_GET['id']])){
113         $this->Result[$_GET['id']]['Open'] =true  ;
114       }
115     }
117     if(isset($_GET['compress']) && isset($_GET['id'])){
118       if(isset($this->Result[$_GET['id']])){
119         unset($this->Result[$_GET['id']]['Open']);
120       }
121     }
123     foreach($_POST as $name => $value ){
125       /* check for open tag request */
126       if(preg_match("/^open_/",$name) && $once){
127         $once  = false;
128         $value = preg_replace("/^open_/","",$name);
129         $value = preg_replace("/_.*$/","",$value); 
130         $this->Result[$value]['Open'] =true  ;
131       }
132       /* Check for close tag request */
133       if(preg_match("/^close_/",$name) && $once){
134         $once  = false;
135         $value = preg_replace("/^close_/","",$name);
136         $value = preg_replace("/_.*$/","",$value); 
137         unset($this->Result[$value]['Open']);
138       }
139       if(preg_match("/^reloadList/",$name)){
140         $this->InitCalled = false;
141       }
142     }
143   }
146   function getBranches()
147   {
148     $dn   = $this->config->current['BASE'];
149     $ldap = $this->config->get_ldap_link();
150     $ldap->cd($dn);
151     $ldap->search("(&(objectClass=organizationalUnit)(objectClass=FAIbranch))",array("ou","cn","dn"));
152     $ret = array("main"=>"/");
153     while($attrs = $ldap->fetch()){
154       if(preg_match("/ou=fai,/",$attrs['dn'])){
155         $name = convert_department_dn(preg_replace("/,ou=fai,.*$/","",$attrs['dn']));
156         $ret[$attrs['dn']] = $name;
157       }
158     }
159     return($ret);
160   }
163   /* This function calls all nescessary functions to generate the fai class summary tree */
164   function execute() 
165   {
166     /* Check if we must (re)init the this tab */
167     if(!$this->InitCalled){
168       $this->init();
169     }
171     if(isset($_GET['show'])){
172       $ldap = $this->config->get_ldap_link();
173       $ldap->cat(base64_decode($_GET['id']), array('FAIscript', 'FAIhook', 'FAItemplate'));
174       $attrs = $ldap->fetch();
175       if($_GET['type'] == "FAIscript"){
176         $str = $attrs['FAIscript'][0];
177       }elseif($_GET['type'] == "FAIhook"){
178         $str = $attrs['FAIscript'][0];
179       }elseif($_GET['type'] == "FAItemplate"){
180         $str = $attrs['FAItemplateFile'][0];
181       }
182       echo nl2br($str);
183       exit();
184     }
186     /* Get smarty class & assign created summary results */
187     $smarty = get_smarty();
188     $this->ObjectList = $this->createSummary($this->Result);
189     $smarty->assign("objectList",$this->ObjectList);
190     return($smarty->fetch (get_template_path('faiSummary.tpl', TRUE, dirname(__FILE__))));
191   }
194   /* Create output of from resolved fai objects */
195   function createSummary($data)
196   {
197     /* Don't generate any output, if there is no result */ 
198     $str ="<table cellspacing=0 cellpadding=2 style='width:100%;background:#F5F5F5;'> ";
199     if(!count($this->Result)){
200       $str.="<tr><td>"; 
201       $str.= (_("This object has no FAI classes assigned."));
202       $str.= "</tr></td></table>";
203       return($str);
204     }
206     /* walk through all classes */      
207     foreach($data as $key => $entry){
208       
209       $image = "<img src='".$this->objs[$key]['Image']."' class='center' alt='".$this->objs[$key]['Name']."' border=0>";
210       if(!isset($entry['Open'])){
211         $str .= "<tr><td colspan=2>
212                     <a href='?plug=".$_GET['plug']."&expand&id=".$key."'>
213                   <img border=0 class='center' src='images/expand.png' alt='"._("Open")."'>&nbsp;";
214         $str .= $image."&nbsp;";
215         $str .= "<b>".$this->objs[$key]['Name']."</b></b></td></tr>";
216       }else{
217         $str .= "<tr><td colspan=2>
218                     <a href='?plug=".$_GET['plug']."&compress&id=".$key."'>
219                    <img border=0 class='center' src='images/sort_down.png' alt='"._("Close")."'>&nbsp;";
220         $str .= $image."&nbsp;";
221         $str .= "<b>".$this->objs[$key]['Name']."</b></a></td></tr>";
222    
223         /* Display FAItemplate FAIhook FAIscript entries */ 
224         if(in_array($key,array("FAItemplate","FAIhook","FAIscript"))){
226           $nums = array();          
228           if($key == "FAIscript"){
229             foreach($entry['Entries'] as $scripts){
230               foreach($scripts as $script){
231                 
232                 if(!isset($nums[$script['cn'][0]])){
233                   $nums[$script['cn'][0]]= 0;
234                 } 
235                 $nums[$script['cn'][0]] ++;
236             
237                 $tmp[$script['FAIpriority'][0].$script['cn'][0].$script['CLASS']] = $script;
238               }
239             }
240             krsort($tmp);;
241             
242             $entry['Entries'] = $tmp;
243           }else{
244             $tmp = array();
245             foreach($entry['Entries'] as $script){
246               $tmp[$script['cn'][0].$script['CLASS']] = $script;
247               if(!isset($nums[$script['cn'][0]])){
248                 $nums[$script['cn'][0]]= 0;
249               } 
250               $nums[$script['cn'][0]] ++;
251             }
252              ksort($tmp);
253             $entry['Entries'] = $tmp;
254           }
256           foreach($entry['Entries'] as $cn => $data){
258             if(isset($nums[$data['cn'][0]]) && ($nums[$data['cn'][0]] > 1)){
259               $str .="<tr style='background: #EE3434;'><td style='padding-left:20px;'>";
260             }else{
261               $str .="<tr><td style='padding-left:20px;'>";
262             }
264             $str .= "<a target='_blank' href='?plug=".$_GET['plug']."&show&type=".$key."&id=".base64_encode($data['dn'])."'>";
265             $str .= $image."&nbsp;";
266             if(isset($data['FAIpriority'][0])){
267               $str .= "(".$data['FAIpriority'][0].")";
268             }
270             $str .= "&nbsp;".$data['cn'][0];
271             if(isset($data['description'][0])){
272               $str .= " [".$data['description'][0]."]";
273             }
274             $str .= "</a>";
275             $str .= "</td><td><i>".$data['CLASS']."</i>";
276             $str .= "</td></tr>";
277           }
278         }
280         /* Create variable entries */
281         if(in_array($key,array("FAIvariable"))) {
282           foreach($entry['Entries'] as $cn => $data){
283             $str .="<tr><td style='padding-left:20px;'>";
284             $str .= $image."&nbsp;".$data['cn'][0];
285             if(isset($data['description'][0])){
286               $str .= " [".$data['description'][0]."] ";
287             }
288             /* Only display FAIvariableContent if description dosn't contain [*] */ 
289             if(isset($data['description'][0]) && !preg_match("#\[\*\]#",$data['description'][0])){
290               $str .=" = '".$data['FAIvariableContent'][0]."'";
291             }
292             $str .= "</td><td><i>".$data['CLASS']."</i>";
293             $str .="</td></tr>";
294           }
295         }
297         /* Create packagelist entries */ 
298         if(in_array($key,array("FAIpackageList"))) {
299           ksort($entry['Entries']);
300           foreach($entry['Entries'] as $data){
301             $str .="<tr><td style='padding-left:20px;'>";
302             $str .= $image."&nbsp;".$data['cn'][0];
303             $str .= "</td><td><i>".$data['CLASS']."</i>";
304             $str .= "</td></tr>";;
305           }
306         }
308         /* Create partition table entries */
309         if(in_array($key,array("FAIpartitionTable"))) {
310           foreach($entry['Entries'] as $cn => $data){
311             $str .= "<tr><td style='padding-left:20px;'>";
312             $str .= $image."&nbsp;".$data['name'];
313             $str .= "</td><td><i>".$data['CLASS']."</i>";
314             $str .= "</td></tr>";
315             ksort($data['partitions']);
317             $str .= "<tr><td colspan=2  style='padding-left:20px;'>";
318             $str .= "<table cellspacing=0 cellpadding=2 style='background:#FFFFFF;border:1px solid #B0B0B0'>
319                       <tr style='background-color:#DBDBDB;'>
320                        <td class='tbhead'>"._("No.")."</td>
321                        <td class='tbhead'>"._("Name")."</td>
322                        <td class='tbhead'>"._("FS options")."</td>
323                        <td class='tbhead'>"._("Mount options")."</td>
324                        <td class='tbhead'>"._("Size in MB")."</td>
325                        <td class='tbhead'>"._("Mount point")."</td>
326                        <td class='tbrhead'>"._("Type")."</td>
327                       </tr>";
329             $i = 0;
330             foreach($data['partitions'] as $key => $part){
331               $i ++;
333               if($i%2 == 1){
334                 $c = " class='rowxp1'";
335                 $d = " class='list1'";
336               }else{
337                 $c = " class='rowxp0'";
338                 $d = " class='list0'";
339               }
341               $str.="<tr $c style='height:10px;'>
342                       <td $d>".$i."</td> 
343                       <td $d>".$part['cn'][0]."</td> 
344                       <td $d>".$part['FAIfsType'][0]."</td> 
345                       <td $d>".$part['FAIfsOptions'][0]."</td> 
346                       <td $d>".$part['FAIpartitionSize'][0]."</td> 
347                       <td $d>".$part['FAImountPoint'][0]."</td> 
348                       <td $d style='border:none'>".$part['FAIpartitionType'][0]."</td> 
349                     </tr>";
350             }
351             $str .="</table>";
352           }
353         }
354       }
355     }
356     $str .="</table>";  
357     return($str);
358   }
361   /* resolve specified object to append it to our object tree */
362   function resolveObject($class)
363   {
364     $ldap       = $this->config->get_ldap_link();
365     $dn           = $this->Releases [$this->Release];
366     $res    = array();
367  
368     /* Try to fetch all types of fai objects with the given cn  */
369     foreach($this->objs as $key =>  $obj){
370       
371       /* Check for objects with cn=$class 
372           and add them to our list */
373       $ldap->ls("(&(objectClass=".$key.")(cn=".$class."))",$obj['Tree'].$dn,array("*"));
374       while($attrs = $ldap->fetch()){
376         if(!isset($this->usedClasses[$key][$class])){
377           $this->usedClasses[$key][$class] = true;
378           switch($key){
379             case "FAIprofile":                  $this->prepare_FAIprofile($attrs);break;
380             case "FAIscript":                   $this->prepare_FAIscript($attrs);break;
381             case "FAIhook":                       $this->prepare_FAIhook($attrs);break;
382             case "FAIvariable":                 $this->prepare_FAIvariable($attrs);break;
383             case "FAItemplate":                 $this->prepare_FAItemplate($attrs);break;
384             case "FAIpackageList":            $this->prepare_FAIpackageList($attrs);break;
385             case "FAIpartitionTable":       $this->prepare_FAIpartitionTable($attrs);break;
386           }
387         }
388       }
389     }
390     return($res);
391   }
394   /* Prepare fai script */
395   function prepare_FAIscript($data)
396   {
397     if(isset($this->Result['FAIscript']['Entries'])){
398       $current = $this->Result['FAIscript']['Entries'];
399     }else{
400       $current = array();
401     }
402     $ldap = $this->config->get_ldap_link();
403     $ldap->cd($data['dn']);
404     $ldap->search("(objectClass=FAIscriptEntry)",array("cn","description","FAIpriority"));
405     while($attrs = $ldap->fetch()){
406       $attrs['CLASS'] = $data['cn'][0];
407       $current[$attrs['FAIpriority'][0]][] = $attrs;
408     }
409     $this->Result['FAIscript']['Entries'] = $current ;
410   }
413   /* Prepare fai script */
414   function prepare_FAIpartitionTable($data)
415   {
416     if(isset($this->Result['FAIpartitionTable']['Entries'])){
417       $current = $this->Result['FAIpartitionTable']['Entries'];
418     }else{
419       $current = array();
420     }
422     /* get subentries */
423     $ldap = $this->config->get_ldap_link();
424     $ldap->cd($data['dn']);
425     $ldap->search("(objectClass=FAIpartitionDisk)",array("cn","description"));
426     $disks = array();
428     /* Create an array which is sortable by priority */
429     while($attrs = $ldap->fetch()){
430       $attrs['CLASS'] = $data['cn'][0];
431       $disks[$attrs['cn'][0]]= $attrs;
432       if(isset($attrs['description'][0])){
433         $disks[$attrs['cn'][0]]['name'] = $attrs['cn'][0]." [".$attrs['description'][0]."]";
434       }else{
435         $disks[$attrs['cn'][0]]['name'] = $attrs['cn'][0];
436       }
437       $disks[$attrs['cn'][0]]['dn'] = $attrs['dn'];
438       $disks[$attrs['cn'][0]]['partitions'] = array();
439     }
441     /* Sort by priority */
442     foreach($disks as $key => $disk){
443       $ldap->cd($disk['dn']);
444       $ldap->search("(objectClass=FAIpartitionEntry)",array("*"));
445       while($attrs = $ldap->fetch()){
446         if(!isset($attrs['FAIfsOptions'][0])){
447           $attrs['FAIfsOptions'][0] = "";
448         }
449         $attrs['CLASS'] = $data['cn'][0];
450         $disks[$key]['partitions'][$attrs['cn'][0]] = $attrs;
451       }
452     }
453     $this->Result['FAIpartitionTable']['Entries'] = $disks;
454   }
457   /* Create template entry */   
458   function prepare_FAItemplate($data)
459   {
460     $current = array();
461     if(isset($this->Result['FAItemplate']['Entries'])){
462       $current = $this->Result['FAItemplate']['Entries'];
463     }
465     $ldap = $this->config->get_ldap_link();
466     $ldap->cd($data['dn']);
467     $ldap->search("(objectClass=FAItemplateEntry)",array("cn","description"));
468     while($attrs = $ldap->fetch()){
469       $attrs['CLASS'] = $data['cn'][0];
470       $current[] = $attrs;
471     }
472     $this->Result['FAItemplate']['Entries'] = $current ;
473   }
476   /* Create a package list  */  
477   function prepare_FAIpackageList($data)
478   {
479     $current = array();
480     if(isset($this->Result['FAIpackageList']['Entries'])){
481       $current = $this->Result['FAIpackageList']['Entries'];
482     }
483     if(isset($data['FAIpackage'])){
484       unset($data['FAIpackage']['count']);
485       foreach($data['FAIpackage'] as $pkg){
486         $attrs['CLASS'] = $data['cn'][0];
487         $attrs['cn'][0] = $pkg;
488         $current[$pkg] = $attrs;
489       }
490     }
491     $this->Result['FAIpackageList']['Entries'] = $current ;
492   }
495   /* Create a variable entry */ 
496   function prepare_FAIvariable($data)
497   {
498     $current = array();
499     if(isset($this->Result['FAIvariable']['Entries'])){
500       $current = $this->Result['FAIvariable']['Entries'];
501     }
502     $ldap = $this->config->get_ldap_link();
503     $ldap->cd($data['dn']);
504     $ldap->search("(objectClass=FAIvariableEntry)",array("cn","description","FAIvariableContent"));
505     while($attrs = $ldap->fetch()){
506       $attrs['CLASS'] = $data['cn'][0];
507       $current[] = $attrs;
508     }
509     $this->Result['FAIvariable']['Entries'] = $current ;
510   }
512   /* Create a hook entry */     
513   function prepare_FAIhook($data)
514   {
515     $current = array();
516     if(isset($this->Result['FAIhook']['Entries'])){
517       $current = $this->Result['FAIhook']['Entries'];
518     }
519     $ldap = $this->config->get_ldap_link();
520     $ldap->cd($data['dn']);
521     $ldap->search("(objectClass=FAIhookEntry)",array("cn","description"));
522     while($attrs = $ldap->fetch()){
523       $attrs['CLASS'] = $data['cn'][0];
524       $current[$attrs['cn'][0]] = $attrs;
525     }
526     $this->Result['FAIhook']['Entries'] = $current ;
527   }
530   /* Create a new Profile entry */
531   function prepare_FAIprofile($data)
532   {
533     $classes = split("\ ",$data['FAIclass'][0]);
534     foreach($classes as $class){
535       $class = trim($class);
536       $this->resolveObject($class);
537     }
538   }
540 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
541 ?>