getting rid of old commerce code. bye bye!

This commit is contained in:
JT Smith 2008-05-27 20:26:16 +00:00
parent f3b14a227c
commit be8ce29f57
36 changed files with 13 additions and 10753 deletions

File diff suppressed because it is too large Load diff

View file

@ -1,905 +0,0 @@
package WebGUI::Operation::ProductManager;
use strict;
use WebGUI::SQL;
use WebGUI::HTMLForm;
use WebGUI::Form;
use WebGUI::International;
use WebGUI::AdminConsole;
use Tie::IxHash;
use WebGUI::Product;
use WebGUI::HTML;
#-------------------------------------------------------------------
=head2 _submenu ( $session )
Returns a rendered Admin Console view, with a standard list of five submenu items.
=head3 $session
The current WebGUI session object.
=head3 workarea
A scalar of HTML that defines the current workarea.
=head3 title
The i18n key of the title of this workarea.
=cut
sub _submenu {
my $session = shift;
my $i18n = WebGUI::International->new($session, "ProductManager");
my $workarea = shift;
my $title = shift;
$title = $i18n->get($title) if ($title);
my $ac = WebGUI::AdminConsole->new($session,"productManager");
my $productId = $session->form->process("productId") || $session->scratch->get('managingProduct');
undef $productId if ($productId eq 'new');
$ac->addSubmenuItem($session->url->page('op=editProduct;productId=new'), $i18n->get('add product'));
$ac->addSubmenuItem($session->url->page('op=listProducts'), $i18n->get('list products'));
$ac->addSubmenuItem($session->url->page('op=manageProduct;productId='.$productId), $i18n->get('manage product')) if ($productId);
$ac->addSubmenuItem($session->url->page('op=listProductVariants;productId='.$productId), $i18n->get('list variants')) if ($productId);
return $ac->render($workarea, $title);
}
#----------------------------------------------------------------------------
=head2 canView ( session [, user] )
Returns true if the user can administrate this operation. user defaults to
the current user.
=cut
sub canView {
my $session = shift;
my $user = shift || $session->user;
return $user->isInGroup( $session->setting->get("groupIdAdminProductManager") );
}
#-------------------------------------------------------------------
=head2 www_deleteProductParameterOption ( $session )
Deletes a product parameter option specified by form parameter 'optionId'.
=head3 $session
The current WebGUI session object.
=cut
sub www_deleteProductParameterOption {
my $session = shift;
my $optionId = $session->form->process("optionId");
return $session->privilege->insufficient unless canView($session);
WebGUI::Product->getByOptionId($session,$optionId)->deleteOption($optionId);
return WebGUI::Operation::execute($session,'manageProduct');
}
#-------------------------------------------------------------------
=head2 www_deleteProductParameter ( $session )
Deletes a product parameter specified by form parameter 'parameterId'.
=head3 $session
The current WebGUI session object.
=cut
sub www_deleteProductParameter {
my $session = shift;
my $parameterId = $session->form->process("parameterId");
return $session->privilege->insufficient unless canView($session);
WebGUI::Product->getByParameterId($session,$parameterId)->deleteParameter($parameterId);
return WebGUI::Operation::execute($session,'manageProduct');
}
#-------------------------------------------------------------------
=head2 www_deleteProduct ( $session )
Deletes a product specified by form parameter 'productId'.
=head3 $session
The current WebGUI session object.
=cut
sub www_deleteProduct {
my $session = shift;
my $productId = $session->form->process("productId");
return $session->privilege->insufficient unless canView($session);
WebGUI::Product->new($session,$productId)->delete;
return WebGUI::Operation::execute($session,'listProducts');
}
#-------------------------------------------------------------------
=head2 www_editProduct ( $session )
Returns a form to edit or add a new product.
=head3 $session
The current WebGUI session object.
=cut
sub www_editProduct {
my $session = shift;
my ($productId, $product, $f, $i18n);
return $session->privilege->insufficient unless canView($session);
$i18n = WebGUI::International->new($session, 'ProductManager');
$productId = $session->form->process("productId");
unless ($productId eq 'new') {
$product = WebGUI::Product->new($session,$productId)->get;
}
$f = WebGUI::TabForm->new($session);
$f->addTab("properties","Properties");
$f->addTab("actions","Actions");
$f->submit;
$f->hidden({
name => 'op',
value => 'editProductSave'
});
$f->hidden({
name => 'productId',
value => $productId
});
$f->getTab("properties")->text(
-name => 'title',
-label => $i18n->get('title'),
-hoverHelp => $i18n->get('title description'),
-value => $session->form->process("title") || $product->{title},
-maxlength => 255,
);
$f->getTab("properties")->textarea(
-name => 'description',
-label => $i18n->get('description'),
-hoverHelp => $i18n->get('description description'),
-value => $session->form->process("decsription") || $product->{description},
);
$f->getTab("properties")->float(
-name => 'price',
-label => $i18n->get('price'),
-hoverHelp => $i18n->get('price description'),
-value => $session->form->process("price") || $product->{price},
-maxlength => 13,
);
$f->getTab("properties")->yesNo(
-name => 'useSalesTax',
-label => $i18n->get('useSalesTax'),
-hoverHelp => $i18n->get('useSalesTax description'),
-value => $session->form->process("useSalesTax") || $product->{useSalesTax},
);
$f->getTab("properties")->float(
-name => 'weight',
-label => $i18n->get('weight'),
-hoverHelp => $i18n->get('weight description'),
-value => $session->form->process("weight") || $product->{weight},
-maxlength => 9,
);
$f->getTab("properties")->text(
-name => 'sku',
-label => $i18n->get('sku'),
-hoverHelp => $i18n->get('sku description'),
-value => $session->form->process("sku") || $product->{sku},
-maxlength => 64,
);
$f->getTab("properties")->template(
-name => 'templateId',
-label => $i18n->get('template'),
-hoverHelp => $i18n->get('template description'),
-value => $session->form->process("templateId") || $product->{templateId},
-namespace => 'Commerce/Product',
);
$f->getTab("properties")->text(
-name => 'skuTemplate',
-label => $i18n->get('sku template'),
-hoverHelp => $i18n->get('sku template description'),
-value => $session->form->process("skuTemplate") || $product->{skuTemplate},
-maxlength => 255,
);
$f->getTab("actions")->group(
-name => 'groupId',
-label => $i18n->get('group id'),
-hoverHelp => $i18n->get('group id description'),
-value => $session->form->process("groupId") || $product->{groupId},
);
my %groupExpiresOffsetOptions;
tie %groupExpiresOffsetOptions, 'Tie::IxHash',
'1month' => $i18n->get("1 month"),
'6month' => $i18n->get("6 months"),
'1year' => $i18n->get("1 year"),
'2year' => $i18n->get("2 years"),
'3year' => $i18n->get("3 years"),
'5year' => $i18n->get("5 years"),
'10year' => $i18n->get("10 years"),
'1000year' => $i18n->get("lifetime"),
;
$f->getTab("actions")->selectBox(
-name => 'groupExpiresOffset',
-label => $i18n->get('group expires offset'),
-hoverHelp => $i18n->get('group expires offset description'),
-value => $session->form->process("groupExpiresOffset") || $product->{groupExpiresOffset},
-options => \%groupExpiresOffsetOptions,
-defaultValue => '1000year',
);
return _submenu($session,$f->print, 'edit product');
}
#-------------------------------------------------------------------
=head2 www_editProductSave ( $session )
Saves the properties of a product.
=head3 $session
The current WebGUI session object.
=cut
sub www_editProductSave {
my $session = shift;
my ($self, @error, $productId, $product, $i18n);
return $session->privilege->insufficient unless canView($session);
$i18n = WebGUI::International->new($session, 'ProductManager');
push(@error, $i18n->get('edit product title error')) unless $session->form->process("title");
push(@error, $i18n->get('edit product price error')) unless ($session->form->process("price") && $session->form->process("price") =~ /^\d+(\.\d+)?$/);
push(@error, $i18n->get('edit product weight error')) unless (defined $session->form->process("weight") && $session->form->process("price") =~ /^\d+(\.\d+)?$/);
push(@error, $i18n->get('edit product sku error')) unless ($session->form->process("sku"));
return '<ul><li>'.join('</li><li>', @error).'</li></ul><br />'.WebGUI::Operation::execute($session,'editProduct') if (@error);
$productId = $session->form->process("productId");
$product = WebGUI::Product->new($session,$productId);
$product->set({
title => $session->form->process("title"),
description => $session->form->process("description"),
price => $session->form->process("price"),
useSalesTax => $session->form->process("useSalesTax"),
weight => $session->form->process("weight"),
sku => $session->form->process("sku"),
templateId => $session->form->process("templateId"),
skuTemplate => $session->form->process("skuTemplate"),
groupId => $session->form->process('groupId'),
groupExpiresOffset => $session->form->process('groupExpiresOffset'),
});
return www_manageProduct($session, $product->get('productId'));
}
#-------------------------------------------------------------------
=head2 www_editProductParameter ( $session )
Returns a form to edit a product parameter.
=head3 $session
The current WebGUI session object.
=cut
sub www_editProductParameter {
my $session = shift;
my ($parameterId, $product, $productId, $parameter, $f, $i18n);
return $session->privilege->insufficient unless canView($session);
$i18n = WebGUI::International->new($session, 'ProductManager');
$parameterId = $session->form->process("parameterId");
$productId = $session->form->process("productId");
unless ($parameterId eq 'new') {
$product = WebGUI::Product->getByParameterId($session,$parameterId);
$parameter = $product->getParameter($parameterId);
$productId = $product->get('productId');
}
$f = WebGUI::HTMLForm->new($session);
$f->submit;
$f->hidden(
-name => 'op',
-value => 'editProductParameterSave',
);
$f->hidden(
-name => 'parameterId',
-value => $parameterId,
);
$f->hidden(
-name => 'productId',
-value => $productId,
);
$f->readOnly(
-label => $i18n->get('parameter ID'),
-value => $parameterId,
);
$f->text(
-name => 'name',
-label => $i18n->get('edit parameter name'),
-hoverHelp => $i18n->get('edit parameter name description'),
-value => $session->form->process("name") || $parameter->{name},
-maxlength => 64,
);
$f->submit;
return _submenu($session,$f->print, 'edit parameter');
}
#-------------------------------------------------------------------
=head2 www_editProductParameterSave ( $session )
Saves the properties of a product parameter.
=head3 $session
The current WebGUI session object.
=cut
sub www_editProductParameterSave {
my $session = shift;
my (@error, $parameterId, $product, $i18n, $skuTemplate, $oldName, $newName);
return $session->privilege->insufficient unless canView($session);
$i18n = WebGUI::International->new($session, 'ProductManager');
$parameterId = $session->form->process("parameterId");
push (@error, $i18n->get('edit parameter name error')) unless $session->form->process("name");
push (@error, $i18n->get('edit parameter productId error')) unless $session->form->process("productId");
return "<ul><li>".join('</li><li>', @error)."</li></ul>".WebGUI::Operation::execute($session,'editProductParameter') if (@error);
$product = WebGUI::Product->new($session,$session->form->process("productId"));
$skuTemplate = $product->get('skuTemplate');
if ($parameterId eq 'new') {
$parameterId = $product->addParameter;
} else {
($oldName = $product->getParameter($parameterId)->{name}) =~ s/[ ><]/\./g;
($newName = $session->form->process("name")) =~ s/[ ><]/\./g;
$skuTemplate = $product->get('skuTemplate');
$skuTemplate =~ s/< *?tmpl_var *?param\.$oldName *?>/<tmpl_var param.$newName>/i;
$product->set({
skuTemplate => $skuTemplate
});
}
$product->setParameter($parameterId, {
name => $session->form->process("name")
});
return WebGUI::Operation::execute($session,'editSkuTemplate') if ($session->form->process("parameterId") eq 'new');
return WebGUI::Operation::execute($session,'manageProduct');
}
#-------------------------------------------------------------------
=head2 www_editProductParameterOption ( $session )
Edits the options of a product parameter.
=head3 $session
The current WebGUI session object.
=cut
sub www_editProductParameterOption {
my $session = shift;
my ($self, $optionId, $option, $f, $i18n);
return $session->privilege->insufficient unless canView($session);
$i18n = WebGUI::International->new($session, 'ProductManager');
$optionId = $session->form->process("optionId");
unless ($optionId eq 'new') {
$option = WebGUI::Product->getByOptionId($session,$optionId)->getOption($optionId);
}
$f = WebGUI::HTMLForm->new($session);
$f->submit;
$f->hidden(
-name => 'op',
-value => 'editProductParameterOptionSave',
);
$f->hidden(
-name => 'optionId',
-value => $optionId,
);
$f->hidden(
-name => 'parameterId',
-value => $session->form->process("parameterId"),
);
$f->readOnly(
-label => $i18n->get('option ID'),
-value => $optionId
);
$f->text(
-name => 'value',
-label => $i18n->get('edit option value'),
-hoverHelp => $i18n->get('edit option value description'),
-value => $session->form->process("value") || $option->{value},
-maxlength => 64,
);
$f->float(
-name => 'priceModifier',
-label => $i18n->get('edit option price modifier'),
-hoverHelp => $i18n->get('edit option price modifier description'),
-value => $session->form->process("priceModifier") || $option->{priceModifier},
-maxlength => 11,
);
$f->float(
-name => 'weightModifier',
-label => $i18n->get('edit option weight modifier'),
-hoverHelp => $i18n->get('edit option weight modifier description'),
-value => $session->form->process("weightModifier") || $option->{weightModifier},
-maxlength => 7,
);
$f->text(
-name => 'skuModifier',
-label => $i18n->get('edit option sku modifier'),
-hoverHelp => $i18n->get('edit option sku modifier description'),
-value => $session->form->process("skuModifier") || $option->{skuModifier},
-maxlength => 64,
);
$f->submit;
return _submenu($session,$f->print, 'edit option');
}
#-------------------------------------------------------------------
=head2 www_editProductParameterOptionSave ( $session )
Saves the properties of a Product Parameter Option
=head3 $session
The current WebGUI session object.
=cut
sub www_editProductParameterOptionSave {
my $session = shift;
my ($self, @error, $optionId, $product, $i18n);
return $session->privilege->insufficient unless canView($session);
$i18n = WebGUI::International->new($session, 'ProductManager');
push (@error, $i18n->get('edit option value error')) unless ($session->form->process("value"));
push (@error, $i18n->get('edit option parameterId error')) unless ($session->form->process("parameterId"));
return '<ul><li>'.join('</li><li>', @error).'</li></ul><br />'.WebGUI::Operation::execute($session,'editProduct') if (@error);
$product = WebGUI::Product->getByParameterId($session,$session->form->process("parameterId"));
$optionId = $session->form->process("optionId");
$optionId = $product->addOptionToParameter($session->form->process("parameterId")) if ($optionId eq 'new');
$product->setOption($optionId, {
value => $session->form->process("value"),
priceModifier => $session->form->process("priceModifier"),
weightModifier => $session->form->process("weightModifier"),
skuModifier => $session->form->process("skuModifier")
});
return WebGUI::Operation::execute($session,'manageProduct');
}
#-------------------------------------------------------------------
=head2 www_editProductVariant ( $session )
Returns a form to edit a Product Variant.
=head3 $session
The current WebGUI session object.
=cut
sub www_editProductVariant {
my $session = shift;
my ($variantId, $variant, $f, $i18n);
return $session->privilege->insufficient unless canView($session);
$i18n = WebGUI::International->new($session, "ProductManager");
$variantId = $session->form->process("variantId");
$variant = WebGUI::Product->getByVariantId($session,$variantId)->getVariant($variantId);
$f = WebGUI::HTMLForm->new($session);
$f->submit;
$f->hidden(
-name => 'op',
-value => 'editProductVariantSave'
);
$f->hidden(
-name => 'variantId',
-value => $variantId
);
$f->readOnly(
-label => $i18n->get('variant ID'),
-value => $variant->{variantId}
);
$f->float(
-name => 'price',
-label => $i18n->get('price override'),
-hoverHelp => $i18n->get('price override description'),
-value => $variant->{priceOverride} ? $variant->{price} : ''
);
$f->float(
-name => 'weight',
-label => $i18n->get('weight override'),
-hoverHelp => $i18n->get('weight override description'),
-value => $variant->{weightOverride} ? $variant->{weight} : ''
);
$f->text(
-name => 'sku',
-label => $i18n->get('sku override'),
-hoverHelp => $i18n->get('sku override description'),
-value => $variant->{skuOverride} ? $variant->{sku} : ''
);
$f->yesNo(
-name => 'available',
-label => $i18n->get('available'),
-hoverHelp => $i18n->get('available description'),
-value => $variant->{available}
);
$f->submit;
return _submenu($session,$f->print, 'edit variant');
}
#-------------------------------------------------------------------
=head2 www_editProductVariantSave ( $session )
Saves the properties of a Product Variant.
=head3 $session
The current WebGUI session object.
=cut
sub www_editProductVariantSave {
my $session = shift;
my $variantId = $session->form->process("variantId");
return $session->privilege->insufficient unless canView($session);
WebGUI::Product->getByVariantId($session,$variantId)->setVariant($variantId, $session->form->paramsHashRef);
return WebGUI::Operation::execute($session,'listProductVariants');
}
#-------------------------------------------------------------------
=head2 www_editSkuTemplate ( $session )
Returns a form to edit a Sku Template.
=head3 $session
The current WebGUI session object.
=cut
sub www_editSkuTemplate {
my $session = shift;
my ($product, $productId, $output, $f, $name, $i18n);
return $session->privilege->insufficient unless canView($session);
$i18n = WebGUI::International->new($session, "ProductManager");
$productId = $session->form->process("productId");
$product = WebGUI::Product->new($session,$productId);
$output .= "Available are: <br />\n";
$output .= "<ul><li>base</li>\n";
foreach (@{$product->getParameter}) {
($name = $_->{name}) =~ s/[ ><]/\./g;
$output .= "<li>param.".$name."</li>\n";
}
$output .= "</ul><br />";
$f = WebGUI::HTMLForm->new($session);
$f->submit;
$f->hidden(
-name => 'op',
-value => 'editSkuTemplateSave'
);
$f->hidden(
-name => 'productId',
-value => $productId
);
$f->text(
-name => 'skuTemplate',
-value => $product->get('skuTemplate'),
-label => $i18n->get('sku template'),
);
$f->submit;
$output .= $f->print;
return _submenu($session,$output, 'edit sku composition label');
}
#-------------------------------------------------------------------
=head2 www_editSkuTemplateSave ( $session )
Saves the properties of a Sku Template.
=head3 $session
The current WebGUI session object.
=cut
sub www_editSkuTemplateSave {
my $session = shift;
my ($productId) = $session->form->process("productId");
return $session->privilege->insufficient unless canView($session);
WebGUI::Product->new($session,$productId)->set({
skuTemplate => $session->form->process("skuTemplate"),
});
return WebGUI::Operation::execute($session,'manageProduct');
}
#-------------------------------------------------------------------
=head2 www_listProducts ( $session )
Returns a list of products with manage and delete buttons.
=head3 $session
The current WebGUI session object.
=cut
sub www_listProducts {
my $session = shift;
my ($self, $sth, $output, $row, $i18n);
return $session->privilege->insufficient unless canView($session);
$i18n = WebGUI::International->new($session, 'ProductManager');
$session->scratch->delete('managingProduct');
$sth = $session->db->read('select * from products order by title');
$output .= '<table>';
while ($row = $sth->hashRef) {
$output .= '<tr>';
$output .= '<td>';
$output .= $session->icon->delete('op=deleteProduct;productId='.$row->{productId}, undef, $i18n->get("confirm delete product"));
$output .= $session->icon->edit('op=manageProduct;productId='.$row->{productId});
$output .= '</td>';
$output .= '<td>'.$row->{title}.'</td>';
$output .= '</tr>';
}
$output .= '</table>';
return _submenu($session,$output, 'list products');
}
#-------------------------------------------------------------------
=head2 www_listProductVariants ( $session )
Returns a list of Product Variants.
=head3 $session
The current WebGUI session object.
=cut
sub www_listProductVariants {
my $session = shift;
my ($productId, $product, @variants, %parameters, %options, $output, %composition, $i18n);
return $session->privilege->insufficient unless canView($session);
$i18n = WebGUI::International->new($session, "ProductManager");
$productId = $session->form->process("productId") || $session->scratch->get('managingProduct');
return WebGUI::Operation::execute($session,'listProducts') if ($productId eq 'new' || !$productId);
$product = WebGUI::Product->new($session,$productId);
@variants = sort {$a->{composition} cmp $b->{composition}} @{$product->getVariant};
tie %parameters, "Tie::IxHash";
%parameters = map {$_->{parameterId} => $_->{name}} sort {$a->{name} <=> $b->{name}} @{$product->getParameter};
%options = map {$_->{optionId} => $_->{value}} @{$product->getOption};
$output = WebGUI::Form::formHeader($session);
$output .= WebGUI::Form::hidden($session,{
name => 'op',
value => 'listProductVariantsSave',
});
$output .= WebGUI::Form::hidden($session,{
name => 'productId',
value => $productId,
});
$output .= '<table><tr align="left">';
$output .= "<th>".join('</th><th>', values(%parameters))."</th>" if (%parameters);
$output .= '<th colspan="2">'.$i18n->get('sku').'</th>'.
'<th colspan="2">'.$i18n->get('price').'</th>'.
'<th colspan="2">'.$i18n->get('weight').'</th>'.
'<th>'.$i18n->get('available').'</th>';
$output .= "</tr>";
foreach (@variants) {
$output .= "<tr>";
%composition = map {split(/\./, $_)} split(/,/, $_->{composition});
foreach (keys(%parameters)) {
$output .= '<td align="left">'.$options{$composition{$_}}.'</td>';
}
$output .= '<td align="left">'.$_->{sku}."</td><td>";
$output .= '*' if ($_->{skuOverride});
$output .= '</td><td align="right">'.$_->{price}."</td><td>";
$output .= '*'if ($_->{priceOverride});
$output .= '</td><td align="right">'.$_->{weight}."</td><td>";
$output .= '*' if ($_->{weightOverride});
$output .= "</td>";
$output .= "<td>".WebGUI::Form::checkbox($session,{
name => 'available',
value => $_->{variantId},
checked => $_->{available},
}).$session->icon->edit('op=editProductVariant;variantId='.$_->{variantId})."</td>";
$output .= "</tr>";
}
$output .= "</table>";
$output .= WebGUI::Form::submit($session,);
$output .= WebGUI::Form::formFooter($session,);
return _submenu($session,$output, 'list variants label');
}
#-------------------------------------------------------------------
=head2 www_listProductVariantsSave ( $session )
Saves the properties of some product variants.
=head3 $session
The current WebGUI session object.
=cut
sub www_listProductVariantsSave {
my $session = shift;
return $session->privilege->insufficient unless canView($session);
my %availableVariants = map {$_ => 1} $session->form->selectList('available');
my $product = WebGUI::Product->new($session,$session->form->process("productId"));
my @variants = @{$product->getVariant};
foreach (@variants) {
$product->setVariant($_->{variantId}, {
available => $availableVariants{$_->{variantId}} ? '1' : '0'});
}
return WebGUI::Operation::execute($session,'listProductVariants');
}
#-------------------------------------------------------------------
=head2 www_manageProduct ( $session )
Returns a screen that displays lots of options for editing all aspects of a product.
=head3 $session
The current WebGUI session object.
=cut
sub www_manageProduct {
my $session = shift;
my ($product, $output, $option, $i18n);
return $session->privilege->insufficient unless canView($session);
$i18n = WebGUI::International->new($session, "ProductManager");
my $productId = shift || $session->form->process("productId") || $session->scratch->get('managingProduct');
return WebGUI::Operation::execute($session,'listProducts') if ($productId eq 'new' || !$productId);
$session->scratch->set('managingProduct', $productId);
$product = WebGUI::Product->new($session,$productId);
$output .= "<h1>".$product->get('title')."</h1>";
$output .= "<h2>".$i18n->get('properties').$session->icon->edit('op=editProduct;productId='.$productId)."</h2>";
$output .= "<table>";
$output .= "<tr><td>".$i18n->get('productId')."</td><td>".$productId."</td></tr>";
$output .= "<tr><td>".$i18n->get('price')."</td><td>".$product->get('price')."</td></tr>";
my $useSalesTax = $product->get('useSalesTax')
? $i18n->get(138, 'WebGUI')
: $i18n->get(139, 'WebGUI');
$output .= "<tr><td>".$i18n->get('useSalesTax')."</td><td>".$useSalesTax."</td></tr>";
$output .= "<tr><td>".$i18n->get('weight')."</td><td>".$product->get('weight')."</td></tr>";
$output .= "<tr><td>".$i18n->get('sku')."</td><td>".$product->get('sku')."</td></tr>";
$output .= "<tr><td>".$i18n->get('description')."</td><td>".$product->get('description')."</td></tr>";
$output .= "<tr><td>".$i18n->get('sku template')."</td><td>".WebGUI::HTML::format($product->get('skuTemplate'), 'text')."</td></tr>";
$output .= "</table>";
$output .= "<h2>Parameters</h2>";
$output .= '<a href="'.$session->url->page('op=editProductParameter;parameterId=new;productId='.$product->get('productId')).'">'.
$i18n->get('add parameter').'</a><br />';
foreach my $parameter (@{$product->getParameter}) {
$output .= $session->icon->delete('op=deleteProductParameter;parameterId='.$parameter->{parameterId}).
$session->icon->edit('op=editProductParameter;parameterId='.$parameter->{parameterId});
$output .= '<span style="margin-left: 10px"><b>'.$parameter->{name}.'</b></span><br />';
$output .= '<a style="margin-left: 20px" href="'.
$session->url->page('op=editProductParameterOption;optionId=new;parameterId='.$parameter->{parameterId}).'">'.
$i18n->get('add option').'</a><br />';
foreach my $optionId (@{$parameter->{options}}) {
$option = $product->getOption($optionId);
$output .= '<span style="margin-left: 20px">'.
$session->icon->delete('op=deleteProductParameterOption;optionId='.$option->{optionId}).
$session->icon->edit('op=editProductParameterOption;parameterId='.$parameter->{parameterId}.';optionId='.$option->{optionId}).$option->{value}.'</span><br />';
}
$output .= '<br />';
}
return _submenu($session,$output, 'manage product');
}
1;

View file

@ -1,669 +0,0 @@
package WebGUI::Operation::Subscription;
use strict;
use WebGUI::SQL;
use WebGUI::HTMLForm;
use Tie::IxHash;
use WebGUI::Paginator;
use WebGUI::Subscription;
use WebGUI::Commerce::ShoppingCart;
use WebGUI::AdminConsole;
use WebGUI::Asset::Template;
use WebGUI::Form;
use WebGUI::International;
=head1 NAME
Package WebGUI::Operation::Subscription
=head1 DESCRIPTION
Operational handler for viewing, editing, listing, purchasing/redeeming, and deleting Subscriptions and Subscription Code Batches.
=head2 _generateCode ( $session, codeLength )
Generates a human-readable subscription code, meant to be typed/pasted in somewhere.
=head3 $session
The current WebGUI session object.
=head3 codeLength
The whole number amount of characters you want returned.
=cut
#-------------------------------------------------------------------
sub _generateCode {
my $session = shift;
my ($codeLength, @codeElements, $code, $i);
$codeLength = shift || 64;
@codeElements = ('A'..'Z', 'a'..'z', 0..9, '-');
for ($i=0; $i < $codeLength; $i++) {
$code .= $codeElements[rand(63)];
}
return $code;
}
=head2 _submenu ( $session )
Returns a rendered Admin Console view, with a standard list of five submenu items.
=head3 $session
The current WebGUI session object.
=head3 workarea
A scalar of HTML that defines the current workarea.
=head3 title
The i18n key of the title of this workarea.
=cut
#-------------------------------------------------------------------
sub _submenu {
my $session = shift;
my $i18n = WebGUI::International->new($session, "Subscription");
my $workarea = shift;
my $title = shift;
$title = $i18n->get($title) if ($title);
my $ac = WebGUI::AdminConsole->new($session,"subscriptions");
$ac->addSubmenuItem($session->url->page('op=editSubscription;sid=new'), $i18n->get('add subscription'));
$ac->addSubmenuItem($session->url->page('op=createSubscriptionCodeBatch'), $i18n->get('generate batch'));
$ac->addSubmenuItem($session->url->page('op=listSubscriptionCodes'), $i18n->get('manage codes'));
$ac->addSubmenuItem($session->url->page('op=listSubscriptionCodeBatches'), $i18n->get('manage batches'));
$ac->addSubmenuItem($session->url->page('op=listSubscriptions'), 'Manage Subscriptions');
return $ac->render($workarea, $title);
}
#----------------------------------------------------------------------------
=head2 canView ( session [, user] )
Returns true if the user can administrate this operation. user defaults to
the current user.
=cut
sub canView {
my $session = shift;
my $user = shift || $session->user;
return $user->isInGroup( $session->setting->get("groupIdAdminSubscription") );
}
#----------------------------------------------------------------------------
=head2 www_createSubscriptionCodeBatch ( $session, error )
Form to accept parameters to create a batch of subscription codes.
=head3 $session
The current WebGUI session object.
=head3 error
An HTML scalar of an error message to be returned to the user.
=cut
sub www_createSubscriptionCodeBatch {
my $session = shift;
my (%subscriptions, $f, $error, $errorMessage);
return $session->privilege->adminOnly() unless canView($session);
$error = shift;
my $i18n = WebGUI::International->new($session, "Subscription");
$errorMessage = $i18n->get('create batch error').'<ul><li>'.join('</li><li>', @{$error}).'</li></ul>' if ($error);
tie %subscriptions, "Tie::IxHash";
%subscriptions = $session->db->buildHash("select subscriptionId, name from subscription where deleted != 1 order by name");
$f = WebGUI::HTMLForm->new($session);
$f->submit;
$f->hidden(
-name => 'op',
-value => 'createSubscriptionCodeBatchSave'
);
$f->integer(
-name => 'noc',
-label => $i18n->get('noc'),
-hoverHelp => $i18n->get('noc description'),
-value => $session->form->process("noc") || 1
);
$f->integer(
-name => 'codeLength',
-label => $i18n->get('code length'),
-hoverHelp => $i18n->get('code length description'),
-value => $session->form->process("codeLength") || 64
);
$f->interval(
-name => 'expires',
-label => $i18n->get('codes expire'),
-hoverHelp => $i18n->get('codes expire description'),
-value => $session->form->process("expires") || $session->datetime->intervalToSeconds(1, 'months')
);
my @sub = $session->form->selectList("subscriptionId");
$f->selectList(
-name => 'subscriptionId',
-label => $i18n->get('association'),
-hoverHelp => $i18n->get('association description'),
-options=> \%subscriptions,
-multiple=>1,
-size => 5,
-value => \@sub
);
$f->textarea(
-name => 'description',
-label => $i18n->get('batch description'),
-hoverHelp => $i18n->get('batch description description'),
-value => $session->form->process("description")
);
$f->submit;
return _submenu($session,$errorMessage.$f->print, 'create batch menu');
}
=head2 www_createSubscriptionCodeBatchSave ( $session )
Method that accepts the form parameters to create a batch of subscription codes.
=head3 $session
The current WebGUI session object.
=cut
#-------------------------------------------------------------------
sub www_createSubscriptionCodeBatchSave {
my $session = shift;
my ($numberOfCodes, $description, $expires, $batchId, @codeElements, $currentCode, $code, $i, @subscriptions,
@error, $creationEpoch);
return $session->privilege->adminOnly() unless canView($session);
my $i18n = WebGUI::International->new($session, "Subscription");
$numberOfCodes = $session->form->process("noc");
$description = $session->form->process("description");
$expires = $session->form->interval('expires');
$batchId = $session->id->generate;
push(@error, $i18n->get('no description error')) unless ($description);
push(@error, $i18n->get('no association error')) unless ($session->form->process("subscriptionId"));
push(@error, $i18n->get('code length error')) unless ($session->form->process("codeLength") >= 10 && $session->form->process("codeLength") <= 64 && $session->form->process("codeLength") =~ m/^\d\d$/);
return www_createSubscriptionCodeBatch($session,\@error) if (@error);
$creationEpoch =$session->datetime->time();
$session->db->write("insert into subscriptionCodeBatch (batchId, description) values (".
$session->db->quote($batchId).", ".$session->db->quote($description).")");
for ($currentCode=0; $currentCode < $numberOfCodes; $currentCode++) {
$code = _generateCode($session,$session->form->process("codeLength"));
$code = _generateCode($session,$session->form->process("codeLength")) while ($session->db->quickArray("select code from subscriptionCode where code=".$session->db->quote($code)));
$session->db->write("insert into subscriptionCode (batchId, code, status, dateCreated, dateUsed, expires, usedBy)".
" values (".$session->db->quote($batchId).",".$session->db->quote($code).", 'Unused', ".$session->db->quote($creationEpoch).", 0, ".$session->db->quote($expires).", 0)");
@subscriptions = $session->form->selectList('subscriptionId');
foreach (@subscriptions) {
$session->db->write("insert into subscriptionCodeSubscriptions (code, subscriptionId) values (".
$session->db->quote($code).", ".$session->db->quote($_).")");
}
}
return www_listSubscriptionCodeBatches($session);
}
=head2 www_deleteSubscription ( $session )
Method that deletes the subscription passed by the user through the form variable 'sid'.
=head3 $session
The current WebGUI session object.
=cut
#-------------------------------------------------------------------
sub www_deleteSubscription {
my $session = shift;
return $session->privilege->adminOnly() unless canView($session);
WebGUI::Subscription->new($session,$session->form->process("sid"))->delete;
return www_listSubscriptions($session);
}
=head2 www_deleteSubscriptionCodeBatch ( $session )
Method that deletes the subscription code batch passed by the user through the form variable 'bid'.
=head3 $session
The current WebGUI session object.
=cut
#-------------------------------------------------------------------
sub www_deleteSubscriptionCodeBatch {
my $session = shift;
return $session->privilege->adminOnly() unless canView($session);
$session->db->write("delete from subscriptionCodeBatch where batchId=".$session->db->quote($session->form->process("bid")));
$session->db->write("delete from subscriptionCode where batchId=".$session->db->quote($session->form->process("bid")));
return www_listSubscriptionCodeBatches($session);
}
=head2 www_deleteSubscriptionCodes ( $session )
Method that deletes some subscription codes.
=head3 $session
The current WebGUI session object.
=cut
#-------------------------------------------------------------------
sub www_deleteSubscriptionCodes {
my $session = shift;
return $session->privilege->adminOnly() unless canView($session);
if ($session->form->process("selection") eq 'dc') {
$session->db->write("delete from subscriptionCode where dateCreated >= ".$session->db->quote($session->form->process("dcStart")).
' and dateCreated <= '.$session->db->quote($session->form->process("dcStop")));
} elsif ($session->form->process("selection") eq 'du') {
$session->db->write("delete from subscriptionCode where dateUsed >= ".$session->db->quote($session->form->process("duStart")).
' and dateUsed <= '.$session->db->quote($session->form->process("duStop")));
}
return www_listSubscriptionCodes($session);
}
=head2 www_editSubscription ( $session )
Returns a form so the user can edit the properties of a subscription. Uses the form variable 'sid'.
=head3 $session
The current WebGUI session object.
=cut
#-------------------------------------------------------------------
sub www_editSubscription {
my $session = shift;
my ($properties, $subscriptionId, $durationInterval, $durationUnits, $f);
return $session->privilege->adminOnly() unless canView($session);
my $i18n = WebGUI::International->new($session, "Subscription");
unless ($session->form->process("sid") eq 'new') {
$properties = WebGUI::Subscription->new($session,$session->form->process("sid"))->get;
}
$subscriptionId = $session->form->process("sid") || 'new';
$f = WebGUI::HTMLForm->new($session);
$f->submit;
$f->hidden(
-name => 'op',
-value => 'editSubscriptionSave'
);
$f->hidden(
-name => 'sid',
-value => $subscriptionId
);
$f->readOnly(
-label => $i18n->get('subscriptionId'),
-value => $subscriptionId
);
$f->text(
-name => 'name',
-label => $i18n->get('subscription name'),
-hoverHelp => $i18n->get('subscription name description'),
-value => $properties->{name}
);
$f->float(
-name => 'price',
-label => $i18n->get('subscription price'),
-hoverHelp => $i18n->get('subscription price description'),
-value => $properties->{price} || '0.00'
);
$f->yesNo(
-name => 'useSalesTax',
-label => $i18n->get('useSalesTax'),
-hoverHelp => $i18n->get('useSalesTax description'),
-value => $properties->{useSalesTax} || 0,
);
$f->textarea(
-name => 'description',
-label => $i18n->get('subscription description'),
-hoverHelp => $i18n->get('subscription description description'),
-value => $properties->{description}
);
$f->group(
-name => 'subscriptionGroup',
-label => $i18n->get('subscription group'),
-hoverHelp => $i18n->get('subscription group description'),
-value => [$properties->{subscriptionGroup} || 2]
);
$f->selectBox(
-name => 'duration',
-label => $i18n->get('subscription duration'),
-hoverHelp => $i18n->get('subscription duration description'),
-value => $properties->{duration} || 'Monthly',
-options=> WebGUI::Commerce::Payment->recurringPeriodValues($session),
);
$f->text(
-name => 'executeOnSubscription',
-label => $i18n->get('execute on subscription'),
-hoverHelp => $i18n->get('execute on subscription description'),
-value => $properties->{executeOnSubscription}
);
if ($session->setting->get("useKarma")) {
$f->integer(
-name => 'karma',
-label => $i18n->get('subscription karma'),
-hoverHelp => $i18n->get('subscription karma description'),
-value => $properties->{karma} || 0
);
}
$f->submit;
return _submenu($session,$f->print, 'edit subscription title');
}
=head2 www_editSubscriptionSave ( $session )
Saves the properties of a subscription.
=head3 $session
The current WebGUI session object.
=cut
#-------------------------------------------------------------------
sub www_editSubscriptionSave {
my $session = shift;
my (@relevantFields);
return $session->privilege->adminOnly() unless canView($session);
my $properties = {};
@relevantFields = qw(subscriptionId name useSalesTax price description subscriptionGroup duration executeOnSubscription karma);
foreach (@relevantFields) {
$properties->{$_} = $session->form->process($_) if (defined $session->form->process($_));
}
WebGUI::Subscription->new($session,$session->form->process("sid"))->set($properties);
return www_listSubscriptions($session);
}
=head2 www_listSubscriptionCodeBatches ( $session )
Returns a paginated list of batches of subscription codes, along with various links for each one.
=head3 $session
The current WebGUI session object.
=cut
#-------------------------------------------------------------------
sub www_listSubscriptionCodeBatches {
my $session = shift;
my ($p, $batches, $output);
return $session->privilege->adminOnly() unless canView($session);
my $i18n = WebGUI::International->new($session, "Subscription");
$p = WebGUI::Paginator->new($session,$session->url->page('op=listSubscriptionCodeBatches'));
$p->setDataByQuery("select * from subscriptionCodeBatch");
$batches = $p->getPageData;
$output = $p->getBarTraditional($session->form->process("pn"));
$output .= '<table border="1" cellpadding="5" cellspacing="0" align="center">';
foreach (@{$batches}) {
$output .= '<tr><td>';
$output .= $session->icon->delete('op=deleteSubscriptionCodeBatch;bid='.$_->{batchId}, undef, $i18n->get('delete batch confirm'));
$output .= '<td>'.$_->{description}.'</td>';
$output .= '<td><a href="'.$session->url->page('op=listSubscriptionCodes;selection=b;bid='.$_->{batchId}).'">'.$i18n->get('list codes in batch').'</a></td>';
$output .= '</tr>';
}
$output .= '</table>';
$output .= $p->getBarTraditional($session->form->process("pn"));
$output = $i18n->get('no subscription code batches') unless (@{$batches});
return _submenu($session,$output, 'manage batches');
}
=head2 www_listSubscriptionCodes ( $session )
Non-templated. Returns a paginated list of subscription codes.
=head3 $session
The current WebGUI session object.
=cut
#-------------------------------------------------------------------
sub www_listSubscriptionCodes {
my $session = shift;
my ($p, $codes, $output, $where, $ops, $delete);
return $session->privilege->adminOnly() unless canView($session);
my $i18n = WebGUI::International->new($session, "Subscription");
my $dcStart = $session->form->date('dcStart');
my $dcStop = $session->datetime->addToTime($session->form->date('dcStop'),23,59);
my $duStart = $session->form->date('duStart');
my $duStop = $session->datetime->addToTime($session->form->date('duStop'),23,59);
my $batches = $session->db->buildHashRef("select batchId, description from subscriptionCodeBatch");
$output .= $i18n->get('selection message');
$output .= WebGUI::Form::formHeader($session);
$output .= WebGUI::Form::hidden($session,{name=>'op', value=>'listSubscriptionCodes'});
$output .= '<table>';
$output .= '<td>'.WebGUI::Form::radio($session,{name=>'selection', value => 'du', checked=>($session->form->process("selection") eq 'du')}).'</td>';
$output .= '<td align="left">'.$i18n->get('selection used').'</td>';
$output .= '<td>'.WebGUI::Form::date($session,{name=>'duStart', value=>$duStart}).' '.$i18n->get('and').' '.WebGUI::Form::date($session,{name=>'duStop', value=>$duStop}).'</td>';
$output .= '</tr><tr>';
$output .= '<td>'.WebGUI::Form::radio($session,{name=>'selection', value => 'dc', checked=>($session->form->process("selection") eq 'dc')}).'</td>';
$output .= '<td align="left">'.$i18n->get('selection created').'</td>';
$output .= '<td>'.WebGUI::Form::date($session,{name=>'dcStart', value=>$dcStart}).' '.$i18n->get('and').' '.WebGUI::Form::date($session,{name=>'dcStop', value=>$dcStop}).'</td>';
$output .= '</tr><tr>';
$output .= '<td>'.WebGUI::Form::radio($session,{name=>'selection', value => 'b', checked=>($session->form->process("selection") eq 'b')}).'</td>';
$output .= '<td align="left">'.$i18n->get('selection batch id').'</td>';
$output .= '<td>'.WebGUI::Form::selectList($session,{name => 'bid', value => [$session->form->process("bid")], options => $batches});
$output .= '</tr><tr>';
$output .= '<td></td>';
$output .= '<td>'.WebGUI::Form::submit($session,{value=>$i18n->get('select')}).'</td>';
$output .= '</tr>';
$output .= '</table>';
$output .= WebGUI::Form::formFooter;
if ($session->form->process("selection") eq 'du') {
$where = " and dateUsed >= ".$session->db->quote($duStart)." and dateUsed <= ".$session->db->quote($duStop);
$ops = ';duStart='.$duStart.';duStop='.$duStop.';selection=du';
$delete = '<a href="'.$session->url->page('op=deleteSubscriptionCodes'.$ops).'">'.$i18n->get('delete codes').'</a>';
} elsif ($session->form->process("selection") eq 'dc') {
$where = " and dateCreated >= ".$session->db->quote($dcStart)." and dateCreated <= ".$session->db->quote($dcStop);
$ops = ';dcStart='.$dcStart.';dcStop='.$dcStop.';selection=dc';
$delete = '<a href="'.$session->url->page('op=deleteSubscriptionCodes'.$ops).'">'.$i18n->get('delete codes').'</a>';
} elsif ($session->form->process("selection") eq 'b') {
$where = " and t1.batchId=".$session->db->quote($session->form->process("bid"));
$ops = ';bid='.$session->form->process("bid").';selection=b';
$delete = '<a href="'.$session->url->page('op=deleteSubscriptionCodeBatch'.$ops).'">'.$i18n->get('delete codes').'</a>';
} else {
return _submenu($session,$output, 'listSubscriptionCodes title');
}
$p = WebGUI::Paginator->new($session,$session->url->page('op=listSubscriptionCodes'.$ops));
$p->setDataByQuery("select t1.*, t2.* from subscriptionCode as t1, subscriptionCodeBatch as t2 where t1.batchId=t2.batchId ".$where);
$codes = $p->getPageData;
$output .= '<br />'.$delete.'<br />' if ($delete);
$output .= $p->getBarTraditional($session->form->process("pn"));
$output .= '<br />';
$output .= '<table border="1" cellpadding="5" cellspacing="0" align="center">';
$output .= '<tr>';
$output .= '<th>'.$i18n->get('batch id').'</th><th>'.$i18n->get('code').'</th><th>'.$i18n->get('creation date').
'</th><th>'.$i18n->get('dateUsed').'</th><th>'.$i18n->get('status').'</th>'; $output .= '</tr>';
foreach (@{$codes}) {
$output .= '<tr>';
$output .= '<td>'.$_->{batchId}.'</td>';
$output .= '<td>'.$_->{code}.'</td>';
$output .= '<td>'.$session->datetime->epochToHuman($_->{dateCreated}).'</td>';
$output .= '<td>';
$output .= $session->datetime->epochToHuman($_->{dateUsed}) if ($_->{dateUsed});
$output .= '</td>';
$output .= '<td>'.$_->{status}.'</td>';
$output .= '</tr>';
}
$output .= '</table>';
$output .= $p->getBarTraditional($session->form->process("pn"));
return _submenu($session,$output, 'listSubscriptionCodes title');
}
=head2 www_listSubscriptions ( $session )
Returns a paginated list of subscriptions along with edit and delete links.
=head3 $session
The current WebGUI session object.
=cut
#-------------------------------------------------------------------
sub www_listSubscriptions {
my $session = shift;
my ($p, $subscriptions, $output);
return $session->privilege->adminOnly() unless canView($session);
my $i18n = WebGUI::International->new($session, "Subscription");
$p = WebGUI::Paginator->new($session,$session->url->page('op=listSubscriptions'));
$p->setDataByQuery('select subscriptionId, name from subscription where deleted != 1');
$subscriptions = $p->getPageData;
$output = $p->getBarTraditional($session->form->process("pn"));
$output .= '<table border="1" cellpadding="5" cellspacing="0" align="center">';
foreach (@{$subscriptions}) {
$output .= '<tr>';
$output .= '<td>'.$session->icon->edit('op=editSubscription;sid='.$_->{subscriptionId});
$output .= $session->icon->delete('op=deleteSubscription;sid='.$_->{subscriptionId}, undef, $i18n->get('delete subscription confirm')).'</td>';
$output .= '<td>'.$_->{name}.'</td>';
$output .= '</tr>';
}
$output .= '</table>';
$output .= $p->getBarTraditional($session->form->process("pn"));
$output = $i18n->get('no subscriptions') unless (@{$subscriptions});
return _submenu($session,$output, 'manage subscriptions');
}
=head2 www_purchaseSubscription ( $session )
Adds subscription 'sid' to the user's shopping cart and returns the checkout screen.
=head3 $session
The current WebGUI session object.
=cut
#-------------------------------------------------------------------
sub www_purchaseSubscription {
my $session = shift;
WebGUI::Commerce::ShoppingCart->new($session)->add($session->form->process("sid"), 'Subscription');
$session->http->setRedirect($session->url->page('op=checkout'));
return undef;
}
=head2 www_redeemSubscriptionCode ( $session )
Returns a form so the user can redeem a subscription code, or actually redeems the subscription code.
=head3 $session
The current WebGUI session object.
=cut
#-------------------------------------------------------------------
sub www_redeemSubscriptionCode {
my $session = shift;
my (%codeProperties, @subscriptions, %var, $f);
my $i18n = WebGUI::International->new($session, "Subscription");
if ($session->form->process("code")) {
%codeProperties = $session->db->quickHash("select * from subscriptionCode as t1, subscriptionCodeBatch as t2 where ".
"t1.batchId = t2.batchId and t1.code=".$session->db->quote($session->form->process("code"))." and (t1.dateCreated + t1.expires) > ".$session->db->quote(time));
if ($codeProperties{status} eq 'Unused') {
# Code is ok
@subscriptions = $session->db->buildArray("select subscriptionId from subscriptionCodeSubscriptions where code=".$session->db->quote($session->form->process("code")));
foreach (@subscriptions) {
WebGUI::Subscription->new($session,$_)->apply;
}
# Set code to Used
$session->db->write("update subscriptionCode set status='Used', dateUsed=".$session->db->quote(time)." where code=".$session->db->quote($session->form->process("code")));
$var{batchDescription} = $codeProperties{description};
$var{message} = $i18n->get('redeem code success');
} else {
$var{message} = $i18n->get('redeem code failure');
}
} else {
$var{message} = $i18n->get('redeem code ask for code');
}
$f = WebGUI::HTMLForm->new($session);
$f->hidden(
-name => 'op',
-value => 'redeemSubscriptionCode'
);
$f->text(
-name => 'code',
-label => $i18n->get('code'),
-hoverHelp => $i18n->get('code description'),
-maxLength => 64,
-size => 30
);
$f->submit;
$var{codeForm} = $f->print;
return $session->style->userStyle(WebGUI::Asset::Template->new($session,"PBtmpl0000000000000053")->process(\%var));
}
1;