Conflicts:
	lib/WebGUI/Asset/Wobject/Thingy.pm
This commit is contained in:
Doug Bell 2011-05-20 14:58:43 -05:00
commit 1fa911029d
5 changed files with 243 additions and 14 deletions

View file

@ -1,6 +1,10 @@
7.10.17
- fixed: Forced to use a PayDriver even with a balance of 0 in the cart.
- add #12134: Access metadata for assets inside an AssetReport
- fixed : Thingy CSV import not counting towards maxEntriesPerUser
- fixed : Thingy CSV new records not updated with createdById and dateCreated and ipAddress
- added : Thingy fields can now be set as unique and checked upon insert
- added : Thingy max entries of thingy records added
7.10.16
- fixed #12121: typ-o Asset_Map.templateIdEditPoint

View file

@ -31,6 +31,7 @@ my $quiet; # this line required
my $session = start(); # this line required
# upgrade functions go here
createThingyDBColumns($session);
finish($session); # this line required
@ -44,6 +45,21 @@ finish($session); # this line required
# print "DONE!\n" unless $quiet;
#}
#----------------------------------------------------------------------------
# Creates new column in tables for Thingy_fields and Thingy_things
sub createThingyDBColumns {
my $session = shift;
print "\tAdding db. columns Thingy_fields.isUnique and Thingy_things.maxEntriesTotal.." unless $quiet;
# and here's our code
my %tfHash = $session->db->quickHash("show columns from Thingy_fields where Field='isUnique'");
my %ttHash = $session->db->quickHash("show columns from Thingy_things where Field='maxEntriesTotal'");
unless ( $tfHash{'Field'}) { $session->db->write("alter table Thingy_fields add isUnique int(1) default 0"); }
unless ( $ttHash{'Field'}) { $session->db->write("alter table Thingy_things add maxEntriesTotal int default null"); }
print "DONE!\n" unless $quiet;
}
# -------------- DO NOT EDIT BELOW THIS LINE --------------------------------

View file

@ -35,7 +35,6 @@ addAddonsToAdminConsole($session);
finish($session); # this line required
#----------------------------------------------------------------------------
# Describe what our function does
sub addAddonsToAdminConsole {

View file

@ -671,6 +671,17 @@ sub editThingDataSave {
$fieldValue = $field->{defaultValue};
#WebGUI::Macro::process($self->session,\$fieldValue);
}
if ($field->{isUnique}) {
unless ( $self->isUniqueEntry($thingId,$fieldName,$fieldValue,$thingDataId)) {
push (@errors,{
"error_message"=>$field->{label}. $i18n->get('needs to be unique error'),
});
}
}
$thingData{$fieldName} = $fieldValue;
}
@ -856,6 +867,16 @@ sub getEditFieldForm {
-options=>\%fieldTypes,
-id=>$dialogPrefix."_fieldType_formId",
);
$f->yesNo(
-name=>'isUnique',
-label=>$i18n->get('unique label'),
-hoverHelp=>$i18n->get('unique description'),
-value=>$field->{isUnique},
-id=>$dialogPrefix."_isUnique_formId",
);
$f->raw($self->getHtmlWithModuleWrapper($dialogPrefix."_fieldInThing_module"));
$f->raw($self->getHtmlWithModuleWrapper($dialogPrefix."_defaultFieldInThing_module"));
@ -1388,6 +1409,68 @@ sub hasEnteredMaxPerUser {
}
}
#-------------------------------------------------------------------
=head2 hasEnteredMaxEntries
Check whether the the maximum number of entries allowed for this thing has been reached.
=head3 thingId
The unique id of a thing.
=cut
sub hasEnteredMaxEntries {
my ($self,$thingId) = @_;
my $session = $self->session;
my $db = $session->db;
my $maxEntriesTotal = $db->quickScalar("select maxEntriesTotal from Thingy_things where thingId=?",[$thingId]);
return 0 unless $maxEntriesTotal;
my $numberOfEntries = $session->db->quickScalar("select count(*) "
."from ".$session->db->dbh->quote_identifier("Thingy_".$thingId));
if($numberOfEntries < $maxEntriesTotal){
return 0;
}
else{
return 1;
}
}
#-------------------------------------------------------------------
=head2 isUniqueEntry ( thingId,fieldName,fieldValue, thingDataId )
Checks if the data entered in thingy record is unique
=cut
sub isUniqueEntry {
my ($self,$thingId,$fieldName,$fieldValue,$thingDataId) = @_;
my $session = $self->session;
my $db = $session->db;
my $nrOfEntries = $session->db->quickScalar("select count(*) "
."from ".$session->db->dbh->quote_identifier("Thingy_".$thingId)." where " .
$session->db->dbh->quote_identifier($fieldName) ."=? and thingDataId !=?",[$fieldValue,$thingDataId]);
if ($nrOfEntries > 0) { return 0; }
return 1;
}
#-------------------------------------------------------------------
=head2 hasPrivileges ( groupId )
@ -1998,8 +2081,9 @@ sub www_editThing {
thingsPerPage=>25,
exportMetaData=>undef,
maxEntriesPerUser=>undef,
maxEntriesTotal=>undef,
);
$thingId = "new";
$thingId = $self->addThing(\%properties,0);
}
else{
%properties = %{$self->getThing($thingId)};
@ -2209,6 +2293,14 @@ sub www_editThing {
-hoverHelp=> $i18n->get('max entries per user description'),
-label => $i18n->get('max entries per user label')
);
$tab->integer(
-name=> "maxEntriesTotal",
-value=> $properties{maxEntriesTotal},
-hoverHelp => $i18n->get('max entries total description'),
-label => $i18n->get('max entries total label')
);
$tab->group(
-name=> "groupIdAdd",
-value=> $properties{groupIdAdd},
@ -2442,9 +2534,10 @@ sub www_editThingSave {
sortBy => $form->process("sortBy") || '',
exportMetaData => $form->process("exportMetaData") || '',
maxEntriesPerUser => $form->process("maxEntriesPerUser") || '',
maxEntriesTotal => $form->process("maxEntriesTotal") || '',
};
$self->setCollateral("Thingy_things", "thingId", $thing, 0, 1);
if($fields->rows < 1){
$self->session->log->warn("Thing failed to create because it had no fields");
my $i18n = WebGUI::International->new($self->session, "Asset_Thingy");
@ -2478,7 +2571,6 @@ sub www_editField {
return $session->privilege->insufficient() unless $self->canEdit;
$fieldId = $session->form->process("fieldId");
$thingId = $session->form->process("thingId");
%properties = $session->db->quickHash("select * from Thingy_fields where thingId=? and fieldId=? and assetId=?",
[$thingId,$fieldId,$self->get("assetId")]);
if($session->form->process("copy")){
@ -2515,12 +2607,12 @@ sub www_editFieldSave {
my $log = $session->log;
my $defaultValue = $session->form->process("defaultValue");
my $fieldType = $session->form->process("fieldType") || "ReadOnly";
my $uniqueField = $session->form->process("isUnique");
if ($fieldType =~ m/^otherThing/){
$defaultValue = $session->form->process("defaultFieldInThing");
}
$thingId = $self->addThing({ thingId => 'new' },0) if $thingId eq 'new';
$fieldId = $session->form->process("fieldId");
%properties = (
@ -2528,6 +2620,7 @@ sub www_editFieldSave {
thingId => $thingId,
label => $label,
fieldType => $fieldType,
isUnique => $uniqueField,
defaultValue => $defaultValue,
possibleValues => $session->form->process("possibleValues"),
pretext => $session->form->process("pretext"),
@ -2585,7 +2678,7 @@ sub www_editFieldSave {
# Make sure we send debug information along with the field.
$log->preventDebugOutput;
$session->output->print($thingId.$newFieldId.$listItemHTML);
$session->output->print($newFieldId.$listItemHTML);
return "chunked";
}
@ -2745,7 +2838,7 @@ sub editThingData {
$var->{"delete_confirm"} = "onclick=\"return confirm('".$i18n->get("delete thing data warning")."')\"";
}
if($self->hasPrivileges($thingProperties->{groupIdAdd}) && !$self->hasEnteredMaxPerUser($thingId)){
if($self->hasPrivileges($thingProperties->{groupIdAdd}) && !$self->hasEnteredMaxPerUser($thingId) && !$self->hasEnteredMaxEntries($thingId)){
$var->{"add_url"} = $session->url->append($url,'func=editThingData;thingId='.$thingId.';thingDataId=new');
}
if($self->hasPrivileges($thingProperties->{groupIdSearch})){
@ -2811,6 +2904,15 @@ sub editThingData {
delete $var->{field_loop};
$var->{editInstructions} = $i18n->get("has entered max per user message");
}
if($thingDataId eq 'new' && $self->hasEnteredMaxEntries($thingId)){
delete $var->{form_start};
delete $var->{form_end};
delete $var->{form_submit};
delete $var->{field_loop};
$var->{editInstructions} = $i18n->get("has entered max total message");
}
return $self->processTemplate($var,$thingProperties->{editTemplateId});
}
@ -2840,6 +2942,10 @@ sub www_editThingDataSave {
if($thingDataId eq 'new' && $self->hasEnteredMaxPerUser($thingId)){
return $i18n->get("has entered max per user message");
}
if($thingDataId eq 'new' && $self->hasEnteredMaxEntries($thingId)){
return $i18n->get("has entered max total message");
}
($newThingDataId,$errors) = $self->editThingDataSave($thingId,$thingDataId);
@ -2910,6 +3016,10 @@ sub www_editThingDataSaveViaAjax {
$session->http->setStatus("400", "Bad Request");
return JSON->new->encode({message => $i18n->get("has entered max per user message")});
}
if($thingDataId eq 'new' && $self->hasEnteredMaxEntries($thingId)){
$session->http->setStatus("400", "Bad Request");
return JSON->new->encode({message => $i18n->get("has entered max total message")});
}
my ($newThingDataId,$errors) = $self->editThingDataSave($thingId,$thingDataId);
@ -3096,6 +3206,9 @@ sub www_import {
my ($sql,$fields,@fields,$fileName,@insertColumns);
my ($handleDuplicates,$newThingDataId);
my $i18n = WebGUI::International->new($self->session, "Asset_Thingy");
my $thingId = $session->form->process('thingId');
my $thingProperties = $self->getThing($thingId);
return $session->privilege->insufficient() unless $self->hasPrivileges($thingProperties->{groupIdImport});
@ -3193,9 +3306,27 @@ sub www_import {
$error->info("Skipping line");
next;
}
$thingData{lastUpdated} = time();
$thingData{updatedByName} = $session->user->username;
$thingData{updatedById} = $session->user->userId;
# Is this a new record or are we updating an existing record?
if ($thingData{thingDataId} eq 'new') {
$thingData{dateCreated} = time();
$thingData{createdById} = $session->user->userId;
}
else {
$thingData{lastUpdated} = time();
$thingData{updatedByName} = $session->user->username;
$thingData{updatedById} = $session->user->userId;
}
$thingData{ipAddress} = $session->env->getIp;
if($thingData{thingDataId} eq 'new' && $self->hasEnteredMaxPerUser($thingId)){
last;
}
if($thingData{thingDataId} eq 'new' && $self->hasEnteredMaxEntries($thingId)){
last;
}
$self->setCollateral("Thingy_".$thingId,"thingDataId",\%thingData,0,0) if ($thingData{thingDataId});
}
close $importFile;
@ -3519,7 +3650,7 @@ sub getSearchTemplateVars {
if ($self->hasPrivileges($thingProperties->{groupIdImport})){
$var->{"import_url"} = $session->url->append($url, 'func=importForm;thingId='.$thingId);
}
if ($self->hasPrivileges($thingProperties->{groupIdAdd}) && !$self->hasEnteredMaxPerUser($thingId)){
if ($self->hasPrivileges($thingProperties->{groupIdAdd}) && !$self->hasEnteredMaxPerUser($thingId) && !$self->hasEnteredMaxEntries($thingId) ){
$var->{"add_url"} = $session->url->append($url,'func=editThingData;thingId='.$thingId.';thingDataId=new');
}
$var->{searchScreenTitle} = $thingProperties->{searchScreenTitle};
@ -3821,7 +3952,7 @@ sub www_viewThingData {
.$thingId.';thingDataId='.$thingDataId);
$var->{"delete_confirm"} = "onclick=\"return confirm('".$i18n->get("delete thing data warning")."')\"";
}
if($self->hasPrivileges($thingProperties->{groupIdAdd}) && !$self->hasEnteredMaxPerUser($thingId)){
if($self->hasPrivileges($thingProperties->{groupIdAdd}) && !$self->hasEnteredMaxPerUser($thingId) && !$self->hasEnteredMaxEntries($thingId) ){
$var->{"add_url"} = $session->url->append($url, 'func=editThingData;thingId='.$thingId.';thingDataId=new');
}
if($self->hasPrivileges($thingProperties->{groupIdSearch})){

View file

@ -16,7 +16,7 @@ use lib "$FindBin::Bin/../../lib";
use WebGUI::Test;
use WebGUI::Session;
use Test::More tests => 30; # increment this value for each test you create
use Test::More tests => 38; # increment this value for each test you create
use Test::Deep;
use JSON;
use WebGUI::Asset::Wobject::Thingy;
@ -129,6 +129,7 @@ cmp_deeply(
field_loop=>[],
exportMetaData=>undef,
maxEntriesPerUser=>undef,
maxEntriesTotal=>undef,
},
'Getting newly added thing as JSON: www_getThingViaAjax returns correct data as JSON.'
);
@ -172,6 +173,7 @@ cmp_deeply(
canSearch=>1,
exportMetaData=>undef,
maxEntriesPerUser=>undef,
maxEntriesTotal=>undef,
}],
'Getting all things in Thingy as JSON: www_getThingsViaAjax returns correct data as JSON.'
);
@ -355,6 +357,58 @@ ok( $thingy->hasEnteredMaxPerUser($otherThingId), 'hasEnteredMaxPerUser returns
);
}
#################################################################
#
# maxEntriesTotal
#
#################################################################
my %otherThingProperties = %thingProperties;
$otherThingProperties{maxEntriesTotal} = 1;
$otherThingProperties{editTemplateId } = $templateId;
my $otherThingId = $thingy->addThing(\%otherThingProperties, 0);
my %otherFieldProperties = %fieldProperties;
$otherFieldProperties{thingId} = $otherThingId;
my $otherFieldId = $thingy->addField(\%otherFieldProperties, 0);
ok( ! $thingy->hasEnteredMaxEntries($otherThingId), 'hasEnteredMaxEntries: returns false with no data entered');
my @edit_thing_form_fields = qw/form_start form_end form_submit field_loop/;
{
WebGUI::Test->mockAssetId($templateId, $templateMock);
$thingy->editThingData($otherThingId);
my %miniVars;
@miniVars{@edit_thing_form_fields} = @{ $templateVars }{ @edit_thing_form_fields };
cmp_deeply(
\%miniVars,
{
form_start => ignore,
form_end => ignore,
form_submit => ignore,
field_loop => ignore,
},
'thing edit form variables exist, because max entries not reached yet'
);
}
$thingy->editThingDataSave($otherThingId, 'new', {"field_".$otherFieldId => 'other test value'} );
ok( $thingy->hasEnteredMaxEntries($otherThingId), 'hasEnteredMaxEntries returns true with one row entered, and maxEntriesTotal=1');
{
WebGUI::Test->mockAssetId($templateId, $templateMock);
$thingy->editThingData($otherThingId);
my %miniVars;
@miniVars{@edit_thing_form_fields} = @{ $templateVars }{ @edit_thing_form_fields };
my $existance = 0;
foreach my $tmplVar (@edit_thing_form_fields) {
$existance ||= exists $templateVars->{$tmplVar}
}
ok(
! $existance,
'thing edit form variables do not exist, because max entries was reached'
);
}
#################################################################
#
# deleteThing
@ -443,3 +497,28 @@ is $json, '{}', 'www_editThingDataSaveViaAjax: Empty JSON hash';
is $session->http->getStatus, 200, '... http status=200';
$session->request->setup_body({ });
#################################################################
#
# Unique fields
#
#################################################################
{
my %newThingProperties = %thingProperties;
$newThingProperties{'groupIdView'} = 3;
my $newThingId = $thingy->addThing(\%newThingProperties, 0);
my %newFieldProperties = %fieldProperties;
$newFieldProperties{thingId} = $newThingId;
$newFieldProperties{isUnique} = 1;
my $newFieldId = $thingy->addField(\%newFieldProperties, 0);
my $fieldValue = 'value 1';
my ($dataId,undef) = $thingy->editThingDataSave($newThingId, 'new', {"field_".$newFieldId => $fieldValue} );
ok( $thingy->isUniqueEntry($newThingId,"field_${newFieldId}",$fieldValue,$dataId), "unique if the same entry" );
ok( !$thingy->isUniqueEntry($newThingId,"field_${newFieldId}",$fieldValue,"new"), "new data is not unique" );
my ( undef, $errors ) = $thingy->editThingDataSave($newThingId, 'new', {"field_".$newFieldId => $fieldValue} );
ok( @$errors >= 1, "an error was returned" );
my $errorMessage = $i18n->get('needs to be unique error');
ok( grep( { $_->{error_message} =~ m/$errorMessage/ } @$errors), "error about uniqueness in right field" );
}