source: trunk/LATMOS-Accounts-Web/lib/LATMOS/Accounts/Web/Model/AttrFilter.pm @ 1237

Last change on this file since 1237 was 1237, checked in by nanardon, 11 years ago

add encryptkey to nethost form, add description as searchable field for nethost

File size: 3.9 KB
Line 
1package LATMOS::Accounts::Web::Model::AttrFilter;
2
3use strict;
4use warnings;
5use base 'Catalyst::Model';
6
7=head1 NAME
8
9LATMOS::Accounts::Web::Model::AttrFilter - Catalyst Model
10
11=head1 DESCRIPTION
12
13Catalyst Model.
14
15=cut
16
17my $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 description) ],
22    netzone => [ qw(net group type site) ],
23    aliases => [ qw(as) ],
24};
25
26sub new {
27    my ($class) = @_;
28    return bless({}, $class);
29}
30
31sub ACCEPT_CONTEXT {
32    my ($self, $c, $otype) = @_;
33
34    my $new = {};
35    $new->{c} = $c;
36    $new->{otype} = $otype;
37    $new->{base} = $c->model('Accounts')->db;
38    $self = bless($new, __PACKAGE__);
39
40    my $base = $new->{base};
41
42    my @objlist = ();
43    if ($c->req->params->{'q'} || exists($c->req->params->{'attr'}) ) {
44        my %objs = ();
45        if ($c->req->params->{'q'}) {
46            foreach my $attr (@{ $c->config->{objects}{$otype}{quick_search} || [ 'cn' ]}) {
47                foreach ($base->search_objects(
48                        $otype, "$attr~" . $c->req->params->{'q'})) {
49                    $objs{$_} = 1;
50                }
51                @objlist = sort keys %objs;
52            }
53        }
54
55        if (exists($c->req->params->{'attr'})) {
56            $self->{filter}{'attr'} = [ $c->req->param('attr') ];
57            $self->{filter}{'attrval'} = [ $c->req->param('attrval') ];
58            my @filter = $self->filter;
59            @objlist = $base->search_objects($otype, @filter);
60            if ($c->req->params->{'q'}) {
61                @objlist = grep { $objs{$_} } @objlist;
62            }
63        }
64    } else {
65        @objlist = $base->list_objects($otype);
66    }
67
68    $self->{objectslist} = \@objlist;
69
70    my $start = $c->req->param('start');
71    $self->{uparams} = { %{ $c->req->params || {} } };
72    my %initials;
73    my @sublist;
74    foreach (@{ $self->{objectslist} }) {
75        $initials{substr($_, 0, 1)} = 1;
76        if ($start) {
77            if (index($_, $start) == 0) {
78                push(@sublist, $_);
79            }
80        }
81    }
82    $self->{sublist} = [ @sublist ];
83    $self->{initials} = [ sort keys %initials ];
84
85    $self
86}
87
88sub otype {
89    my ($self) = @_;
90    $self->{otype};
91}
92
93sub submit {
94    my ($self) = @_;
95    return sprintf(
96        '<input type="submit" name="%s">',
97        $self->escape($self->label),
98    );
99}
100
101sub current_value {
102    my ($self) = @_;
103    my @vals = @{ $self->{filter}{attrval} || [] };
104    my @fies = @{ $self->{filter}{attr} || [] };
105    my @couples;
106    foreach (@fies) {
107        my $val = shift(@vals);
108        $_ && $val or next;
109        push(@couples, { f => $_, v => $val});
110    }
111    push(@couples, {});
112    @couples;
113}
114
115sub count_field {
116    my ($self) = @_;
117    return [ (0 .. (scalar($self->current_value) -1)) ];
118}
119
120sub search_field {
121    my ($self, $count) = @_;
122    my $form = $self->{c}->model('AttrForms', '', $self->otype, $self->{c}->stash->{db});
123    my $html = '<select name="attr">
124    <option value=""></option>';
125    my @values = $self->current_value;
126    foreach (@{ $fields->{$self->otype} }) {
127        $html .= sprintf('<option value="%s"%s>%s</option>' . "\n",
128            $_,
129            (($values[$count]->{f} || '') eq $_ ? ' selected="on"' : ''),
130            $form->attr_label($_),
131        );
132    }
133
134    $html .= '</select>';
135}
136
137sub val_field {
138    my ($self, $count) = @_;
139    my @values = $self->current_value;
140    return sprintf('<input type="text" name="attrval" value="%s">',
141        $values[$count]->{v} || '');
142}
143
144sub filter {
145    my ($self) = @_;
146    map { $_->{f} . '~' .  $_->{v} } grep { $_->{f} } $self->current_value;
147}
148
149sub objectslist {
150    my ($self) = @_;
151    $self->{objectslist};
152}
153
154=head1 AUTHOR
155
156Thauvin Olivier
157
158=head1 LICENSE
159
160This library is free software, you can redistribute it and/or modify
161it under the same terms as Perl itself.
162
163=cut
164
1651;
Note: See TracBrowser for help on using the repository browser.