1 | package LATMOS::Accounts::Web::Model::AttrFilter; |
---|
2 | |
---|
3 | use strict; |
---|
4 | use warnings; |
---|
5 | use base 'Catalyst::Model'; |
---|
6 | |
---|
7 | =head1 NAME |
---|
8 | |
---|
9 | LATMOS::Accounts::Web::Model::AttrFilter - Catalyst Model |
---|
10 | |
---|
11 | =head1 DESCRIPTION |
---|
12 | |
---|
13 | Catalyst Model. |
---|
14 | |
---|
15 | =cut |
---|
16 | |
---|
17 | my $fields = { |
---|
18 | user => [ qw(uid sn givenName description uidNumber gidNumber company department manager) ], |
---|
19 | group => [ qw(cn description gidNumber sutype) ], |
---|
20 | site => [ qw(l) ], |
---|
21 | nethost => [ qw(name cname otherName ip macaddr owner serialNumber) ], |
---|
22 | netzone => [ qw(net group type site) ], |
---|
23 | }; |
---|
24 | |
---|
25 | sub new { |
---|
26 | my ($class) = @_; |
---|
27 | return bless({}, $class); |
---|
28 | } |
---|
29 | |
---|
30 | sub ACCEPT_CONTEXT { |
---|
31 | my ($self, $c, $otype) = @_; |
---|
32 | |
---|
33 | my $new = {}; |
---|
34 | $new->{c} = $c; |
---|
35 | $new->{otype} = $otype; |
---|
36 | $c->stash->{filter} = { |
---|
37 | otype => $otype, |
---|
38 | } unless($c->stash->{filter}); |
---|
39 | if (exists($c->req->params->{'attr'})) { |
---|
40 | $c->stash->{filter}{'attr'} = [ $c->req->param('attr') ]; |
---|
41 | $c->stash->{filter}{'attrval'} = [ $c->req->param('attrval') ]; |
---|
42 | } |
---|
43 | |
---|
44 | $c->stash->{ofilter} = bless($new, __PACKAGE__); |
---|
45 | $c->stash->{db} ||= $c->model('Accounts')->db; |
---|
46 | my $base = $c->stash->{db}; |
---|
47 | |
---|
48 | my @filter = $c->stash->{ofilter}->filter; |
---|
49 | $c->stash->{objectslist} = [ |
---|
50 | map { $base->get_object($otype, $_) } |
---|
51 | (@filter |
---|
52 | ? ($base->search_objects($otype, @filter) ) |
---|
53 | : ($base->list_objects($otype) )) |
---|
54 | ]; |
---|
55 | |
---|
56 | $c->stash->{ofilter} |
---|
57 | } |
---|
58 | |
---|
59 | sub otype { |
---|
60 | my ($self) = @_; |
---|
61 | $self->{otype}; |
---|
62 | } |
---|
63 | |
---|
64 | sub submit { |
---|
65 | my ($self) = @_; |
---|
66 | return sprintf( |
---|
67 | '<input type="submit" name="%s">', |
---|
68 | $self->escape($self->label), |
---|
69 | ); |
---|
70 | } |
---|
71 | |
---|
72 | sub current_value { |
---|
73 | my ($self) = @_; |
---|
74 | my @vals = @{ $self->{c}->stash->{filter}{attrval} || [] }; |
---|
75 | my @fies = @{ $self->{c}->stash->{filter}{attr} || [] }; |
---|
76 | my @couples; |
---|
77 | foreach (@fies) { |
---|
78 | my $val = shift(@vals); |
---|
79 | $_ && $val or next; |
---|
80 | push(@couples, { f => $_, v => $val}); |
---|
81 | } |
---|
82 | push(@couples, {}); |
---|
83 | @couples; |
---|
84 | } |
---|
85 | |
---|
86 | sub count_field { |
---|
87 | my ($self) = @_; |
---|
88 | return [ (0 .. (scalar($self->current_value) -1)) ]; |
---|
89 | } |
---|
90 | |
---|
91 | sub search_field { |
---|
92 | my ($self, $count) = @_; |
---|
93 | my $form = $self->{c}->model('AttrForms'); |
---|
94 | my $html = '<select name="attr"> |
---|
95 | <option value=""></option>'; |
---|
96 | my @values = $self->current_value; |
---|
97 | foreach (@{ $fields->{$self->otype} }) { |
---|
98 | $html .= sprintf('<option value="%s"%s>%s</option>' . "\n", |
---|
99 | $_, |
---|
100 | (($values[$count]->{f} || '') eq $_ ? ' selected="on"' : ''), |
---|
101 | $form->attr_label($_), |
---|
102 | ); |
---|
103 | } |
---|
104 | |
---|
105 | $html .= '</select>'; |
---|
106 | } |
---|
107 | |
---|
108 | sub val_field { |
---|
109 | my ($self, $count) = @_; |
---|
110 | my @values = $self->current_value; |
---|
111 | return sprintf('<input type="text" name="attrval" value="%s">', |
---|
112 | $values[$count]->{v} || ''); |
---|
113 | } |
---|
114 | |
---|
115 | sub filter { |
---|
116 | my ($self) = @_; |
---|
117 | map { $_->{f} . '~' . $_->{v} } grep { $_->{f} } $self->current_value; |
---|
118 | } |
---|
119 | |
---|
120 | =head1 AUTHOR |
---|
121 | |
---|
122 | Thauvin Olivier |
---|
123 | |
---|
124 | =head1 LICENSE |
---|
125 | |
---|
126 | This library is free software, you can redistribute it and/or modify |
---|
127 | it under the same terms as Perl itself. |
---|
128 | |
---|
129 | =cut |
---|
130 | |
---|
131 | 1; |
---|