fix: Shortcuts now follow their linked asset when trashing, purging, and restoring from trash

This commit is contained in:
Doug Bell 2007-10-05 00:27:14 +00:00
parent e41f544f4f
commit f684e728c8
4 changed files with 162 additions and 18 deletions

View file

@ -1976,6 +1976,16 @@ sub publish {
$cache->deleteChunk(["asset",$id]);
}
$self->{_properties}{state} = "published";
# Also publish any shortcuts to this asset that are in the trash
my $shortcuts
= WebGUI::Asset::Shortcut->getShortcutsForAssetId($self->session, $self->getId, {
returnObjects => 1,
statesToInclude => ['trash','trash-limbo'],
});
for my $shortcut ( @$shortcuts ) {
$shortcut->publish;
}
}

View file

@ -11,6 +11,7 @@ package WebGUI::Asset::Shortcut;
#-------------------------------------------------------------------
use strict;
use Carp;
use Tie::IxHash;
use WebGUI::Asset;
use WebGUI::International;
@ -906,5 +907,43 @@ sub www_view {
return $output;
}
#----------------------------------------------------------------------------
=head1 STATIC METHODS
These methods are called using CLASS->method
#----------------------------------------------------------------------------
=head2 getShortcutsForAssetId ( session, assetId [, properties] )
Get an arrayref of assetIds of all the shortcuts for the passed-in assetId.
"properties" is a hash reference of properties to give to getLineage.
Probably the only useful key will be "returnObjects".
=cut
sub getShortcutsForAssetId {
my $class = shift;
my $session = shift;
my $assetId = shift;
my $properties = shift || {};
croak "First argument to getShortcutsForAssetId must be WebGUI::Session"
unless $session && $session->isa("WebGUI::Session");
croak "Second argument to getShortcutsForAssetId must be assetId"
unless $assetId;
croak "Third argument to getShortcutsForAssetId must be hash reference"
if $properties && !ref $properties eq "HASH";
my $db = $session->db;
$properties->{ joinClass } = 'WebGUI::Asset::Shortcut';
$properties->{ whereClause } = 'Shortcut.shortcutToAssetId = ' . $db->quote($assetId);
return WebGUI::Asset->getRoot($session)->getLineage(['descendants'], $properties);
}
1;

View file

@ -134,6 +134,16 @@ sub purge {
}
}
# Delete shortcuts to this asset
# Also publish any shortcuts to this asset that are in the trash
my $shortcuts
= WebGUI::Asset::Shortcut->getShortcutsForAssetId($self->session, $self->getId, {
returnObjects => 1,
});
for my $shortcut ( @$shortcuts ) {
$shortcut->purge;
}
# gotta delete stuff we've exported
unless ($options->{skipExported}) {
$self->_invokeWorkflowOnExportedFiles($self->session->setting->get('purgeWorkflow'), 1);
@ -178,29 +188,40 @@ sub purge {
=head2 trash ( )
Removes asset from lineage, places it in trash state. The "gap" in the lineage is changed in state to trash-limbo.
Removes asset from lineage, places it in trash state. The "gap" in the
lineage is changed in state to trash-limbo.
=cut
sub trash {
my $self = shift;
return undef if ($self->getId eq $self->session->setting->get("defaultPage") || $self->getId eq $self->session->setting->get("notFoundPage"));
foreach my $asset ($self, @{$self->getLineage(['descendants'], {returnObjects => 1})}) {
$asset->_invokeWorkflowOnExportedFiles($self->session->setting->get('trashWorkflow'), 1);
}
my $self = shift;
return undef if ($self->getId eq $self->session->setting->get("defaultPage") || $self->getId eq $self->session->setting->get("notFoundPage"));
for my $asset ($self, @{$self->getLineage(['descendants'], {returnObjects => 1})}) {
$asset->_invokeWorkflowOnExportedFiles($self->session->setting->get('trashWorkflow'), 1);
}
my $db = $self->session->db;
$db->beginTransaction;
my $sth = $db->read("select assetId from asset where lineage like ?",[$self->get("lineage").'%']);
while (my ($id) = $sth->array) {
$db->write("delete from assetIndex where assetId=?",[$id]);
}
$db->write("update asset set state='trash-limbo' where lineage like ?",[$self->get("lineage").'%']);
$db->write("update asset set state='trash', stateChangedBy=?, stateChanged=? where assetId=?",[$self->session->user->userId, $self->session->datetime->time(), $self->getId]);
$db->commit;
$self->{_properties}{state} = "trash";
$self->updateHistory("trashed");
$self->purgeCache;
# Trash any shortcuts to this asset
my $shortcuts
= WebGUI::Asset::Shortcut->getShortcutsForAssetId($self->session, $self->getId, { returnObjects => 1});
for my $shortcut ( @$shortcuts ) {
$shortcut->trash;
}
# Raw database work is more efficient than $asset->update
my $db = $self->session->db;
$db->beginTransaction;
my $sth = $db->read("select assetId from asset where lineage like ?",[$self->get("lineage").'%']);
while (my ($id) = $sth->array) {
$db->write("delete from assetIndex where assetId=?",[$id]);
}
$db->write("update asset set state='trash-limbo' where lineage like ?",[$self->get("lineage").'%']);
$db->write("update asset set state='trash', stateChangedBy=?, stateChanged=? where assetId=?",[$self->session->user->userId, $self->session->datetime->time(), $self->getId]);
$db->commit;
# Update ourselves since we didn't use update()
$self->{_properties}{state} = "trash";
$self->updateHistory("trashed");
$self->purgeCache;
}
require WebGUI::Workflow::Activity::DeleteExportedFiles;

View file

@ -0,0 +1,74 @@
#-------------------------------------------------------------------
# WebGUI is Copyright 2001-2007 Plain Black Corporation.
#-------------------------------------------------------------------
# Please read the legal notices (docs/legal.txt) and the license
# (docs/license.txt) that came with this distribution before using
# this software.
#-------------------------------------------------------------------
# http://www.plainblack.com info@plainblack.com
#-------------------------------------------------------------------
use FindBin;
use strict;
use lib "$FindBin::Bin/../../lib";
## The goal of this test is to test the creation and deletion of shortcut assets
use WebGUI::Test;
use WebGUI::Session;
use Test::More;
use WebGUI::Asset::Snippet;
#----------------------------------------------------------------------------
# Init
my $session = WebGUI::Test->session;
my $node = WebGUI::Asset->getImportNode($session);
my $versionTag = WebGUI::VersionTag->getWorking($session);
$versionTag->set({name=>"Shortcut Test"});
# Make a snippet to shortcut
my $snippet
= $node->addChild({
className => "WebGUI::Asset::Snippet",
});
#----------------------------------------------------------------------------
# Cleanup
END {
$versionTag->rollback();
}
#----------------------------------------------------------------------------
# Tests
plan tests => 2;
#----------------------------------------------------------------------------
# Test module compiles okay
# plan tests => 0
BEGIN { use_ok("WebGUI::Asset::Shortcut"); }
#----------------------------------------------------------------------------
# Test creating a shortcut to snippet
# plan tests => 2
my $shortcut
= $node->addChild({
className => "WebGUI::Asset::Shortcut",
shortcutToAssetId => $snippet->getId,
});
isa_ok(
$shortcut, "WebGUI::Asset::Shortcut",
);
isa_ok(
$shortcut, "WebGUI::Asset",
);
#----------------------------------------------------------------------------
# Test deleting a shortcut
# plan tests =>
TODO: {
local $TODO = "Test deleting a shortcut.";
}