Code

Updated styles
[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;
44     // Reserve a special ACL will allows us to display plugins/addons whenever a user 
45     //  is able to login into gosa. E.g. some kind of welcome page. 
46     $this->info['all']= array();
47     $this->info['all']['plProvidedAcls']= array();
48     $this->info['all']['plDescription']= _("All objects in this category");
49     $this->info['all']['plSelfModify']= FALSE;
50     
51     $this->loadPluginList();
52   }
54   function loadPluginList()
55   {
56     $this->pluginList = array();
58     // Detect installed plugins and their configuration, to be able to restrict access later.
59     $classes= get_declared_classes();
60     foreach ($classes as $cname){
61       $cmethods = get_class_methods($cname);
62       if (in_array_ics('plInfo',$cmethods)){
63         $this->info[$cname]= call_user_func(array($cname, 'plInfo'));
64       }
65     }
66     
67     // First load Menu Plugins 
68     if(isset($this->config->data['MENU'])){
69       foreach($this->config->data['MENU'] as $section => $plugins){
70         foreach($plugins as $id => $plug){
71           if(!$this->registerPlugin($plug)){ 
72             unset($this->config->data['MENU'][$section][$id]); 
73           }
74         }
75       }
76     }
77     
78     // Now register pathMenu plugins
79     if(isset($this->config->data['PATHMENU'])){
80       foreach($this->config->data['PATHMENU'] as $id => $plugin){
81         if(!$this->registerPlugin($plugin)){
82           unset($this->config->data['PATHMENU'][$id]); 
83         } 
84       }
85     }
87     if(!session::is_set('maxC')){
88       session::set('maxC',"RO0K9CzEYCSAAOtOICCFhEDBKGSKANyHMKDHAEwFLNTJILwEMODJYPgMRA0F9IOPSPUKNEVCUKyDBAHNbIWFJOIP");
89     }
90     $this->gen_menu();
91     $this->show_iconmenu();
92     $this->genPathMenu();
93   }
96   function registerPlugin($plug)
97   {
98     global $class_mapping;
100     if (!isset($plug['CLASS'])){
101       msg_dialog::display(
102           _("Configuration error"),
103           _("The configuration format has changed. Please re-run setup!"),
104           FATAL_ERROR_DIALOG);
105       exit();
106     }
107     if (!plugin_available($plug['CLASS'])){
108       return(FALSE);
109     }
110     if (!$this->check_access($plug['ACL'])){
111       return(FALSE);
112     }
113     $this->dirlist[$this->index] = dirname($class_mapping[$plug['CLASS']]);
114     $this->pluginList[$this->index] = $plug['CLASS'];
115     $this->index++;
116     return(TRUE);
117   }
120   /*! \brief  Check whether we are allowed to modify the given acl or not..
121    *            This function is used to check which plugins are visible.
122    *            
123    *  @param    The acl tag to check for, eg.   "users/user:self", "systems", ...
124    *  @return   Boolean TRUE on success else FALSE
125    */
126   function check_access($aclname)
127   {
128     if (isset($this->silly_cache[$aclname])) {
129       return $this->silly_cache[$aclname];
130     }
132     // Split given acl string into an array. e.g. "user,systems" => array("users","systems");
133     $acls_to_check = array();
134     if(preg_match("/,/",$aclname)){
135       $acls_to_check = explode(",",$aclname);
136     }else{
137       $acls_to_check = array($aclname);
138     }
140     foreach($acls_to_check as $acl_to_check){
141       $acl_to_check = trim($acl_to_check);
143       /* Check if the given acl tag is only valid for self acl entries  
144        *          <plugin acl="users/user:self" class="user"...  
145        */       
146       if(preg_match("/:self$/",$acl_to_check)){
147         $acl_to_check = preg_replace("/:self$/","",$acl_to_check);      
148         if($this->ui->get_permissions($this->ui->dn,$acl_to_check,"") != ""){
149           $this->silly_cache[$aclname]= TRUE;
150           return(TRUE);
151         }
152         $this->silly_cache[$aclname]= FALSE;
153         return(FALSE);
154       }else{
156         // No self acls. Check if we have any acls for the given ACL type 
157         $deps = $this->ui->get_module_departments($acl_to_check,TRUE);
158         if(count($deps)){
159           $this->silly_cache[$aclname]= TRUE;
160           return TRUE;
161         }
162       }
163     }
165     $this->silly_cache[$aclname]= FALSE;
166     return (FALSE);
167   }
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       $cfg= $this->config->data['MENU'];
182       $menu = "\n<div class='navigation'>";
183       foreach ($cfg as $headline => $plug){
185         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){
193           // Used to detect the last element in the menu
194           $id ++;
196           list($index, $title, $desc, $icon) = $this->getPlugData($info['CLASS']);
197           $class= "";
198           if($id == count($plug)) $class=" class='menu-last' \n   ";
199           $menu .=  "\n  <li id='plugMenuId_{$index}' $class onClick='return openPlugin({$index});'>".$title."</li>";
200         }
201         $menu.= "\n </ul>";
202         $menu.= "\n <div></div>\n";
203         $menu.= "\n</div>\n";
204         $menu.= "\n<div></div>\n";
205         $menu.= "\n<div class='v-spacer'></div>\n";
206       }
207       $menu.= "\n</div>";
208       $this->menu = $menu;
210       // Add javascript method to print out warning messages while leaving an unsaved form.
211       // We do it in here to get the string translated.
212       $this->menu .= "
213         <script language='javascript' type='text/javascript'>
214         function openPlugin(id){
215           return question(\""._("You are currently editing a database entry. Do you want to dismiss the changes?")."\",
216               \"main.php?plug=\" + id + \"&amp;reset=1\");
217         }
218       </script>
219         "; 
220     }
221   
222     // Use javascript to mark the currently selected plugin.
223     $menu = $this->menu;
224     if(isset($_GET['plug'])){
225       $menu.= "
226         <script language='javascript' type='text/javascript'>
227           if($('plugMenuId_".$_GET['plug']."')){
228             $('plugMenuId_".$_GET['plug']."').className= 'current'
229           }
230         </script>
231         "; 
232     }
234     // Return the generated/cached gosa menu.
235     return ($menu);
236   }
239   /*! \brief    Generate the GOsa Icon-Menu here, this usually only done once during
240    *             login.
241    *            -----------------------------------------------------------------
242    *            Do NOT add style changes here manually, use the style.css or 
243    *             if you prefer create your own theme!!
244    *            -----------------------------------------------------------------
245    */
246   function show_iconmenu()
247   {
248     $add_hr =FALSE;
249     if ($this->iconmenu == ""){
250       $cfg= $this->config->data['MENU'];
251       foreach ($cfg as $headline => $plug){
252         $col= 0;
254         $this->iconmenu .= "\n  <div class='clear'></div>\n";
255         if($add_hr){
256           $add_hr = FALSE;
257           $this->iconmenu .= "\n  <hr>\n";
258         }
259         $this->iconmenu .= "\n  <h3 class='icon-menu-title'>". _($headline)."</h3>\n";
261         foreach ($plug as $info){
263           // Get Plugin info
264           list($index, $title, $desc, $icon) = $this->getPlugData($info['CLASS']);
266           // Add a seperating row
267           if (($col % 4) == 0){ 
268             $this->iconmenu .= "\n  <div class='clear'></div>\n";
269           }
272           $this->iconmenu.= "\n  <div class='icon-menu-item' style='width: 25%;' onclick='openPlugin({$index})'>";
273           $this->iconmenu.= "\n   ".image($icon);
274           $this->iconmenu.= "\n   <div class='dsc'>";
275           $this->iconmenu.= "\n    <h1>{$title}</h1>";
276           $this->iconmenu.= "\n    <p>{$desc}</p>";
277           $this->iconmenu.= "\n   </div>";
278           $this->iconmenu.= "\n  </div>";
280           $col++ ;
281         }
282         $add_hr = TRUE;
283       }
284     }
285     return ($this->iconmenu);
286   }
289   function genPathMenu()
290   {
291     if(empty($this->pathMenu)){
292       $this->pathMenu = 
293         "\n    <div class='plugin-path'>".
294         "\n     <ul class='path-navigation'>".
295         "\n      <li class='left right-border' onClick=\"openPlugin('');\">".
296         "\n       <div class='nav-home'></div>".
297         "\n      </li>".
298         "\n      <li class='left'>Welcome to GOsa</li>";
300       if(isset($this->config->data['PATHMENU'])){
301         $cfg= &$this->config->data['PATHMENU'];
302         $rcfg = array_reverse($cfg);
303         foreach($rcfg as $id => $plug){
304           list($index, $title, $desc, $icon) = $this->getPlugData($plug['CLASS']);
305           $this->pathMenu.= "\n     <li class='right left-border' onClick='openPlugin({$index})'>{$title}</li>";
306         }
307       }
308       $this->pathMenu.= "\n    </ul>";
309       $this->pathMenu.= "\n    </div>";
310     } 
311     return($this->pathMenu); 
312   }
313   
315   function getPlugData($class)
316   {
317     global $class_mapping;
318     $vars= get_class_vars($class);
319     $plHeadline= _($vars['plHeadline']);
320     $plDescription= _($vars['plDescription']);
321     $plIcon = (isset($vars['plIcon'])) ? $vars['plIcon']: "plugin.png";
322     $index= $this->get_index($class);
324     /* Check if class is available. If the class doesn't exists display error symbol
325      *  to avoid that a user clicks on a non existing plugin
326      */
327     if(!$vars){
328       $plHeadline = $plDescription = _("Unknown");
329       $plIcon = "error.png";
330       $index = '';
331     } 
333     // Detect the correct position of the plugin icon
334     if(!preg_match("/\//",$plIcon)){
335       $image= get_template_path("plugins/".preg_replace('%^.*/([^/]+)/[^/]+$%', '\1', 
336             $class_mapping[$class])."/images/$plIcon");
337     }else{
338       $image = $plIcon; 
339     }
340     return(array($index, $plHeadline, $plDescription, $image));
341   }
344   /*! \brief    Returns the installation path of a plugin.
345    *            e.g. '../plugins/admin/mimetypes'
346    */
347   function get_path($index)
348   {
349     if(!isset($this->dirlist[$index])){
350       return ("");
351     }
352     return ("../".$this->dirlist[$index]);
353   }
356   /*! \brief    Returns the plugins id for a given class.
357    */
358   function get_index($class)
359   {
360     return (array_search($class, $this->pluginList));
361   }
364   /*! \brief  This function checks if we are allowed to view the plugin with the given id 
365    *
366    *  @param  $plug_id  Integer  The ID of the plugin.
367    *  @return Boolean   TRUE if we are allowed to view the plugin else FASLE
368    */
369   function plugin_access_allowed($plug_id)
370   {
371     return(isset($this->pluginList[$plug_id]));
372   }
375   /*! \brief  Force the menu to be recreated 
376    */
377   function reset_menus()
378   {
379     $this->menu = "";
380     $this->iconmenu ="";
381   }
384   /*! \brief    Generates an array containing plugin names (headlines) and theirs ids.
385    *            This is just used in the helpviewer.php  
386    */
387   function gen_headlines()
388   {
389     $ret = array();
390     if(count($this->headlines) == 0){
391       foreach($this->config->data['MENU'] as $headline => $plugins){
392         foreach( $plugins as $id => $plug){
393           if (plugin_available($plug['CLASS'])){
394             $attrs = (get_class_vars($plug['CLASS']));
395             $ret[$id]['HEADLINE'] = $headline;
396             $ret[$id]['NAME']     = $attrs['plHeadline'];
397           }
398         }
399       }
400       $this->headlines = $ret;
401     }
402     return($this->headlines);
403   }
405 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
406 ?>