source: branches/New_Config_Format-branch/gsdl/perllib/classify/List.pm@ 1279

Last change on this file since 1279 was 1279, checked in by sjboddie, 24 years ago

merged changes to trunk into New_Config_Format branch

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 4.9 KB
Line 
1###########################################################################
2#
3# List.pm --
4# A component of the Greenstone digital library software
5# from the New Zealand Digital Library Project at the
6# University of Waikato, New Zealand.
7#
8# Copyright (C) 1999 New Zealand Digital Library Project
9#
10# This program is free software; you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation; either version 2 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program; if not, write to the Free Software
22# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23#
24###########################################################################
25
26# simple list classifier plugin
27# options are:
28# metadata=Metaname -- (optional) all documents with Metaname metadata
29# will be included in list. if not included all documents
30# will be included in list.
31# sort=Meta -- (optional) sort documents in list alphabetically by
32# Meta. by default it will sort by Metaname, if neither
33# are set documents will be in build (random) order.
34# Meta may be Filename to sort by original filename or
35# nosort to force not to sort
36# title=Title -- (optional) the title field for this classification.
37# if not included title field will be Metaname.
38# if metadata is also not included title will be 'List'
39
40package List;
41
42use sorttools;
43
44sub new {
45 my ($class, @options) = @_;
46
47 my $list = [];
48 my ($metaname, $title, $sortname);
49
50 foreach $option (@options) {
51 if ($option =~ /^metadata=(.*)$/i) {
52 $metaname = $1;
53 $list = {};
54 } elsif ($option =~ /^title=(.*)$/i) {
55 $title = $1;
56 } elsif ($option =~ /^sort=(.*)$/i) {
57 $sortname = $1;
58 }
59 }
60
61 if (!defined $title) {
62 if (defined $metaname) {
63 $title = $metaname;
64 } else {
65 $title = 'List';
66 }
67 }
68
69 if (defined $sortname && $sortname =~ /^nosort$/i) {
70 $sortname = undef;
71 } elsif (!defined $sortname && defined $metaname) {
72 $sortname = $metaname;
73 }
74
75 return bless {
76 'list'=>$list,
77 'metaname' => $metaname,
78 'title' => $title,
79 'sortname' => $sortname
80 }, $class;
81}
82
83sub init {
84 my $self = shift (@_);
85
86 if (defined $self->{'sortname'}) {
87 $self->{'list'} = {};
88 } else {
89 $self->{'list'} = [];
90 }
91}
92
93sub classify {
94 my $self = shift (@_);
95 my ($doc_obj) = @_;
96
97 my $doc_OID = $doc_obj->get_OID();
98
99 my $sortmeta = "";
100 if (defined $self->{'sortname'}) {
101 if ($self->{'sortname'} =~ /^filename$/i) {
102 $sortmeta = $doc_obj->get_source_filename();
103 } else {
104 $sortmeta = $doc_obj->get_metadata_element($doc_obj->get_top_section(),
105 $self->{'sortname'});
106 if (defined $sortmeta) {
107 if ($self->{'sortname'} eq "Creator") {
108 &sorttools::format_string_name_english (\$sortmeta);
109 } else {
110 &sorttools::format_string_english (\$sortmeta);
111 }
112 }
113 }
114 $sortmeta = "" unless defined $sortmeta;
115
116 if (defined $self->{'metaname'}) {
117 my $metavalue = $doc_obj->get_metadata_element ($doc_obj->get_top_section(),
118 $self->{'metaname'});
119 if (defined $metavalue) {
120 if (defined $self->{'list'}->{$doc_OID}) {
121 print STDERR "WARNING: List::classify called multiple times for $doc_OID\n";
122 }
123 $self->{'list'}->{$doc_OID} = $sortmeta;
124 }
125 } else {
126 if (defined $self->{'list'}->{$doc_OID}) {
127 print STDERR "WARNING: List::classify called multiple times for $doc_OID\n";
128 }
129 $self->{'list'}->{$doc_OID} = $sortmeta;
130 }
131 } else {
132 if (defined $self->{'metaname'}) {
133 my $metavalue = $doc_obj->get_metadata_element ($doc_obj->get_top_section(),
134 $self->{'metaname'});
135 if (defined $metavalue) {
136 push (@{$self->{'list'}}, $doc_OID);
137 }
138 } else {
139 push (@{$self->{'list'}}, $doc_OID);
140 }
141 }
142}
143
144sub get_classify_info {
145 my $self = shift (@_);
146 my ($no_thistype) = @_;
147 $no_thistype = 0 unless defined $no_thistype;
148
149 my @list = ();
150 if (defined $self->{'sortname'}) {
151 if (keys %{$self->{'list'}}) {
152 @list = sort {$self->{'list'}->{$a}
153 cmp $self->{'list'}->{$b};} keys %{$self->{'list'}};
154 }
155 } else {
156 @list = @{$self->{'list'}};
157 }
158
159 # organise into classification structure
160 my %classifyinfo = ('childtype'=>'VList',
161 'Title'=>$self->{'title'},
162 'contains'=>[]);
163 $classifyinfo{'thistype'} = 'Invisible' unless $no_thistype;
164
165 foreach $OID (@list) {
166 push (@{$classifyinfo{'contains'}}, {'OID'=>$OID});
167 }
168
169 return \%classifyinfo;
170}
171
172
1731;
Note: See TracBrowser for help on using the repository browser.