[Bio] / WebApplication / WebMenu.pm Repository:
ViewVC logotype

Diff of /WebApplication/WebMenu.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1, Thu Jan 11 16:45:32 2007 UTC revision 1.4, Thu Jun 14 16:34:57 2007 UTC
# Line 1  Line 1 
1  package WebMenu;  package WebMenu;
2    
3    # WebMenu - manage menu for the WeApplication framework
4    
5    # $Id$
6    
7  use strict;  use strict;
8  use warnings;  use warnings;
9    
# Line 32  Line 36 
36    
37  =head1 DESCRIPTION  =head1 DESCRIPTION
38    
39  The WebMenu module defines a mechanism to build a menu structure by defining categories (top level menu entries) and optional links, as well as sub entries for each of the categories (consisting of a entry name, an url and an optional browser target.  The WebMenu module defines a mechanism to build a menu structure by defining
40    categories (top level menu entries) and optional links, as well as sub entries
41  The html output of the menu consists of an unordered list of lists, ie. a two level hierarchy of html links (<a href> tags) embedded in <ul> tags representing categories and their entries.  for each of the categories (consisting of a entry name, an url and an optional
42    browser target.
43    
44    The html output of the menu consists of an unordered list of lists, ie. a two
45    level hierarchy of html links (<a href> tags) embedded in <ul> tags representing
46    categories and their entries.
47    
48  =head1 METHODS  =head1 METHODS
49    
# Line 82  Line 91 
91    
92  =item * B<home> (I<url>)  =item * B<home> (I<url>)
93    
94  Returns the link of the home page. If the optional parameter I<url> is given, home will be set.  Returns the link of the home page. If the optional parameter I<url> is given,
95  I<url> may be undef.  home will be set. I<url> may be undef.
96    
97  =cut  =cut
98    
# Line 98  Line 107 
107    
108  =pod  =pod
109    
110  =item * B<add_category> (I<category>, I<url>, I<target>)  =item * B<add_category> (I<category>, I<url>, I<target>, I<right>)
111    
112  Adds a category to the menu. I<category> is mandatory and expects the name of the menu category. I<url> is optional and will add a link to the category name in the menu. I<target> is optional and defines a href target for that link.  Adds a category to the menu. I<category> is mandatory and expects the name of the
113    menu category. I<url> is optional and will add a link to the category name in the menu.
114    I<target> is optional and defines a href target for that link. The optional I<right>
115    parameter specifies the right a user must have to be able to see this category.
116    
117  =cut  =cut
118    
119  sub add_category {  sub add_category {
120      my ($self, $category, $url, $target) = @_;      my ($self, $category, $url, $target, $right, $order) = @_;
121    
122      unless ($category) {      unless ($category) {
123          confess 'No category given.';          confess 'No category given.';
124      }      }
125    
126        unless ($order) {
127          $order = scalar(@{$self->{categories}});
128        }
129    
130      if (exists($self->{categories_index}->{$category})) {      if (exists($self->{categories_index}->{$category})) {
131          confess "Trying to add category '$category' which already exists.";          confess "Trying to add category '$category' which already exists.";
132      }      }
# Line 122  Line 138 
138      $self->{categories_index}->{$category} = scalar(@{$self->{categories}});      $self->{categories_index}->{$category} = scalar(@{$self->{categories}});
139    
140      # add the category and link      # add the category and link
141      push @{$self->{categories}}, [ $category, $url, $target ];      push @{$self->{categories}}, [ $category, $url, $target, $right, $order ];
142    
143      # init the entries array for that category      # init the entries array for that category
144      $self->{entries}->{$category} = [];      $self->{entries}->{$category} = [];
# Line 135  Line 151 
151    
152  =item * B<delete_category> (I<category>)  =item * B<delete_category> (I<category>)
153    
154  Deletes a category from the menu. I<category> is mandatory and expects the name of the menu category.  Deletes a category from the menu. I<category> is mandatory and expects the
155  If the category does not exist a warning is printed.  name of the menu category. If the category does not exist a warning is printed.
156    
157  =cut  =cut
158    
# Line 178  Line 194 
194    
195  =item * B<add_entry> (I<category>, I<entry>, I<url>)  =item * B<add_entry> (I<category>, I<entry>, I<url>)
196    
197  Adds an entry and link to a existing category of the menu. I<category>, I<entry> and I<url> are mandatory. I<category> expects the name of the menu category. I<entry> can be any string, I<url> expects a url.  Adds an entry and link to a existing category of the menu. I<category>, I<entry>
198  I<target> is optional and defines a href target for that link.  and I<url> are mandatory. I<category> expects the name of the menu category.
199    I<entry> can be any string, I<url> expects a url. I<target> is optional and
200    defines a href target for that link.
201    
202  =cut  =cut
203    
# Line 203  Line 221 
221    
222  =pod  =pod
223    
224  =item * B<output> ()  =item * B<output> (I<application>)
225    
226  Returns the html output of the menu.  Returns the html output of the menu. I<application> must be a reference to the
227    application this menu is being printed in. This is only neccessary if rights
228    are required for any category to be displayed.
229    
230  =cut  =cut
231    
232  sub output {  sub output {
233    my $self = shift;    my ($self, $application) = @_;
234    
235      return '' unless scalar(@{$self->{categories}});
236    
237    my $html = "<div id='menu'>\n";    my $html = "<div id='menu'>\n";
238    $html .= "\t<ul id='nav'>\n";    $html .= "\t<ul id='nav'>\n";
239    
240    foreach (@{$self->{categories}}) {    my @ordered_categories = sort { $a->[4] <=> $b->[4] } @{$self->{categories}};
241      foreach (@ordered_categories) {
242    
243        my ($cat, $c_url, $c_target, $right, $order) = @$_;
244    
245        # check if a right is required to see this category
246        if (defined($right)) {
247          unless (defined($application) && ref($application) eq 'WebApplication') {
248            confess "When using rights for a menu category, an application reference must be passed.";
249          }
250          next unless ($application->session->user && $application->session->user->has_right($application, @$right));
251        }
252    
     my ($cat, $c_url, $c_target) = @$_;  
253      my $url = ($c_url) ? qq~href="$c_url"~ : '';      my $url = ($c_url) ? qq~href="$c_url"~ : '';
254      my $target = ($c_target) ? qq~target="$c_target"~ : '';      my $target = ($c_target) ? qq~target="$c_target"~ : '';
255    
# Line 243  Line 275 
275    }    }
276    
277    $html .= "\t</ul>\n";    $html .= "\t</ul>\n";
278    
279      # display user string
280      if ($application->session->user) {
281        $html .= qq~<div id="user" style="padding-top: 4px; padding-right: 5px; text-align: right;">
282                    <img height="15px" src="./Html/user.gif" title="Current User" />
283                    <strong>~ . $application->session->user->firstname . " " . $application->session->user->lastname . qq~</strong></div>~;
284      }
285    
286    $html .= "</div>\n";    $html .= "</div>\n";
287    
288    return $html;    return $html;

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.4

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3