Update ThingyRecord for Moose.
This commit is contained in:
parent
504741061c
commit
97c233daa6
1 changed files with 85 additions and 74 deletions
|
|
@ -16,7 +16,70 @@ package WebGUI::Asset::Sku::ThingyRecord;
|
||||||
|
|
||||||
use strict;
|
use strict;
|
||||||
use Tie::IxHash;
|
use Tie::IxHash;
|
||||||
use base 'WebGUI::Asset::Sku';
|
use WebGUI::Definition::Asset;
|
||||||
|
extends 'WebGUI::Asset::Sku';
|
||||||
|
aspect assetName => ['assetName', 'Asset_ThingyRecord'];
|
||||||
|
aspect icon => 'thingyRecord.gif';
|
||||||
|
aspect tableName => 'ThingyRecord';
|
||||||
|
property templateIdView => (
|
||||||
|
tab => "display",
|
||||||
|
fieldType => "template",
|
||||||
|
namespace => "ThingyRecord/View",
|
||||||
|
label => ['templateIdView label', 'Asset_ThingyRecord'],
|
||||||
|
hoverHelp => ['templateIdView description', 'Asset_ThingyRecord'],
|
||||||
|
);
|
||||||
|
property thingId => (
|
||||||
|
tab => "properties",
|
||||||
|
fieldType => "selectBox",
|
||||||
|
options => \&_thingId_options,
|
||||||
|
label => ['thingId label', 'Asset_ThingyRecord'],
|
||||||
|
hoverHelp => ['thingId description', 'Asset_ThingyRecord'],
|
||||||
|
);
|
||||||
|
sub _thingId_options {
|
||||||
|
my $self = shift;
|
||||||
|
return $self->getThingOptions($self->session);
|
||||||
|
}
|
||||||
|
property thingFields => (
|
||||||
|
tab => "properties",
|
||||||
|
fieldType => "selectList",
|
||||||
|
options => {}, # populated by ajax call
|
||||||
|
label => ['thingFields label', 'Asset_ThingyRecord'],
|
||||||
|
hoverHelp => ['thingFields description', 'Asset_ThingyRecord'],
|
||||||
|
);
|
||||||
|
property thankYouText => (
|
||||||
|
tab => "properties",
|
||||||
|
fieldType => "HTMLArea",
|
||||||
|
builder => '_thankYouMessage_default',
|
||||||
|
lazy => 1,
|
||||||
|
label => [ "thank you message", 'Asset_Product' , 'Asset_ThingyRecord'],
|
||||||
|
hoverHelp => [ "thank you message help", 'Asset_Product' , 'Asset_ThingyRecord'],
|
||||||
|
);
|
||||||
|
sub _thankYouMessage_default {
|
||||||
|
my $session = shift->session;
|
||||||
|
my $i18n = WebGUI::International->new($session, "Asset_Product");
|
||||||
|
return $i18n->get( 'default thank you message', 'Asset_Product' ) . " ^ViewCart;";
|
||||||
|
}
|
||||||
|
property price => (
|
||||||
|
tab => "properties",
|
||||||
|
fieldType => "float",
|
||||||
|
label => [ '10', "Asset_Product" , 'Asset_ThingyRecord'], #Price
|
||||||
|
hoverHelp => [ 'price', 'Asset_Product' , 'Asset_ThingyRecord'],
|
||||||
|
);
|
||||||
|
property fieldPrice => (
|
||||||
|
tab => "properties",
|
||||||
|
fieldType => "textarea",
|
||||||
|
customDrawMethod => 'drawEditFieldPrice',
|
||||||
|
label => [ 'fieldPrice label' , 'Asset_ThingyRecord'],
|
||||||
|
hoverHelp => [ 'fieldPrice description', 'Asset_ThingyRecord'],
|
||||||
|
);
|
||||||
|
property duration => (
|
||||||
|
tab => "properties",
|
||||||
|
fieldType => "interval",
|
||||||
|
default => 60 * 60 * 24 * 7, # One week
|
||||||
|
label => ['duration label', 'Asset_ThingyRecord'],
|
||||||
|
hoverHelp => ['duration description', 'Asset_ThingyRecord'],
|
||||||
|
);
|
||||||
|
|
||||||
use WebGUI::Utility;
|
use WebGUI::Utility;
|
||||||
use HTML::Entities qw( encode_entities );
|
use HTML::Entities qw( encode_entities );
|
||||||
|
|
||||||
|
|
@ -60,60 +123,8 @@ sub definition {
|
||||||
my $definition = shift;
|
my $definition = shift;
|
||||||
my $i18n = WebGUI::International->new( $session, "Asset_ThingyRecord" );
|
my $i18n = WebGUI::International->new( $session, "Asset_ThingyRecord" );
|
||||||
tie my %properties, 'Tie::IxHash', (
|
tie my %properties, 'Tie::IxHash', (
|
||||||
templateIdView => {
|
|
||||||
tab => "display",
|
|
||||||
fieldType => "template",
|
|
||||||
namespace => "ThingyRecord/View",
|
|
||||||
label => $i18n->get('templateIdView label'),
|
|
||||||
hoverHelp => $i18n->get('templateIdView description'),
|
|
||||||
},
|
|
||||||
thingId => {
|
|
||||||
tab => "properties",
|
|
||||||
fieldType => "selectBox",
|
|
||||||
options => $class->getThingOptions($session),
|
|
||||||
label => $i18n->get('thingId label'),
|
|
||||||
hoverHelp => $i18n->get('thingId description'),
|
|
||||||
},
|
|
||||||
thingFields => {
|
|
||||||
tab => "properties",
|
|
||||||
fieldType => "selectList",
|
|
||||||
options => {}, # populated by ajax call
|
|
||||||
label => $i18n->get('thingFields label'),
|
|
||||||
hoverHelp => $i18n->get('thingFields description'),
|
|
||||||
},
|
|
||||||
thankYouText => {
|
|
||||||
tab => "properties",
|
|
||||||
fieldType => "HTMLArea",
|
|
||||||
defaultValue => $i18n->get( 'default thank you message', 'Asset_Product' ) . " ^ViewCart;",
|
|
||||||
label => $i18n->get( "thank you message", 'Asset_Product' ),
|
|
||||||
hoverHelp => $i18n->get( "thank you message help", 'Asset_Product' ),
|
|
||||||
},
|
|
||||||
price => {
|
|
||||||
tab => "properties",
|
|
||||||
fieldType => "float",
|
|
||||||
label => $i18n->get( '10', "Asset_Product" ), #Price
|
|
||||||
hoverHelp => $i18n->get( 'price', 'Asset_Product' ),
|
|
||||||
},
|
|
||||||
fieldPrice => {
|
|
||||||
tab => "properties",
|
|
||||||
fieldType => "textarea",
|
|
||||||
customDrawMethod => 'drawEditFieldPrice',
|
|
||||||
label => $i18n->get( 'fieldPrice label' ),
|
|
||||||
hoverHelp => $i18n->get( 'fieldPrice description'),
|
|
||||||
},
|
|
||||||
duration => {
|
|
||||||
tab => "properties",
|
|
||||||
fieldType => "interval",
|
|
||||||
defaultValue => 60 * 60 * 24 * 7, # One week
|
|
||||||
label => $i18n->get('duration label'),
|
|
||||||
hoverHelp => $i18n->get('duration description'),
|
|
||||||
},
|
|
||||||
);
|
);
|
||||||
push @{$definition}, {
|
push @{$definition}, {
|
||||||
assetName => $i18n->get('assetName'),
|
|
||||||
icon => 'thingyRecord.gif',
|
|
||||||
autoGenerateForms => 1,
|
|
||||||
tableName => 'ThingyRecord',
|
|
||||||
className => __PACKAGE__,
|
className => __PACKAGE__,
|
||||||
properties => \%properties,
|
properties => \%properties,
|
||||||
};
|
};
|
||||||
|
|
@ -133,19 +144,19 @@ sub appendVarsEditRecord {
|
||||||
my ( $self, $var, $recordId ) = @_;
|
my ( $self, $var, $recordId ) = @_;
|
||||||
my $session = $self->session;
|
my $session = $self->session;
|
||||||
my $thingy = $self->getThingy;
|
my $thingy = $self->getThingy;
|
||||||
my $fieldPrice = JSON->new->decode( $self->get('fieldPrice') || '{}' );
|
my $fieldPrice = JSON->new->decode( $self->fieldPrice || '{}' );
|
||||||
my $record = {};
|
my $record = {};
|
||||||
if ($recordId) {
|
if ($recordId) {
|
||||||
|
|
||||||
# Get an existing record
|
# Get an existing record
|
||||||
$record = $self->getThingRecord( $self->get('thingId'), $recordId );
|
$record = $self->getThingRecord( $self->thingId, $recordId );
|
||||||
if ( !%$record ) { # Record is hidden
|
if ( !%$record ) { # Record is hidden
|
||||||
$record = JSON->new->decode( $RECORD_CLASS->new( $session, $recordId )->get('fields') );
|
$record = JSON->new->decode( $RECORD_CLASS->new( $session, $recordId )->get('fields') );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
my $fields = $self->getThingFields( $self->get('thingId') );
|
my $fields = $self->getThingFields( $self->thingId );
|
||||||
my @allowed = split "\n", $self->get('thingFields');
|
my @allowed = split "\n", $self->thingFields;
|
||||||
for my $field ( @{$fields} ) {
|
for my $field ( @{$fields} ) {
|
||||||
next unless grep { $_ eq $field->{fieldId} } @allowed;
|
next unless grep { $_ eq $field->{fieldId} } @allowed;
|
||||||
|
|
||||||
|
|
@ -205,7 +216,7 @@ Draw the field to edit field prices. Add appropriate javascript.
|
||||||
sub drawEditFieldPrice {
|
sub drawEditFieldPrice {
|
||||||
my ( $self ) = @_;
|
my ( $self ) = @_;
|
||||||
|
|
||||||
my $fieldHtml = sprintf <<'ENDHTML', encode_entities( $self->get('fieldPrice') );
|
my $fieldHtml = sprintf <<'ENDHTML', encode_entities( $self->fieldPrice );
|
||||||
<div id="fieldPrice"></div><input type="hidden" name="fieldPrice" value="%s" id="fieldPrice_formId"/>
|
<div id="fieldPrice"></div><input type="hidden" name="fieldPrice" value="%s" id="fieldPrice_formId"/>
|
||||||
ENDHTML
|
ENDHTML
|
||||||
|
|
||||||
|
|
@ -292,8 +303,8 @@ Get the price
|
||||||
|
|
||||||
sub getPrice {
|
sub getPrice {
|
||||||
my ($self) = @_;
|
my ($self) = @_;
|
||||||
my $price = $self->get('price');
|
my $price = $self->price;
|
||||||
my $fieldPrice = JSON->new->decode( $self->get('fieldPrice') || '{}' );
|
my $fieldPrice = JSON->new->decode( $self->fieldPrice || '{}' );
|
||||||
my $option = $self->getOptions;
|
my $option = $self->getOptions;
|
||||||
my $record = $RECORD_CLASS->new( $self->session, $option->{recordId} );
|
my $record = $RECORD_CLASS->new( $self->session, $option->{recordId} );
|
||||||
my $fields = JSON->new->decode( $record->get('fields') );
|
my $fields = JSON->new->decode( $record->get('fields') );
|
||||||
|
|
@ -359,7 +370,7 @@ sub getThingOptions {
|
||||||
while ( my $thingy = $thingyIter->() ) {
|
while ( my $thingy = $thingyIter->() ) {
|
||||||
tie my %things, 'Tie::IxHash', (
|
tie my %things, 'Tie::IxHash', (
|
||||||
$session->db->buildHash( "SELECT thingId, label FROM Thingy_things WHERE assetId=?", [ $thingy->getId ] ) );
|
$session->db->buildHash( "SELECT thingId, label FROM Thingy_things WHERE assetId=?", [ $thingy->getId ] ) );
|
||||||
$options{ $thingy->get('title') } = \%things;
|
$options{ $thingy->title } = \%things;
|
||||||
}
|
}
|
||||||
|
|
||||||
return \%options;
|
return \%options;
|
||||||
|
|
@ -391,7 +402,7 @@ sub getThingy {
|
||||||
my ($self) = @_;
|
my ($self) = @_;
|
||||||
my $thingyId = $self->session->db->quickScalar(
|
my $thingyId = $self->session->db->quickScalar(
|
||||||
"SELECT assetId FROM Thingy_things WHERE thingId=?",
|
"SELECT assetId FROM Thingy_things WHERE thingId=?",
|
||||||
[ $self->get('thingId') ],
|
[ $self->thingId ],
|
||||||
);
|
);
|
||||||
return WebGUI::Asset->newById( $self->session, $thingyId );
|
return WebGUI::Asset->newById( $self->session, $thingyId );
|
||||||
}
|
}
|
||||||
|
|
@ -415,7 +426,7 @@ sub onCompletePurchase {
|
||||||
|
|
||||||
# Update record
|
# Update record
|
||||||
$record->update( {
|
$record->update( {
|
||||||
expires => $now + $self->get('duration'),
|
expires => $now + $self->duration,
|
||||||
transactionId => $item->transaction->getId,
|
transactionId => $item->transaction->getId,
|
||||||
isHidden => 0,
|
isHidden => 0,
|
||||||
}
|
}
|
||||||
|
|
@ -423,26 +434,26 @@ sub onCompletePurchase {
|
||||||
|
|
||||||
# Add to thingy data
|
# Add to thingy data
|
||||||
my $data = JSON->new->decode( $record->get('fields') );
|
my $data = JSON->new->decode( $record->get('fields') );
|
||||||
$self->updateThingRecord( $self->get('thingId'), $record->getId, $data );
|
$self->updateThingRecord( $self->thingId, $record->getId, $data );
|
||||||
}
|
}
|
||||||
elsif ( $option->{action} eq "renew" ) {
|
elsif ( $option->{action} eq "renew" ) {
|
||||||
|
|
||||||
# Renew a currently active record
|
# Renew a currently active record
|
||||||
if ( $record->get('expires') > $now ) {
|
if ( $record->get('expires') > $now ) {
|
||||||
$record->update( { expires => $record->get('expires') + $self->get('duration'), } );
|
$record->update( { expires => $record->get('expires') + $self->duration, } );
|
||||||
}
|
}
|
||||||
|
|
||||||
# Renew an expired but not deleted record
|
# Renew an expired but not deleted record
|
||||||
else {
|
else {
|
||||||
$record->update( {
|
$record->update( {
|
||||||
expires => $now + $self->get('duration'),
|
expires => $now + $self->duration,
|
||||||
isHidden => 0,
|
isHidden => 0,
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
# Add to thingy data
|
# Add to thingy data
|
||||||
my $data = JSON->new->decode( $record->get('fields') );
|
my $data = JSON->new->decode( $record->get('fields') );
|
||||||
$self->updateThingRecord( $self->get('thingId'), $record->getId, $data );
|
$self->updateThingRecord( $self->thingId, $record->getId, $data );
|
||||||
}
|
}
|
||||||
} ## end elsif ( $option->{action}...)
|
} ## end elsif ( $option->{action}...)
|
||||||
} ## end sub onCompletePurchase
|
} ## end sub onCompletePurchase
|
||||||
|
|
@ -479,7 +490,7 @@ See WebGUI::Asset::prepareView() for details.
|
||||||
sub prepareView {
|
sub prepareView {
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
$self->SUPER::prepareView();
|
$self->SUPER::prepareView();
|
||||||
my $template = WebGUI::Asset::Template->new( $self->session, $self->get("templateIdView") );
|
my $template = WebGUI::Asset::Template->new( $self->session, $self->templateIdView );
|
||||||
$template->prepare( $self->getMetaDataAsTemplateVariables );
|
$template->prepare( $self->getMetaDataAsTemplateVariables );
|
||||||
$self->{_viewTemplate} = $template;
|
$self->{_viewTemplate} = $template;
|
||||||
}
|
}
|
||||||
|
|
@ -495,9 +506,9 @@ Process the edit record form and return the record
|
||||||
sub processEditRecordForm {
|
sub processEditRecordForm {
|
||||||
my ($self) = @_;
|
my ($self) = @_;
|
||||||
my $var = {};
|
my $var = {};
|
||||||
my $fieldPrice = JSON->new->decode( $self->get('fieldPrice') );
|
my $fieldPrice = JSON->new->decode( $self->fieldPrice );
|
||||||
|
|
||||||
my $fields = $self->getThingFields( $self->get('thingId') );
|
my $fields = $self->getThingFields( $self->thingId );
|
||||||
for my $field ( @{$fields} ) {
|
for my $field ( @{$fields} ) {
|
||||||
my $fieldName = 'field_' . $field->{fieldId};
|
my $fieldName = 'field_' . $field->{fieldId};
|
||||||
my $fieldType = $field->{fieldType};
|
my $fieldType = $field->{fieldType};
|
||||||
|
|
@ -575,7 +586,7 @@ sub view {
|
||||||
$var->{isNew} = 1;
|
$var->{isNew} = 1;
|
||||||
$var->{message}
|
$var->{message}
|
||||||
= $options->{addedToCart}
|
= $options->{addedToCart}
|
||||||
? $self->get('thankYouText')
|
? $self->thankYouText
|
||||||
: $options->{message};
|
: $options->{message};
|
||||||
if ( $options->{addedToCart} ) {
|
if ( $options->{addedToCart} ) {
|
||||||
$var->{addedToCart} = 1;
|
$var->{addedToCart} = 1;
|
||||||
|
|
@ -672,7 +683,7 @@ sub www_editRecord {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $self->processStyle( $self->processTemplate( $var, $self->get('templateIdView') ) );
|
return $self->processStyle( $self->processTemplate( $var, $self->templateIdView ) );
|
||||||
} ## end sub www_editRecord
|
} ## end sub www_editRecord
|
||||||
|
|
||||||
#----------------------------------------------------------------------------
|
#----------------------------------------------------------------------------
|
||||||
|
|
@ -701,10 +712,10 @@ sub www_editRecordSave {
|
||||||
);
|
);
|
||||||
|
|
||||||
if ($hide) {
|
if ($hide) {
|
||||||
$self->deleteThingRecord( $self->get('thingId'), $recordId );
|
$self->deleteThingRecord( $self->thingId, $recordId );
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$self->updateThingRecord( $self->get('thingId'), $recordId, $recordData );
|
$self->updateThingRecord( $self->thingId, $recordId, $recordData );
|
||||||
}
|
}
|
||||||
|
|
||||||
return $self->www_editRecord( { message => $i18n->get('saved') } );
|
return $self->www_editRecord( { message => $i18n->get('saved') } );
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue