line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Gantry::Utils::Threeway; |
2
|
1
|
|
|
1
|
|
855
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
935
|
|
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
############################################################ |
5
|
|
|
|
|
|
|
# Functions # |
6
|
|
|
|
|
|
|
############################################################ |
7
|
|
|
|
|
|
|
sub new { |
8
|
0
|
|
|
0
|
1
|
|
my ( $class, $opt ) = @_; |
9
|
|
|
|
|
|
|
|
10
|
0
|
|
|
|
|
|
my $self = { options => $opt }; |
11
|
0
|
|
|
|
|
|
bless( $self, $class ); |
12
|
|
|
|
|
|
|
|
13
|
0
|
|
|
|
|
|
my @errors; |
14
|
0
|
|
|
|
|
|
foreach( qw/join_table primary_table secondary_table self/ ) { |
15
|
0
|
0
|
|
|
|
|
push( @errors, "$_ is not set properly" ) if ! $opt->{$_}; |
16
|
|
|
|
|
|
|
} |
17
|
|
|
|
|
|
|
|
18
|
0
|
0
|
|
|
|
|
if ( scalar( @errors ) ) { |
19
|
0
|
|
|
|
|
|
die join( "\n", @errors ); |
20
|
|
|
|
|
|
|
} |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
# populate self with data from site |
23
|
0
|
|
|
|
|
|
return( $self ); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
} # end new |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
#------------------------------------------------- |
28
|
|
|
|
|
|
|
# process() |
29
|
|
|
|
|
|
|
#------------------------------------------------- |
30
|
|
|
|
|
|
|
sub process { |
31
|
0
|
|
|
0
|
1
|
|
my( $self ) = @_; |
32
|
|
|
|
|
|
|
|
33
|
0
|
|
|
|
|
|
my $gself = $self->{options}{self}; |
34
|
|
|
|
|
|
|
|
35
|
0
|
|
0
|
|
|
|
my $template = $self->{options}{template} || 'threeway.tt'; |
36
|
0
|
|
|
|
|
|
my $join_table = $self->{options}{join_table}; |
37
|
0
|
|
0
|
|
|
|
my $form_action = $self->{options}{action} || $gself->uri; |
38
|
0
|
|
0
|
|
|
|
my $type = $self->{options}{type} || 'checkbox'; |
39
|
0
|
|
|
|
|
|
my $primary_table = $self->{options}{primary_table}; |
40
|
0
|
|
|
|
|
|
my $secondary_table = $self->{options}{secondary_table}; |
41
|
0
|
|
0
|
|
|
|
my $redirect_loc = $self->{options}{redirect_loc} || $gself->location; |
42
|
0
|
|
0
|
|
|
|
my $order_by = $self->{options}{order_by} || 'id'; |
43
|
0
|
|
0
|
|
|
|
my $legend = $self->{options}{legend} || "Add ${secondary_table}s"; |
44
|
|
|
|
|
|
|
|
45
|
0
|
|
|
|
|
|
my $sch = $gself->get_schema(); |
46
|
0
|
|
|
|
|
|
my %param = $gself->get_param_hash; |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
# pull from the member table |
49
|
0
|
|
|
|
|
|
my $selected = $sch->resultset( $join_table )->search( |
50
|
|
|
|
|
|
|
{ $primary_table => $self->{options}{primary_id} }, { } |
51
|
|
|
|
|
|
|
); |
52
|
|
|
|
|
|
|
|
53
|
0
|
|
|
|
|
|
my %selected; |
54
|
0
|
|
|
|
|
|
while ( my $r = $selected->next ) { |
55
|
0
|
|
|
|
|
|
$selected{$r->$secondary_table . ''} = $r->id; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
0
|
0
|
|
|
|
|
if ( $gself->is_post() ) { |
59
|
|
|
|
|
|
|
|
60
|
0
|
0
|
|
|
|
|
if ( $param{cancel} ) { |
61
|
0
|
|
|
|
|
|
$gself->relocate( $redirect_loc ); |
62
|
0
|
|
|
|
|
|
return; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
0
|
|
|
|
|
|
my $available = $sch->resultset( $secondary_table )->search( |
66
|
|
|
|
|
|
|
{}, { order_by => $order_by } |
67
|
|
|
|
|
|
|
); |
68
|
|
|
|
|
|
|
|
69
|
0
|
|
|
|
|
|
my %available; |
70
|
0
|
|
|
|
|
|
while ( my $r = $available->next ) { |
71
|
0
|
|
|
|
|
|
++$available{$r->id . ''}; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
0
|
|
|
|
|
|
my $values_ref; |
75
|
0
|
0
|
|
|
|
|
if ( $type eq 'checkbox' ) { |
76
|
0
|
|
|
|
|
|
foreach my $k ( keys %param ) { |
77
|
0
|
0
|
|
|
|
|
next if $k !~ /^subscribe/; |
78
|
|
|
|
|
|
|
|
79
|
0
|
|
|
|
|
|
my @val = split( ':', $k ); |
80
|
0
|
|
|
|
|
|
push( @{ $values_ref }, $val[1] ); |
|
0
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
# multiselect |
84
|
|
|
|
|
|
|
else { |
85
|
|
|
|
|
|
|
|
86
|
0
|
0
|
|
|
|
|
if ( $gself->engine =~ /CGI/ ) { |
87
|
0
|
|
|
|
|
|
$values_ref = $gself->cgi->{subscribe}; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
else { |
90
|
0
|
0
|
|
|
|
|
$values_ref = ref( $gself->params->{'subscribe'} ) eq 'ARRAY' |
91
|
|
|
|
|
|
|
? $gself->params->{'subscribe'} |
92
|
|
|
|
|
|
|
: [ $gself->params->{'subscribe'} ]; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
0
|
|
|
|
|
|
foreach my $val ( @{ $values_ref } ) { |
|
0
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
|
98
|
0
|
|
|
|
|
|
$sch->resultset( $join_table )->update_or_create( |
99
|
|
|
|
|
|
|
{ |
100
|
|
|
|
|
|
|
$primary_table => $self->{options}{primary_id}, |
101
|
|
|
|
|
|
|
$secondary_table => $val } |
102
|
|
|
|
|
|
|
); |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
# delete from available hash |
105
|
0
|
|
|
|
|
|
delete( $selected{$val} ); |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
0
|
|
|
|
|
|
my @remove; |
110
|
0
|
|
|
|
|
|
foreach my $k ( keys %selected ) { |
111
|
0
|
|
|
|
|
|
push( @remove, $selected{$k} ); |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
0
|
0
|
|
|
|
|
if ( scalar( @remove ) > 0 ) { |
115
|
0
|
|
|
|
|
|
$sch->resultset( $join_table )->search( |
116
|
|
|
|
|
|
|
id => \@remove |
117
|
|
|
|
|
|
|
)->delete; |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
$gself->relocate( |
121
|
0
|
0
|
|
|
|
|
ref( $redirect_loc ) eq 'CODE' |
122
|
|
|
|
|
|
|
? $redirect_loc->( $gself ) : $redirect_loc |
123
|
|
|
|
|
|
|
); |
124
|
|
|
|
|
|
|
|
125
|
0
|
|
|
|
|
|
return; |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
0
|
|
|
|
|
|
my $available = $sch->resultset( $secondary_table )->search( {}, { |
130
|
|
|
|
|
|
|
order_by => $order_by, |
131
|
|
|
|
|
|
|
} ); |
132
|
|
|
|
|
|
|
|
133
|
0
|
|
|
|
|
|
$gself->stash->view->template( $template ); |
134
|
0
|
|
|
|
|
|
$gself->stash->view->form( { |
135
|
|
|
|
|
|
|
legend => $legend, |
136
|
|
|
|
|
|
|
action => $form_action |
137
|
|
|
|
|
|
|
} ); |
138
|
0
|
|
|
|
|
|
$gself->stash->view->data( { |
139
|
|
|
|
|
|
|
type => $type, |
140
|
|
|
|
|
|
|
available => $available, |
141
|
|
|
|
|
|
|
selected => \%selected |
142
|
|
|
|
|
|
|
} ); |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
} # END process |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
# EOF |
147
|
|
|
|
|
|
|
1; |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
__END__ |