Code

Disbled dashBoard in icon menu by default.
[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 {
25   // The id of the last registered plugin id 
26   var $index= 0;
28   var $config= NULL;
29   var $ui= NULL;
31   // Contains a list of the paths for all registered plugins.
32   var $dirlist= array();
34   // Seems to be used in the help menu to generate an overview.
35   var $headlines = array();
36   // Remember stuff in here.
37   var $silly_cache= array();
39   // The pluglist keeps track of all registered plugins.
40   // (Registered plugins are those we are able to access)
41   var $pluginList = array();
43   // Some cache vars to avoid regenration of the menus.   
44   var $pathMenu = "";
45   var $menu= "";
46   var $iconmenu= "";
48   // Classes plInfo list
49   var $info = array();
51   function pluglist(&$config, &$ui)
52   {
53     $this->ui= &$ui;
54     $this->config= &$config;
55     $this->loadPluginList();
56   }
58   function loadPluginList()
59   {
60     $this->pluginList = array();
62                 /* Fill info part of pluglist */
63                 $classes= get_declared_classes();
64                 foreach ($classes as $cname){
65                         $cmethods = get_class_methods($cname);
66                         if (in_array_ics('plInfo',$cmethods)){
67                                 $this->info[$cname]= call_user_func(array($cname, 'plInfo'));
68                         }
69                 }
71     if(!session::is_set('maxC')){
72       session::set('maxC',"RO0K9CzEYCSAAOtOICCFhEDBKGSKANyHMKDHAEwFLNTJILwEMODJYPgMRA0F9IOPSPUKNEVCUKyDBAHNbIWFJOIP");
73     }
74 //
75 //   // Now generate menu - usually they are cached
76 //   $this->gen_menu();
77 //   $this->show_iconmenu();
78 //   $this->genPathMenu();
79   }
82   /*! \brief    Tries to register a plugin in the pluglist
83    *            Checks existence and ACL for the given plugin.
84    *            Returns true in case of success else false. 
85    */
86   function registerPlugin($plug)
87   {
88     global $class_mapping;
90     // Clean ACL string, we do not want any spaces or lines breaks here. 
91     $plug['ACL'] = trim($plug['ACL']);
93     // Check class
94     if (!isset($plug['CLASS'])){
95       msg_dialog::display(
96           _("Configuration error"),
97           _("The configuration format has changed: please run the setup again!"),
98           FATAL_ERROR_DIALOG);
99       exit();
100     }
101     if (!plugin_available($plug['CLASS'])){
102       return(FALSE);
103     }
104     if (!$this->check_access($plug['ACL'])){
105       return(FALSE);
106     }
107     $this->dirlist[$this->index] = dirname($class_mapping[$plug['CLASS']]);
108     $this->pluginList[$this->index] = $plug['CLASS'];
109     $this->index++;
110     return(TRUE);
111   }
114   /*! \brief  Check whether we are allowed to modify the given acl or not..
115    *            This function is used to check which plugins are visible.
116    *            
117    *  @param    The acl tag to check for, eg.   "users/user:self", "systems", ...
118    *  @return   Boolean TRUE on success else FALSE
119    */
120   function check_access($aclname)
121   {
122     if (isset($this->silly_cache[$aclname])) {
123       return $this->silly_cache[$aclname];
124     }
126     // Split given acl string into an array. e.g. "user,systems" => array("users","systems");
127     $acls_to_check = array();
128     if(preg_match("/,/",$aclname)){
129       $acls_to_check = explode(",",$aclname);
130     }else{
131       $acls_to_check = array($aclname);
132     }
134     foreach($acls_to_check as $acl_to_check){
135     
136       // Remove spaces and line breaks.
137       $acl_to_check = trim($acl_to_check);
138       $acl_to_check = preg_replace("/ /","",$acl_to_check);
139         
141       /* Check if the given acl tag is only valid for self acl entries  
142        *          <plugin acl="users/user:self" class="user"...  
143        */       
144       if(preg_match("/:self$/",$acl_to_check)){
145         $acl_to_check = preg_replace("/:self$/","",$acl_to_check);      
146         if(strpos($acl_to_check,"/")){
147           if($this->ui->get_permissions($this->ui->dn,$acl_to_check,"") != ""){
148             $this->silly_cache[$aclname]= TRUE;
149             return(TRUE);
150           }
151         }else{
152           if($this->ui->get_category_permissions($this->ui->dn,$acl_to_check,"") != ""){
153             $this->silly_cache[$aclname]= TRUE;
154             return(TRUE);
155           }
156         }
157       }else{
159         // No self acls. Check if we have any acls for the given ACL type 
160         $deps = $this->ui->get_module_departments($acl_to_check,TRUE);
161         if(count($deps)){
162           $this->silly_cache[$aclname]= TRUE;
163           return TRUE;
164         }
165       }
166     }
168     $this->silly_cache[$aclname]= FALSE;
169     return (FALSE);
170   }
174   /*! \brief    Generate the GOsa Main-Menu here (The menu on the left), 
175    *             this usually only done once during login.
176    *            -----------------------------------------------------------------
177    *            Do NOT add style changes here manually, use the style.css or 
178    *             if you prefer create your own theme!!
179    *            -----------------------------------------------------------------
180    */
181   function gen_menu()
182   {
183     if ($this->menu == ""){
185       // First load the menu plugins and try to register them in the pluglist
186       //  if this fails for some reason, then remove the plugin from the menu.
187       if(isset($this->config->data['MENU'])){
188         foreach($this->config->data['MENU'] as $section => $plugins){
189           foreach($plugins as $id => $plug){
190             if(!$this->registerPlugin($plug)){ 
191               unset($this->config->data['MENU'][$section][$id]); 
192             }
193           }
194         }
195       }
197       $disabledPlugins = $this->config->configRegistry->getDisabledPlugins();
199       $cfg= $this->config->data['MENU'];
200       $menu = "\n      <div class='navigation'>";
201       foreach ($cfg as $headline => $plug){
204         if(!count($plug)) continue;
206         $menu.= "\n        <div class='menu'>";
207         $menu.= "\n          <ul>";
208         $menu.= "\n            <li class='menu-header'>"._($headline)."</li>";
209         $id = 0;
210         foreach ($plug as $info){
212             // The Plugin has been deactivated for some reason, perhabs a missing ldap schema.
213             if(isset($disabledPlugins[$info['CLASS']])) continue;
215           // Used to detect the last element in the menu
216           $id ++;
218           list($index, $title, $desc, $icon) = $this->getPlugData($info['CLASS']);
219           $class= "";
220           if($id == count($plug)) $class=" class='menu-last' ";
221           $menu .=  "\n            <li id='plugMenuId_{$index}' $class onClick='return openPlugin({$index});'>".$title."</li>";
222         }
223         $menu.= "\n          </ul>";
224         $menu.= "\n          <div style='font-size:0'>&nbsp;</div>";
225         $menu.= "\n        </div>\n";
226         $menu.= "\n        <div style='font-size:0'>&nbsp;</div>";
227         $menu.= "\n        <div class='v-spacer'></div>\n";
228       }
229       $menu.= "\n      </div>\n";
230       $this->menu = $menu;
232       // Add javascript method to print out warning messages while leaving an unsaved form.
233       // We do it in here to get the string translated.
234       $this->menu .=  
235         "\n      <script language='javascript' type='text/javascript'>".
236         "\n        function openPlugin(id){".
237         "\n          return question(\""._("You are currently editing a database entry. Do you want to discard the changes?")."\",".
238         "\n            \"main.php?plug=\" + id + \"&amp;reset=1\");".
239         "\n        }".
240         "\n      </script>\n"; 
241     }
242   
243     // Use javascript to mark the currently selected plugin.
244     $menu = $this->menu;
245     if(isset($_GET['plug'])){
246       $menu.= 
247      "\n      <script language='javascript' type='text/javascript'>".
248      "\n        if($('plugMenuId_".$_GET['plug']."')){".
249      "\n          $('plugMenuId_".$_GET['plug']."').className= 'current'".
250      "\n        }".
251      "\n      </script>\n";
252     }
254     // Return the generated/cached gosa menu.
255     return ($menu);
256   }
259   /*! \brief    Generate the GOsa Icon-Menu here, this usually only done once during
260    *             login.
261    *            -----------------------------------------------------------------
262    *            Do NOT add style changes here manually, use the style.css or 
263    *             if you prefer create your own theme!!
264    *            -----------------------------------------------------------------
265    */
266   function show_iconmenu()
267   {
268     $add_hr =FALSE;
269     if ($this->iconmenu == ""){
271       $disabledPlugins = $this->config->configRegistry->getDisabledPlugins();
273       $cfg= $this->config->data['MENU'];
274       foreach ($cfg as $headline => $plug){
275         $col= 0;
277         $this->iconmenu .= "\n        <div class='clear'></div>";
278         if($add_hr){
279           $add_hr = FALSE;
280           $this->iconmenu .= "\n        <hr>";
281         }
282         $this->iconmenu .= "\n        <h3 class='icon-menu-title'>". _($headline)."</h3>";
284         foreach ($plug as $info){
286           // Get Plugin info
287           list($index, $title, $desc, $icon) = $this->getPlugData($info['CLASS']);
289           // The Plugin has been deactivated for some reason, perhabs a missing ldap schema.
290           if(isset($disabledPlugins[$info['CLASS']])) continue;
292           // Add a seperating row
293           if (($col % 4) == 0){ 
294             $this->iconmenu .= "\n        <div class='clear'></div>";
295           }
297           $this->iconmenu.= "\n        <div class='icon-menu-item' style='width: 25%;' onclick='openPlugin({$index})'>";
298           $this->iconmenu.= "\n          ".image($icon);
299           $this->iconmenu.= "\n          <div class='dsc'>";
300           $this->iconmenu.= "\n            <h1>{$title}</h1>";
301           $this->iconmenu.= "\n            <p>{$desc}</p>";
302           $this->iconmenu.= "\n          </div>";
303           $this->iconmenu.= "\n        </div>";
304           $col++ ;
305         }
306         $add_hr = TRUE;
307       }
308     }
309     return ($this->iconmenu);
310   }
313   /*! \brieg    Generates and the path menu (the one on the upper right) and keeps
314    *             the generated HTML content, so we are not forced to generate it on every 
315    *             page request.
316    *            (See <pathMenu> of your gosa.conf)
317    */
318   function genPathMenu()
319   {
320     if(empty($this->pathMenu)){
322       $disabledPlugins = $this->config->configRegistry->getDisabledPlugins();
324       // Now load the icon menu and try to register the plugins in the pluglist
325       //  if this fails for some reason, then remove the plugins from the menu.
326       if(isset($this->config->data['SHORTCUTMENU'])){
327         foreach($this->config->data['SHORTCUTMENU'] as $id => $plugin){
328           if(!$this->registerPlugin($plugin)){
329             unset($this->config->data['SHORTCUTMENU'][$id]); 
330           } 
331         }
332       }
334       // Now load the path menu and try to register the plugins in the pluglist
335       //  if this fails for some reason, then remove the plugin from the menu.
336       if(isset($this->config->data['PATHMENU'])){
337         foreach($this->config->data['PATHMENU'] as $id => $plugin){
338           if(!$this->registerPlugin($plugin)){
339             unset($this->config->data['PATHMENU'][$id]); 
340           } 
341         }
342       }
344       $this->pathMenu = 
345           "\n        <div class='plugin-path'>".
346           "\n          <ul class='path-navigation'>";
348       // Check if we've at least one entry defined ih the iconmenu
349       if(isset($this->config->data['SHORTCUTMENU'])){
350           $icfg= &$this->config->data['SHORTCUTMENU'];
351           $rcfg = array_reverse($icfg);
352           foreach($rcfg as $id => $plug){
353               list($index, $title, $desc, $icon, $shortIcon) = $this->getPlugData($plug['CLASS']);
355               // The Plugin has been deactivated for some reason, perhabs a missing ldap schema.
356               if(isset($disabledPlugins[$plug['CLASS']])) continue;
358               $cssClass= (!isset($rcfg[$id+1]))? 'left' : 'left right-border';
359               $this->pathMenu.= "            
360                   <li class='{$cssClass}' onClick='openPlugin({$index})' title='{$title}'>".
361                   image(get_template_path($shortIcon))."</li>";
362           }
363       }
365       // Place the navigator part, this will be replaced during runtime.
366       $this->pathMenu .= "\n            %navigator%";
368       // Check if we've at least one entry defined ih the pathmenu
369       if(isset($this->config->data['PATHMENU'])){
370         $cfg= &$this->config->data['PATHMENU'];
371         $rcfg = array_reverse($cfg);
372         foreach($rcfg as $id => $plug){
373           list($index, $title, $desc, $icon) = $this->getPlugData($plug['CLASS']);
375           // The Plugin has been deactivated for some reason, perhabs a missing ldap schema.
376           if(isset($disabledPlugins[$plug['CLASS']])) continue;
378           $this->pathMenu.= "\n            <li class='right left-border' onClick='openPlugin({$index})'>{$title}</li>";
379         }
380       }
381       $this->pathMenu.= "\n          </ul>";
382       $this->pathMenu.= "\n        </div>";
383     }
385     $menu = pathNavigator::getCurrentPath();
386     return(preg_replace("/%navigator%/", $menu, $this->pathMenu)); 
387   }
388   
390   /*! \brief    Returns additional info for a given class name, like 
391    *             plugin-icon, title, description and the index of the element 
392                  in the pluglist which uses this class.
393    */
394   function getPlugData($class)
395   {
396     global $class_mapping;
397     $vars= get_class_vars($class);
398     $plHeadline= _($vars['plHeadline']);
399     $plDescription= _($vars['plDescription']);
400     $plIcon = (isset($vars['plIcon'])) ? $vars['plIcon']: "plugin.png";
401     $plShortIcon = (isset($vars['plShortIcon'])) ? $vars['plShortIcon']: "plugin.png";
403     $index= $this->get_index($class);
405     /* Check if class is available. If the class doesn't exists display error symbol
406      *  to avoid that a user clicks on a non existing plugin
407      */
408     if(!$vars){
409       $plHeadline = $plDescription = _("Unknown");
410       $plIcon = "error.png";
411       $index = '';
412     } 
414     // Detect the correct position of the plugin icon
415     if(!preg_match("/\//",$plIcon)){
416       $image= get_template_path("plugins/".preg_replace('%^.*/([^/]+)/[^/]+$%', '\1', 
417             $class_mapping[$class])."/images/$plIcon");
418     }else{
419       $image = $plIcon; 
420     }
421     // Detect the correct position of the plugin icon
422     if(!preg_match("/\//",$plShortIcon)){
423       $shortImage= get_template_path("plugins/".preg_replace('%^.*/([^/]+)/[^/]+$%', '\1', 
424             $class_mapping[$class])."/images/$plShortIcon");
425     }else{
426       $shortImage = $plShortIcon; 
427     }
428     return(array($index, $plHeadline, $plDescription, $image, $shortImage));
429   }
432   /*! \brief    Returns the installation path of a plugin.
433    *            e.g. '../plugins/admin/mimetypes'
434    */
435   function get_path($index)
436   {
437     if(!isset($this->dirlist[$index])){
438       return ("");
439     }
440     return ("../".$this->dirlist[$index]);
441   }
444   /*! \brief    Returns the plugins id for a given class.
445    */
446   function get_index($class)
447   {
448     return (array_search($class, $this->pluginList));
449   }
452   /*! \brief  This function checks if we are allowed to view the plugin with the given id 
453    *
454    *  @param  $plug_id  Integer  The ID of the plugin.
455    *  @return Boolean   TRUE if we are allowed to view the plugin else FASLE
456    */
457   function plugin_access_allowed($plug_id)
458   {
459     return(isset($this->pluginList[$plug_id]));
460   }
463   /*! \brief  Force the menu to be recreated 
464    */
465   function reset_menus()
466   {
467     $this->menu = "";
468     $this->iconmenu ="";
469   }
472   /*! \brief    Generates an array containing plugin names (headlines) and theirs ids.
473    *            This is just used in the helpviewer.php  
474    */
475   function gen_headlines()
476   {
477     $ret = array();
478     if(count($this->headlines) == 0){
479       foreach($this->config->data['MENU'] as $headline => $plugins){
480         foreach( $plugins as $id => $plug){
481           if (plugin_available($plug['CLASS'])){
482             $attrs = (get_class_vars($plug['CLASS']));
483             $ret[$id]['HEADLINE'] = $headline;
484             $ret[$id]['NAME']     = $attrs['plHeadline'];
485           }
486         }
487       }
488       $this->headlines = $ret;
489     }
490     return($this->headlines);
491   }
493 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
494 ?>