Code

Added script sorting
[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     if(isset($_GET['show'])){
153       $ldap = $this->config->get_ldap_link();
154       $ldap->cat(base64_decode($_GET['id']));
155       $attrs = $ldap->fetch();
156       if($_GET['type'] == "FAIscript"){
157         $str = $attrs['FAIscript'][0];
158       }elseif($_GET['type'] == "FAIhook"){
159         $str = $attrs['FAIscript'][0];
160       }elseif($_GET['type'] == "FAItemplate"){
161         $str = $attrs['FAItemplateFile'][0];
162       }
163       echo nl2br($str);
164       exit();
165     }
167     /* Get smarty class & assign created summary results */
168     $smarty = get_smarty();
169     $this->ObjectList = $this->createSummary($this->Result);
170     $smarty->assign("objectList",$this->ObjectList);
171     return($smarty->fetch (get_template_path('faiSummary.tpl', TRUE, dirname(__FILE__))));
172   }
175   /* Create output of from resolved fai objects */
176   function createSummary($data)
177   {
178     /* Don't generate any output, if there is no result */ 
179     $str ="<table style='width:100%;background:#F5F5F5;'> ";
180     if(!count($this->Result)){
181       $str.="<tr><td>"; 
182       $str.= (_("This object has no FAI classes assigned."));
183       $str.= "</tr></td></table>";
184       return($str);
185     }
187     /* walk through all classes */      
188     foreach($data as $key => $entry){
189       
190       $image = "<img src='".$this->objs[$key]['Image']."' class='center' alt='".$this->objs[$key]['Name']."' border=0>";
191       if(!isset($entry['Open'])){
192         $str .= "<tr><td colspan=2><input type='image' class='center' src='images/expand.png' name='open_".$key."'>&nbsp;";
193         $str .= $image."&nbsp;";
194         $str .= "<b>".$this->objs[$key]['Name']."</b></td></tr>";
195       }else{
196         $str .= "<tr><td colspan=2><input type='image' class='center' src='images/sort_down.png' name='close_".$key."'>&nbsp;";
197         $str .= $image."&nbsp;";
198         $str .= "<b>".$this->objs[$key]['Name']."</b></td></tr>";
199    
200         /* Display FAItemplate FAIhook FAIscript entries */ 
201         if(in_array($key,array("FAItemplate","FAIhook","FAIscript"))){
203           $nums = array();          
205           if($key == "FAIscript"){
206             foreach($entry['Entries'] as $scripts){
207               foreach($scripts as $script){
208                 
209                 if(!isset($nums[$script['cn'][0]])){
210                   $nums[$script['cn'][0]]= 0;
211                 } 
212                 $nums[$script['cn'][0]] ++;
213             
214                 $tmp[$script['FAIpriority'][0].$script['cn'][0].$script['CLASS']] = $script;
215               }
216             }
217             krsort($tmp);;
218             
219             $entry['Entries'] = $tmp;
220           }else{
221             foreach($entry['Entries'] as $script){
222               if(!isset($nums[$script['cn'][0]])){
223                 $nums[$script['cn'][0]]= 0;
224               } 
225               $nums[$script['cn'][0]] ++;
226             }
227           }
229           foreach($entry['Entries'] as $cn => $data){
231             if(isset($nums[$data['cn'][0]]) && ($nums[$data['cn'][0]] > 1)){
232               $str .="<tr style='background: #EE3434;'><td style='padding-left:20px;'>";
233             }else{
234               $str .="<tr><td style='padding-left:20px;'>";
235             }
237             $str .= "<a target='_blank'href='?plug=".$_GET['plug']."&show&type=".$key."&id=".base64_encode($data['dn'])."'>";
238             $str .= $image."&nbsp;";
239             if(isset($data['FAIpriority'][0])){
240               $str .= "(".$data['FAIpriority'][0].")";
241             }
243             $str .= "&nbsp;".$data['cn'][0];
244             if(isset($data['description'][0])){
245               $str .= " [".$data['description'][0]."]";
246             }
247             $str .= "</a>";
248             $str .= "</td><td><i>".$data['CLASS']."</i>";
249             $str .= "</td></tr>";
250           }
251         }
253         /* Create variable entries */
254         if(in_array($key,array("FAIvariable"))) {
255           foreach($entry['Entries'] as $cn => $data){
256             $str .="<tr><td style='padding-left:20px;'>";
257             $str .= $image."&nbsp;".$data['cn'][0];
258             if(isset($data['description'][0])){
259               $str .= " [".$data['description'][0]."] ";
260             }
261             $str .=" = '".$data['FAIvariableContent'][0]."'";
263             $str .= "</td><td><i>".$data['CLASS']."</i>";
264             
266             $str .="</td></tr>";
267           }
268         }
270         /* Create packagelist entries */ 
271         if(in_array($key,array("FAIpackageList"))) {
272 //          ksort($entry['Entries']);
273           foreach($entry['Entries'] as $data){
274             $str .="<tr><td style='padding-left:20px;'>";
275             $str .= $image."&nbsp;".$data['cn'][0];
276             $str .= "</td><td><i>".$data['CLASS']."</i>";
277             $str .= "</td></tr>";;
278           }
279         }
281         /* Create partition table entries */
282         if(in_array($key,array("FAIpartitionTable"))) {
283           foreach($entry['Entries'] as $cn => $data){
284             $str .= "<tr><td style='padding-left:20px;'>";
285             $str .= $image."&nbsp;".$data['name'];
286             $str .= "</td><td><i>".$data['CLASS']."</i>";
287             $str .= "</td></tr>";
288             ksort($data['partitions']);
290             $str .= "<tr><td colspan=2  style='padding-left:20px;'>";
291             $str .= "<table cellspacing=1 style='background:#FFFFFF;'>
292                       <tr style='background-color:#BBBBBB;'>
293                        <td>"._("No.")."</td>
294                        <td>"._("Name")."</td>
295                        <td>"._("FS options")."</td>
296                        <td>"._("Mount options")."</td>
297                        <td>"._("Size in MB")."</td>
298                        <td>"._("Mount point")."</td>
299                        <td>"._("Type")."</td>
300                       </tr>";
302             $i = 0;
303             foreach($data['partitions'] as $key => $part){
304               $i ++;
306               if($i%2 == 1){
307                 $c = " class='rowxp1'";
308                 $d = " class='list1' style='border:0px;'";
309               }else{
310                 $c = " class='rowxp0'";
311                 $d = " class='list0' style='border:0px;'";
312               }
314               $str.="<tr $c style='height:10px;'>
315                       <td $d>".$i."</td> 
316                       <td $d>".$part['cn'][0]."</td> 
317                       <td $d>".$part['FAIfsType'][0]."</td> 
318                       <td $d>".$part['FAIfsOptions'][0]."</td> 
319                       <td $d>".$part['FAIpartitionSize'][0]."</td> 
320                       <td $d>".$part['FAImountPoint'][0]."</td> 
321                       <td $d>".$part['FAIpartitionType'][0]."</td> 
322                     </tr>";
323             }
324             $str .="</table>";
325           }
326         }
327       }
328     }
329     $str .="</table>";  
330     return($str);
331   }
334   /* resolve specified object to append it to our object tree */
335   function resolveObject($class)
336   {
337     $ldap       = $this->config->get_ldap_link();
338     $dn           = $this->Releases [$this->Release];
339     $res    = array();
340  
341     /* Try to fetch all types of fai objects with the given cn  */
342     foreach($this->objs as $key =>  $obj){
343       
344       /* Check for objects with cn=$class 
345           and add them to our list */
346       $ldap->ls("(&(objectClass=".$key.")(cn=".$class."))",$obj['Tree'].$dn,array("*"));
347       while($attrs = $ldap->fetch()){
349         if(!isset($this->usedClasses[$key][$class])){
350           $this->usedClasses[$key][$class] = true;
351           switch($key){
352             case "FAIprofile":                  $this->prepare_FAIprofile($attrs);break;
353             case "FAIscript":                   $this->prepare_FAIscript($attrs);break;
354             case "FAIhook":                       $this->prepare_FAIhook($attrs);break;
355             case "FAIvariable":                 $this->prepare_FAIvariable($attrs);break;
356             case "FAItemplate":                 $this->prepare_FAItemplate($attrs);break;
357             case "FAIpackageList":            $this->prepare_FAIpackageList($attrs);break;
358             case "FAIpartitionTable":       $this->prepare_FAIpartitionTable($attrs);break;
359           }
360         }
361       }
362     }
363     return($res);
364   }
367   /* Prepare fai script */
368   function prepare_FAIscript($data)
369   {
370     if(isset($this->Result['FAIscript']['Entries'])){
371       $current = $this->Result['FAIscript']['Entries'];
372     }else{
373       $current = array();
374     }
375     $ldap = $this->config->get_ldap_link();
376     $ldap->cd($data['dn']);
377     $ldap->search("(objectClass=FAIscriptEntry)",array("cn","description","FAIpriority"));
378     while($attrs = $ldap->fetch()){
379       $attrs['CLASS'] = $data['cn'][0];
380       $current[$attrs['FAIpriority'][0]][] = $attrs;
381     }
382     $this->Result['FAIscript']['Entries'] = $current ;
383   }
386   /* Prepare fai script */
387   function prepare_FAIpartitionTable($data)
388   {
389     if(isset($this->Result['FAIpartitionTable']['Entries'])){
390       $current = $this->Result['FAIpartitionTable']['Entries'];
391     }else{
392       $current = array();
393     }
395     /* get subentries */
396     $ldap = $this->config->get_ldap_link();
397     $ldap->cd($data['dn']);
398     $ldap->search("(objectClass=FAIpartitionDisk)",array("cn","description"));
399     $disks = array();
401     /* Create an array which is sortable by priority */
402     while($attrs = $ldap->fetch()){
403       $attrs['CLASS'] = $data['cn'][0];
404       $disks[$attrs['cn'][0]]= $attrs;
405       if(isset($attrs['description'][0])){
406         $disks[$attrs['cn'][0]]['name'] = $attrs['cn'][0]." [".$attrs['description'][0]."]";
407       }else{
408         $disks[$attrs['cn'][0]]['name'] = $attrs['cn'][0];
409       }
410       $disks[$attrs['cn'][0]]['dn'] = $attrs['dn'];
411       $disks[$attrs['cn'][0]]['partitions'] = array();
412     }
414     /* Sort by priority */
415     foreach($disks as $key => $disk){
416       $ldap->cd($disk['dn']);
417       $ldap->search("(objectClass=FAIpartitionEntry)",array("*"));
418       while($attrs = $ldap->fetch()){
419         if(!isset($attrs['FAIfsOptions'][0])){
420           $attrs['FAIfsOptions'][0] = "";
421         }
422         $attrs['CLASS'] = $data['cn'][0];
423         $disks[$key]['partitions'][$attrs['cn'][0]] = $attrs;
424       }
425     }
426     $this->Result['FAIpartitionTable']['Entries'] = $disks;
427   }
430   /* Create template entry */   
431   function prepare_FAItemplate($data)
432   {
433     $current = array();
434     if(isset($this->Result['FAItemplate']['Entries'])){
435       $current = $this->Result['FAItemplate']['Entries'];
436     }
438     $ldap = $this->config->get_ldap_link();
439     $ldap->cd($data['dn']);
440     $ldap->search("(objectClass=FAItemplateEntry)",array("cn","description"));
441     while($attrs = $ldap->fetch()){
442       $attrs['CLASS'] = $data['cn'][0];
443       $current[] = $attrs;
444     }
445     $this->Result['FAItemplate']['Entries'] = $current ;
446   }
449   /* Create a package list  */  
450   function prepare_FAIpackageList($data)
451   {
452     $current = array();
453     if(isset($this->Result['FAIpackageList']['Entries'])){
454       $current = $this->Result['FAIpackageList']['Entries'];
455     }
456     if(isset($data['FAIpackage'])){
457       unset($data['FAIpackage']['count']);
458       foreach($data['FAIpackage'] as $pkg){
459         $attrs['CLASS'] = $data['cn'][0];
460         $attrs['cn'][0] = $pkg;
461         $current[$pkg] = $attrs;
462       }
463     }
464     $this->Result['FAIpackageList']['Entries'] = $current ;
465   }
468   /* Create a variable entry */ 
469   function prepare_FAIvariable($data)
470   {
471     $current = array();
472     if(isset($this->Result['FAIvariable']['Entries'])){
473       $current = $this->Result['FAIvariable']['Entries'];
474     }
475     $ldap = $this->config->get_ldap_link();
476     $ldap->cd($data['dn']);
477     $ldap->search("(objectClass=FAIvariableEntry)",array("cn","description","FAIvariableContent"));
478     while($attrs = $ldap->fetch()){
479       $attrs['CLASS'] = $data['cn'][0];
480       $current[] = $attrs;
481     }
482     $this->Result['FAIvariable']['Entries'] = $current ;
483   }
485   /* Create a hook entry */     
486   function prepare_FAIhook($data)
487   {
488     $current = array();
489     if(isset($this->Result['FAIhook']['Entries'])){
490       $current = $this->Result['FAIhook']['Entries'];
491     }
492     $ldap = $this->config->get_ldap_link();
493     $ldap->cd($data['dn']);
494     $ldap->search("(objectClass=FAIhookEntry)",array("cn","description"));
495     while($attrs = $ldap->fetch()){
496       $attrs['CLASS'] = $data['cn'][0];
497       $current[$attrs['cn'][0]] = $attrs;
498     }
499     $this->Result['FAIhook']['Entries'] = $current ;
500   }
503   /* Create a new Profile entry */
504   function prepare_FAIprofile($data)
505   {
506     $classes = split("\ ",$data['FAIclass'][0]);
507     foreach($classes as $class){
508       $class = trim($class);
509       $this->resolveObject($class);
510     }
511   }
513 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
514 ?>