Ignore:
Timestamp:
12/22/15 12:59:07 (9 years ago)
Author:
nanardon
Message:

Set all attributes RO for pasted employment

This patch is done to deny modification on completly pasted employment, however
deletion and modifying the lastday are still possible.
Setting lastday to the futur allow to modify it again.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql/Employment.pm

    r1511 r1516  
    4747    my ($class, $base) = @_; 
    4848 
    49     $class->SUPER::_get_attr_schema($base, 
    50         { 
    51             name =>         { inline => 1, }, 
    52             exported =>     { inline => 1, }, 
    53             user =>         { inline => 1, 
    54                 reference => 'user', 
    55             }, 
    56             description => { }, 
    57             firstday => { 
    58                 inline => 1, 
    59                 formtype => 'DATE', 
    60                 monitored => 1, 
    61             }, 
    62             lastday => { 
    63                 inline => 1, 
    64                 formtype => 'DATE', 
    65                 monitored => 1, 
    66             }, 
    67             'state' => { 
    68                 managed => 1, 
    69                 ro => 1, 
    70                 get => sub { 
    71                     my ($self) = @_; 
    72                     my $now = DateTime->now; 
    73                     if ($now->epoch < str2time($self->object->get_attributes('firstday'))) { 
    74                         return 1; 
    75                     } elsif ( my $end = $self->object->get_attributes('lastday') ) { 
    76                         my $eend = str2time($end) + 86400; 
    77                         if ($now->epoch > $eend) { 
    78                             return -1; 
    79                         } 
    80                     } else { 
    81                         return 0; 
     49    my $attrs = { 
     50        name =>         { inline => 1, }, 
     51        exported =>     { inline => 1, }, 
     52        user =>         { inline => 1, 
     53            reference => 'user', 
     54        }, 
     55        description => { }, 
     56        firstday => { 
     57            inline => 1, 
     58            formtype => 'DATE', 
     59            monitored => 1, 
     60        }, 
     61        lastday => { 
     62            inline => 1, 
     63            formtype => 'DATE', 
     64            monitored => 1, 
     65        }, 
     66        'state' => { 
     67            managed => 1, 
     68            ro => 1, 
     69            get => sub { 
     70                my ($self) = @_; 
     71                my $now = DateTime->now; 
     72                if ($now->epoch < str2time($self->object->get_attributes('firstday'))) { 
     73                    return 1; 
     74                } elsif ( my $end = $self->object->get_attributes('lastday') ) { 
     75                    my $eend = str2time($end) + 86400; 
     76                    if ($now->epoch > $eend) { 
     77                        return -1; 
    8278                    } 
    83                 }, 
    84             }, 
    85             contratType => { 
    86                 reference => 'group', 
    87                 can_values => sub { 
    88                     $base->search_objects('group', 'sutype=contrattype') 
    89                 }, 
    90                 monitored => 1, 
    91             }, 
    92             managerContact => { 
    93                 delayed => 1, 
    94                 can_values => sub { 
    95                     my %uniq = map { $_ => 1 } grep { $_ } 
    96                     ($_[1] ? $_[1]->get_attributes('managerContact') : ()), 
    97                     $base->search_objects('user', 'active=*'); 
    98                     sort keys %uniq; 
    99                 }, 
    100                 reference => 'user', 
    101             }, 
    102             department => { 
    103                 reference => 'group', 
    104                 can_values => sub { 
    105                     $base->search_objects('group', 'sutype=dpmt') 
    106                 }, 
    107                 monitored => 1, 
    108             }, 
    109             company => { }, 
    110             endcircuit    => { formtype => 'DATE', monitored => 1, }, 
    111             previous => { 
    112                 ro => 1, 
    113                 managed => 1, 
    114                 get => sub { 
    115                     my ($self) = @_; 
    116                     my $find = $self->object->base->db->prepare_cached(q{ 
    117                         SELECT name from employment where "user" = ? 
    118                             and (lastday is not null and lastday < ?) 
    119                             order by lastday desc 
    120                     }); 
    121                     $find->execute( 
    122                         $self->object->get_field('user'), 
    123                         $self->object->get_field('firstday') 
    124                     ); 
    125                     my $res = $find->fetchrow_hashref; 
    126                     $find->finish; 
    127                     return $res->{name}; 
    128                 }, 
    129             }, 
    130             next => { 
    131                 ro => 1, 
    132                 managed => 1, 
    133                 get => sub { 
    134                     my ($self) = @_; 
    135                     my $find = $self->object->base->db->prepare_cached(q{ 
    136                         SELECT name from employment where "user" = ? 
    137                             and firstday > ? 
    138                             order by firstday asc 
    139                     }); 
    140                     $find->execute( 
    141                         $self->object->get_field('user'), 
    142                         $self->object->get_field('lastday') 
    143                     ); 
    144                     my $res = $find->fetchrow_hashref; 
    145                     $find->finish; 
    146                     return $res->{name}; 
    147                 }, 
    148             }, 
    149         } 
    150     ); 
     79                } else { 
     80                    return 0; 
     81                } 
     82            }, 
     83        }, 
     84        contratType => { 
     85            reference => 'group', 
     86            can_values => sub { 
     87                $base->search_objects('group', 'sutype=contrattype') 
     88            }, 
     89            monitored => 1, 
     90        }, 
     91        managerContact => { 
     92            delayed => 1, 
     93            can_values => sub { 
     94                my %uniq = map { $_ => 1 } grep { $_ } 
     95                ($_[1] ? $_[1]->get_attributes('managerContact') : ()), 
     96                $base->search_objects('user', 'active=*'); 
     97                sort keys %uniq; 
     98            }, 
     99            reference => 'user', 
     100        }, 
     101        department => { 
     102            reference => 'group', 
     103            can_values => sub { 
     104                $base->search_objects('group', 'sutype=dpmt') 
     105            }, 
     106            monitored => 1, 
     107        }, 
     108        company => { }, 
     109        endcircuit    => { formtype => 'DATE', monitored => 1, }, 
     110        previous => { 
     111            ro => 1, 
     112            managed => 1, 
     113            get => sub { 
     114                my ($self) = @_; 
     115                my $find = $self->object->base->db->prepare_cached(q{ 
     116                    SELECT name from employment where "user" = ? 
     117                        and (lastday is not null and lastday < ?) 
     118                        order by lastday desc 
     119                }); 
     120                $find->execute( 
     121                    $self->object->get_field('user'), 
     122                    $self->object->get_field('firstday') 
     123                ); 
     124                my $res = $find->fetchrow_hashref; 
     125                $find->finish; 
     126                return $res->{name}; 
     127            }, 
     128        }, 
     129        next => { 
     130            ro => 1, 
     131            managed => 1, 
     132            get => sub { 
     133                my ($self) = @_; 
     134                my $find = $self->object->base->db->prepare_cached(q{ 
     135                    SELECT name from employment where "user" = ? 
     136                        and firstday > ? 
     137                        order by firstday asc 
     138                }); 
     139                $find->execute( 
     140                    $self->object->get_field('user'), 
     141                    $self->object->get_field('lastday') 
     142                ); 
     143                my $res = $find->fetchrow_hashref; 
     144                $find->finish; 
     145                return $res->{name}; 
     146            }, 
     147        }, 
     148    }; 
     149 
     150    # Completed contract are RO, we allow to still set lastday 
     151    foreach (qw(endCircuit firstday contratType department managerContact company)) { 
     152        $attrs->{$_}{ro} = sub { 
     153            my ($self) = $_[0]; 
     154            $self or return 0; 
     155            return $self->get_attributes('state') < 0 ? 1 : 0; 
     156        }; 
     157    } 
     158    $class->SUPER::_get_attr_schema($base, $attrs); 
    151159} 
    152160 
Note: See TracChangeset for help on using the changeset viewer.