Skip to content

Commit

Permalink
Make existing page detection aware of ParentID
Browse files Browse the repository at this point in the history
The existing logic moved existing pages around into the imported
structure if they were matching the same title.
  • Loading branch information
chillu committed Dec 18, 2014
1 parent 831eb06 commit df512f0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
15 changes: 9 additions & 6 deletions code/SiteTreeImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,20 @@ function bulkimport($data, $form) {
// Allow custom classes based on meta data
$className = (array_key_exists('ClassName', $json)) ? $json['ClassName'] : 'Page';

// Try to find an existing page, or create a new one
$page = Page::get()->find('URLSegment', $url);
if(!$page) $page = new $className();

// If we've got too many tabs, then outdent until we find a page to attach to.
while(!isset($parentRefs[$numTabs-1]) && $numTabs > 0) $numTabs--;

// Set parent based on parentRefs;
if($numTabs > 0) $page->ParentID = $parentRefs[$numTabs-1];
$parentID = ($numTabs > 0) ? $parentRefs[$numTabs-1] : 0;

// Try to find an existing page, or create a new one
$page = Page::get()->filter(array(
'URLSegment' => $url,
'ParentID' => $parentID
))->First();
if(!$page) $page = new $className();

// Apply any meta data properties to the page
$page->ParentID = $parentID;
$page->Title = $title;
$page->URLSegment = $url;
if($json) $page->update($json);
Expand Down
27 changes: 27 additions & 0 deletions tests/SiteTreeImporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,33 @@ function testImportWithJSON() {
$this->assertEquals($child2_2->ClassName, 'SiteTreeImporterTest_TestPage');
}

function testDetectsDuplicatesByParent() {
$page = new Page();
$page->Title = 'MyPage';
$page->URLSegment = 'mypage';
$page->write();
$page->publish('Stage', 'Live');

// Import page with same name but a different hierarchy
$data = <<<YML
Parent
MyPage
YML;
$this->import($data);

$pages = Page::get()->filter('Title', 'MyPage')->sort('Created');

$existingPage = $pages->filter('ID', $page->ID)->First();
$this->assertNotNull($existingPage);
$this->assertEquals($existingPage->Title, 'MyPage');
$this->assertEquals((int)$existingPage->ParentID, (int)$page->ParentID);

$newPage = $pages->exclude('ID', $page->ID)->First();
$this->assertNotNull($newPage);
$this->assertEquals($newPage->Title, 'MyPage');
$this->assertNotEquals((int)$newPage->ParentID, (int)$page->ParentID);
}

protected function import($yml, $data = null) {
$data = $data ? $data : array();
$data['SourceFile'] = array();
Expand Down

0 comments on commit df512f0

Please sign in to comment.