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::Ad - Perl extension for blah blah blah

=head1 SYNOPSIS

  use LATMOS::Accounts::Bases;
  my $base = LATMOS::Accounts::Bases->new('sql');
  ...

=head1 DESCRIPTION

Account base access over standard unix file format.

=head1 FUNCTIONS

=cut

=head2 new(%options)

Create a new LATMOS::Ad object for windows AD $domain.

domain / server: either the Ad domain or directly the server

ldap_args is an optionnal list of arguments to pass to L<Net::LDAP>.

=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_fields {
    my ($self, %data) = @_;
    my %fdata;
    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) {
        $self->SUPER::set_fields(%fdata);
    }
}

1;

__END__

=head1 SEE ALSO

=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
