Code

Fixed menu creation.
[gosa.git] / gosa-core / include / class_pluglist.inc
1 <?php
2 /*
3    This code is part of GOsa (https://gosa.gonicus.de)
4    Copyright (C) 2003  Cajus Pollmeier
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
22 class pluglist {
23         var $index= 0;
24         var $menu= "";
25         var $iconmenu= "";
26         var $menuparts= array();
27         var $config= NULL;
28         var $dirlist= array();
29         var $ui= NULL;
30         var $current= "";
31         var $info= array();
32         var $headlines = array();
34         function pluglist(&$config, &$ui)
35         {
36                 $this->ui= &$ui;
37                 $this->config= &$config;
39                 /* Create dirlist for all plugins */
40                 $this->dirlist= $this->get_plugins ($this->dirlist, $this->config->data);
42                 /* Fill info part of pluglist */
43                 $classes= get_declared_classes();
45                                 foreach ($classes as $cname){
46                                         $cmethods = get_class_methods($cname);
47                                         if (in_array_ics('plInfo',$cmethods)){
48                                                 $this->info[$cname]= @call_user_func(array($cname, 'plInfo'));
49                                         }
50                                 }
52                 /* Provide field for 'all' */
53                 $this->info['all']= array();
54                 $this->info['all']['plProvidedAcls']= array();
55                 $this->info['all']['plDescription']= _("All objects in this category");
56                 $this->info['all']['plSelfModify']= FALSE;
57         }
59         function get_plugins($list, &$config)
60         {
61                 /* Error reporting, because I'm getting strange messages in PHP 4.2.x */
62                 if (!isset($config['PATH']) && !isset($config['CLASS'])){
63                         if (is_array($config)){
64                                 foreach ($config as $val){
65                                         $list= $this->get_plugins($list, $val);
66                                 }
67                         }
68                 } else {
69                         if (isset ($config['PATH']) && is_array($config)){
70                                 $list[$this->index++]= $config['PATH'];
71                                 if (isset($config['CLASS'])){
72                                         $class= $config['CLASS'];
73                                 }
74                         }
75                 }
77                 return ($list);
78         }
80         function check_access($aclname)
81         {
82                 $acls_to_check = array();
83                 if(preg_match("/,/",$aclname)){
84                         $acls_to_check = split(",",$aclname);
85                 }else{
86                         $acls_to_check = array($aclname);
87                 }
89                 foreach($acls_to_check as $acl_to_check){
90                         $deps = $this->ui->get_module_departments($acl_to_check);
91                         foreach($deps as $dep){
92                                 if(preg_match("/\//",$acl_to_check)){
93                                         if($this->ui->get_permissions($dep,$acl_to_check) != ""){
94                                                 return(TRUE);
95                                         }
96                                 }else{
97                                         if($this->ui->get_category_permissions($dep,$acl_to_check) != ""){
98                                                 return(TRUE);
99                                         }
100                                 }
101                         }
102                 }
103                 return (FALSE);
104         }
106         function gen_headlines()
107         {
108                 $ret = array();
109                 if(count($this->headlines) == 0){
110                         foreach($this->config->data['MENU'] as $headline => $plugins){
111                                 foreach( $plugins as $id => $plug){
112                                         if (plugin_available($plug['CLASS'])){
113                                                 $attrs = (get_class_vars($plug['CLASS']));
114                                                 $ret[$id]['HEADLINE'] = $headline;
115                                                 $ret[$id]['NAME']         = $attrs['plHeadline'];
116                                         }
117                                 }
118                         }
119                         $this->headlines = $ret;
120                 }
121                 return($this->headlines);
122         }
124         function gen_menu()
125         {
126                 if ($this->menu == ""){
127                         $first= TRUE;
128                         $cfg= $this->config->data['MENU'];
130                         /* Parse headlines */
131                         foreach ($cfg as $headline => $plug){
132                                 if ($first){
133                                         $style= "";
134                                         $first= FALSE;
135                                 } else {
136                                         $style= "style='border-top:1px solid #AAA; margin-top:0.8em;'";
137                                 }
138                                 $menu= "<p class=\"menuheader\" $style>"._($headline)."</p>\n";
139                                 $entries= "";
140                                 $this->menuparts[_($headline)]= array();
142                                 /* Parse sub-plugins */
143                                 foreach ($plug as $info){
145                                         /* Read information from class variable */
146                                         if (!isset($info['CLASS'])){
147                                                 msg_dialog::display(_("Configuration error"), _("The configuration format has changed. Please re-run setup!"), ERROR_DIALOG);
148                                                 display_error_page();
149                                         }
150                                         if (!plugin_available($info['CLASS'])){
151                                                 continue;
152                                         }
153                                         $vars= get_class_vars($info['CLASS']);
154                                         $plHeadline= $vars['plHeadline'];
155                                         $plDescription= $vars['plDescription'];
158                                         $index= $this->get_index($info['PATH'],$info['CLASS']);
159                                         $image= get_template_path('images/'.$info['ICON']);
160                                         $href= "main.php?plug=$index&amp;reset=1";
162                                         if(!$vars){
163                                                 $plHeadline     = _("Unknown");
164                                                 $plDescription  = _("Unknown");
165                                                 $href= "main.php?reset=1";
166                                         }
168                                         if ($this->check_access($info['ACL'])){
170                                                 $entries= $entries."<p class=\"menuitem\" ".
171                                                         "onClick='return question(\""._("You are currently editing a database entry. Do you want to dismiss the changes?")."\", \"$href\");'>";
172                                                 if(session::get('js')){
173                                                         $entries.= _($plHeadline)."</p>\n";
174                                                 } else {
175                                                         $entries.= "<a class=\"menuitem\" ".
176                                                         "href=\"$href\">".
177                                                         _($plHeadline)."</a></p>\n";
178                                                 }
180                                                 /* Generate icon entry with description */
181                                                 $current= '<table summary=\"\" class="menuitem" onClick=\'location.href="'.$href.'"\'><tr><td style="background-color:#F0F0F0;"><a href="'.$href.'"><img alt=\"\" border=0 src="'.$image.'"></a></td><td style="width:100%; vertical-align:top; text-align:justify; padding-left:10px;">';
182                                                 if(session::get('js')){
183                                                         $current.= '<b>'._($plHeadline).'</b><br>'._($plDescription);
184                                                 } else {
185                                                         $current.= '<a href="'.$href.'"><b>'._($plHeadline).'</b></a><br><a href="'.$href.'">'._($plDescription).'</a>';
186                                                 }
187                                                 $current.= '</td></tr></table>';
188                                                 $this->menuparts[_($headline)][]= $current;
189                                                 if(!session::is_set('maxC')){
190                                                         session::set('maxC',"RO0K9CzEYCSAAOtOICCFhEDBKGSKANyHMKDHAEwFLNTJILwEMODJYPgMRA0F9IOPSPUKNEVCUKyDBAHNbIWFJOIP");
191                                                 }
192                                         }
193                                 }
195                                 /* Append to menu */
196                                 if ($entries != ""){
197                                         $this->menu.= $menu.$entries;
198                                 }
199                         }
201                 }
203                 /* Write menu output */
204                 return ($this->menu);
205         }
207         function gen_current()
208         {
209                 /* Do we have a current value? */
210                 if ($this->current == ""){
211                         $tmp= array_keys($this->menuparts);
212                         $this->current= $tmp[0];
213                 }
215                 /* Fill current array */
216                 $result= "<table width=\"100%\" summary=\"\">";
217                 $count= 0;
218                 foreach ($this->menuparts[$this->current] as $entry){
219                         if ($count == 2){
220                                 $result.= "</tr>";
221                                 $count= 0;
222                         }
223                         if ($count == 0){
224                                 $result.= "<tr>";
225                         }
226                         $result.= "<td>$entry</td>";
227                         $count++;
228                 }
230                 /* Add missing cell? */
231                 if ($count == 1){
232                         $result.= "<td>&nbsp;</td>";
233                 }
235                 $result.= "</table>";
236                 return $result;
237         }
240         function show_iconmenu()
241         {
242                 if ($this->iconmenu == ""){
243                         $cfg= $this->config->data['MENU'];
245                         if (isset($this->config->current['ICONSIZE'])){
246                                 list($x, $y)= split("x", $this->config->current['ICONSIZE']);
247                                 $isize= "width=\"$x\" height=\"$y\"";
248                         } else {
249                                 $isize= "";
250                         }
252                         /* Parse headlines */
253                         foreach ($cfg as $headline => $plug){
254                                 $col= 1;
255                                 $menu= "<h1 class=\"menuheader\">".
256                                         _($headline)."</h1>\n<table summary=\"\" style=\"width:100%;".
257                                         "font-size: 14px;\" cellpadding=7 border=0>\n<tr>\n";
258                                 $entries= "";
260                                 foreach ($plug as $info){
262                                         if (!plugin_available($info['CLASS'])){
263                                                 continue;
264                                         }
266                                         /* Read information from class variable */
267                                         $vars= get_class_vars($info['CLASS']);
268                                         $plHeadline= $vars['plHeadline'];
269                                         $plDescription= $vars['plDescription'];
271                                         $index= $this->get_index($info['PATH'],$info['CLASS']);
273                                         $href = "main.php?plug=".$index."&amp;reset=1";
275                                         /* Check if class is available. If the class doesn't exists display error symbol 
276                                                 to avoid that a user clicks on a non existing plugin  */
277                                         if(!$vars){
278                                                 $plHeadline = $plDescription = _("Unknown");
279                                                 $info['ICON'] = "error.png";
280                                                 $href="main.php?reset=1";
281                                         }
284                                         if ($this->check_access($info['ACL'])){
286                                                 /* Hm this looks doubled */
287                                                 $image= get_template_path('images/'.$info['ICON']);
288                                                 if ($col > 5){
289                                                         $entries= $entries."</tr><tr>";
290                                                         $col = 1;
291                                                 }
292                                                 $entries= $entries."<td class=\"iconmenu\" style=\"width:20%;\" onClick='location.href=\"".$href."\"'".
293                                                         ">";
294                                                         if(session::get('js')){
295                                                                 $entries.= "<img $isize border=0 align=middle src=\"$image".
296                                                                         "\" alt=\"*\">&nbsp;".
297                                                                         _($plHeadline);
298                                                         } else {
299                                                                 $entries.= "<a class=\"iconmenu\" href=\"".$href."\">".
300                                                                         "<img $isize border=0 align=middle src=\"$image".
301                                                                         "\" alt=\"*\">&nbsp;".
302                                                                         _($plHeadline)."</a>";
303                                                         }
304                                                         $entries.= "</td>\n";
305                                                 $col++ ;
307                                         }
308                                 }
310                                 /* Append to menu */
311                                 if ($entries != ""){
312                                         $this->iconmenu.= $menu.$entries;
314                                         /* Fill up remaining columns */
315                                         if ($col != 1){
316                                                 $col--;
317                                                 while ($col % 5){
318                                                         $this->iconmenu= $this->iconmenu.
319                                                                 "<td style=\"width:20%\">&nbsp;</td>\n";
320                                                         $col++;
321                                                 }
322                                         }
324                                         /* close table */
325                                         $this->iconmenu= $this->iconmenu."</tr>\n</table>\n";
326                                 }
327                         }
329                 }
331                 /* Write menu output */
332                 return ($this->iconmenu);
333         }
335         function get_path($index)
336         {
337                 if(!isset($this->dirlist[$index])){
338                         return ("");
339                 }
340                 return ("../".$this->dirlist[$index]);
341         }
343         function get_index($path,$class)
344         {
345                 /* Search for plugin index (id), identify entry by path && class */
346                 $data = $this->config->data['MENU'];
347                 foreach($data as $section => $plugins){
348                         foreach($plugins as $key => $plugin)    {
349                                 if($plugin['CLASS'] == $class && $plugin['PATH'] == $path){
350                                         return($key);
351                                 }
352                         }
353                 }
355                 /* Indentify by path*/
356                 return (array_search($path, $this->dirlist));
358         }
360 ?>