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

Last change on this file since 939 was 861, checked in by nanardon, 13 years ago
  • reimport missing files from previous svn
File size: 3.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) ],
22    netzone => [ qw(net group type site) ],
23};
24
25sub new {
26    my ($class) = @_;
27    return bless({}, $class);
28}
29
30sub 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
59sub otype {
60    my ($self) = @_;
61    $self->{otype};
62}
63
64sub submit {
65    my ($self) = @_;
66    return sprintf(
67        '<input type="submit" name="%s">',
68        $self->escape($self->label),
69    );
70}
71
72sub 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
86sub count_field {
87    my ($self) = @_;
88    return [ (0 .. (scalar($self->current_value) -1)) ];
89}
90
91sub 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
108sub 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
115sub filter {
116    my ($self) = @_;
117    map { $_->{f} . '~' .  $_->{v} } grep { $_->{f} } $self->current_value;
118}
119
120=head1 AUTHOR
121
122Thauvin Olivier
123
124=head1 LICENSE
125
126This library is free software, you can redistribute it and/or modify
127it under the same terms as Perl itself.
128
129=cut
130
1311;
Note: See TracBrowser for help on using the repository browser.