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

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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3