[238] | 1 | package LATMOS::Accounts::Web::Model::AttrFilter; |
---|
| 2 | |
---|
| 3 | use strict; |
---|
| 4 | use warnings; |
---|
| 5 | use base 'Catalyst::Model'; |
---|
| 6 | |
---|
| 7 | =head1 NAME |
---|
| 8 | |
---|
| 9 | LATMOS::Accounts::Web::Model::AttrFilter - Catalyst Model |
---|
| 10 | |
---|
| 11 | =head1 DESCRIPTION |
---|
| 12 | |
---|
| 13 | Catalyst Model. |
---|
| 14 | |
---|
| 15 | =cut |
---|
| 16 | |
---|
| 17 | my $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) ], |
---|
| 21 | }; |
---|
| 22 | |
---|
| 23 | sub new { |
---|
| 24 | my ($class) = @_; |
---|
| 25 | return bless({}, $class); |
---|
| 26 | } |
---|
| 27 | |
---|
| 28 | sub ACCEPT_CONTEXT { |
---|
| 29 | my ($self, $c, $otype) = @_; |
---|
| 30 | |
---|
| 31 | my $new = {}; |
---|
| 32 | $new->{c} = $c; |
---|
| 33 | $new->{otype} = $otype; |
---|
[240] | 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 | |
---|
[632] | 46 | my @filter = $c->stash->{ofilter}->filter; |
---|
[240] | 47 | $c->stash->{objectslist} = [ |
---|
| 48 | map { $base->get_object($otype, $_) } |
---|
[632] | 49 | (@filter |
---|
| 50 | ? ($base->search_objects($otype, @filter) ) |
---|
[240] | 51 | : ($base->list_objects($otype) )) |
---|
| 52 | ]; |
---|
| 53 | |
---|
| 54 | $c->stash->{ofilter} |
---|
[238] | 55 | } |
---|
| 56 | |
---|
| 57 | sub otype { |
---|
| 58 | my ($self) = @_; |
---|
| 59 | $self->{otype}; |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | sub submit { |
---|
| 63 | my ($self) = @_; |
---|
| 64 | return sprintf( |
---|
| 65 | '<input type="submit" name="%s">', |
---|
| 66 | $self->escape($self->label), |
---|
| 67 | ); |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | sub current_value { |
---|
| 71 | my ($self) = @_; |
---|
[240] | 72 | my @vals = @{ $self->{c}->stash->{filter}{attrval} || [] }; |
---|
| 73 | my @fies = @{ $self->{c}->stash->{filter}{attr} || [] }; |
---|
[238] | 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 | |
---|
| 84 | sub count_field { |
---|
| 85 | my ($self) = @_; |
---|
| 86 | return [ (0 .. (scalar($self->current_value) -1)) ]; |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | sub 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 | |
---|
| 106 | sub 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 | |
---|
[240] | 113 | sub filter { |
---|
| 114 | my ($self) = @_; |
---|
[632] | 115 | map { $_->{f} . '~' . $_->{v} } grep { $_->{f} } $self->current_value; |
---|
[240] | 116 | } |
---|
| 117 | |
---|
[238] | 118 | =head1 AUTHOR |
---|
| 119 | |
---|
| 120 | Thauvin Olivier |
---|
| 121 | |
---|
| 122 | =head1 LICENSE |
---|
| 123 | |
---|
| 124 | This library is free software, you can redistribute it and/or modify |
---|
| 125 | it under the same terms as Perl itself. |
---|
| 126 | |
---|
| 127 | =cut |
---|
| 128 | |
---|
| 129 | 1; |
---|