Code

Do not render path menu if empty
[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     // Get list of plugin paths, this allows us to open the plugins main.inc if available.
43     $this->dirlist= $this->get_plugins ($this->dirlist, $this->config->data['MENU']);
45     // Detect installed plugins and their configuration, to be able to restrict access later.
46     $classes= get_declared_classes();
47     foreach ($classes as $cname){
48       $cmethods = get_class_methods($cname);
49       if (in_array_ics('plInfo',$cmethods)){
50         $this->info[$cname]= call_user_func(array($cname, 'plInfo'));
51       }
52     }
54     // Reserve a special ACL will allows us to display plugins/addons whenever a user 
55     //  is able to login into gosa. E.g. some kind of welcome page. 
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     
61     $this->gen_menu();
62   }
65   /*! \brief    Detect plugin installation paths, by walking through the config. 
66    */
67   function get_plugins($list, &$config)
68   {
69     global $class_mapping;
70     if (!isset($config['CLASS'])){
71       if (is_array($config)){
72         foreach ($config as $val){
73           $list= $this->get_plugins($list, $val);
74         }
75       }
76     } else {
77       if (is_array($config) && isset($class_mapping[$config['CLASS']])){
78         $list[$this->index++]= dirname($class_mapping[$config['CLASS']]);
79       } else {
80         $list[$this->index++]= "";
81       }
82     }
84     return ($list);
85   }
88   /*! \brief  Check whether we are allowed to modify the given acl or not..
89    *            This function is used to check which plugins are visible.
90    *            
91    *  @param    The acl tag to check for, eg.   "users/user:self", "systems", ...
92    *  @return   Boolean TRUE on success else FALSE
93    */
94   function check_access($aclname)
95   {
96     if (isset($this->silly_cache[$aclname])) {
97       return $this->silly_cache[$aclname];
98     }
100     // Split given acl string into an array. e.g. "user,systems" => array("users","systems");
101     $acls_to_check = array();
102     if(preg_match("/,/",$aclname)){
103       $acls_to_check = explode(",",$aclname);
104     }else{
105       $acls_to_check = array($aclname);
106     }
108     foreach($acls_to_check as $acl_to_check){
109       $acl_to_check = trim($acl_to_check);
111       /* Check if the given acl tag is only valid for self acl entries  
112        *          <plugin acl="users/user:self" class="user"...  
113        */       
114       if(preg_match("/:self$/",$acl_to_check)){
115         $acl_to_check = preg_replace("/:self$/","",$acl_to_check);      
116         if($this->ui->get_permissions($this->ui->dn,$acl_to_check,"") != ""){
117           $this->silly_cache[$aclname]= TRUE;
118           return(TRUE);
119         }
120         $this->silly_cache[$aclname]= FALSE;
121         return(FALSE);
122       }else{
124         // No self acls. Check if we have any acls for the given ACL type 
125         $deps = $this->ui->get_module_departments($acl_to_check,TRUE);
126         if(count($deps)){
127           $this->silly_cache[$aclname]= TRUE;
128           return TRUE;
129         }
130       }
131     }
133     $this->silly_cache[$aclname]= FALSE;
134     return (FALSE);
135   }
138   /*! \brief    Check the accessibility of the configured plugins.
139    *            We may simply have now permissions to access some plugins 
140    *             but some may be broken or missing!.
141    */
142   function checkMenu()
143   {
144     $cfg= &$this->config->data['MENU'];
145     foreach ($cfg as $headline => $plug){
146       $this->menuparts[_($headline)]= array();
147       foreach ($plug as $id => $info){
148         if (!isset($info['CLASS'])){
149           msg_dialog::display(
150               _("Configuration error"), 
151               _("The configuration format has changed. Please re-run setup!"), 
152               FATAL_ERROR_DIALOG);
153           exit();
154         }
155         if (!plugin_available($info['CLASS'])){
156           unset($cfg[$headline][$id]); 
157           continue; 
158         }
159         if (!$this->check_access($info['ACL'])){
160           unset($cfg[$headline][$id]);
161           continue; 
162         }
163       }
164     }
165     if(!session::is_set('maxC')){
166       session::set('maxC',"RO0K9CzEYCSAAOtOICCFhEDBKGSKANyHMKDHAEwFLNTJILwEMODJYPgMRA0F9IOPSPUKNEVCUKyDBAHNbIWFJOIP");
167     }
168   }
171   /*! \brief    Generate the GOsa Main-Menu here (The menu on the left), 
172    *             this usually only done once during login.
173    *            -----------------------------------------------------------------
174    *            Do NOT add style changes here manually, use the style.css or 
175    *             if you prefer create your own theme!!
176    *            -----------------------------------------------------------------
177    */
178   function gen_menu()
179   {
180     if ($this->menu == ""){
181       $this->checkMenu();
182       $cfg= $this->config->data['MENU'];
183       $menu = "\n<div class='navigation'>";
184       foreach ($cfg as $headline => $plug){
186         if(!count($plug)) continue;
187         $menu.= "\n<div class='menu'>";
188         $menu.= "\n <ul>";
189         $menu.= "\n  <li class='menu-header'>"._($headline)."</li>";
190         $id = 0;
191         foreach ($plug as $info){
192           $id ++;
193           list($index, $title, $desc, $icon) = $this->getPlugData($info['CLASS']);
194           $this->allowed_plugins[$index] = $index;
195           $class= "";
196           if($id == count($plug)) $class=" class='menu-last' \n   ";
197           $menu .=  "\n  <li id='plugMenuId_{$index}' $class onClick='return openPlugin({$index});'>".$title."</li>";
198         }
199         $menu.= "\n </ul>";
200         $menu.= "\n <div></div>\n";
201         $menu.= "\n</div>\n";
202         $menu.= "\n<div></div>\n";
203         $menu.= "\n<div class='v-spacer'></div>\n";
204       }
205       $menu.= "\n</div>";
206       $this->menu = $menu;
208       // Add javascript method to print out warning messages while leaving an unsaved form.
209       // We do it in here to get the string translated.
210       $this->menu .= "
211         <script language='javascript' type='text/javascript'>
212         function openPlugin(id){
213           return question(\""._("You are currently editing a database entry. Do you want to dismiss the changes?")."\",
214               \"main.php?plug=\" + id + \"&amp;reset=1\");
215         }
216       </script>
217         "; 
218     }
219   
220     // Use javascript to mark the currently selected plugin.
221     $menu = $this->menu;
222     if(isset($_GET['plug'])){
223       $menu.= "
224         <script language='javascript' type='text/javascript'>
225           if($('plugMenuId_".$_GET['plug']."')){
226             $('plugMenuId_".$_GET['plug']."').className= 'current'
227           }
228         </script>
229         "; 
230     }
232     // Return the generated/cached gosa menu.
233     return ($menu);
234   }
237   /*! \brief    Generate the GOsa Icon-Menu here, this usually only done once during
238    *             login.
239    *            -----------------------------------------------------------------
240    *            Do NOT add style changes here manually, use the style.css or 
241    *             if you prefer create your own theme!!
242    *            -----------------------------------------------------------------
243    */
244   function show_iconmenu()
245   {
246     $add_hr =FALSE;
247     if ($this->iconmenu == ""){
248       $cfg= $this->config->data['MENU'];
249       foreach ($cfg as $headline => $plug){
250         $col= 0;
252         $this->iconmenu .= "\n  <div class='clear'></div>\n";
253         if($add_hr){
254           $add_hr = FALSE;
255           $this->iconmenu .= "\n  <hr>\n";
256         }
257         $this->iconmenu .= "\n  <h3 class='icon-menu-title'>". _($headline)."</h3>\n";
259         foreach ($plug as $info){
261           // Get Plugin info
262           list($index, $title, $desc, $icon) = $this->getPlugData($info['CLASS']);
264           // Add a seperating row
265           if (($col % 4) == 0){ 
266             $this->iconmenu .= "\n  <div class='clear'></div>\n";
267           }
269           $this->iconmenu.= "\n  <div class='container-element' style='width: 25%;' onclick='openPlugin({$index})'> ";
270           $this->iconmenu.= "\n   <div class='icon-menu-item' style='background-image: url({$icon});'>";
271           $this->iconmenu.= "\n    <h1>{$title}</h1>";
272           $this->iconmenu.= "\n    <p>{$desc}</p>";
273           $this->iconmenu.= "\n   </div>";
274           $this->iconmenu.= "\n  </div>\n";
275           $col++ ;
276         }
277         $add_hr = TRUE;
278       }
279     }
280     return ($this->iconmenu);
281   }
284   function genPathMenu()
285   {
286     if(1 | empty($this->pathMenu)){
288       $this->pathMenu = 
289         "\n    <div class='plugin-path'>".
290         "\n     <ul class='path-navigation'>".
291         "\n      <li class='left right-border' onClick=\"openPlugin('');\">".
292         "\n       <div class='nav-home'></div>".
293         "\n      </li>".
294         "\n      <li class='left'>Welcome to GOsa</li>";
296       if(isset($this->config->data['PATHMENU'])){
297         $cfg= &$this->config->data['PATHMENU'];
298         $rcfg = array_reverse($cfg);
299         foreach($rcfg as $id => $plug){
301           // Do not add inaccessible plugins
302           if (!$this->check_access($plug['ACL'])){
303             unset($rcfg[$id]);
304             continue; 
305           }
307           list($index, $title, $desc, $icon) = $this->getPlugData($plug['CLASS']);
308           $this->pathMenu.= "\n     <li class='right left-border'>{$title}</li>";
309         }
310       }
311       $this->pathMenu.= "\n    </ul>";
312       $this->pathMenu.= "\n    </div>";
313     } 
314     return($this->pathMenu); 
315   }
316   
318   function getPlugData($class)
319   {
320     global $class_mapping;
321     $vars= get_class_vars($class);
322     $plHeadline= _($vars['plHeadline']);
323     $plDescription= _($vars['plDescription']);
324     $plIcon = (isset($vars['plIcon'])) ? $vars['plIcon']: "plugin.png";
325     $index= $this->get_index($class);
327     /* Check if class is available. If the class doesn't exists display error symbol
328      *  to avoid that a user clicks on a non existing plugin
329      */
330     if(!$vars){
331       $plHeadline = $plDescription = _("Unknown");
332       $plIcon = "error.png";
333       $index = '';
334     } 
336     // Detect the correct position of the plugin icon
337     if(!preg_match("/\//",$plIcon)){
338       $image= get_template_path("plugins/".preg_replace('%^.*/([^/]+)/[^/]+$%', '\1', 
339             $class_mapping[$class])."/images/$plIcon");
340     }else{
341       $image = $plIcon; 
342     }
343     return(array($index, $plHeadline, $plDescription, $image));
344   }
347   /*! \brief    Returns the installation path of a plugin.
348    *            e.g. '../plugins/admin/mimetypes'
349    */
350   function get_path($index)
351   {
352     if(!isset($this->dirlist[$index])){
353       return ("");
354     }
355     return ("../".$this->dirlist[$index]);
356   }
359   /*! \brief    Returns the plugins id for a given class.
360    */
361   function get_index($class)
362   {
363     /* Search for plugin index (id), identify entry by path && class */
364     $data = $this->config->data['MENU'];
365     foreach($data as $section => $plugins){
366       foreach($plugins as $key => $plugin)    {
367         if($plugin['CLASS'] == $class){
368           return($key);
369         }
370       }
371     }
372     return (0);
373   }
376   /*! \brief  This function checks if we are allowed to view the plugin with the given id 
377    *
378    *  @param  $plug_id  Integer  The ID of the plugin.
379    *  @return Boolean   TRUE if we are allowed to view the plugin else FASLE
380    */
381   function plugin_access_allowed($plug_id)
382   {
383     return(isset($this->allowed_plugins[$plug_id]));
384   }
387   /*! \brief  Force the menu to be recreated 
388    */
389   function reset_menus()
390   {
391     $this->menu = "";
392     $this->iconmenu ="";
393   }
396   /*! \brief    Generates an array containing plugin names (headlines) and theirs ids.
397    *            This is just used in the helpviewer.php  
398    */
399   function gen_headlines()
400   {
401     $ret = array();
402     if(count($this->headlines) == 0){
403       foreach($this->config->data['MENU'] as $headline => $plugins){
404         foreach( $plugins as $id => $plug){
405           if (plugin_available($plug['CLASS'])){
406             $attrs = (get_class_vars($plug['CLASS']));
407             $ret[$id]['HEADLINE'] = $headline;
408             $ret[$id]['NAME']     = $attrs['plHeadline'];
409           }
410         }
411       }
412       $this->headlines = $ret;
413     }
414     return($this->headlines);
415   }
417 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
418 ?>