package LATMOS::Accounts::Bases::Sql::Address;

use 5.010000;
use strict;
use warnings;

use base qw(LATMOS::Accounts::Bases::Sql::objects);

our $VERSION = (q$Rev: 594 $ =~ /^Rev: (\d+) /)[0];

=head1 NAME

LATMOS::Accounts::Bases::Sql::Address - Physical office Adress Support

=head1 DESCRIPTION

C<Address> objects allow to store user's several office addresses.

Notice most of other bases (Ldap, ...) support only one address.

=cut

sub _object_table { 'address' }

sub _key_field { 'name' }

sub _has_extended_attributes { 1 }

sub _get_attr_schema {
    my ($class, $base) = @_;

    $class->SUPER::_get_attr_schema($base,
        {
            name =>         { inline => 1, },
            exported =>     { inline => 1, },
            user =>         { inline => 1,
                reference => 'user',
            },
            site =>         {
                reference => 'site',
            },
            sn =>           { ro => 1, },
            mail =>         { ro => 1, },
            givenName =>    { ro => 1, },
            postalAddress => { ro => 1, },
            displayName =>  { ro => 1, },
            co =>           {
                ro => sub { $_[0] && $_[0]->get_attributes('site') ? 1 : 0 },
            },
            l =>            {
                ro => sub { $_[0] && $_[0]->get_attributes('site') ? 1 : 0 },
            },
            postalCode =>   {
                ro => sub { $_[0] && $_[0]->get_attributes('site') ? 1 : 0 },
            },
            streetAddress => {
                formtype => 'TEXTAREA',
                ro => sub { $_[0] && $_[0]->get_attributes('site') ? 1 : 0 },
            },
            postOfficeBox => {
                ro => sub { $_[0] && $_[0]->get_attributes('site') ? 1 : 0 },
            },
            st =>           {
                ro => sub { $_[0] && $_[0]->get_attributes('site') ? 1 : 0 },
            },
            facsimileTelephoneNumber => {
                ro => sub { $_[0] && $_[0]->get_attributes('site') ? 1 : 0 },
            },
            o =>            {
                ro => sub { $_[0] && $_[0]->get_attributes('site') ? 1 : 0 },
            },
            isMainAddress => { formtype => 'CHECKBOX', },
            active => { ro => 1 },
        }
    );
}

sub _create {
    my ($class, $base, $id, %data) = @_;
    $data{user} or return;
    my $user = $base->get_object('user', $data{user});
    $user or return;
    if (!$user->get_c_field('otheraddress')) {
        $data{isMainAddress} = 1;
    }
    $class->SUPER::_create($base, $id, %data);
}

sub get_field {
    my ($self, $field) = @_;
    if ((grep { $field eq $_ } (qw(
            co l
            postalCode streetAddress
            postOfficeBox st
            facsimileTelephoneNumber o
            ))) && (my $fsite = $self->get_c_field('site'))) {
        my $site = $self->base->get_object('site', $fsite);
        if ($site) {
            return $site->get_field($field);
        }
    } elsif ($field =~ /^(sn|givenName|mail)$/) {
        my $user = $self->base->get_object('user', $self->_get_c_field('user'))
            or return;
        return $user->_get_c_field($field);
    } elsif ($field eq 'postalAddress' ) {
        my $bp = $self->_get_c_field('postOfficeBox');
        return join("\n", grep { $_ } (
            $self->_get_c_field('o'),
            $self->_get_c_field('streetAddress'),
            ($bp ? 'BP ' . $bp : ''),
            join(' ', grep { $_ } (
                    $self->_get_c_field('postalCode'),
                    $self->_get_c_field('l'),)),
            join(' ', grep { $_ } (
                    $self->_get_c_field('st'),
                    $self->_get_c_field('co'),),
            )
        ));
    } elsif ($field eq 'displayName') {
        return join(' ', grep { $_ } (
            $self->_get_c_field('givenName'),
            $self->_get_c_field('sn'),
        ));
    } else {
        return $self->SUPER::get_field($field);
    }
}

sub _set_c_fields {
    my ($self, %cdata) = @_;
    if ($cdata{site} &&
        $self->base->get_object('site', $cdata{site})) {
        foreach (qw(co l postalCode streetAddress postOfficeBox st
                    facsimileTelephoneNumber o)) {
            delete($cdata{$_});
        }
    }

    $self->SUPER::_set_c_fields(%cdata);
}

sub set_fields {
    my ($self, %data) = @_;
    my %fdata;
    my $res = 0;
    foreach my $attr (keys %data) {
        $attr =~ /^site$/ and do {
            my $site =
            $self->base->get_object('site',
                $data{$attr});
            if ($site) {
                foreach (qw(co l postalCode streetAddress postOfficeBox st
                        facsimileTelephoneNumber o)) {
                    $fdata{$_} = undef;
                    $data{$_} = undef;
                }
            }
            $fdata{$attr} = $data{$attr} || undef;
            next;
        };
        $fdata{$attr} = $data{$attr} || undef;
    }
    if  (keys %fdata) {
        $res += $self->SUPER::set_fields(%fdata);
    }

    $res;
}

1;

__END__

=head1 SEE ALSO

L<LATMOS::Accounts::Bases::Sql>

L<LATMOS::Accounts::Bases::Sql::Onlyaddress>

=head1 AUTHOR

Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2008, 2009 CNRS SA/CETP/LATMOS

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.10.0 or,
at your option, any later version of Perl 5 you may have available.


=cut
