1 | #!/usr/bin/perl |
---|
2 | |
---|
3 | use strict; |
---|
4 | use warnings; |
---|
5 | use LATMOS::Accounts; |
---|
6 | use Getopt::Long; |
---|
7 | use Pod::Usage; |
---|
8 | use Spreadsheet::WriteExcel; |
---|
9 | |
---|
10 | =head1 NAME |
---|
11 | |
---|
12 | la2xls - Output data in XLS format |
---|
13 | |
---|
14 | =head1 SYNOPSIS |
---|
15 | |
---|
16 | la-search [options] filter [filter2 [...]] [attributes [...]] |
---|
17 | |
---|
18 | =head1 OPTIONS |
---|
19 | |
---|
20 | =over 4 |
---|
21 | |
---|
22 | =item --config configdir |
---|
23 | |
---|
24 | Use this configuration directory instead default |
---|
25 | |
---|
26 | =item --base basename |
---|
27 | |
---|
28 | Perform search in this specific base instead default |
---|
29 | |
---|
30 | =item --with-unexp |
---|
31 | |
---|
32 | Take into account all objects (even non propagated ones, with attribute |
---|
33 | 'exported'=0) (default) |
---|
34 | |
---|
35 | =item --wo-unexp |
---|
36 | |
---|
37 | Take into account only propagated objects (attribute 'exported'=1) |
---|
38 | |
---|
39 | =item -f FILE |
---|
40 | |
---|
41 | The output filename. The default is la2xls.xls. |
---|
42 | |
---|
43 | =item FILTER |
---|
44 | |
---|
45 | Filter are in form "[ATTRIBUTE][OPERATOR][VALUE]" |
---|
46 | |
---|
47 | where attribute is a supported attribute. |
---|
48 | |
---|
49 | OPERATOR can be: |
---|
50 | |
---|
51 | =over 4 |
---|
52 | |
---|
53 | =item C<=> |
---|
54 | |
---|
55 | Search object having attribute strictely equal to VALUE. If VALUE is C<*> match |
---|
56 | any non empty value. |
---|
57 | |
---|
58 | =item C<~> |
---|
59 | |
---|
60 | Search object having attribute containing VALUE. |
---|
61 | |
---|
62 | =back |
---|
63 | |
---|
64 | =back |
---|
65 | |
---|
66 | =cut |
---|
67 | |
---|
68 | GetOptions( |
---|
69 | 'c|config=s' => \my $config, |
---|
70 | 'b|base=s' => \my $base, |
---|
71 | 'o|object=s' => \my $otype, |
---|
72 | 'no-unexp|wo-unexp' => \my $nounexp, |
---|
73 | 'with-unexp' => \my $unexp, |
---|
74 | 'f|file=s' => \my $file, |
---|
75 | 'help' => sub { pod2usage(0) }, |
---|
76 | ) or pod2usage(); |
---|
77 | |
---|
78 | if (!$ARGV[0]) {warn "You must specify 'filter', aborting\n"; pod2usage(); } |
---|
79 | |
---|
80 | $otype ||= 'user'; |
---|
81 | $file ||= 'la2xls.xls'; |
---|
82 | |
---|
83 | my $LA = LATMOS::Accounts->new($config, noacl => 1); |
---|
84 | my $labase = $LA->base($base); |
---|
85 | $labase && $labase->load or die "Cannot load base"; |
---|
86 | |
---|
87 | $labase->unexported($nounexp ? 0 : 1); |
---|
88 | |
---|
89 | my (@filters, @attr); |
---|
90 | |
---|
91 | foreach (@ARGV) { |
---|
92 | if (/(~|=)/) { |
---|
93 | push(@filters, $_); |
---|
94 | } else { |
---|
95 | push(@attr, $_); |
---|
96 | } |
---|
97 | } |
---|
98 | |
---|
99 | my @result = $labase->search_objects($otype, @filters); |
---|
100 | |
---|
101 | my @objs = map { $labase->get_object($otype, $_) } @result; |
---|
102 | |
---|
103 | # Create a new Excel workbook |
---|
104 | my $workbook = Spreadsheet::WriteExcel->new($file); |
---|
105 | |
---|
106 | # Add a worksheet |
---|
107 | my $worksheet = $workbook->add_worksheet(); |
---|
108 | |
---|
109 | # Add and define a format |
---|
110 | my $format = $workbook->add_format(); # Add a format |
---|
111 | $format->set_bold(); |
---|
112 | $format->set_align('center'); |
---|
113 | |
---|
114 | $worksheet->write(0, 0, 'name', $format); |
---|
115 | my $row = 0; |
---|
116 | foreach my $obj (@objs) { |
---|
117 | $row++; |
---|
118 | $worksheet->write($row, 0, $obj->id); |
---|
119 | } |
---|
120 | |
---|
121 | |
---|
122 | |
---|
123 | foreach my $col (0 .. $#attr) { |
---|
124 | $worksheet->write(0, $col+1, $attr[$col], $format); |
---|
125 | |
---|
126 | my $row = 0; |
---|
127 | foreach my $obj (@objs) { |
---|
128 | $row++; |
---|
129 | $worksheet->write($row, $col+1, $obj->get_attributes($attr[$col])); |
---|
130 | } |
---|
131 | } |
---|
132 | |
---|
133 | $workbook->close(); |
---|