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

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

Merge branch

File size: 4.0 KB
RevLine 
[238]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 = {
[638]18    user => [ qw(uid sn givenName description uidNumber gidNumber company department manager) ],
[632]19    group => [ qw(cn description gidNumber sutype) ],
[238]20    site => [ qw(l) ],
[1237]21    nethost => [ qw(name cname otherName ip macaddr owner serialNumber description) ],
[861]22    netzone => [ qw(net group type site) ],
[1081]23    aliases => [ qw(as) ],
[238]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;
[1131]37    $new->{base} = $c->model('Accounts')->db;
38    $self = bless($new, __PACKAGE__);
[1081]39
[1131]40    my $base = $new->{base};
[1081]41
42    my @objlist = ();
[1135]43    if ($c->req->params->{'q'} || exists($c->req->params->{'attr'}) ) {
44        my %objs = ();
45        if ($c->req->params->{'q'}) {
[1343]46            foreach my $attr (@{ $c->config->{objects}{$otype}{quick_search} || [ 'name' ]}) {
[1135]47                foreach ($base->search_objects(
[1865]48                        $otype, "$attr~" . $c->req->params->{'q'},
49                                'oalias=NULL',
50                    )) {
[1135]51                    $objs{$_} = 1;
52                }
53                @objlist = sort keys %objs;
[1081]54            }
55        }
[1135]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        }
[1081]66    } else {
67        @objlist = $base->list_objects($otype);
[240]68    }
69
[1131]70    $self->{objectslist} = \@objlist;
[240]71
[1081]72    my $start = $c->req->param('start');
[1131]73    $self->{uparams} = { %{ $c->req->params || {} } };
[1081]74    my %initials;
75    my @sublist;
[1131]76    foreach (@{ $self->{objectslist} }) {
[1082]77        $initials{substr($_, 0, 1)} = 1;
[1081]78        if ($start) {
[1082]79            if (index($_, $start) == 0) {
[1081]80                push(@sublist, $_);
81            }
82        }
83    }
[1131]84    $self->{sublist} = [ @sublist ];
85    $self->{initials} = [ sort keys %initials ];
[1081]86
[1131]87    $self
[238]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) = @_;
[1131]105    my @vals = @{ $self->{filter}{attrval} || [] };
106    my @fies = @{ $self->{filter}{attr} || [] };
[238]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) = @_;
[959]124    my $form = $self->{c}->model('AttrForms', '', $self->otype, $self->{c}->stash->{db});
[238]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
[240]146sub filter {
147    my ($self) = @_;
[632]148    map { $_->{f} . '~' .  $_->{v} } grep { $_->{f} } $self->current_value;
[240]149}
150
[1131]151sub objectslist {
152    my ($self) = @_;
153    $self->{objectslist};
154}
155
[238]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.