cw
public text v1 · immutablesub cat_list_by_form : Path('/catalog') : Args(1)
{
my ($self, $c, $material) = @_;
$material = join(' ', map { ucfirst } split(/\-/, $material));
# Make sure the material exists in the database
my $mat = $c->model('MXWebDB::Product')->single({material => $material, enabled => 1});
unless ($mat) { $c->detach('/default'); }
# Data structure passed to template:
# E.g., Aluminum, 30 products per page
#$cats = { 'Angle' => [ {
# 'cat' => '6061-t6'
# 'page' => 1,
# 'from' => '1/8 x 3/4 x 3/4',
# 'to' => '1/2 x 3 x 3',
# },
# ....
# ]
# 'Flat' => [...
# };
my $cats = {};
my $nprods_cat_by_form_rs = $c->model('MXWebDB::Product')->search(
{
material => $material,
enabled => '1',
},
{
select => [ 'form', 'cat', { count => 'primaryKey' } ],
as => [qw/ form cat num_prods /],
group_by => [qw/ form cat /],
order_by => qw/ form cat sequence ASC /,
},
);
my $curr_form = "";
my $prev_form = "";
my $cats_arr = [];
while (my $nprod = $nprods_cat_by_form_rs->next)
{
$curr_form = $nprod->form;
my $num_prods = $nprod->get_column('num_prods');
my $cnum_prods = $c->config->{catalog}->{num_prods};
my $num_pages = ( ($num_prods % $cnum_prods) ?
(floor($num_prods / $cnum_prods) + 1)
: (floor($num_prods / $cnum_prods)) );
# Form has changed, reinit the cats array
if ($curr_form ne $prev_form && $prev_form ne "")
{
$cats_arr = [];
}
for (my $i = 0; $i < $num_pages; $i++)
{
my $cat = {};
$cat->{cat} = $nprod->cat;
my @prods = $c->model('MXWebDB::Product')->search(
{
material => $material,
form => $nprod->form,
cat => $nprod->cat,
enabled => 1,
},
{
page => ($i + 1),
rows => $c->config->{catalog}->{num_prods},
order_by => qw/ sequence ASC /,
},
);
my $page = $i + 1;
if ($num_pages > 1)
{
my $from = "";
my $to = "";
my $val = 0;
my @denoms = (32, 16, 8, 5, 4, 2);
foreach my $dim ($prods[0]->dims(undef, { order_by => 'sequence' }))
{
$val = $dim->frac;
if ($dim->frac =~ /^.*\/(\d+)$/)
{
my @d = grep {/^$1$/} @denoms;
if (@d > 0)
{
$val = $dim->frac;
}
else
{
$val = $dim->value;
}
}
if ($from)
$from .= " x ".$val;
}
else
{
$from .= $val;
}
}
foreach my $dim ($prods[@prods - 1]->dims(undef, { order_by => 'sequence' }))
{
$val = $dim->frac;
if ($dim->frac =~ /^.*\/(\d+)$/)
{
my @d = grep {/^$1$/} @denoms;
if (@d > 0)
{
$val = $dim->frac;
}
else
{
$val = $dim->value;
}
}
if ($to)
{
$to .= " x ".$val;
}
else
{
$to .= $val;
}
}
$cat->{from} = $from;
$cat->{to} = $to;
}
$cat->{page} = $page;
push(@$cats_arr, $cat);
$cats->{$nprod->form} = \@$cats_arr;
}
$prev_form = $curr_form;
}
#$c->log->debug("cats = ".Dumper($cats));
$c->stash->{cats} = $cats;
$c->stash->{material} = $material;
$c->stash->{template} = 'catalog/cat_list_by_form.tt2';
}
sub jump : Path('/catalog/jump')
{
my ($self, $c) = @_;
$c->log->debug("mats = ".$c->req->params->{mats});
$c->detach('cat_list_by_form', $c->req->params->{mats});
}