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

Last change on this file since 677 was 638, checked in by nanardon, 14 years ago
  • allow search over description
File size: 2.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};
22
23sub new {
24    my ($class) = @_;
25    return bless({}, $class);
26}
27
28sub ACCEPT_CONTEXT {
29    my ($self, $c, $otype) = @_;
30
31    my $new = {};
32    $new->{c} = $c;
33    $new->{otype} = $otype;
34    $c->stash->{filter} = {
35        otype => $otype,
36    } unless($c->stash->{filter});
37    if (exists($c->req->params->{'attr'})) {
38        $c->stash->{filter}{'attr'} = [ $c->req->param('attr') ];
39        $c->stash->{filter}{'attrval'} = [ $c->req->param('attrval') ];
40    }
41   
42    $c->stash->{ofilter} = bless($new, __PACKAGE__);
43    $c->stash->{db} ||= $c->model('Accounts')->db;
44    my $base = $c->stash->{db};
45
46    my @filter = $c->stash->{ofilter}->filter;
47    $c->stash->{objectslist} = [
48        map { $base->get_object($otype, $_) }
49        (@filter
50            ? ($base->search_objects($otype, @filter) )
51            : ($base->list_objects($otype) ))
52    ];
53
54    $c->stash->{ofilter}
55}
56
57sub otype {
58    my ($self) = @_;
59    $self->{otype};
60}
61
62sub submit {
63    my ($self) = @_;
64    return sprintf(
65        '<input type="submit" name="%s">',
66        $self->escape($self->label),
67    );
68}
69
70sub current_value {
71    my ($self) = @_;
72    my @vals = @{ $self->{c}->stash->{filter}{attrval} || [] };
73    my @fies = @{ $self->{c}->stash->{filter}{attr} || [] };
74    my @couples;
75    foreach (@fies) {
76        my $val = shift(@vals);
77        $_ && $val or next;
78        push(@couples, { f => $_, v => $val});
79    }
80    push(@couples, {});
81    @couples;
82}
83
84sub count_field {
85    my ($self) = @_;
86    return [ (0 .. (scalar($self->current_value) -1)) ];
87}
88
89sub search_field {
90    my ($self, $count) = @_;
91    my $form = $self->{c}->model('AttrForms');
92    my $html = '<select name="attr">
93    <option value=""></option>';
94    my @values = $self->current_value;
95    foreach (@{ $fields->{$self->otype} }) {
96        $html .= sprintf('<option value="%s"%s>%s</option>' . "\n",
97            $_,
98            (($values[$count]->{f} || '') eq $_ ? ' selected="on"' : ''),
99            $form->attr_label($_),
100        );
101    }
102
103    $html .= '</select>';
104}
105
106sub val_field {
107    my ($self, $count) = @_;
108    my @values = $self->current_value;
109    return sprintf('<input type="text" name="attrval" value="%s">',
110        $values[$count]->{v} || '');
111}
112
113sub filter {
114    my ($self) = @_;
115    map { $_->{f} . '~' .  $_->{v} } grep { $_->{f} } $self->current_value;
116}
117
118=head1 AUTHOR
119
120Thauvin Olivier
121
122=head1 LICENSE
123
124This library is free software, you can redistribute it and/or modify
125it under the same terms as Perl itself.
126
127=cut
128
1291;
Note: See TracBrowser for help on using the repository browser.