Commit | Line | Data |
---|---|---|
ca9aa2bf TC |
1 | package BSE::Edit::Product; |
2 | use strict; | |
3 | use base 'BSE::Edit::Article'; | |
4 | use Products; | |
5 | use HTML::Entities; | |
6 | ||
7 | my %money_fields = | |
8 | ( | |
9 | retailPrice => "Retail price", | |
10 | wholesalePrice => "Wholesale price", | |
11 | gst => "GST", | |
12 | ); | |
13 | ||
14 | sub generator { 'Generate::Product' } | |
15 | ||
16 | sub base_template_dirs { | |
17 | return ( "products" ); | |
18 | } | |
19 | ||
20 | sub extra_templates { | |
21 | my ($self, $article) = @_; | |
22 | ||
23 | my @extras = $self->SUPER::extra_templates($article); | |
24 | my $basedir = $self->{cfg}->entry('paths', 'templates', $Constants::TMPLDIR); | |
25 | push @extras, 'shopitem.tmpl' if -f "$basedir/shopitem.tmpl"; | |
26 | ||
27 | return @extras; | |
28 | } | |
29 | ||
30 | sub hash_tag { | |
31 | my ($article, $arg) = @_; | |
32 | ||
33 | my $value = $article->{$arg}; | |
34 | defined $value or $value = ''; | |
35 | ||
36 | return encode_entities($value); | |
37 | } | |
38 | ||
39 | sub low_edit_tags { | |
40 | my ($self, $acts, $req, $article, $articles, $msg, $errors) = @_; | |
41 | ||
42 | return | |
43 | ( | |
44 | product => [ \&hash_tag, $article ], | |
45 | $self->SUPER::low_edit_tags($acts, $req, $article, $articles, $msg, | |
46 | $errors), | |
47 | alloptions => join(",", sort keys %Constants::SHOP_PRODUCT_OPTS), | |
48 | ); | |
49 | } | |
50 | ||
51 | sub edit_template { | |
52 | my ($self, $article, $cgi) = @_; | |
53 | ||
54 | my $base = 'product'; | |
55 | my $t = $cgi->param('_t'); | |
56 | if ($t && $t =~ /^\w+$/) { | |
57 | $base = $t; | |
58 | } | |
59 | return $self->{cfg}->entry('admin templates', $base, | |
60 | "admin/edit_$base"); | |
61 | } | |
62 | ||
63 | sub add_template { | |
64 | my ($self, $article, $cgi) = @_; | |
65 | ||
66 | return $self->{cfg}->entry('admin templates', 'add_product', | |
67 | 'admin/add_product'); | |
68 | } | |
69 | ||
70 | sub validate_parent { | |
71 | my ($self, $data, $articles, $parent, $rmsg) = @_; | |
72 | ||
73 | my $shopid = $self->{cfg}->entryErr('articles', 'shop'); | |
74 | unless ($parent && | |
75 | $parent->{generator} eq 'Generate::Catalog') { | |
76 | $$rmsg = "Products must be in a catalog (not $parent->{generator})"; | |
77 | return; | |
78 | } | |
79 | ||
80 | return $self->SUPER::validate_parent($data, $articles, $parent, $rmsg); | |
81 | } | |
82 | ||
83 | sub _validate_common { | |
84 | my ($self, $data, $articles, $errors) = @_; | |
85 | ||
86 | for my $col (keys %money_fields) { | |
87 | my $value = $data->{$col}; | |
88 | unless ($value =~ /^\d+(\.\d{1,2})?\s*/) { | |
89 | $errors->{$col} = "$money_fields{$col} invalid"; | |
90 | } | |
91 | } | |
92 | ||
93 | my @bad_opts =grep !$Constants::SHOP_PRODUCT_OPTS{$_}, | |
94 | split /,/, $data->{options}; | |
95 | if (@bad_opts) { | |
96 | $errors->{options} = "Bad product options '". join(",", @bad_opts)."' entered"; | |
97 | } | |
98 | ||
99 | return !keys %$errors; | |
100 | } | |
101 | ||
102 | sub validate { | |
103 | my ($self, $data, $articles, $rmsg, $errors) = @_; | |
104 | ||
105 | my $ok = $self->SUPER::validate($data, $articles, $rmsg, $errors); | |
106 | $self->_validate_common($data, $articles, $errors); | |
107 | ||
108 | for my $field (qw(title summary body)) { | |
109 | unless ($data->{$field} =~ /\S/) { | |
110 | $errors->{$field} = "No $field entered"; | |
111 | } | |
112 | } | |
113 | ||
114 | return $ok && !keys %$errors; | |
115 | } | |
116 | ||
117 | sub validate_old { | |
118 | my ($self, $article, $data, $articles, $rmsg, $errors) = @_; | |
119 | ||
120 | $self->SUPER::validate($data, $articles, $rmsg, $errors) | |
121 | or return; | |
122 | ||
123 | return !keys %$errors; | |
124 | } | |
125 | ||
126 | sub possible_parents { | |
127 | my ($self, $article, $articles) = @_; | |
128 | ||
129 | my %labels; | |
130 | my @values; | |
131 | ||
132 | my $shopid = $self->{cfg}->entryErr('articles', 'shop'); | |
133 | # the parents of a catalog can be other catalogs or the shop | |
134 | my $shop = $articles->getByPkey($shopid); | |
135 | my @work = [ $shopid, $shop->{title} ]; | |
136 | while (@work) { | |
137 | my ($id, $title) = @{pop @work}; | |
138 | push(@values, $id); | |
139 | $labels{$id} = $title; | |
140 | push @work, map [ $_->{id}, $title.' / '.$_->{title} ], | |
141 | sort { $b->{displayOrder} <=> $a->{displayOrder} } | |
142 | grep $_->{generator} eq 'Generate::Catalog', | |
143 | $articles->getBy(parentid=>$id); | |
144 | } | |
145 | shift @values; | |
146 | delete $labels{$shopid}; | |
147 | return (\@values, \%labels); | |
148 | } | |
149 | ||
150 | sub table_object { | |
151 | my ($self, $articles) = @_; | |
152 | ||
153 | 'Products'; | |
154 | } | |
155 | ||
156 | sub get_article { | |
157 | my ($self, $articles, $article) = @_; | |
158 | ||
159 | return Products->getByPkey($article->{id}); | |
160 | } | |
161 | ||
162 | sub make_link { | |
163 | my ($self, $article) = @_; | |
164 | ||
165 | my $shop_uri = $self->{cfg}->entry('uri', 'shop', '/shop'); | |
166 | my $urlbase = $self->{cfg}->entryVar('site', 'secureurl'); | |
167 | return $urlbase.$shop_uri."/shop$article->{id}.html"; | |
168 | } | |
169 | ||
170 | sub _fill_product_data { | |
171 | my ($self, $req, $data, $src) = @_; | |
172 | ||
173 | for my $money_col (qw(retailPrice wholesalePrice gst)) { | |
174 | if (exists $src->{$money_col}) { | |
175 | if ($src->{$money_col} =~ /^\d+(\.\d\d)?\s*/) { | |
176 | $data->{$money_col} = 100 * $src->{$money_col}; | |
177 | } | |
178 | else { | |
179 | $data->{$money_col} = 0; | |
180 | } | |
181 | } | |
182 | } | |
183 | if (exists $src->{leadTime}) { | |
184 | $src->{leadTime} =~ /^\d+\s*$/ | |
185 | or $src->{leadTime} = 0; | |
186 | $data->{leadTime} = $src->{leadTime}; | |
187 | } | |
188 | } | |
189 | ||
190 | sub fill_new_data { | |
191 | my ($self, $req, $data, $articles) = @_; | |
192 | ||
193 | $self->_fill_product_data($req, $data, $data); | |
194 | ||
195 | return $self->SUPER::fill_new_data($req, $data, $articles); | |
196 | } | |
197 | ||
198 | sub fill_old_data { | |
199 | my ($self, $req, $article, $src) = @_; | |
200 | ||
201 | $self->_fill_product_data($req, $article, $src); | |
202 | ||
203 | return $self->SUPER::fill_old_data($req, $article, $src); | |
204 | } | |
205 | ||
206 | 1; |