/
Matrix.pm
397 lines (331 loc) · 9.46 KB
/
Matrix.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# Vend::Options::Matrix - Interchange Matrix product options
#
# Copyright (C) 2002-2007 Interchange Development Group <interchange@icdevgroup.org>
# Copyright (C) 2002-2003 Mike Heins <mikeh@perusion.net>
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with this program; if not, write to the Free
# Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
# MA 02110-1301 USA.
#
package Vend::Options::Matrix;
$VERSION = substr(q$Revision: 1.17 $, 10);
=head1 NAME
Vend::Options::Matrix - Interchange Matrix Options Support
=head1 SYNOPSIS
[item-options]
or
[price code=SKU]
=head1 PREREQUISITES
Vend::Options
=head1 DESCRIPTION
The Vend::Options::Matrix module implements matrix product options for
Interchange. It is compatible with Interchange 4.8.x matrix options.
If the Interchange Variable MV_OPTION_TABLE is not set, it defaults
to "options", which combines options for Simple, Matrix, and
Modular into that one table. This goes along with foundation and
construct demos up until Interchange 4.9.8.
The "options" table remains the default for matrix options.
=head1 AUTHORS
Mike Heins <mikeh@perusion.net>
=head1 CREDITS
Jon Jensen <jon@swelter.net>
=cut
use Vend::Util;
use Vend::Data;
use Vend::Interpolate;
use Vend::Options;
use strict;
use vars qw/%Default/;
%Default = (
no_pricing => 1,
item_add_routine => 'Vend::Options::Matrix::testit',
table => 'options',
sort => 'o_sort',
variant_table => 'variants',
variant_sort => 'description',
);
my $Admin_page;
sub price_options { }
sub testit {
::logDebug("triggered routine testit! args=" . ::uneval(\@_));
}
sub display_options {
my ($item, $opt, $loc) = @_;
#::logDebug("Matrix options by module");
my $sku = $item->{mv_sku} || $item->{code};
$loc ||= $Vend::Cfg->{Options_repository}{Matrix} || \%Default;
my $map = $loc->{map} || {};
my $tab = $opt->{table} ||= $loc->{table} || 'options';
my $db = database_exists_ref($tab)
or do {
logOnce(
"Matrix options: unable to find table %s for item %s",
$tab,
$sku,
);
return undef;
};
my $record = $db->row_hash($sku) || {};
$record->{display_type} ||= $loc->{display_type};
my $tname = $db->name();
$opt->{display_type} ||= $record->{display_type};
$opt->{display_type} = lc $opt->{display_type};
$opt->{translate} = $loc->{translate} unless defined $opt->{translate};
my @rf;
my @out;
my $out;
# Will be different based on whether separate or not....
my $rsort;
my $inv_func;
if($opt->{inventory}) {
my ($tab, $col) = split /:+/, $opt->{inventory};
MAKEFUNC: {
my $idb = dbref($tab)
or do {
logError("Bad table %s for inventory function.", $tab);
last MAKEFUNC;
};
$idb->test_column($col)
or do {
logError(
"Bad column %s in table %s for inventory function.",
$col,
$tab,
);
last MAKEFUNC;
};
$inv_func = sub {
my $key = shift;
return $idb->field($key, $col);
};
}
}
use constant SEP_CODE => 0;
use constant SEP_GROUP => 1;
use constant SEP_VALUE => 2;
use constant SEP_LABEL => 3;
use constant SEP_WIDGET => 4;
use constant SEP_PRICE => 5;
use constant SEP_WHOLE => 6;
use constant SEP_HEIGHT => 7;
use constant SEP_WIDTH => 8;
use constant SEP_SIMPLE => 9;
use constant CODE => 0;
use constant DESCRIPTION => 1;
use constant PRICE => 2;
#::logDebug("ready to query options");
if($opt->{display_type} eq 'separate') {
for(qw/
code
o_group
o_value
o_label
o_widget
price
wholesale
o_height
o_width
o_simple
/) {
push @rf, ($map->{$_} || $_);
}
my @def;
if($item and $item->{code}) {
@def = split /-/, $item->{code};
}
my $fsel = $map->{sku} || 'sku';
my $rsel = $db->quote($sku, $fsel);
$rsort = find_sort($opt, $db, $loc);
my $q = "SELECT " .
join (",", @rf) .
" FROM $tname where $fsel = $rsel $rsort";
#::logDebug("tag_options matrix query: $q");
my $ary = $db->query($q);
#::logDebug("tag_options matrix ary: " . ::uneval($ary));
my $ishash = defined $item->{mv_ip} ? 1 : 0;
my $ref;
my $i = 0;
my $phony = { %{$item || { }} };
foreach $ref (@$ary) {
next unless $ref->[SEP_VALUE];
# skip based on inventory if enabled
if($inv_func) {
my $oh = $inv_func->($ref->[SEP_CODE]);
next if $oh <= 0;
}
$i++;
# skip unless o_value
$phony->{mv_sku} = $def[$i];
my $passed = $ref->[SEP_VALUE];
if($opt->{blank_label}) {
$passed = "=$opt->{blank_label}, $passed";
}
if ($opt->{label}) {
my $lab = $ref->[SEP_LABEL];
$lab = errmsg($lab) if $opt->{translate};
$lab = "<b>$lab</b>" if $opt->{bold};
push @out, $lab;
}
my $ary = Vend::Form::options_to_array($passed);
if($opt->{translate}) {
for(@$ary) {
$_->[1] = errmsg($_->[1]);
}
}
my $precursor = '';
$precursor = "$ref->[SEP_GROUP]$opt->{separator}" if $opt->{report};
$precursor = qq{<input type="hidden" name="mv_item_option" value="$ref->[SEP_GROUP]">} if $ref->[SEP_SIMPLE];
if ($ref->[SEP_SIMPLE]) {
push @out, $precursor . Vend::Interpolate::tag_accessories(
$sku,
'',
{
attribute => $ref->[SEP_GROUP],
default => undef,
extra => qq/id="$ref->[SEP_GROUP]" $opt->{extra}/,
item => $item,
js => $opt->{js},
name => $ishash ? undef : "mv_order_$ref->[SEP_GROUP]",
option_template => $opt->{option_template},
passed => $ary,
price => $opt->{price},
price_data => $ref->[SEP_PRICE],
height => $opt->{height} || $ref->[SEP_HEIGHT],
width => $opt->{width} || $ref->[SEP_WIDTH],
type => $opt->{type} || $ref->[SEP_WIDGET] || 'select',
},
$item || undef,
);
} else {
push @out, $precursor . Vend::Interpolate::tag_accessories(
$sku,
'',
{
passed => $ary,
type => $opt->{type} || $ref->[SEP_WIDGET] || 'select',
attribute => 'mv_sku',
price_data => $ref->[SEP_PRICE],
price => $opt->{price},
extra => qq/id="$ref->[SEP_GROUP]" $opt->{extra}/,
js => $opt->{js},
item => $phony,
},
$phony || undef,
);
}
}
$phony->{mv_sku} = $sku;
my $begin = '';
unless ($opt->{report}) {
$begin = Vend::Interpolate::tag_accessories(
$sku,
'',
{
type => 'hidden',
attribute => 'mv_sku',
item => $phony,
default => $sku,
},
$phony,
);
}
if($opt->{td}) {
for(@out) {
$out .= "<td>$begin$_</td>";
$begin = '';
}
}
else {
$opt->{joiner} = "<br$Vend::Xtrailer>" if ! $opt->{joiner};
$out .= $begin;
$out .= join $opt->{joiner}, @out;
}
}
else {
my $vtab = $opt->{variant_table} || $loc->{variant_table};
my $vdb = database_exists_ref($vtab)
or do {
logOnce(
"Matrix options: unable to find variant table %s for item %s",
$vtab,
$sku,
);
return undef;
};
$opt->{type} ||= $record->{widget};
$rsort = $opt->{variant_sort} || $loc->{variant_sort};
$rsort = "ORDER BY $rsort" if $rsort;
$rsort ||= '';
for(qw/code description price/) {
push @rf, ($map->{$_} || $_);
}
my $lcol = $map->{sku} || 'sku';
my $lval = $vdb->quote($sku, $lcol);
my $vname = $vdb->name();
my $q = "SELECT " . join(",", @rf);
$q .= " FROM $vname WHERE $lcol = $lval $rsort";
#::logDebug("tag_options matrix query: $q");
my $ary = $vdb->query($q);
#::logDebug("tag_options matrix ary: " . ::uneval($ary));
my $ref;
my $price = {};
foreach $ref (@$ary) {
# skip unless description
next unless $ref->[DESCRIPTION];
# skip based on inventory if enabled
if($inv_func) {
my $oh = $inv_func->($ref->[CODE]);
next if $oh <= 0;
}
my $desc = $ref->[DESCRIPTION];
$desc =~ s/,/,/g;
$desc =~ s/=/=/g;
$price->{$ref->[CODE]} = $ref->[PRICE];
push @out, "$ref->[CODE]=$desc";
}
if($opt->{blank_label}) {
unshift @out, "=$opt->{blank_label}";
}
$out .= "<td>" if $opt->{td};
$out .= Vend::Interpolate::tag_accessories(
$sku,
'',
{
attribute => 'code',
default => undef,
extra => $opt->{extra},
item => $item,
js => $opt->{js},
name => 'mv_sku',
passed => join(",", @out),
price => $opt->{price},
price_data => $price,
type => $opt->{type} || 'select',
},
$item || undef,
);
$out .= "</td>" if $opt->{td};
#::logDebug("matrix option returning $out");
}
return $out;
}
sub admin_page {
my $item = shift;
my $opt = shift;
my $page = $Tag->file('include/Options/Matrix') || $Admin_page;
Vend::Util::parse_locale(\$page);
return interpolate_html($page);
}
$Admin_page = <<'EoAdminPage';
EoAdminPage
1;