diff --git a/docs/upgrades/packages-thingy-rfe/templates_thingy-default.wgpkg b/docs/upgrades/packages-thingy-rfe/templates_thingy-default.wgpkg new file mode 100644 index 000000000..d1ec87948 Binary files /dev/null and b/docs/upgrades/packages-thingy-rfe/templates_thingy-default.wgpkg differ diff --git a/lib/WebGUI/Asset/Wobject/Thingy.pm b/lib/WebGUI/Asset/Wobject/Thingy.pm index 2b17c3925..76e9a2c16 100644 --- a/lib/WebGUI/Asset/Wobject/Thingy.pm +++ b/lib/WebGUI/Asset/Wobject/Thingy.pm @@ -277,6 +277,41 @@ sub duplicate { #------------------------------------------------------------------- +=head2 duplicateThing ( thingId ) + +Duplicates a thing. + +=head3 thingId + +The id of the Thing that will be duplicated. + +=cut + +sub duplicateThing { + + my $self = shift; + my $oldThingId = shift; + my $db = $self->session->db; + + my $thingProperties = $self->getThing($oldThingId); + $thingProperties->{thingId} = 'new'; + $thingProperties->{label} = $thingProperties->{label}.' (copy)'; + + my $newThingId = $self->addThing($thingProperties); + my $fields = $db->buildArrayRefOfHashRefs('select * from Thingy_fields where assetId=? and thingId=?' + ,[$self->getId,$oldThingId]); + foreach my $field (@$fields) { + # set thingId to newly created thing's id. + $field->{thingId} = $newThingId; + $self->addField($field,0); + } + + return $newThingId; + +} + +#------------------------------------------------------------------- + =head2 deleteField ( fieldId , thingId ) Deletes a field from Collateral and drops the fields column in the thingy table. @@ -1395,6 +1430,26 @@ sub www_deleteFieldConfirm { return 1; } + +#------------------------------------------------------------------- + +=head2 www_deleteFieldConfirm ( ) + +Duplicates a Thing. + +=cut + +sub www_duplicateThing { + my $self = shift; + my $session = $self->session; + my $thingId = $session->form->process("thingId"); + return $session->privilege->insufficient() unless $self->canEdit; + + $self->duplicateThing($thingId); + + return $self->www_manage; +} + #------------------------------------------------------------------- =head2 www_copyThingData( ) @@ -2821,6 +2876,8 @@ sub www_manage { "",$i18n->get('delete thing warning')), 'thing_editUrl' => $session->url->append($url, 'func=editThing;thingId='.$thing->{thingId}), 'thing_editIcon' => $session->icon->edit('func=editThing;thingId='.$thing->{thingId}), + 'thing_copyUrl' => $session->url->append($url, 'func=duplicateThing;thingId='.$thing->{thingId}), + 'thing_copyIcon' => $session->icon->copy('func=duplicateThing;thingId='.$thing->{thingId}), 'thing_addUrl' => $session->url->append($url, 'func=editThingData;thingId='.$thing->{thingId}.';thingDataId=new'), 'thing_searchUrl' => $session->url->append($url, 'func=search;thingId='.$thing->{thingId}), diff --git a/t/Asset/Wobject/Thingy.t b/t/Asset/Wobject/Thingy.t index 45c198f04..646266512 100644 --- a/t/Asset/Wobject/Thingy.t +++ b/t/Asset/Wobject/Thingy.t @@ -17,7 +17,7 @@ use lib "$FindBin::Bin/../../lib"; use WebGUI::Test; use WebGUI::Session; use WebGUI::PseudoRequest; -use Test::More tests => 16; # increment this value for each test you create +use Test::More tests => 17; # increment this value for each test you create use Test::Deep; use JSON; use WebGUI::Asset::Wobject::Thingy; @@ -192,6 +192,14 @@ my ($fieldLabel, $columnType, $Null, $Key, $Default, $Extra) = $session->db->qui is($fieldLabel,"field_".$fieldId,"A column for the new field Field_$fieldId exists."); is($columnType,"longtext","The columns is the right type"); +# Test duplicating a Thing + +my $copyThingId = $thingy->duplicateThing($thingId); + +$isValidId = $session->id->valid($copyThingId); + +is($isValidId,1,"duplicating a Thing: duplicateThing returned a valid id: ".$copyThingId); + # Test adding, editing, getting and deleting thing data my ($newThingDataId,$errors) = $thingy->editThingDataSave($thingId,'new',{"field_".$fieldId => 'test value'});