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

use 5.010000;
use strict;
use warnings;

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

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 stringify {
    my ($self) = @_;

    my $user = $self->base->get_object('user', $self->_get_c_field('user'))
        or return $self->id;

    return join(', ', grep { $_ }
        (
            $user,
            $self->get_c_field('l'),
        )
    );
}

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',
                label => l('User'),
            },
            site =>         {
                reference => 'site',
                label => l('Site'),
            },
            sn =>           {
                managed => 1,
                ro => 1,
                get => sub {
                    my ($attr) = @_;
                    my $self = $attr->object;
                    my $user = $self->base->get_object('user', $self->_get_c_field('user'))
                        or return;
                    return $user->_get_c_field($attr->name);
                },
                label => l('Last name'),
            },
            mail =>         {
                managed => 1,
                ro => 1,
                get => sub {
                    my ($attr) = @_;
                    my $self = $attr->object;
                    my $user = $self->base->get_object('user', $self->_get_c_field('user'))
                        or return;
                    return $user->_get_c_field($attr->name);
                },
                label => l('Email'),
            },
            givenName => {
                managed => 1,
                ro => 1,
                get => sub {
                    my ($attr) = @_;
                    my $self = $attr->object;
                    my $user = $self->base->get_object('user', $self->_get_c_field('user'))
                        or return;
                    return $user->_get_c_field($attr->name);
                },
                label => l('First name'),
            },
            postalAddress => {
                managed => 1,
                ro => 1,
                get => sub {
                    my ($attr) = @_;
                    my $self = $attr->object;
                    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'),),
                            )
                        )
                    );
                },
                label => l('Postal address'),
            },
            displayName =>  {
                managed => 1,
                ro => 1,
                get => sub {
                    my $self = $_[0]->object;
                    return join(' ', grep { $_ } (
                            $self->_get_c_field('givenName'),
                            $self->_get_c_field('sn'),
                        )
                    );
                },
                label => l('Name'),
            },
            co => {
                managed => 1,
                ro => sub { $_[0] && $_[0]->get_attributes('site') ? 1 : 0 },
                get => sub {
                    my ($self) = @_;
                    if (my $site = $self->object->get_attributes('site')) {
                        if (my $osite = $self->base->get_object('site', $site)) {
                            return $osite->get_attributes($self->name);
                        }
                    }
                    return $self->object->get_field($self->name);
                },
                set => sub {
                    my ($self, $data) = @_;
                    if (my $site = $self->object->get_attributes('site')) {
                        return 1;
                    } else {
                        return $self->object->set_fields($self->name, $data)
                    }
                },
                label => l('Country'),
            },
            l => {
                managed => 1,
                ro => sub { $_[0] && $_[0]->get_attributes('site') ? 1 : 0 },
                get => sub {
                    my ($self) = @_;
                    if (my $site = $self->object->get_attributes('site')) {
                        if (my $osite = $self->base->get_object('site', $site)) {
                            return $osite->get_attributes($self->name);
                        }
                    }
                    return $self->object->get_field($self->name);
                },
                set => sub {
                    my ($self, $data) = @_;
                    if (my $site = $self->object->get_attributes('site')) {
                        return 1;
                    } else {
                        return $self->object->set_fields($self->name, $data)
                    }
                },
                label => l('City'),
            },
            postalCode =>   {
                managed => 1,
                ro => sub { $_[0] && $_[0]->get_attributes('site') ? 1 : 0 },
                get => sub {
                    my ($self) = @_;
                    if (my $site = $self->object->get_attributes('site')) {
                        if (my $osite = $self->base->get_object('site', $site)) {
                            return $osite->get_attributes($self->name);
                        }
                    }
                    return $self->object->get_field($self->name);
                },
                set => sub {
                    my ($self, $data) = @_;
                    if (my $site = $self->object->get_attributes('site')) {
                        return 1;
                    } else {
                        return $self->object->set_fields($self->name, $data)
                    }
                },
                label => l('Postal code'),
            },
            streetAddress => {
                managed => 1,
                formtype => 'TEXTAREA',
                ro => sub { $_[0] && $_[0]->get_attributes('site') ? 1 : 0 },
                get => sub {
                    my ($self) = @_;
                    if (my $site = $self->object->get_attributes('site')) {
                        if (my $osite = $self->base->get_object('site', $site)) {
                            return $osite->get_attributes($self->name);
                        }
                    }
                    return $self->object->get_field($self->name);
                },
                set => sub {
                    my ($self, $data) = @_;
                    if (my $site = $self->object->get_attributes('site')) {
                        return 1;
                    } else {
                        return $self->object->set_fields($self->name, $data)
                    }
                },
                label => l('Street'),
            },
            postOfficeBox => {
                managed => 1,
                ro => sub { $_[0] && $_[0]->get_attributes('site') ? 1 : 0 },
                get => sub {
                    my ($self) = @_;
                    if (my $site = $self->object->get_attributes('site')) {
                        if (my $osite = $self->base->get_object('site', $site)) {
                            return $osite->get_attributes($self->name);
                        }
                    }
                    return $self->object->get_field($self->name);
                },
                set => sub {
                    my ($self, $data) = @_;
                    if (my $site = $self->object->get_attributes('site')) {
                        return 1;
                    } else {
                        return $self->object->set_fields($self->name, $data)
                    }
                },
                label => l('Post office box'),
            },
            st => {
                managed => 1,
                ro => sub { $_[0] && $_[0]->get_attributes('site') ? 1 : 0 },
                get => sub {
                    my ($self) = @_;
                    if (my $site = $self->object->get_attributes('site')) {
                        if (my $osite = $self->base->get_object('site', $site)) {
                            return $osite->get_attributes($self->name);
                        }
                    }
                    return $self->object->get_field($self->name);
                },
                set => sub {
                    my ($self, $data) = @_;
                    if (my $site = $self->object->get_attributes('site')) {
                        return 1;
                    } else {
                        return $self->object->set_fields($self->name, $data)
                    }
                },
                label => l('Street'),
            },
            facsimileTelephoneNumber => {
                managed => 1,
                ro => sub { $_[0] && $_[0]->get_attributes('site') ? 1 : 0 },
                get => sub {
                    my ($self) = @_;
                    if (my $site = $self->object->get_attributes('site')) {
                        if (my $osite = $self->base->get_object('site', $site)) {
                            return $osite->get_attributes($self->name);
                        }
                    }
                    return $self->object->get_field($self->name);
                },
                set => sub {
                    my ($self, $data) = @_;
                    if (my $site = $self->object->get_attributes('site')) {
                        return 1;
                    } else {
                        return $self->object->set_fields($self->name, $data)
                    }
                },
                label => l('Fax number'),
            },
            o =>            {
                managed => 1,
                ro => sub { $_[0] && $_[0]->get_attributes('site') ? 1 : 0 },
                get => sub {
                    my ($self) = @_;
                    if (my $site = $self->object->get_attributes('site')) {
                        if (my $osite = $self->base->get_object('site', $site)) {
                            return $osite->get_attributes($self->name);
                        }
                    }
                    return $self->object->get_field($self->name);
                },
                set => sub {
                    my ($self, $data) = @_;
                    if (my $site = $self->object->get_attributes('site')) {
                        return 1;
                    } else {
                        return $self->object->set_fields($self->name, $data)
                    }
                },
                label => l('Company'),
            },
            isMainAddress => {
                formtype => 'CHECKBOX',
                label => l('Main address'),
            },
            contactOnly => {
                formtype => 'CHECKBOX',
                label => l('For contact only'),
            },
            active => { ro => 1 },
            telephoneNumber => {
                label => l('Phone number'),
            },
            physicalDeliveryOfficeName => {
                label => l('Office'),
            },
            otherTelephone => { },
            ipPhone => { },
            homePhone => { },
            description => {
                label => l('Description'),
            },
            expire    => {
                inline => 1,
                formtype => 'DATE',
                label => l('Expire'),
            },
        }
    );
}

sub ParentObject {
    my ($self) = @_;

    return $self->base->get_object('user', $self->_get_attributes('user'));
}

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('mainaddress')) {
        $data{isMainAddress} = 1;
    }
    $data{exported} = $user->get_attributes('exported');
    $class->SUPER::_create($base, $id, %data);
}

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
