[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.2, Wed Mar 14 20:19:03 2007 UTC revision 1.8, Thu Dec 6 16:11:19 2007 UTC
# Line 6  Line 6 
6    
7  use strict;  use strict;
8  use warnings;  use warnings;
9    use Tracer;
10    
 use Carp qw( confess );  
   
 use CGI;  
   
 1;  
11    
12  =pod  =pod
13    
# Line 62  Line 58 
58                   entries => {},                   entries => {},
59                   categories => [],                   categories => [],
60                   categories_index => {},                   categories_index => {},
61                     search => 0,
62      };      };
63      bless $self, $class;      bless $self, $class;
64    
# Line 83  Line 80 
80      $self->{entries} = {};      $self->{entries} = {};
81      $self->{categories} = [];      $self->{categories} = [];
82      $self->{categories_index} = {};      $self->{categories_index} = {};
83        $self->{search} = 0;
84      return $self;      return $self;
85  }  }
86    
# Line 107  Line 105 
105    
106  =pod  =pod
107    
108  =item * B<add_category> (I<category>, I<url>, I<target>)  =item * B<add_category> (I<category>, I<url>, I<target>, I<right>)
109    
110  Adds a category to the menu. I<category> is mandatory and expects the name of the  Adds a category to the menu. I<category> is mandatory and expects the name of the
111  menu category. I<url> is optional and will add a link to the category name in the menu.  menu category. I<url> is optional and will add a link to the category name in the menu.
112  I<target> is optional and defines a href target for that link.  I<target> is optional and defines a href target for that link. The optional I<right>
113    parameter specifies the right a user must have to be able to see this category.
114    
115  =cut  =cut
116    
117  sub add_category {  sub add_category {
118      my ($self, $category, $url, $target) = @_;      my ($self, $category, $url, $target, $right, $order) = @_;
119    
120      unless ($category) {      unless ($category) {
121          confess 'No category given.';          die 'No category given.';
122        }
123    
124        unless ($order) {
125          $order = scalar(@{$self->{categories}});
126      }      }
127    
128      if (exists($self->{categories_index}->{$category})) {      if (exists($self->{categories_index}->{$category})) {
129          confess "Trying to add category '$category' which already exists.";          die "Trying to add category '$category' which already exists.";
130      }      }
131    
132      $url = '' unless ($url);      $url = '' unless ($url);
# Line 133  Line 136 
136      $self->{categories_index}->{$category} = scalar(@{$self->{categories}});      $self->{categories_index}->{$category} = scalar(@{$self->{categories}});
137    
138      # add the category and link      # add the category and link
139      push @{$self->{categories}}, [ $category, $url, $target ];      push @{$self->{categories}}, [ $category, $url, $target, $right, $order ];
140    
141      # init the entries array for that category      # init the entries array for that category
142      $self->{entries}->{$category} = [];      $self->{entries}->{$category} = [];
# Line 155  Line 158 
158      my ($self, $category) = @_;      my ($self, $category) = @_;
159    
160      unless ($category) {      unless ($category) {
161          confess 'No category given.';          die 'No category given.';
162      }      }
163    
164      my $i = $self->{categories_index}->{$category};      my $i = $self->{categories_index}->{$category};
# Line 184  Line 187 
187      return keys(%{$_[0]->{categories_index}});      return keys(%{$_[0]->{categories_index}});
188  }  }
189    
190    =pod
191    
192    =item * B<search> (I<search_component>)
193    
194    Getter / Setter for the search component of the menu
195    
196    =cut
197    
198    sub search {
199      my ($self, $search) = @_;
200    
201      if (defined($search)) {
202        $self->{search} = $search;
203      }
204    
205      return $self->{search};
206    }
207    
208  =pod  =pod
209    
# Line 199  Line 219 
219  sub add_entry {  sub add_entry {
220      my ($self, $category, $entry, $url, $target) = @_;      my ($self, $category, $entry, $url, $target) = @_;
221    
222      unless ($category and $entry and $url) {      unless ($category and $entry){# and $url) {
223          confess "Missing parameter ('$category', '$entry', '$url').";          die "Missing parameter ('$category', '$entry', '$url').";
224      }      }
225    
226      unless (exists($self->{categories_index}->{$category})) {      unless (exists($self->{categories_index}->{$category})) {
227          confess "Trying to add to non-existant category '$category'.";          die "Trying to add to non-existant category '$category'.";
228      }      }
229    
230      $target = '' unless ($target);      $target = '' unless ($target);
# Line 216  Line 236 
236    
237  =pod  =pod
238    
239  =item * B<output> ()  =item * B<output> (I<application>)
240    
241  Returns the html output of the menu.  Returns the html output of the menu. I<application> must be a reference to the
242    application this menu is being printed in. This is only neccessary if rights
243    are required for any category to be displayed.
244    
245  =cut  =cut
246    
247  sub output {  sub output {
248    my $self = shift;    my ($self, $application) = @_;
249    
250      return '' unless scalar(@{$self->{categories}});
251    
252    my $html = "<div id='menu'>\n";    my $html = "<div id='menu'>\n";
253    $html .= "\t<ul id='nav'>\n";    $html .= "\t<ul id='nav'>\n";
254    
255    foreach (@{$self->{categories}}) {    my @ordered_categories = sort { $a->[4] <=> $b->[4] } @{$self->{categories}};
256      foreach (@ordered_categories) {
257    
258        my ($cat, $c_url, $c_target, $right, $order) = @$_;
259    
260        # check if a right is required to see this category
261        if (defined($right)) {
262          unless (defined($application) && ref($application) eq 'WebApplication') {
263            die "When using rights for a menu category, an application reference must be passed.";
264          }
265          next unless ($application->session->user &&
266                       $application->session->user->has_right($application, @$right));
267        }
268    
     my ($cat, $c_url, $c_target) = @$_;  
269      my $url = ($c_url) ? qq~href="$c_url"~ : '';      my $url = ($c_url) ? qq~href="$c_url"~ : '';
270      my $target = ($c_target) ? qq~target="$c_target"~ : '';      my $target = ($c_target) ? qq~target="$c_target"~ : '';
271    
# Line 243  Line 278 
278        foreach (@{$self->{entries}->{$cat}}) {        foreach (@{$self->{entries}->{$cat}}) {
279    
280          my ($entry, $e_url, $e_target) = @$_;          my ($entry, $e_url, $e_target) = @$_;
281            if ($e_url) {
282          my $target = ($e_target) ? qq~target="$e_target"~ : '';          my $target = ($e_target) ? qq~target="$e_target"~ : '';
283          $html .= qq~\t\t\t<li><a href="$e_url" $target>$entry</a></li>\n~;          $html .= qq~\t\t\t<li><a href="$e_url" $target>$entry</a></li>\n~;
284            } else {
285              $html .= qq~\t\t\t<li>$entry</li>\n~;
286            }
287        }        }
288    
289        $html .= "\t\t</ul>\n";        $html .= "\t\t</ul>\n";
# Line 256  Line 295 
295    }    }
296    
297    $html .= "\t</ul>\n";    $html .= "\t</ul>\n";
298    
299    $html .= "</div>\n";    $html .= "</div>\n";
300    
301    return $html;    return $html;
302    
303  }  }
304    
305    1;

Legend:
Removed from v.1.2  
changed lines
  Added in v.1.8

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3