+"product" category in general.
[www.jankratochvil.net.git] / product / Lib.pm
1 # $Id$
2 # Common functions for HTML/XHTML output generation
3 # Copyright (C) 2003 Jan Kratochvil <project-www.jankratochvil.net@jankratochvil.net>
4
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; exactly version 2 of June 1991 is required
8
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
18
19 package product::Lib;
20 require 5.6.0;  # at least 'use warnings;' but we need some 5.6.0+ modules anyway
21 our $VERSION=do { my @r=(q$Revision$=~/\d+/g); sprintf "%d.".("%03d"x$#r),@r; };
22 our $CVS_ID=q$Id$;
23 use strict;
24 use warnings;
25
26 use My::Web;
27 use Carp qw(cluck confess);
28
29 use Exporter;
30 our @EXPORT=qw();
31 Wrequire 'Lib';
32 our @ISA=qw(My::Web Lib Exporter);
33
34
35 my $LIST_ABSPATH="/product/SUBDIRS";
36
37
38 # Returns: hashref if !wantarray(), list if wantarray().
39 sub list($)
40 {
41 my($self)=@_;
42
43         return $self->list_abspath($LIST_ABSPATH);
44 }
45
46 sub products_sorted($)
47 {
48 my($self)=@_;
49
50         my %item=$self->name_to_hashref();
51         return sort {
52                 ($item{$b}{"priority"} <=> $item{$a}{"priority"})
53                 or
54                 (lc($item{$a}{"name"}) cmp lc($item{$b}{"name"}));
55                 } keys(%item);
56 }
57
58 # $args{"is_index"}=1;
59 sub products($;$%)
60 {
61 my($class,$product_selected,%args)=@_;
62
63         my $r="";
64         $r.='<table border="0" class="margin-center"><tr>'."\n";
65                 $r.='<td>';
66                         $r.='<table border="1" style="border-collapse: collapse; border-style: solid; border-width: 1px;">'."\n";
67                                 $r.='<tr>'."\n";
68                                         $r.='<td style="padding: 5px; font-weight: bold;">'."\n";
69                                                 $r.='Products';
70                                         $r.='</td>'."\n";
71                                 $r.='</tr>'."\n";
72                         $r.='</table>';
73                 $r.='</td>';
74                 $r.='<td>';
75                         $r.='<table border="1" style="border-collapse: collapse; border-style: solid;">'."\n";
76                                 $r.='<tr>'."\n";
77                                         my %item=$class->name_to_hashref();
78                                         for my $product_found ($class->products_sorted()) {
79                                                 my $chosen=($product_selected && $product_selected eq $product_found);
80                                                 $r.='<td style="padding: 5px;">';
81                                                         $r.=($chosen && $args{"is_index"} ? "<b>".$item{$product_found}{"name"}."</b>"
82                                                                         : a_href "/product/$product_found/",$item{$product_found}{"name"},
83                                                                                         "attr"=>($chosen
84                                                                                                         ? 'style="text-decoration: underline; font-weight: bold;"'
85                                                                                                         : 'style="text-decoration: inherit; /* revoke underline */"'));
86                                                 $r.="</td>\n";
87                                                 }
88                                 $r.='</tr>'."\n";
89                         $r.='</table>'."\n";
90                 $r.='</td>'."\n";
91         $r.='</tr></table>'."\n";
92
93         # Products menu DISABLED:
94         $r="";
95
96         if (!$args{"novskip"}) {
97                 Wrequire 'WebConfig';
98                 $r.=WebConfig->vskip_hr();
99                 }
100         return $r;
101 }
102
103 sub init($%)
104 {
105 my($class,%args)=@_;
106
107         $args{"__PACKAGE__"}||=caller();
108         $args{"product_name"}||=($args{"__PACKAGE__"}=~/^product::(\w+)::/)[0]
109                         or cluck "Error finding product name of the package: ".$args{"__PACKAGE__"};
110         my $ListItem=$class->name_to_hashref($args{"product_name"});
111         my $W=$class->SUPER::init(
112                         "title"=>My::Web->a_href_inhibit(sub { return $class->title($ListItem); }),
113                         "footer_ids"=>0,
114                         "no_job"=>1,
115                         "rel_up"=>'/product/',
116 #                       "rel_start"=>"/",       # TODO:homepage
117                         "css_push"=>"/product/Lib.css",
118                         %args,
119                         "heading_novskip"=>1,
120                         );
121         $class->heading();
122         my $is_index=($args{"__PACKAGE__"}=~/^product::\w+::Index$/);
123         print $class->products($args{"product_name"},
124                         "is_index"=>$is_index,
125                         %args,
126                         );
127         return $W;
128 }
129
130 1;