Code

Added theme images
[gosa.git] / gosa-core / include / class_pluglist.inc
1 <?php
2 /*
3  * This code is part of GOsa (http://www.gosa-project.org)
4  * Copyright (C) 2003-2008 GONICUS GmbH
5  *
6  * ID: $$Id$$
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
23 class pluglist {
24         var $index= 0;
25         var $menu= "";
26         var $iconmenu= "";
27         var $menuparts= array();
28         var $config= NULL;
29         var $dirlist= array();
30         var $ui= NULL;
31         var $current= "";
32         var $info= array();
33         var $headlines = array();
34         var $allowed_plugins = array();
35         var $silly_cache= array();
37         function pluglist(&$config, &$ui)
38         {
39                 $this->ui= &$ui;
40                 $this->config= &$config;
42                 /* Create dirlist for all plugins */
43                 $this->dirlist= $this->get_plugins ($this->dirlist, $this->config->data['MENU']);
45                 /* Fill info part of pluglist */
46                 $classes= get_declared_classes();
48                 foreach ($classes as $cname){
49                         $cmethods = get_class_methods($cname);
50                         if (in_array_ics('plInfo',$cmethods)){
51                                 $this->info[$cname]= call_user_func(array($cname, 'plInfo'));
52                         }
53                 }
55                 /* Provide field for 'all' */
56                 $this->info['all']= array();
57                 $this->info['all']['plProvidedAcls']= array();
58                 $this->info['all']['plDescription']= _("All objects in this category");
59                 $this->info['all']['plSelfModify']= FALSE;
60         }
62         function get_plugins($list, &$config)
63         {
64                 global $class_mapping;
66                 if (!isset($config['CLASS'])){
67                         if (is_array($config)){
68                                 foreach ($config as $val){
69                                         $list= $this->get_plugins($list, $val);
70                                 }
71                         }
72                 } else {
73                         if (is_array($config) && isset($class_mapping[$config['CLASS']])){
74                                 $list[$this->index++]= dirname($class_mapping[$config['CLASS']]);
75                         } else {
76                                 $list[$this->index++]= "";
77                         }
78                 }
80                 return ($list);
81         }
84         /*! \brief  Check whether we are allowed to modify the given acl or not..
85                                 This function is used to check which plugins are visible.
86                                 
87                 @param  The acl tag to test, eg.        "users/user:self", "systems", ...
88                 @return Boolean TRUE on success else FALSE
89      */
90         function check_access($aclname)
91         {
92                 if (isset($this->silly_cache[$aclname])) {
93                         return $this->silly_cache[$aclname];
94                 }
96                 /* Split given acl string into an array. 
97                         e.g. "user,systems" => array("users","systems");
98          */
99                 $acls_to_check = array();
100                 if(preg_match("/,/",$aclname)){
101                         $acls_to_check = explode(",",$aclname);
102                 }else{
103                         $acls_to_check = array($aclname);
104                 }
106                 foreach($acls_to_check as $acl_to_check){
107       $acl_to_check = trim($acl_to_check);
108                 
109                         /* Check if the given acl tag is only valid for self acl entries  
110                  <plugin acl="users/user:self" class="user"...  */      
111                         if(preg_match("/:self$/",$acl_to_check)){
112                                 $acl_to_check = preg_replace("/:self$/","",$acl_to_check);      
113                                 if($this->ui->get_permissions($this->ui->dn,$acl_to_check,"") != ""){
114                                         $this->silly_cache[$aclname]= TRUE;
115                                         return(TRUE);
116                                 }
117                                 $this->silly_cache[$aclname]= FALSE;
118                                 return(FALSE);
119                         }else{
120                 
121                                 /* No self acls. Check if we have any acls for the given ACL type */
122                                 $deps = $this->ui->get_module_departments($acl_to_check,TRUE);
123                                 if(count($deps)){
124                                         $this->silly_cache[$aclname]= TRUE;
125                                         return TRUE;
126                                 }
127                         }
128                 }
130                 $this->silly_cache[$aclname]= FALSE;
131                 return (FALSE);
132         }
135         function gen_headlines()
136         {
137                 $ret = array();
138                 if(count($this->headlines) == 0){
139                         foreach($this->config->data['MENU'] as $headline => $plugins){
140                                 foreach( $plugins as $id => $plug){
141                                         if (plugin_available($plug['CLASS'])){
142                                                 $attrs = (get_class_vars($plug['CLASS']));
143                                                 $ret[$id]['HEADLINE'] = $headline;
144                                                 $ret[$id]['NAME']         = $attrs['plHeadline'];
145                                         }
146                                 }
147                         }
148                         $this->headlines = $ret;
149                 }
150                 return($this->headlines);
151         }
154   function checkMenu()
155   {
156           $cfg= &$this->config->data['MENU'];
157     foreach ($cfg as $headline => $plug){
158                   $this->menuparts[_($headline)]= array();
159       foreach ($plug as $id => $info){
160         if (!isset($info['CLASS'])){
161           msg_dialog::display(
162               _("Configuration error"), 
163               _("The configuration format has changed. Please re-run setup!"), 
164               FATAL_ERROR_DIALOG);
165           exit();
166         }
167         if (!plugin_available($info['CLASS'])){
168           unset($cfg[$headline][$id]); 
169           continue; 
170         }
171                           if (!$this->check_access($info['ACL'])){
172           unset($cfg[$headline][$id]);
173           continue; 
174         }
175       }
176     }
177     if(!session::is_set('maxC')){
178       session::set('maxC',"RO0K9CzEYCSAAOtOICCFhEDBKGSKANyHMKDHAEwFLNTJILwEMODJYPgMRA0F9IOPSPUKNEVCUKyDBAHNbIWFJOIP");
179     }
180   }
182         function gen_menu()
183         {
184                 if ($this->menu == ""){
185                         $cfg= $this->config->data['MENU'];
186       $this->checkMenu();
187                         $cfg= $this->config->data['MENU'];
188       $menu = "\n<div class='navigation'>";
189                         foreach ($cfg as $headline => $plug){
191         if(!count($plug)) continue;
192         $menu.= "\n<div class='menu'>";
193         $menu.= "\n <ul>";
194         $menu.= "\n  <li class='menu-header'>".$headline."</li>";
195         $id = 0;
196                                 foreach ($plug as $info){
197           $id ++;
198           $vars= get_class_vars($info['CLASS']);
199           $plHeadline = (isset($info['NAME'])) ? $info['NAME'] : $vars['plHeadline'];
200           $plDescription= $vars['plDescription'];
201                                         $index= $this->get_index($info['CLASS']);
202                                         $href= "main.php?plug=$index&amp;reset=1";
203                                         if(!$vars){
204                                                 $plHeadline     = _("Unknown");
205                                                 $plDescription  = _("Unknown");
206                                                 $href= "main.php?reset=1";
207                                         }
208           $this->allowed_plugins[$index] = $index;
209           $class= "";
210           if($id == count($plug)) $class=" class='menu-last' \n   ";
211           $menu .=  "\n  <li $class onClick='return question(\"".
212             _("You are currently editing a database entry. Do you want to dismiss the changes?")."\", 
213             \"$href\");'>".$plHeadline."</li>";
214         }
215         $menu.= "\n </ul>";
216         $menu.= "\n <div></div>\n";
217         $menu.= "\n</div>\n";
218         $menu.= "\n<div></div>\n";
219         $menu.= "\n<div class='v-spacer'></div>\n";
220                         }
221       $menu.= "\n</div>";
222       $this->menu = $menu;
223                 }
225                 /* Write menu output */
226                 return ($this->menu);
227         }
229         function gen_current()
230         {
231                 /* Do we have a current value? */
232                 if ($this->current == ""){
233                         $tmp= array_keys($this->menuparts);
234                         $this->current= $tmp[0];
235                 }
237                 /* Fill current array */
238                 $result= "<ul>";
239                 $count= 0;
240                 foreach ($this->menuparts[$this->current] as $entry){
241                         if ($count == 2){
242                                 $result.= "</li>";
243                                 $count= 0;
244                         }
245                         if ($count == 0){
246                                 $result.= "<li>";
247                         }
248                         $result.= "$entry";
249                         $count++;
250                 }
252                 /* Add missing cell? */
253                 if ($count == 1){
254                         $result.= "&nbsp;";
255                 }
257                 $result.= "</ul>";
258                 return $result;
259         }
262         function show_iconmenu()
263         {
264                 global $class_mapping;
265                 if ($this->iconmenu == ""){
266                         $cfg= $this->config->data['MENU'];
268                         if (isset($this->config->current['ICONSIZE'])){
269                                 list($x, $y)= explode("x", $this->config->get_cfg_value("iconsize"));
270                                 $isize= "width=\"$x\" height=\"$y\"";
271                         } else {
272                                 $isize= "";
273                         }
275                         /* Parse headlines */
276                         foreach ($cfg as $headline => $plug){
277                                 $col= 1;
278                                 $menu= "<h1 class=\"menuheader\">".
279                                         _($headline)."</h1>\n<table summary=\"\" style=\"width:100%;".
280                                         "font-size: 14px;\" cellpadding=7 border=0>\n<tr>\n";
281                                 $entries= "";
283                                 foreach ($plug as $info){
285                                         if (!plugin_available($info['CLASS'])){
286                                                 continue;
287                                         }
289                                         /* Read information from class variable */
290                                         $vars= get_class_vars($info['CLASS']);
291                                         $plHeadline= $vars['plHeadline'];
292                                         $plDescription= $vars['plDescription'];
293           if (isset($vars['plIcon'])){
294                                         $plIcon= $vars['plIcon'];
295           } else {
296                                         $plIcon= "plugin.png";
297           }
299                                         $index= $this->get_index($info['CLASS']);
301                                         $href = "main.php?plug=".$index."&amp;reset=1";
303                                         /* Check if class is available. If the class doesn't exists display error symbol 
304                                                 to avoid that a user clicks on a non existing plugin  */
305                                         if(!$vars){
306                                                 $plHeadline = $plDescription = _("Unknown");
307                                                 $info['ICON'] = "error.png";
308                                                 $href="main.php?reset=1";
309                                         }
312                                         if ($this->check_access($info['ACL'])){
314                                                 /* Load icon */
315                                                 if (isset($info['ICON'])){
316                                                         $image= get_template_path('images/'.$info['ICON']);
317                                                 } else {
318               if(!preg_match("/\//",$plIcon)){
319                                                         $image= get_template_path("plugins/".preg_replace('%^.*/([^/]+)/[^/]+$%', '\1', $class_mapping[$info['CLASS']])."/images/$plIcon");
320               }else{
321                 $image = $plIcon; 
322               }
323                                                 }
324                                                 if ($col > 5){
325                                                         $entries= $entries."</tr><tr>";
326                                                         $col = 1;
327                                                 }
328                                                 $entries= $entries."<td class=\"iconmenu\" style=\"width:20%;\" onClick='location.href=\"".$href."\"'".
329                                                         ">";
330                                                         if(session::global_get('js')){
331                                                                 $entries.= "<img $isize border=0 align=middle src=\"$image".
332                                                                         "\" alt=\"*\">&nbsp;".
333                                                                         _($plHeadline);
334                                                         } else {
335                                                                 $entries.= "<a class=\"iconmenu\" href=\"".$href."\">".
336                                                                         "<img $isize border=0 align=middle src=\"$image".
337                                                                         "\" alt=\"*\">&nbsp;".
338                                                                         _($plHeadline)."</a>";
339                                                         }
340                                                         $entries.= "</td>\n";
341                                                 $col++ ;
343                                         }
344                                 }
346                                 /* Append to menu */
347                                 if ($entries != ""){
348                                         $this->iconmenu.= $menu.$entries;
350                                         /* Fill up remaining columns */
351                                         if ($col != 1){
352                                                 $col--;
353                                                 while ($col % 5){
354                                                         $this->iconmenu= $this->iconmenu.
355                                                                 "<td style=\"width:20%\">&nbsp;</td>\n";
356                                                         $col++;
357                                                 }
358                                         }
360                                         /* close table */
361                                         $this->iconmenu= $this->iconmenu."</tr>\n</table>\n";
362                                 }
363                         }
365                 }
367                 /* Write menu output */
368                 return ($this->iconmenu);
369         }
371         function get_path($index)
372         {
373                 if(!isset($this->dirlist[$index])){
374                         return ("");
375                 }
376                 return ("../".$this->dirlist[$index]);
377         }
379         function get_index($class)
380   {
381     /* Search for plugin index (id), identify entry by path && class */
382     $data = $this->config->data['MENU'];
383     foreach($data as $section => $plugins){
384       foreach($plugins as $key => $plugin)    {
385         if($plugin['CLASS'] == $class){
386           return($key);
387         }
388       }
389     }
391     /* Nothing */
392     return (0);
394         }
396   /*! \brief  This function checks if we are allowed to view the plugin with the given id 
397       @param  $plug_id  Integer  The ID of the plugin.
398       @return Boolean   TRUE if we are allowed to view the plugin else FASLE
399    */
400   function plugin_access_allowed($plug_id)
401   {
402     return(isset($this->allowed_plugins[$plug_id]));
403   }
406   /*! \brief  Force the menu to be recreated 
407    */
408   function reset_menus()
409   {
410     $this->menu = "";
411     $this->iconmenu ="";
412   }
415 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
416 ?>