Line # Revision Author
1 523 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 566 ahitrov my $template = $name.'_name_orig_';
21 523 ahitrov if ( $key =~ /^$template/ ) {
22 my $id = $key =~ /(\d+)/ ? $1 : 'new';
23 if ( $keeper->{users}->_phone_format( $val ) ) {
24 $struct{$id}{orig} = $val;
25 $struct{$id}{name} = $keeper->{users}->_phone_reduction($val);
26 $struct{$id}{format} = $keeper->{users}->_phone_format($val);
27 $struct{$id}{id} = $id;
28 }
29 }
30 $template = $name.'_active_';
31 if ( $key =~ /^$template/ ) {
32 my $id = $key =~ /(\d+)/ ? $1 : 'new';
33 if ( $val ) {
34 $struct{$id}{status} = 1;
35 $struct{$id}{id} = $id;
36 }
37 }
38 }
39 if ( exists $SETS->{$name.'.delete'} ) {
40 if ( ref $SETS->{$name.'.delete'} eq 'ARRAY' ) {
41 map { $struct{$_}{delete} = 1 } @{$SETS->{$name.'.delete'}};
42 } elsif ( $SETS->{$name.'.delete'} ) {
43 $struct{$SETS->{$name.'.delete'}}{delete} = 1;
44 }
45 }
46 if ( exists $SETS->{$name.'.main'} && $SETS->{$name.'.main'} && exists $struct{$SETS->{$name.'.main'}} && !exists $struct{$SETS->{$name.'.main'}}{delete} ) {
47 $struct{$SETS->{$name.'.main'}}{main} = 1;
48 } elsif ( exists $struct{new} ) {
49 $struct{new}{main} = 1;
50 } else {
51 my @main = grep { exists $_->{name} && exists $_->{main} && $_->{main} && !exists $_->{delete} } values %struct;
52 unless ( @main ) {
53 my @valid = grep { exists $_->{name} && !exists $_->{delete} } values %struct;
54 if ( @valid ) {
55 $valid[0]->{main} = 1;
56 }
57 }
58 }
59 my @delete_ids = map { $_->{id} } grep { $_->{id} && exists $_->{delete} } values %struct;
60 if ( @delete_ids ) {
61 my $sql = $keeper->SQL->prepare('delete from profile_credentials where class = ? and uid = ? and id in ('.join(',', map {'?'} @delete_ids).')');
62 $sql->execute( 'users::Phone', $object->id, @delete_ids );
63 $sql->finish;
64 }
65 my @credentials = $keeper->get_documents(
66 class => 'users::Phone',
67 uid => $object->id,
68 order_by => 'main desc, name',
69 );
70 foreach my $cred ( @credentials ) {
71 my $check = exists $struct{$cred->id} ? $struct{$cred->id} : undef;
72 my $store = 0;
73 if ( ref $check && exists $check->{name} && ($cred->name ne $check->{name} || $cred->name_orig ne $check->{orig}) ) {
74 my $exists_foreign = $keeper->get_documents( class => 'users::Phone', name => $check->{name}, uid_not => $object->id, count => 1 );
75 unless ( $exists_foreign ) {
76 $cred->name( $check->{name} );
77 $cred->name_orig( $check->{orig} );
78 $cred->name_format( $check->{format} );
79 $store = 1;
80 }
81 }
82 if ( ref $check && int($check->{main}) != int($cred->main) ) {
83 $cred->main( int($check->{main}) );
84 $store = 1;
85 }
86 if ( ref $check && int($check->{status}) != int($cred->status) ) {
87 $cred->status( int($check->{status}) );
88 $store = 1;
89 }
90 if ( $store ) {
91 $cred->store;
92 }
93 }
94 if ( exists $struct{new} && exists $struct{new}{name} ) {
95 my @found = grep { $_->name eq $struct{new}{name} } @credentials;
96 unless ( @found ) {
97 my $check = $struct{new};
98 my $exists_foreign = $keeper->get_documents( class => 'users::Phone', name => $check->{name}, count => 1 );
99 unless ( $exists_foreign ) {
100 my $cred = $object->create_credential( phone => $check->{orig}, main => $check->{main}, status => ($check->{status} || 0) );
101 push @credentials, $cred if ref $cred;
102 }
103 }
104 }
105 my ($main) = grep { $_->main } @credentials;
106 $object->{$name.'s'} = \@credentials;
107 return $main;
108 } else {
109 return $keeper->{users}->_phone_format( $SETS->{$name} );
110 }
111
112 </%init>