Revision 379 (by ahitrov, 2013/08/21 12:49:37) Emails control for credentials
<%args>

	$object
	$name	=> undef
	$SETS	=> undef

</%args>
<%init>

    return undef	unless ref $SETS;
    return undef	unless $name;

    my @props = $object->structure();
    my ($prop) = grep { $_->{attr} eq $name } @props;
    return undef    unless ref $prop;

    if ( $state->{users}->use_credentials && $object->id ) {
	my %struct;
	while ( my ($key, $val) = each %$SETS ) {
		my $template = $name.'_name_';
		if ( $key =~ /^$template/ ) {
			my $id = $key =~ /(\d+)/ ? $1 : 'new';
			if ( $keeper->{users}->_email_format( $val ) ) {
				$struct{$id}{orig} = $val;
				$struct{$id}{name} = $keeper->{users}->_email_reduction($val);
				$struct{$id}{id} = $id;
			}
		}
		$template = $name.'_active_';
		if ( $key =~ /^$template/ ) {
			my $id = $key =~ /(\d+)/ ? $1 : 'new';
			if ( $val ) {
				$struct{$id}{status} = 1;
				$struct{$id}{id} = $id;
			}
		}
	}
	if ( exists $SETS->{$name.'.delete'} ) {
		if ( ref $SETS->{$name.'.delete'} eq 'ARRAY' ) {
			map { $struct{$_}{delete} = 1 } @{$SETS->{$name.'.delete'}};
		} elsif ( $SETS->{$name.'.delete'} ) {
			$struct{$SETS->{$name.'.delete'}}{delete} = 1;
		}
	}
	if ( exists $SETS->{$name.'.main'} && $SETS->{$name.'.main'} && exists $struct{$SETS->{$name.'.main'}} && !exists $struct{$SETS->{$name.'.main'}}{delete} ) {
		$struct{$SETS->{$name.'.main'}}{main} = 1;
	} elsif ( exists $struct{new} ) {
		$struct{new}{main} = 1;
	} else {
		my @main = grep { exists $_->{name} && exists $_->{main} && $_->{main} && !exists $_->{delete} } values %struct;
		unless ( @main ) {
			my @valid = grep { exists $_->{name} && !exists $_->{delete} } values %struct;
			if ( @valid ) {
				$valid[0]->{main} = 1;
			}
		}
	}
	my @delete_ids = map { $_->{id} } grep { $_->{id} && exists $_->{delete} } values %struct;
	if ( @delete_ids ) {
		my $sql = $keeper->SQL->prepare('delete from profile_credentials where class = ? and uid = ? and id in ('.join(',', map {'?'} @delete_ids).')');
		$sql->execute( 'users::Email', $object->id, @delete_ids );
		$sql->finish;
	}
	my @credentials = $keeper->get_documents(
			class   => 'users::Email',
			uid     => $object->id,
			order_by        => 'main desc, name',
		);
	foreach my $cred ( @credentials ) {
		my $check = exists $struct{$cred->id} ? $struct{$cred->id} : undef;
		my $store = 0;
		if ( ref $check && exists $check->{name} && ($cred->name ne $check->{name} || $cred->name_orig ne $check->{orig}) ) {
			my $exists_foreign = $keeper->get_documents( class => 'users::Email', name => $check->{name}, uid_not => $object->id, count => 1 );
			unless ( $exists_foreign ) {
				$cred->name( $check->{name} );
				$cred->name_orig( $check->{orig} );
				$store = 1;
			}
		}
		if ( ref $check && int($check->{main}) != int($cred->main) ) {
			$cred->main( int($check->{main}) );
			$store = 1;
		}
		if ( ref $check && int($check->{status}) != int($cred->status) ) {
			$cred->status( int($check->{status}) );
			$store = 1;
		}
		if ( $store ) {
			$cred->store;
		}
	}
	if ( exists $struct{new} && exists $struct{new}{name} ) {
		my @found = grep { $_->name eq $struct{new}{name} } @credentials;
		unless ( @found ) {
			my $check = $struct{new};
			my $exists_foreign = $keeper->get_documents( class => 'users::Email', name => $check->{name}, count => 1 );
			unless ( $exists_foreign ) {
				my $cred = $object->create_credential( email => $check->{orig}, main => $check->{main}, status => ($check->{status} || 0) );
				push @credentials, $cred	if ref $cred;
			}
		}
	}
	my ($main) = grep { $_->main } @credentials;
	$object->{$name.'s'} = \@credentials;
	return $main;
    } else {
	return $keeper->{users}->_email_format( $SETS->{$name} );
    }

</%init>