BUGFIX: Fixed image link rewriting in virtual pages. (from r89904)

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.4@96721 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Sam Minnee 2010-01-12 22:56:51 +00:00
parent f8f0759df9
commit f0173f5531
2 changed files with 52 additions and 3 deletions

View File

@ -115,7 +115,7 @@ class VirtualPage extends Page {
) {
$source = DataObject::get_one("SiteTree",sprintf('"SiteTree"."ID" = %d', $this->CopyContentFromID));
if($source) {
$this->copyFrom($source);
$this->copyFrom($source, false);
$this->URLSegment = $source->URLSegment . '-' . $this->ID;
}
}
@ -124,10 +124,25 @@ class VirtualPage extends Page {
parent::onBeforeWrite();
}
function onAfterWrite() {
parent::onAfterWrite();
// Don't do this stuff when we're publishing
if(!$this->extension_instances['Versioned']->migratingVersion) {
if(
$this->isChanged('CopyContentFromID')
&& $this->CopyContentFromID != 0
&& $this instanceof VirtualPage
) {
$this->updateImageTracking();
}
}
}
/**
* Ensure we have an up-to-date version of everything.
*/
function copyFrom($source) {
function copyFrom($source, $updateImageTracking = true) {
if($source) {
foreach($this->getVirtualFields() as $virtualField) {
$this->$virtualField = $source->$virtualField;
@ -138,9 +153,22 @@ class VirtualPage extends Page {
if($this->isChanged('CopyContentFromID')) {
$this->ShowInMenus = $source->ShowInMenus;
}
if($updateImageTracking) $this->updateImageTracking();
}
}
function updateImageTracking() {
// Doesn't work on unsaved records
if(!$this->ID) return;
// Remove CopyContentFrom() from the cache
unset($this->components['CopyContentFrom']);
// Update ImageTracking
$this->ImageTracking()->setByIdList($this->CopyContentFrom()->ImageTracking()->column('ID'));
}
/**
* Allow attributes on the master page to pass
* through to the virtual page

View File

@ -35,6 +35,27 @@ class FileLinkTrackingTest extends SapphireTest {
DB::query("SELECT \"Content\" FROM \"SiteTree\" WHERE \"ID\" = $page->ID")->value());
}
function testFileLinkRewritingOnVirtualPages() {
// Publish the source page
$page = $this->objFromFixture('Page', 'page1');
$this->assertTrue($page->doPublish());
// Create a virtual page from it, and publish that
$svp = new VirtualPage();
$svp->CopyContentFromID = $page->ID;
$svp->write();
$svp->doPublish();
// Rename the file
$file = $this->objFromFixture('File', 'file1');
$file->Name = 'renamed-test-file.pdf';
// Verify that the draft and publish virtual pages both have the corrected link
$this->assertContains('<img src="assets/renamed-test-file.pdf" />',
DB::query("SELECT \"Content\" FROM \"SiteTree\" WHERE \"ID\" = $svp->ID")->value());
$this->assertContains('<img src="assets/renamed-test-file.pdf" />',
DB::query("SELECT \"Content\" FROM \"SiteTree_Live\" WHERE \"ID\" = $svp->ID")->value());
}
}
?>