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

Last change on this file since 2078 was 1865, checked in by nanardon, 8 years ago

Merge branch

File size: 4.0 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} || [ 'name' ]}) {
47                foreach ($base->search_objects(
48                        $otype, "$attr~" . $c->req->params->{'q'},
49                                'oalias=NULL',
50                    )) {
51                    $objs{$_} = 1;
52                }
53                @objlist = sort keys %objs;
54            }
55        }
56
57        if (exists($c->req->params->{'attr'})) {
58            $self->{filter}{'attr'} = [ $c->req->param('attr') ];
59            $self->{filter}{'attrval'} = [ $c->req->param('attrval') ];
60            my @filter = $self->filter;
61            @objlist = $base->search_objects($otype, @filter);
62            if ($c->req->params->{'q'}) {
63                @objlist = grep { $objs{$_} } @objlist;
64            }
65        }
66    } else {
67        @objlist = $base->list_objects($otype);
68    }
69
70    $self->{objectslist} = \@objlist;
71
72    my $start = $c->req->param('start');
73    $self->{uparams} = { %{ $c->req->params || {} } };
74    my %initials;
75    my @sublist;
76    foreach (@{ $self->{objectslist} }) {
77        $initials{substr($_, 0, 1)} = 1;
78        if ($start) {
79            if (index($_, $start) == 0) {
80                push(@sublist, $_);
81            }
82        }
83    }
84    $self->{sublist} = [ @sublist ];
85    $self->{initials} = [ sort keys %initials ];
86
87    $self
88}
89
90sub otype {
91    my ($self) = @_;
92    $self->{otype};
93}
94
95sub submit {
96    my ($self) = @_;
97    return sprintf(
98        '<input type="submit" name="%s">',
99        $self->escape($self->label),
100    );
101}
102
103sub current_value {
104    my ($self) = @_;
105    my @vals = @{ $self->{filter}{attrval} || [] };
106    my @fies = @{ $self->{filter}{attr} || [] };
107    my @couples;
108    foreach (@fies) {
109        my $val = shift(@vals);
110        $_ && $val or next;
111        push(@couples, { f => $_, v => $val});
112    }
113    push(@couples, {});
114    @couples;
115}
116
117sub count_field {
118    my ($self) = @_;
119    return [ (0 .. (scalar($self->current_value) -1)) ];
120}
121
122sub search_field {
123    my ($self, $count) = @_;
124    my $form = $self->{c}->model('AttrForms', '', $self->otype, $self->{c}->stash->{db});
125    my $html = '<select name="attr">
126    <option value=""></option>';
127    my @values = $self->current_value;
128    foreach (@{ $fields->{$self->otype} }) {
129        $html .= sprintf('<option value="%s"%s>%s</option>' . "\n",
130            $_,
131            (($values[$count]->{f} || '') eq $_ ? ' selected="on"' : ''),
132            $form->attr_label($_),
133        );
134    }
135
136    $html .= '</select>';
137}
138
139sub val_field {
140    my ($self, $count) = @_;
141    my @values = $self->current_value;
142    return sprintf('<input type="text" name="attrval" value="%s">',
143        $values[$count]->{v} || '');
144}
145
146sub filter {
147    my ($self) = @_;
148    map { $_->{f} . '~' .  $_->{v} } grep { $_->{f} } $self->current_value;
149}
150
151sub objectslist {
152    my ($self) = @_;
153    $self->{objectslist};
154}
155
156=head1 AUTHOR
157
158Thauvin Olivier
159
160=head1 LICENSE
161
162This library is free software, you can redistribute it and/or modify
163it under the same terms as Perl itself.
164
165=cut
166
1671;
Note: See TracBrowser for help on using the repository browser.