From e9d54a31714f06300934f321b5be7464bbdd2f7b Mon Sep 17 00:00:00 2001 From: David Delikat Date: Sun, 22 Mar 2009 01:25:19 +0000 Subject: [PATCH] 90% functionally complete --- docs/upgrades/upgrade_7.7.0-7.7.1.pl | 27 +++++ installAdspace.pl | 3 +- lib/WebGUI/Asset/Sku/Ad.pm | 131 +++++++++++++++++------- lib/WebGUI/AssetCollateral/Sku/Ad/Ad.pm | 14 +-- lib/WebGUI/Shop/Transaction.pm | 2 +- 5 files changed, 132 insertions(+), 45 deletions(-) diff --git a/docs/upgrades/upgrade_7.7.0-7.7.1.pl b/docs/upgrades/upgrade_7.7.0-7.7.1.pl index 1bfaeb404..4d03f414d 100644 --- a/docs/upgrades/upgrade_7.7.0-7.7.1.pl +++ b/docs/upgrades/upgrade_7.7.0-7.7.1.pl @@ -31,10 +31,37 @@ my $quiet; # this line required my $session = start(); # this line required # upgrade functions go here +adSkuInstall($session); finish($session); # this line required +#---------------------------------------------------------------------------- +# Describe what our function does +sub adSkuInstall { + my $session = shift; + print "\tCreate AdSku database table\n" unless $quiet; + $session->db->write("CREATE TABLE AdSku ( + assetId VARCHAR(22) BINARY NOT NULL, + revisionDate BIGINT NOT NULL, + purchaseTemplate VARCHAR(22) BINARY NOT NULL, + manageTemplate VARCHAR(22) BINARY NOT NULL, + adSpace VARCHAR(22) BINARY NOT NULL, + priority INTEGER DEFAULT '1', + pricePerClick Float DEFAULT '0', + pricePerImpression Float DEFAULT '0', + clickDiscounts VARCHAR(1024) default '', + impressionDiscounts VARCHAR(1024) default '', + PRIMARY KEY (assetId,revisionDate) + )"); + print "\tCreate Adsku crud table\n" unless $quiet; + use WebGUI::AssetCollateral::Sku::Ad::Ad; + WebGUI::AssetCollateral::Sku::Ad::Ad->crud_createTable($session); + print "\tinstall the AdSku Asset\n" unless $quiet; + $session->config->addToHash("assets", 'WebGUI::Asset::Sku::Ad' => { category => 'shop' } ); + print "DONE!\n" unless $quiet; +} + #---------------------------------------------------------------------------- # Describe what our function does #sub exampleFunction { diff --git a/installAdspace.pl b/installAdspace.pl index c5934b4f7..c08b791f6 100644 --- a/installAdspace.pl +++ b/installAdspace.pl @@ -14,8 +14,8 @@ sub install { print "Installing asset.\n"; my $session = WebGUI::Session->open($home, $confg); print "create crud table"; + WebGUI::AssetCollateral::Sku::Ad::Ad->crud_dropTable($session); WebGUI::AssetCollateral::Sku::Ad::Ad->crud_createTable($session); -if(0) { print "Add wobject to confg file\n"; $session->config->addToHash("assets",$className => { category => 'shop' } ); print "Create database tables\n"; @@ -32,6 +32,7 @@ if(0) { impressionDiscounts VARCHAR(1024) default '', PRIMARY KEY (assetId,revisionDate) )"); +if(0) { print "Create a folder asset to store the default templates\n"; my $importNode = WebGUI::Asset->getImportNode($session); my $newFolder = $importNode->addChild({ diff --git a/lib/WebGUI/Asset/Sku/Ad.pm b/lib/WebGUI/Asset/Sku/Ad.pm index d12e98193..320212655 100644 --- a/lib/WebGUI/Asset/Sku/Ad.pm +++ b/lib/WebGUI/Asset/Sku/Ad.pm @@ -24,6 +24,7 @@ use WebGUI::Asset::Template; use WebGUI::Form; use WebGUI::Shop::Pay; use WebGUI::AssetCollateral::Sku::Ad::Ad; +use WebGUI::AdSpace::Ad; =head1 NAME @@ -268,22 +269,18 @@ sub manage { $var{purchaseLink} = $self->getUrl; my $iterator = WebGUI::AssetCollateral::Sku::Ad::Ad->getAllIterator($session,{ constraints => [ { "adSkuPurchase.userId = ?" => $self->session->user->userId } ], - joinUsing => [ { "advertisement" => "adId" }, ], - 'join' => [ "transactionItem on transactionItem.itemId = adSkuPurchase.transactionItemId", - "transaction on transaction.transactionId = transactionItem.transactionId", - ], - orderBy => 'transaction.dateOfPurchase', + orderBy => 'dateOfPurchase', }); - my %testHash; # used to eliminate duplicate ads + my %ads; while( my $object = $iterator->() ) { - next if exists $testHash{$object->get('adId')}; - $testHash{$object->get('adId')} = 1; + next if exists $ads{$object->get('adId')}; + my $ad = $ads{$object->get('adId')} = WebGUI::AdSpace::Ad->new($session,$object->get('adId')); push @{$var{myAds}}, { - rowTitle => $object->get('title'), - rowClicks => $object->get('clicks') . '/' . $object->get('clicksBought'), - rowImpressions => $object->get('impressions') . '/' . $object->get('impressionsBought'), + rowTitle => $ad->get('title'), + rowClicks => $ad->get('clicks') . '/' . $ad->get('clicksBought'), + rowImpressions => $ad->get('impressions') . '/' . $ad->get('impressionsBought'), rowDeleted => $object->get('isDeleted'), - rowRenewLink => $self->getUrl('renew=' . $object->get('adId') ), + rowRenewLink => $self->getUrl('func=renew;adId=' . $object->get('adId') ), }; } return $self->processTemplate(\%var,undef,$self->{_viewTemplate}); @@ -299,11 +296,12 @@ inserts the ad intothe adspace... sub onCompletePurchase { my $self = shift; + my $item = shift; my $options = $self->getOptions; - # TODO insert crud +# LATER: if we use Temp Storage for the image we need to move it to perm storage - WegGUI::AdSpace::Ad->create($self->session,$self->get('adSpace'),{ + my $ad = WebGUI::AdSpace::Ad->create($self->session,$self->get('adSpace'),{ title => $options->{'adtitle'}, clicksBought => $options->{'clicks'}, impressionsBought => $options->{'impressions'}, @@ -313,8 +311,49 @@ sub onCompletePurchase { isActive => 1, type => 'image', priority => $self->get('priority'), + adSpace => $self->get('adSpace'), }); + WebGUI::AssetCollateral::Sku::Ad::Ad->create($self->session,{ + userId => $item->transaction->get('userId'), + transactionItemId => $item->getId, + adId => $ad->getId, + clicksPurchased => $options->{'clicks'}, + impressionsPurchased => $options->{'impressions'}, + dateOfPurchase => $item->transaction->get('dateOfPurchase'), + storedImage => $options->{'image'}, + isDeleted => 0, + }); + +} + +#------------------------------------------------------------------- + +=head2 onRemoveFromCart + +deletes the image if it gets removed from the cart + +LATER: if we switch to using Temp Storage we do not need to do this. + +=cut + +sub onRemoveFromCart { + my $self = shift; + my $item = shift; + my $options = $self->getOptions; + WebGUI::Storage->new($self->session,$options->{'image'})->delete; +} + +#------------------------------------------------------------------- + +=head2 onRefund + +delete the add if it gets refunded + +=cut + +sub onRefund { +# TODO delete the ad... } #------------------------------------------------------------------- @@ -438,6 +477,40 @@ my $options = $self->getOptions(); #------------------------------------------------------------------- +=head2 www_addToCart + +Add this subscription to the cart. + +=cut + +sub www_addToCart { + my $self = shift; + if ($self->canView) { + $self->{_hasAddedToCart} = 1; + my $form = $self->session->form; + my $imageStorage = WebGUI::Storage->create( $self->session); # LATER should be createTemp + $imageStorage->addFileFromFormPost('formImage',1); + my $imageStorageId = $imageStorage->getId; + # TODO error in case image does not upload +dav::log 'addToCart:data:', + 'adtitle:' => $form->get('formTitle'),',', + 'link:' => $form->get('formLink','url'),',', + 'image:' => $imageStorageId,',', + 'clicks:' => $form->get('formClicks'),',', + 'impressions:' => $form->get('formImpressions'); + $self->addToCart({ + adtitle => $form->get('formTitle'), + link => $form->process('formLink','url'), + clicks => $form->process('formClicks','integer'), + impressions => $form->process('formImpressions','integer'), + image => $imageStorageId, + }); + } + return $self->www_view; +} + +#------------------------------------------------------------------- + =head2 www_manage manage previously purchased ads @@ -461,32 +534,18 @@ sub www_manage { #------------------------------------------------------------------- -=head2 www_addToCart +=head2 www_renew -Add this subscription to the cart. +renew an ad =cut -sub www_addToCart { - my $self = shift; - if ($self->canView) { - $self->{_hasAddedToCart} = 1; - my $form = $self->session->form; -dav::log 'addToCart:data:', - 'adtitle:' => $form->get('formTitle'),',', - 'link:' => $form->get('formLink','url'),',', - 'image:' => $form->get('formImage'),',', - 'clicks:' => $form->get('formClicks'),',', - 'impressions:' => $form->get('formImpressions'); - $self->addToCart({ - adtitle => $form->get('formTitle'), - link => $form->process('formLink','url'), - clicks => $form->process('formClicks','integer'), - impressions => $form->process('formImpressions','integer'), - image => $form->get('formImage'), - }); - } - return $self->www_view; +sub www_renew { + my $self = shift; + my $adPurchaseId = ''; # TODO get the adPurchaseId param + my $crud = WebGUI::AssetCollateral::Sku::Ad::Ad->new($self->session,$adPurchaseId); + # TODO assign params for purchase form + return $self->www_view; } 1; diff --git a/lib/WebGUI/AssetCollateral/Sku/Ad/Ad.pm b/lib/WebGUI/AssetCollateral/Sku/Ad/Ad.pm index aae5ea6d2..a2edeffa8 100644 --- a/lib/WebGUI/AssetCollateral/Sku/Ad/Ad.pm +++ b/lib/WebGUI/AssetCollateral/Sku/Ad/Ad.pm @@ -29,8 +29,8 @@ transactionItemid = the id if the transaction item that completes this purchase adId = th id if the ad purchased clicksPurchased = the number of clicks the user purchased impressionsPurchased = the number of impressions the user purchased -storedImage = temp storage for the image -isRenewal = indicates if this purchase is a renewal -- are the counts increased or just assigned +dateOfPurchase = the date of purchase +storedImage = storage for the image isDeleted = boolean that indicates whether the ad has been deleted from the system =cut @@ -39,7 +39,7 @@ sub crud_definition { my ($class, $session) = @_; my $definition = $class->SUPER::crud_definition($session); $definition->{tableName} = 'adSkuPurchase'; - $definition->{tableKey} = 'userId'; + $definition->{tableKey} = 'adSkuPurchaseId'; $definition->{properties} = { userId => { fieldType => 'user', @@ -61,14 +61,14 @@ sub crud_definition { fieldType => 'integer', defaultValue => undef, }, + dateOfPurchase => { + fieldType => 'date', + defaultValue => undef, + }, storedImage => { fieldType => 'guid', defaultValue => undef, }, - isRenewal => { - fieldType => 'yesNo', - defaultValue => 0, - }, isDeleted => { fieldType => 'yesNo', defaultValue => 0, diff --git a/lib/WebGUI/Shop/Transaction.pm b/lib/WebGUI/Shop/Transaction.pm index 8fb618a6a..8a3d9893e 100644 --- a/lib/WebGUI/Shop/Transaction.pm +++ b/lib/WebGUI/Shop/Transaction.pm @@ -162,7 +162,7 @@ sub create { $posUser = $cart->getPosUser; } $session->db->write('insert into transaction (transactionId, userId, username, cashierUserId, dateOfPurchase) values (?,?,?,?,now())', - [$transactionId, $posUser->userId, $posUser->username, $cashier->userId]); + [$transactionId, $posUser->userId, $posUser->username || 'noname', $cashier->userId]); my $self = $class->new($session, $transactionId); $self->update($properties); return $self;