Code

Fixed alignment of the icon menu
[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   var $index= 0;
26   var $config= NULL;
27   var $dirlist= array();
28   var $ui= NULL;
29   var $info= array();
30   var $headlines = array();
31   var $silly_cache= array();
33   var $pluginList = array();
35   var $pathMenu = "";
36   var $menu= "";
37   var $iconmenu= "";
39   function pluglist(&$config, &$ui)
40   {
41     $this->ui= &$ui;
42     $this->config= &$config;
43     $this->loadPluginList();
44   }
46   function loadPluginList()
47   {
48     $this->pluginList = array();
50     // First load Menu Plugins 
51     if(isset($this->config->data['MENU'])){
52       foreach($this->config->data['MENU'] as $section => $plugins){
53         foreach($plugins as $id => $plug){
54           if(!$this->registerPlugin($plug)){ 
55             unset($this->config->data['MENU'][$section][$id]); 
56           }
57         }
58       }
59     }
60     
61     // Now register pathMenu plugins
62     if(isset($this->config->data['PATHMENU'])){
63       foreach($this->config->data['PATHMENU'] as $id => $plugin){
64         if(!$this->registerPlugin($plugin)){
65           unset($this->config->data['PATHMENU'][$id]); 
66         } 
67       }
68     }
70     if(!session::is_set('maxC')){
71       session::set('maxC',"RO0K9CzEYCSAAOtOICCFhEDBKGSKANyHMKDHAEwFLNTJILwEMODJYPgMRA0F9IOPSPUKNEVCUKyDBAHNbIWFJOIP");
72     }
73     $this->gen_menu();
74     $this->show_iconmenu();
75     $this->genPathMenu();
76   }
79   function registerPlugin($plug)
80   {
81     global $class_mapping;
83     if (!isset($plug['CLASS'])){
84       msg_dialog::display(
85           _("Configuration error"),
86           _("The configuration format has changed. Please re-run setup!"),
87           FATAL_ERROR_DIALOG);
88       exit();
89     }
90     if (!plugin_available($plug['CLASS'])){
91       return(FALSE);
92     }
93     if (!$this->check_access($plug['ACL'])){
94       return(FALSE);
95     }
96     $this->dirlist[$this->index] = dirname($class_mapping[$plug['CLASS']]);
97     $this->pluginList[$this->index] = $plug['CLASS'];
98     $this->index++;
99     return(TRUE);
100   }
103   /*! \brief  Check whether we are allowed to modify the given acl or not..
104    *            This function is used to check which plugins are visible.
105    *            
106    *  @param    The acl tag to check for, eg.   "users/user:self", "systems", ...
107    *  @return   Boolean TRUE on success else FALSE
108    */
109   function check_access($aclname)
110   {
111     if (isset($this->silly_cache[$aclname])) {
112       return $this->silly_cache[$aclname];
113     }
115     // Split given acl string into an array. e.g. "user,systems" => array("users","systems");
116     $acls_to_check = array();
117     if(preg_match("/,/",$aclname)){
118       $acls_to_check = explode(",",$aclname);
119     }else{
120       $acls_to_check = array($aclname);
121     }
123     foreach($acls_to_check as $acl_to_check){
124       $acl_to_check = trim($acl_to_check);
126       /* Check if the given acl tag is only valid for self acl entries  
127        *          <plugin acl="users/user:self" class="user"...  
128        */       
129       if(preg_match("/:self$/",$acl_to_check)){
130         $acl_to_check = preg_replace("/:self$/","",$acl_to_check);      
131         if($this->ui->get_permissions($this->ui->dn,$acl_to_check,"") != ""){
132           $this->silly_cache[$aclname]= TRUE;
133           return(TRUE);
134         }
135         $this->silly_cache[$aclname]= FALSE;
136         return(FALSE);
137       }else{
139         // No self acls. Check if we have any acls for the given ACL type 
140         $deps = $this->ui->get_module_departments($acl_to_check,TRUE);
141         if(count($deps)){
142           $this->silly_cache[$aclname]= TRUE;
143           return TRUE;
144         }
145       }
146     }
148     $this->silly_cache[$aclname]= FALSE;
149     return (FALSE);
150   }
154   /*! \brief    Generate the GOsa Main-Menu here (The menu on the left), 
155    *             this usually only done once during login.
156    *            -----------------------------------------------------------------
157    *            Do NOT add style changes here manually, use the style.css or 
158    *             if you prefer create your own theme!!
159    *            -----------------------------------------------------------------
160    */
161   function gen_menu()
162   {
163     if ($this->menu == ""){
164       $cfg= $this->config->data['MENU'];
165       $menu = "\n      <div class='navigation'>";
166       foreach ($cfg as $headline => $plug){
168         if(!count($plug)) continue;
170         $menu.= "\n        <div class='menu'>";
171         $menu.= "\n          <ul>";
172         $menu.= "\n            <li class='menu-header'>"._($headline)."</li>";
173         $id = 0;
174         foreach ($plug as $info){
176           // Used to detect the last element in the menu
177           $id ++;
179           list($index, $title, $desc, $icon) = $this->getPlugData($info['CLASS']);
180           $class= "";
181           if($id == count($plug)) $class=" class='menu-last' ";
182           $menu .=  "\n            <li id='plugMenuId_{$index}' $class onClick='return openPlugin({$index});'>".$title."</li>";
183         }
184         $menu.= "\n          </ul>";
185         $menu.= "\n          <div></div>";
186         $menu.= "\n        </div>\n";
187         $menu.= "\n        <div></div>";
188         $menu.= "\n        <div class='v-spacer'></div>\n";
189       }
190       $menu.= "\n      </div>\n";
191       $this->menu = $menu;
193       // Add javascript method to print out warning messages while leaving an unsaved form.
194       // We do it in here to get the string translated.
195       $this->menu .=  
196         "\n      <script language='javascript' type='text/javascript'>".
197         "\n        function openPlugin(id){".
198         "\n          return question(\""._("You are currently editing a database entry. Do you want to dismiss the changes?")."\",".
199         "\n            \"main.php?plug=\" + id + \"&amp;reset=1\");".
200         "\n        }".
201         "\n      </script>\n"; 
202     }
203   
204     // Use javascript to mark the currently selected plugin.
205     $menu = $this->menu;
206     if(isset($_GET['plug'])){
207       $menu.= 
208      "\n      <script language='javascript' type='text/javascript'>".
209      "\n        if($('plugMenuId_".$_GET['plug']."')){".
210      "\n          $('plugMenuId_".$_GET['plug']."').className= 'current'".
211      "\n        }".
212      "\n      </script>\n";
213     }
215     // Return the generated/cached gosa menu.
216     return ($menu);
217   }
220   /*! \brief    Generate the GOsa Icon-Menu here, this usually only done once during
221    *             login.
222    *            -----------------------------------------------------------------
223    *            Do NOT add style changes here manually, use the style.css or 
224    *             if you prefer create your own theme!!
225    *            -----------------------------------------------------------------
226    */
227   function show_iconmenu()
228   {
229     $add_hr =FALSE;
230     $this->iconmenu = "";
231     if ($this->iconmenu == ""){
232       $cfg= $this->config->data['MENU'];
233       foreach ($cfg as $headline => $plug){
234         $col= 0;
236         $this->iconmenu .= "\n        <div class='clear'></div>";
237         if($add_hr){
238           $add_hr = FALSE;
239           $this->iconmenu .= "\n        <hr>";
240         }
241         $this->iconmenu .= "\n        <h3 class='icon-menu-title'>". _($headline)."</h3>";
243         foreach ($plug as $info){
245           // Get Plugin info
246           list($index, $title, $desc, $icon) = $this->getPlugData($info['CLASS']);
248           // Add a seperating row
249           if (($col % 4) == 0){ 
250             $this->iconmenu .= "\n        <div class='clear'></div>";
251           }
253           $this->iconmenu.= "\n        <div class='icon-menu-item' style='width: 25%;' onclick='openPlugin({$index})'>";
254           $this->iconmenu.= "\n          ".image($icon);
255           $this->iconmenu.= "\n          <div class='dsc'>";
256           $this->iconmenu.= "\n            <h1>{$title}</h1>";
257           $this->iconmenu.= "\n            <p>{$desc}</p>";
258           $this->iconmenu.= "\n          </div>";
259           $this->iconmenu.= "\n        </div>";
260           $col++ ;
261         }
262         $add_hr = TRUE;
263       }
264     }
265     return ($this->iconmenu);
266   }
269   function genPathMenu()
270   {
271     if(empty($this->pathMenu)){
272       $this->pathMenu = 
273         "\n        <div class='plugin-path'>".
274         "\n          <ul class='path-navigation'>".
275         "\n            <li class='left right-border' onClick=\"openPlugin('');\">".
276         "\n              <div class='nav-home'></div>".
277         "\n            </li>".
278         "\n            <li class='left'>Welcome to GOsa</li>";
280       if(isset($this->config->data['PATHMENU'])){
281         $cfg= &$this->config->data['PATHMENU'];
282         $rcfg = array_reverse($cfg);
283         foreach($rcfg as $id => $plug){
284           list($index, $title, $desc, $icon) = $this->getPlugData($plug['CLASS']);
285           $this->pathMenu.= "\n            <li class='right left-border' onClick='openPlugin({$index})'>{$title}</li>";
286         }
287       }
288       $this->pathMenu.= "\n          </ul>";
289       $this->pathMenu.= "\n        </div>";
290     } 
291     return($this->pathMenu); 
292   }
293   
295   function getPlugData($class)
296   {
297     global $class_mapping;
298     $vars= get_class_vars($class);
299     $plHeadline= _($vars['plHeadline']);
300     $plDescription= _($vars['plDescription']);
301     $plIcon = (isset($vars['plIcon'])) ? $vars['plIcon']: "plugin.png";
302     $index= $this->get_index($class);
304     /* Check if class is available. If the class doesn't exists display error symbol
305      *  to avoid that a user clicks on a non existing plugin
306      */
307     if(!$vars){
308       $plHeadline = $plDescription = _("Unknown");
309       $plIcon = "error.png";
310       $index = '';
311     } 
313     // Detect the correct position of the plugin icon
314     if(!preg_match("/\//",$plIcon)){
315       $image= get_template_path("plugins/".preg_replace('%^.*/([^/]+)/[^/]+$%', '\1', 
316             $class_mapping[$class])."/images/$plIcon");
317     }else{
318       $image = $plIcon; 
319     }
320     return(array($index, $plHeadline, $plDescription, $image));
321   }
324   /*! \brief    Returns the installation path of a plugin.
325    *            e.g. '../plugins/admin/mimetypes'
326    */
327   function get_path($index)
328   {
329     if(!isset($this->dirlist[$index])){
330       return ("");
331     }
332     return ("../".$this->dirlist[$index]);
333   }
336   /*! \brief    Returns the plugins id for a given class.
337    */
338   function get_index($class)
339   {
340     return (array_search($class, $this->pluginList));
341   }
344   /*! \brief  This function checks if we are allowed to view the plugin with the given id 
345    *
346    *  @param  $plug_id  Integer  The ID of the plugin.
347    *  @return Boolean   TRUE if we are allowed to view the plugin else FASLE
348    */
349   function plugin_access_allowed($plug_id)
350   {
351     return(isset($this->pluginList[$plug_id]));
352   }
355   /*! \brief  Force the menu to be recreated 
356    */
357   function reset_menus()
358   {
359     $this->menu = "";
360     $this->iconmenu ="";
361   }
364   /*! \brief    Generates an array containing plugin names (headlines) and theirs ids.
365    *            This is just used in the helpviewer.php  
366    */
367   function gen_headlines()
368   {
369     $ret = array();
370     if(count($this->headlines) == 0){
371       foreach($this->config->data['MENU'] as $headline => $plugins){
372         foreach( $plugins as $id => $plug){
373           if (plugin_available($plug['CLASS'])){
374             $attrs = (get_class_vars($plug['CLASS']));
375             $ret[$id]['HEADLINE'] = $headline;
376             $ret[$id]['NAME']     = $attrs['plHeadline'];
377           }
378         }
379       }
380       $this->headlines = $ret;
381     }
382     return($this->headlines);
383   }
385 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
386 ?>