App-Dochazka-REST

 view release on metacpan or  search on metacpan

lib/App/Dochazka/REST/Model/Privhistory.pm  view on Meta::CPAN

are contained in L<App::Dochazka::REST::Model::Privhistory>. The most important
methods are:

=over

=item * constructor (L<spawn>)

=item * basic accessors (L<phid>, L<eid>, L<priv>, L<effective>, L<remark>)

=item * L<reset> (recycles an existing object by setting it to desired state)

=item * L<load> (loads a single privhistory record)

=item * L<load_by_phid> (wrapper for load_by_id)

=item * L<load_by_id> (load a single privhistory record by its PHID)

=item * L<insert> (inserts object into database)

=item * L<delete> (deletes object from database)

=back

For basic C<privhistory> workflow, see C<t/model/privhistory.t>.




=head1 EXPORTS

This module provides the following exports:

=over 

=item L<phid_exists> (boolean)

=item L<get_privhistory>

=back

=cut

use Exporter qw( import );
our @EXPORT_OK = qw( phid_exists get_privhistory );




=head1 METHODS


=head2 load_by_eid

Supposed to be a class method, but in reality we just don't care what the first
argument is.

=cut

sub load_by_eid {
    shift; # discard the first argument
    my ( $conn, $eid, $ts ) = validate_pos( @_,
        { isa => 'DBIx::Connector' },
        { type => SCALAR },                # EID
        { type => SCALAR|UNDEF, optional => 1 }, # timestamp
    );
  
    if ( $ts ) {
        return load(
            conn => $conn,
            class => __PACKAGE__,
            sql => $site->SQL_PRIVHISTORY_SELECT_ARBITRARY,
            keys => [ $eid, $ts ],
        );
    }

    return load(
        conn => $conn,
        class => __PACKAGE__,
        sql => $site->SQL_PRIVHISTORY_SELECT_CURRENT,
        keys => [ $eid ],
    );
}


=head2 load_by_id

Class method.

=cut

sub load_by_id {
    my $self = shift;
    my ( $conn, $phid ) = validate_pos( @_, 
        { isa => 'DBIx::Connector' },
        { type => SCALAR }, 
    );

    return load(
        conn => $conn,
        class => __PACKAGE__,
        sql => $site->SQL_PRIVHISTORY_SELECT_BY_PHID,
        keys => [ $phid ],
    );
}


=head2 load_by_phid

Wrapper for load_by_id

=cut

sub load_by_phid {
    my $self = shift;
    my ( $conn, $phid ) = validate_pos( @_, 
        { isa => 'DBIx::Connector' },
        { type => SCALAR }, 
    );
    return $self->load_by_id( $conn, $phid );
}


=head2 insert

Instance method. Attempts to INSERT a record into the 'privhistory' table.
Field values are taken from the object. Returns a status object.

=cut

sub insert {
    my $self = shift;
    my ( $context ) = validate_pos( @_, { type => HASHREF } );

    my $status = cud(
        conn => $context->{'dbix_conn'},
        eid => $context->{'current'}->{'eid'},
        object => $self,
        sql => $site->SQL_PRIVHISTORY_INSERT,
        attrs => [ 'eid', 'priv', 'effective', 'remark' ],
    );

    return $status;
}


=head2 update

Instance method. Updates the record. Returns status object.

=cut

sub update {
    my $self = shift;
    my ( $context ) = validate_pos( @_, { type => HASHREF } );

    my $status = cud(
        conn => $context->{'dbix_conn'},
        eid => $context->{'current'}->{'eid'},
        object => $self,
        sql => $site->SQL_PRIVHISTORY_UPDATE,
        attrs => [ 'priv', 'effective', 'remark', 'phid' ],
    );

    return $status;
}


=head2 delete

Instance method. Deletes the record. Returns status object.

=cut

sub delete {
    my $self = shift;
    my ( $context ) = validate_pos( @_, { type => HASHREF } );

    my $status = cud(
        conn => $context->{'dbix_conn'},
        eid => $context->{'current'}->{'eid'},
        object => $self,
        sql => $site->SQL_PRIVHISTORY_DELETE,
        attrs => [ 'phid' ],
    );
    $self->reset( 'phid' => $self->{phid} ) if $status->ok;

    return $status;
}



=head1 FUNCTIONS


=head2 phid_exists

Boolean function

=cut

BEGIN {
    no strict 'refs';
    *{'phid_exists'} = App::Dochazka::REST::Model::Shared::make_test_exists( 'phid' );
}


=head2 get_privhistory

Takes a PARAMHASH which can have one or more of the properties 'eid', 'nick',
and 'tsrange'.

At least one of { 'eid', 'nick' } must be specified. If both are specified,
the employee is determined according to 'eid'.

The function returns the history of privilege level changes for that employee
over the given tsrange, or the entire history if no tsrange is supplied. 

The return value will always be an L<App::CELL::Status|status> object.

Upon success, the payload will contain a 'history' key, the value of which will
be a reference to an array of C<privhistory> objects. If nothing is found, the
array will be empty. If there is a DBI error, the payload will be undefined.

=cut

sub get_privhistory {
    my $context = shift;
    return get_history( 'priv', $context->{'dbix_conn'}, @_ );
}




=head1 EXAMPLES

In this section, some examples are presented to help understand how this



( run in 0.659 second using v1.01-cache-2.11-cpan-d7f47b0818f )