1 | # +-----------------------------------------------------------------------+ |
---|
2 | # | pLoader - a Perl photo uploader for Piwigo | |
---|
3 | # +-----------------------------------------------------------------------+ |
---|
4 | # | Copyright(C) 2008 Piwigo Team http://piwigo.org | |
---|
5 | # +-----------------------------------------------------------------------+ |
---|
6 | # | This program is free software; you can redistribute it and/or modify | |
---|
7 | # | it under the terms of the GNU General Public License as published by | |
---|
8 | # | the Free Software Foundation | |
---|
9 | # | | |
---|
10 | # | This program is distributed in the hope that it will be useful, but | |
---|
11 | # | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
12 | # | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
13 | # | General Public License for more details. | |
---|
14 | # | | |
---|
15 | # | You should have received a copy of the GNU General Public License | |
---|
16 | # | along with this program; if not, write to the Free Software | |
---|
17 | # | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
---|
18 | # | USA. | |
---|
19 | # +-----------------------------------------------------------------------+ |
---|
20 | package Uploader::Object; |
---|
21 | use strict; |
---|
22 | use Data::Dumper; |
---|
23 | use Storable; |
---|
24 | use File::Slurp ; |
---|
25 | use Data::Dumper; |
---|
26 | use base qw/ |
---|
27 | Class::Accessor::Fast |
---|
28 | /; |
---|
29 | |
---|
30 | # $param is a hash with : |
---|
31 | # key : member name |
---|
32 | sub new { |
---|
33 | my ( $class, $params ) = @_; |
---|
34 | |
---|
35 | $params ||= {}; |
---|
36 | |
---|
37 | my $self = bless $params, $class ; |
---|
38 | |
---|
39 | # create member accessors |
---|
40 | #__PACKAGE__->mk_accessors( |
---|
41 | # keys %$params |
---|
42 | #); |
---|
43 | |
---|
44 | |
---|
45 | if(defined $self->{plugin_file}){ |
---|
46 | die "Plugin file is missing." unless -e $self->{plugin_file}; |
---|
47 | $self->InitPlugins(); |
---|
48 | } |
---|
49 | |
---|
50 | # member accessors initialization |
---|
51 | eval { |
---|
52 | map { |
---|
53 | $self->$_( |
---|
54 | $params->{$_} |
---|
55 | ); |
---|
56 | } |
---|
57 | keys %$params; |
---|
58 | }; |
---|
59 | if($@){ |
---|
60 | warn $@; |
---|
61 | } |
---|
62 | |
---|
63 | # must be defined in child class |
---|
64 | $self->Init(); |
---|
65 | |
---|
66 | |
---|
67 | return $self; |
---|
68 | } |
---|
69 | |
---|
70 | |
---|
71 | sub InitPlugins { |
---|
72 | my ( $self ) = @_; |
---|
73 | |
---|
74 | my $data; |
---|
75 | eval { $data = read_file( $self->{plugin_file} ); } ; |
---|
76 | |
---|
77 | my $plugins = [] ; |
---|
78 | if( defined $data){ |
---|
79 | my $expr = '$plugins = ' ; |
---|
80 | $expr .= "$data " ; |
---|
81 | eval $expr ; |
---|
82 | } |
---|
83 | # load plugins |
---|
84 | |
---|
85 | my $hplugins = $plugins->[0] if scalar @$plugins; |
---|
86 | while( my ( $key, $value ) = each %$hplugins ) { |
---|
87 | $self->ReadParams( $value, $key); |
---|
88 | } |
---|
89 | |
---|
90 | } |
---|
91 | |
---|
92 | sub ReadParams { |
---|
93 | my( $self, $file, $key ) = @_ ; |
---|
94 | |
---|
95 | my $expr_params ; |
---|
96 | eval { $expr_params = read_file( $file ); } ; |
---|
97 | |
---|
98 | my $paramValues = [] ; |
---|
99 | if($expr_params){ |
---|
100 | my $expr = '$paramValues = ' ; |
---|
101 | $expr .= "$expr_params " ; |
---|
102 | eval $expr ; |
---|
103 | } |
---|
104 | return unless 'ARRAY' eq ref $paramValues ; |
---|
105 | if($@){ |
---|
106 | die "Cannot load data $@"; |
---|
107 | } |
---|
108 | if(scalar(@$paramValues )){ |
---|
109 | my $params = $paramValues->[0] ; |
---|
110 | $self->SetKeyValues($params, $key); |
---|
111 | } |
---|
112 | } |
---|
113 | |
---|
114 | sub SetKeyValues { |
---|
115 | my ( $self, $params, $key )= @_; |
---|
116 | if (defined $key) { |
---|
117 | foreach( keys %$params ) { |
---|
118 | $self->{$key}->{$_} = $params->{$_} ; |
---|
119 | } |
---|
120 | } |
---|
121 | else { |
---|
122 | foreach( keys %$params ) { |
---|
123 | $self->{$_} = $params->{$_} ; |
---|
124 | } |
---|
125 | } |
---|
126 | } |
---|
127 | |
---|
128 | |
---|
129 | sub get_storable { |
---|
130 | my ( $self, $keys ) = @_; |
---|
131 | |
---|
132 | my $data = { |
---|
133 | map { |
---|
134 | $_ => $self->{$_}, |
---|
135 | } |
---|
136 | @$keys |
---|
137 | }; |
---|
138 | |
---|
139 | $data; |
---|
140 | } |
---|
141 | |
---|
142 | 1; |
---|