| 1 |
379 |
ahitrov |
<%args> |
| 2 |
|
|
|
| 3 |
|
|
$object |
| 4 |
|
|
$name => undef |
| 5 |
|
|
$SETS => undef |
| 6 |
|
|
|
| 7 |
|
|
</%args> |
| 8 |
|
|
<%init> |
| 9 |
|
|
|
| 10 |
|
|
return undef unless ref $SETS; |
| 11 |
|
|
return undef unless $name; |
| 12 |
|
|
|
| 13 |
|
|
my @props = $object->structure(); |
| 14 |
|
|
my ($prop) = grep { $_->{attr} eq $name } @props; |
| 15 |
|
|
return undef unless ref $prop; |
| 16 |
|
|
|
| 17 |
|
|
if ( $state->{users}->use_credentials && $object->id ) { |
| 18 |
|
|
my %struct; |
| 19 |
|
|
while ( my ($key, $val) = each %$SETS ) { |
| 20 |
|
|
my $template = $name.'_name_'; |
| 21 |
|
|
if ( $key =~ /^$template/ ) { |
| 22 |
|
|
my $id = $key =~ /(\d+)/ ? $1 : 'new'; |
| 23 |
|
|
if ( $keeper->{users}->_email_format( $val ) ) { |
| 24 |
|
|
$struct{$id}{orig} = $val; |
| 25 |
|
|
$struct{$id}{name} = $keeper->{users}->_email_reduction($val); |
| 26 |
|
|
$struct{$id}{id} = $id; |
| 27 |
|
|
} |
| 28 |
|
|
} |
| 29 |
|
|
$template = $name.'_active_'; |
| 30 |
|
|
if ( $key =~ /^$template/ ) { |
| 31 |
|
|
my $id = $key =~ /(\d+)/ ? $1 : 'new'; |
| 32 |
|
|
if ( $val ) { |
| 33 |
|
|
$struct{$id}{status} = 1; |
| 34 |
|
|
$struct{$id}{id} = $id; |
| 35 |
|
|
} |
| 36 |
|
|
} |
| 37 |
|
|
} |
| 38 |
|
|
if ( exists $SETS->{$name.'.delete'} ) { |
| 39 |
|
|
if ( ref $SETS->{$name.'.delete'} eq 'ARRAY' ) { |
| 40 |
|
|
map { $struct{$_}{delete} = 1 } @{$SETS->{$name.'.delete'}}; |
| 41 |
|
|
} elsif ( $SETS->{$name.'.delete'} ) { |
| 42 |
|
|
$struct{$SETS->{$name.'.delete'}}{delete} = 1; |
| 43 |
|
|
} |
| 44 |
|
|
} |
| 45 |
|
|
if ( exists $SETS->{$name.'.main'} && $SETS->{$name.'.main'} && exists $struct{$SETS->{$name.'.main'}} && !exists $struct{$SETS->{$name.'.main'}}{delete} ) { |
| 46 |
|
|
$struct{$SETS->{$name.'.main'}}{main} = 1; |
| 47 |
|
|
} elsif ( exists $struct{new} ) { |
| 48 |
|
|
$struct{new}{main} = 1; |
| 49 |
|
|
} else { |
| 50 |
|
|
my @main = grep { exists $_->{name} && exists $_->{main} && $_->{main} && !exists $_->{delete} } values %struct; |
| 51 |
|
|
unless ( @main ) { |
| 52 |
|
|
my @valid = grep { exists $_->{name} && !exists $_->{delete} } values %struct; |
| 53 |
|
|
if ( @valid ) { |
| 54 |
|
|
$valid[0]->{main} = 1; |
| 55 |
|
|
} |
| 56 |
|
|
} |
| 57 |
|
|
} |
| 58 |
|
|
my @delete_ids = map { $_->{id} } grep { $_->{id} && exists $_->{delete} } values %struct; |
| 59 |
|
|
if ( @delete_ids ) { |
| 60 |
|
|
my $sql = $keeper->SQL->prepare('delete from profile_credentials where class = ? and uid = ? and id in ('.join(',', map {'?'} @delete_ids).')'); |
| 61 |
|
|
$sql->execute( 'users::Email', $object->id, @delete_ids ); |
| 62 |
|
|
$sql->finish; |
| 63 |
|
|
} |
| 64 |
|
|
my @credentials = $keeper->get_documents( |
| 65 |
|
|
class => 'users::Email', |
| 66 |
|
|
uid => $object->id, |
| 67 |
|
|
order_by => 'main desc, name', |
| 68 |
|
|
); |
| 69 |
|
|
foreach my $cred ( @credentials ) { |
| 70 |
|
|
my $check = exists $struct{$cred->id} ? $struct{$cred->id} : undef; |
| 71 |
|
|
my $store = 0; |
| 72 |
|
|
if ( ref $check && exists $check->{name} && ($cred->name ne $check->{name} || $cred->name_orig ne $check->{orig}) ) { |
| 73 |
|
|
my $exists_foreign = $keeper->get_documents( class => 'users::Email', name => $check->{name}, uid_not => $object->id, count => 1 ); |
| 74 |
|
|
unless ( $exists_foreign ) { |
| 75 |
|
|
$cred->name( $check->{name} ); |
| 76 |
|
|
$cred->name_orig( $check->{orig} ); |
| 77 |
|
|
$store = 1; |
| 78 |
|
|
} |
| 79 |
|
|
} |
| 80 |
|
|
if ( ref $check && int($check->{main}) != int($cred->main) ) { |
| 81 |
|
|
$cred->main( int($check->{main}) ); |
| 82 |
|
|
$store = 1; |
| 83 |
|
|
} |
| 84 |
|
|
if ( ref $check && int($check->{status}) != int($cred->status) ) { |
| 85 |
|
|
$cred->status( int($check->{status}) ); |
| 86 |
|
|
$store = 1; |
| 87 |
|
|
} |
| 88 |
|
|
if ( $store ) { |
| 89 |
|
|
$cred->store; |
| 90 |
|
|
} |
| 91 |
|
|
} |
| 92 |
|
|
if ( exists $struct{new} && exists $struct{new}{name} ) { |
| 93 |
|
|
my @found = grep { $_->name eq $struct{new}{name} } @credentials; |
| 94 |
|
|
unless ( @found ) { |
| 95 |
|
|
my $check = $struct{new}; |
| 96 |
|
|
my $exists_foreign = $keeper->get_documents( class => 'users::Email', name => $check->{name}, count => 1 ); |
| 97 |
|
|
unless ( $exists_foreign ) { |
| 98 |
|
|
my $cred = $object->create_credential( email => $check->{orig}, main => $check->{main}, status => ($check->{status} || 0) ); |
| 99 |
|
|
push @credentials, $cred if ref $cred; |
| 100 |
|
|
} |
| 101 |
|
|
} |
| 102 |
|
|
} |
| 103 |
|
|
my ($main) = grep { $_->main } @credentials; |
| 104 |
|
|
$object->{$name.'s'} = \@credentials; |
| 105 |
|
|
return $main; |
| 106 |
|
|
} else { |
| 107 |
|
|
return $keeper->{users}->_email_format( $SETS->{$name} ); |
| 108 |
|
|
} |
| 109 |
|
|
|
| 110 |
|
|
</%init> |