avoid an undefined value warning from make_select
[bse.git] / site / cgi-bin / modules / BSE / ImportTargetArticle.pm
CommitLineData
3709451d
TC
1package BSE::ImportTargetArticle;
2use strict;
3use base 'BSE::ImportTargetBase';
4use BSE::API qw(bse_make_article bse_add_image bse_add_step_parent);
5use Articles;
6use Products;
7use OtherParents;
8
df2663f0 9our $VERSION = "1.001";
cb7fd78d 10
3709451d
TC
11sub new {
12 my ($class, %opts) = @_;
13
14 my $self = $class->SUPER::new(%opts);
15
16 my $importer = delete $opts{importer};
17
18 my $map = $importer->maps;
19 defined $map->{title}
20 or die "No title mapping found\n";
21
22 $self->{use_codes} = $importer->cfg_entry('codes', 0);
23 $self->{code_field} = $importer->cfg_entry("code_field", $self->default_code_field);
24
25 $self->{parent} = $importer->cfg_entry("parent", $self->default_parent);
26
27 if ($self->{use_codes} && !defined $map->{$self->{code_field}}) {
28 die "No product_code mapping found with 'codes' enabled\n";
29 }
30 $self->{ignore_missing} = $importer->cfg_entry("ignore_missing", 1);
31 $self->{reset_images} = $importer->cfg_entry("reset_images", 0);
32 $self->{reset_steps} = $importer->cfg_entry("reset_steps", 0);
33
34 return $self;
35}
36
37sub start {
38 my ($self) = @_;
39
40 $self->{parent_cache} = {};
41 $self->{leaves} = [];
42 $self->{parents} = [];
43}
44
45sub xform_entry {
46 my ($self, $importer, $entry) = @_;
47
3709451d
TC
48 $entry->{title} =~ /\S/
49 or die "title blank\n";
50
51 $entry->{title} =~ /\n/
52 and die "Title may not contain newlines";
53 $entry->{summary}
54 or $entry->{summary} = $entry->{title};
55 $entry->{description}
56 or $entry->{description} = $entry->{title};
57 $entry->{body}
58 or $entry->{body} = $entry->{title};
59}
60
61sub children_of {
62 my ($self, $parent) = @_;
63
64 Articles->children($parent);
65}
66
67sub make_parent {
68 my ($self, $importer, %entry) = @_;
69
70 return bse_make_article(%entry);
71}
72
73sub find_leaf {
74 my ($self, $leaf_id) = @_;
75
76 $leaf_id =~ tr/A-Za-z0-9_/_/cds;
77
78 my ($leaf) = Articles->getBy($self->{code_field}, $leaf_id)
79 or return;
80
81 return $leaf;
82}
83
84sub make_leaf {
85 my ($self, $importer, %entry) = @_;
86
87 return bse_make_article(%entry);
88}
89
0cca6ce6
TC
90sub fill_leaf {
91 my ($self, $importer, $leaf, %entry) = @_;
92
df2663f0
TC
93 if ($entry{tags}) {
94print "Setting tags $leaf->{id}: $entry{tags}\n";
95 my @tags = split '/', $entry{tags};
96 my $error;
97 unless ($leaf->set_tags(\@tags, \$error)) {
98 die "Error setting tags: $error";
99 }
100 }
101
0cca6ce6
TC
102 return 1;
103}
104
3709451d
TC
105sub row {
106 my ($self, $importer, $entry, $parents) = @_;
107
0cca6ce6 108 $self->xform_entry($importer, $entry);
3709451d
TC
109
110 $entry->{parentid} = $self->_find_parent($importer, $self->{parent}, @$parents);
111 my $leaf;
112 if ($self->{use_codes}) {
113 my $leaf_id = $entry->{$self->{code_field}};
114
115 $leaf = $self->find_leaf($leaf_id);
116 }
117 if ($leaf) {
118 @{$leaf}{keys %$entry} = values %$entry;
119 $leaf->save;
120 $importer->info("Updated $leaf->{id}: $entry->{title}");
121 if ($self->{reset_images}) {
122 $leaf->remove_images($importer->cfg);
123 $importer->info(" $leaf->{id}: Reset images");
124 }
125 if ($self->{reset_steps}) {
126 my @steps = OtherParents->getBy(childId => $leaf->{id});
127 for my $step (@steps) {
128 $step->remove;
129 }
130 }
131 }
132 else {
133 $leaf = $self->make_leaf
134 (
135 $importer,
136 cfg => $importer->cfg,
137 %$entry
138 );
139 $importer->info("Added $leaf->{id}: $entry->{title}");
140 }
141 for my $image_index (1 .. 10) {
142 my $file = $entry->{"image${image_index}_file"};
143 $file
144 or next;
145 my $full_file = $importer->find_file($file);
146
147 unless ($full_file) {
148 $self->{ignore_missing}
149 and next;
150 die "File '$file' not found for image$image_index\n";
151 }
152
153 my %opts = ( file => $full_file );
154 for my $key (qw/alt name url storage/) {
155 my $fkey = "image${image_index}_$key";
156 $entry->{$fkey}
157 and $opts{$key} = $entry->{$fkey};
158 }
159
160 my %errors;
161 my $im = bse_add_image($importer->cfg, $leaf, %opts,
162 errors => \%errors);
163 $im
164 or die join(", ",map "$_: $errors{$_}", keys %errors), "\n";
165 $importer->info(" $leaf->{id}: Add image '$file'");
166 }
167 for my $step_index (1 .. 10) {
168 my $step_id = $entry->{"step$step_index"};
169 $step_id
170 or next;
171 my $step;
172 if ($step_id =~ /^\d+$/) {
173 $step = Articles->getByPkey($step_id);
174 }
175 else {
176 $step = Articles->getBy(linkAlias => $step_id);
177 }
178 $step
179 or die "Cannot find stepparent with id $step_id\n";
180
181 bse_add_step_parent($importer->cfg, child => $leaf, parent => $step);
182 }
0cca6ce6 183 $self->fill_leaf($importer, $leaf, %$entry);
3709451d
TC
184 push @{$self->{leaves}}, $leaf;
185}
186
187sub _find_parent {
188 my ($self, $importer, $parent, @parents) = @_;
189
190 @parents
191 or return $parent;
192 my $cache = $self->{parent_cache};
193 unless ($cache->{$parent}) {
194 my @kids = $self->children_of($parent);
195 $cache->{$parent} = \@kids;
196 }
197
198 my $title = shift @parents;
199 my ($cat) = grep lc $_->{title} eq lc $title, @{$cache->{$parent}};
200 unless ($cat) {
201 my %opts =
202 (
203 cfg => $importer->cfg,
204 parentid => $parent,
205 title => $title,
206 body => $title,
207 );
208 $self->{catalog_template}
209 and $opts{template} = $self->{catalog_template};
210 $cat = $self->make_parent($importer, %opts);
211 $importer->info("Add parent $cat->{id}: $title");
212 push @{$cache->{$parent}}, $cat;
213 }
214
215 unless ($self->{catseen}{$cat->{id}}) {
216 $self->{catseen}{$cat->{id}} = 1;
217 push @{$self->{parents}}, $cat;
218 }
219
220 return $self->_find_parent($importer, $cat->{id}, @parents);
221}
222
223sub default_parent { -1 }
224
225sub default_code_field { "linkAlias" }
226
227sub leaves {
228 return @{$_[0]{leaves}}
229}
230
231sub parents {
232 return @{$_[0]{parents}}
233}
234
2351;