[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.4, Thu Jun 14 16:34:57 2007 UTC revision 1.13, Mon Oct 12 18:58:32 2009 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                     style => 'vertical',
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 120  Line 118 
118      my ($self, $category, $url, $target, $right, $order) = @_;      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) {      unless ($order) {
# Line 128  Line 126 
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 160  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 189  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    
210  =item * B<add_entry> (I<category>, I<entry>, I<url>)  =item * B<add_entry> (I<category>, I<entry>, I<url>, I<right>)
211    
212  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>
213  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.
214  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
215  defines a href target for that link.  defines a href target for that link.
216    The optional I<right> parameter specifies the right a user must have to
217    be able to see this category.
218    
219  =cut  =cut
220    
221  sub add_entry {  sub add_entry {
222      my ($self, $category, $entry, $url, $target) = @_;      my ($self, $category, $entry, $url, $target, $right) = @_;
223    
224      unless ($category and $entry and $url) {      unless ($category and $entry){# and $url) {
225          confess "Missing parameter ('$category', '$entry', '$url').";          die "Missing parameter ('$category', '$entry', '$url').";
226      }      }
227    
228      unless (exists($self->{categories_index}->{$category})) {      unless (exists($self->{categories_index}->{$category})) {
229          confess "Trying to add to non-existant category '$category'.";          die "Trying to add to non-existant category '$category'.";
230      }      }
231    
232      $target = '' unless ($target);      $target = '' unless ($target);
233    
234      push @{$self->{entries}->{$category}}, [ $entry, $url, $target ];      push @{$self->{entries}->{$category}}, [ $entry, $url, $target, $right ];
235    
236      return $self;      return $self;
237  }  }
# Line 234  Line 251 
251    
252    return '' unless scalar(@{$self->{categories}});    return '' unless scalar(@{$self->{categories}});
253    
254      my $html = "";
255    
256      if ($self->style eq 'vertical') {
257        $html = $self->output_vertical($application);
258      } elsif ($self->style eq 'horizontal') {
259        $html = $self->output_horizontal($application);
260      }
261    
262      return $html;
263    }
264    
265    sub output_vertical {
266      my ($self, $application) = @_;
267    
268    my $html = "<div id='menu'>\n";    my $html = "<div id='menu'>\n";
269    $html .= "\t<ul id='nav'>\n";    $html .= "\t<ul id='nav'>\n";
270    
# Line 245  Line 276 
276      # check if a right is required to see this category      # check if a right is required to see this category
277      if (defined($right)) {      if (defined($right)) {
278        unless (defined($application) && ref($application) eq 'WebApplication') {        unless (defined($application) && ref($application) eq 'WebApplication') {
279          confess "When using rights for a menu category, an application reference must be passed.";          die "When using rights for a menu category, an application reference must be passed.";
280        }        }
281        next unless ($application->session->user && $application->session->user->has_right($application, @$right));        next unless ($application->session->user &&
282                       $application->session->user->has_right($application, @$right));
283      }      }
284    
285      my $url = ($c_url) ? qq~href="$c_url"~ : '';      my $url = ($c_url) ? qq~href="$c_url"~ : '';
# Line 261  Line 293 
293    
294        foreach (@{$self->{entries}->{$cat}}) {        foreach (@{$self->{entries}->{$cat}}) {
295    
296          my ($entry, $e_url, $e_target) = @$_;          my ($entry, $e_url, $e_target, $e_right) = @$_;
297    
298            # check if a right is required to see this category
299            if (defined($e_right)) {
300              unless (defined($application) && ref($application) eq 'WebApplication') {
301                die "When using rights for a menu category, an application reference must be passed.";
302              }
303              next unless ($application->session->user &&
304                           $application->session->user->has_right($application, @$e_right));
305            }
306    
307            if ($e_url) {
308          my $target = ($e_target) ? qq~target="$e_target"~ : '';          my $target = ($e_target) ? qq~target="$e_target"~ : '';
309          $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~;
310            } else {
311              $html .= qq~\t\t\t<li><span>$entry</span></li>\n~;
312            }
313        }        }
314    
315        $html .= "\t\t</ul>\n";        $html .= "\t\t</ul>\n";
# Line 276  Line 322 
322    
323    $html .= "\t</ul>\n";    $html .= "\t</ul>\n";
324    
325    # display user string    $html .= "</div>\n";
326    if ($application->session->user) {  
327      $html .= qq~<div id="user" style="padding-top: 4px; padding-right: 5px; text-align: right;">    return $html;
                 <img height="15px" src="./Html/user.gif" title="Current User" />  
                 <strong>~ . $application->session->user->firstname . " " . $application->session->user->lastname . qq~</strong></div>~;  
328    }    }
329    
330    $html .= "</div>\n";  sub output_horizontal {
331      my ($self, $application) = @_;
332    
333      my $html = "<table class='menu_table'><tr class='menu_cats'>";
334    
335      my @ordered_categories = sort { $a->[4] <=> $b->[4] } @{$self->{categories}};
336      my $i = 0;
337      foreach (@ordered_categories) {
338    
339        my ($cat, $c_url, $c_target, $right, $order) = @$_;
340    
341        # check if a right is required to see this category
342        if (defined($right)) {
343          unless (defined($application) && ref($application) eq 'WebApplication') {
344            die "When using rights for a menu category, an application reference must be passed.";
345          }
346          next unless ($application->session->user &&
347                       $application->session->user->has_right($application, @$right));
348        }
349    
350        $html .= qq~<td class='menu_cat_inactive' onclick='activate_menu_cat("$i");' id='menu_cat_$i'>$cat</td>~;
351        $i++;
352      }
353      $html .= "<td class='menu_filler'>&nbsp</td></tr></table>";
354    
355      $i = 0;
356      foreach (@ordered_categories) {
357    
358        my ($cat, $c_url, $c_target, $right, $order) = @$_;
359    
360        # check if a right is required to see this category
361        if (defined($right)) {
362          unless (defined($application) && ref($application) eq 'WebApplication') {
363            die "When using rights for a menu category, an application reference must be passed.";
364          }
365          next unless ($application->session->user &&
366                       $application->session->user->has_right($application, @$right));
367        }
368        if (scalar(@{$self->{entries}->{$cat}})) {
369    
370          $html .= "<table id='menu_cat_bar_$i' class='menu_cat_bar_inactive'><tr>";
371    
372          foreach (@{$self->{entries}->{$cat}}) {
373    
374            my ($entry, $e_url, $e_target, $e_right) = @$_;
375    
376            # check if a right is required to see this category
377            if (defined($e_right)) {
378              unless (defined($application) && ref($application) eq 'WebApplication') {
379                die "When using rights for a menu category, an application reference must be passed.";
380              }
381              next unless ($application->session->user &&
382                           $application->session->user->has_right($application, @$e_right));
383            }
384    
385            if ($e_url) {
386              my $target = ($e_target) ? qq~target="$e_target"~ : '';
387              $html .= qq~<td class='menu_item'><a href="$e_url" $target>$entry</a></td>~;
388            } else {
389              $html .= qq~<td class='menu_item'><span>$entry</span></td>~;
390            }
391          }
392    
393          $html .= "<td class='menu_item_filler'></td></tr></table>";
394    
395        }
396        $i++;
397      }
398    
399      $html .= qq~
400    <script>
401    function activate_menu_cat (which) {
402      for (i=0; i<1000; i++) {
403        var cat_bar = document.getElementById('menu_cat_bar_'+i);
404        var cat = document.getElementById('menu_cat_'+i);
405        if (cat_bar) {
406          if (i == which) {
407            cat_bar.className = 'menu_cat_bar_active';
408            cat.className = 'menu_cat_active';
409          } else {
410            cat_bar.className = 'menu_cat_bar_inactive';
411            cat.className = 'menu_cat_inactive';
412          }
413        } else {
414          break;
415        }
416      }
417    }
418    </script>
419    ~;
420    
421    return $html;    return $html;
422    }
423    
424    sub style {
425      my ($self, $style) = @_;
426    
427      if (defined($style)) {
428        $self->{style} = $style;
429      }
430    
431      return $self->{style};
432  }  }
433    
434    1;

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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3