Merge pull request #1519 from open-sausages/pulls/4.0/config-table-name

Support custom table_name and DataObjectSchema
This commit is contained in:
Hamish Friedlander 2016-06-14 16:01:07 +12:00 committed by GitHub
commit 6d5d7c0e11
4 changed files with 34 additions and 43 deletions

View File

@ -1,4 +1,6 @@
<?php
use SilverStripe\Model\FieldType\DBField;
/**
* Utility class representing links to different views of a record
* for CMS authors, usually for {@link SiteTree} objects with "stage" and "live" links.
@ -192,13 +194,9 @@ abstract class SilverStripeNavigatorItem extends ViewableData {
if(!$this->record->hasExtension('Versioned')) return false;
if(!isset($this->record->_cached_isArchived)) {
$baseTable = ClassInfo::baseDataClass($this->record->class);
$currentDraft = Versioned::get_one_by_stage($baseTable, 'Stage', array(
"\"$baseTable\".\"ID\"" => $this->record->ID
));
$currentLive = Versioned::get_one_by_stage($baseTable, 'Live', array(
"\"$baseTable\".\"ID\"" => $this->record->ID
));
$baseClass = $this->record->baseClass();
$currentDraft = Versioned::get_by_stage($baseClass, Versioned::DRAFT)->byID($this->record->ID);
$currentLive = Versioned::get_by_stage($baseClass, Versioned::LIVE)->byID($this->record->ID);
$this->record->_cached_isArchived = (
(!$currentDraft || ($currentDraft && $this->record->Version != $currentDraft->Version))
@ -300,10 +298,8 @@ class SilverStripeNavigatorItem_StageLink extends SilverStripeNavigatorItem {
}
protected function getDraftPage() {
$baseTable = ClassInfo::baseDataClass($this->record->class);
return Versioned::get_one_by_stage($baseTable, 'Stage', array(
"\"$baseTable\".\"ID\"" => $this->record->ID
));
$baseClass = $this->record->baseClass();
return Versioned::get_by_stage($baseClass, Versioned::DRAFT)->byID($this->record->ID);
}
}
@ -352,10 +348,8 @@ class SilverStripeNavigatorItem_LiveLink extends SilverStripeNavigatorItem {
}
protected function getLivePage() {
$baseTable = ClassInfo::baseDataClass($this->record->class);
return Versioned::get_one_by_stage($baseTable, 'Live', array(
"\"$baseTable\".\"ID\"" => $this->record->ID
));
$baseClass = $this->record->baseClass();
return Versioned::get_by_stage($baseClass, Versioned::LIVE)->byID($this->record->ID);
}
}
@ -368,8 +362,8 @@ class SilverStripeNavigatorItem_ArchiveLink extends SilverStripeNavigatorItem {
private static $priority = 40;
public function getHTML() {
$this->recordLink = $this->record->AbsoluteLink();
return "<a class=\"ss-ui-button". ($this->isActive() ? ' current' : '') ."\" href=\"$this->recordLink?archiveDate={$this->record->LastEdited}\" target=\"_blank\">". _t('ContentController.ARCHIVEDSITE', 'Preview version') ."</a>";
$this->recordLink = $this->record->AbsoluteLink();
return "<a class=\"ss-ui-button". ($this->isActive() ? ' current' : '') ."\" href=\"$this->recordLink?archiveDate={$this->record->LastEdited}\" target=\"_blank\">". _t('ContentController.ARCHIVEDSITE', 'Preview version') ."</a>";
}
public function getTitle() {

View File

@ -482,7 +482,7 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
* @return string
*/
public function getAbsoluteLiveLink($includeStageEqualsLive = true) {
$oldStage = Versioned::get_stage();
$oldReadingMode = Versioned::get_reading_mode();
Versioned::set_stage(Versioned::LIVE);
$live = Versioned::get_one_by_stage('SiteTree', Versioned::LIVE, array(
'"SiteTree"."ID"' => $this->ID
@ -494,7 +494,7 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
$link = null;
}
Versioned::set_stage($oldStage);
Versioned::set_reading_mode($oldReadingMode);
return $link;
}
@ -2292,7 +2292,7 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
if(method_exists($conn, 'allowPrimaryKeyEditing')) $conn->allowPrimaryKeyEditing('SiteTree', false);
}
$oldStage = Versioned::get_stage();
$oldReadingMode = Versioned::get_reading_mode();
Versioned::set_stage(Versioned::DRAFT);
$this->forceChange();
$this->write();
@ -2305,7 +2305,7 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
$page->write();
}
Versioned::set_stage($oldStage);
Versioned::set_reading_mode($oldReadingMode);
$this->invokeWithExtensions('onAfterRestoreToStage', $this);

View File

@ -1,4 +1,6 @@
<?php
use SilverStripe\Model\FieldType\DBDatetime;
/**
* Identify "orphaned" pages which point to a parent
* that no longer exists in a specific stage.
@ -85,23 +87,18 @@ in the other stage:<br />
if($orphans) foreach($orphans as $orphan) {
$latestVersion = Versioned::get_latest_version($this->orphanedSearchClass, $orphan->ID);
$latestAuthor = DataObject::get_by_id('Member', $latestVersion->AuthorID);
$orphanBaseClass = ClassInfo::baseDataClass($this->orphanedSearchClass);
$stageRecord = Versioned::get_one_by_stage(
$this->orphanedSearchClass,
'Stage',
array("\"$orphanBaseClass\".\"ID\"" => $orphan->ID)
);
$orphanBaseTable = DataObject::getSchema()->baseDataTable($this->orphanedSearchClass);
$liveRecord = Versioned::get_one_by_stage(
$this->orphanedSearchClass,
'Live',
array("\"$orphanBaseClass\".\"ID\"" => $orphan->ID)
array("\"$orphanBaseTable\".\"ID\"" => $orphan->ID)
);
$label = sprintf(
'<a href="admin/pages/edit/show/%d">%s</a> <small>(#%d, Last Modified Date: %s, Last Modifier: %s, %s)</small>',
$orphan->ID,
$orphan->Title,
$orphan->ID,
Date::create($orphan->LastEdited)->Nice(),
$orphan->dbObject('LastEdited')->Nice(),
($latestAuthor) ? $latestAuthor->Title : 'unknown',
($liveRecord) ? 'is published' : 'not published'
);
@ -124,7 +121,7 @@ in the other stage:<br />
_t('RemoveOrphanedPagesTask.UNSELECTALL', 'unselect all')
)
));
$fields->push(new OptionSetField(
$fields->push(new OptionsetField(
'OrphanOperation',
_t('RemoveOrphanedPagesTask.CHOOSEOPERATION', 'Choose operation:'),
array(
@ -183,6 +180,7 @@ in the other stage:<br />
if(!isset($data['OrphanIDs']) || !isset($data['OrphanOperation'])) return false;
$successIDs = null;
switch($data['OrphanOperation']) {
case 'remove':
$successIDs = $this->removeOrphans($data['OrphanIDs']);
@ -213,13 +211,13 @@ in the other stage:<br />
protected function removeOrphans($orphanIDs) {
$removedOrphans = array();
$orphanBaseClass = ClassInfo::baseDataClass($this->orphanedSearchClass);
$orphanBaseTable = DataObject::getSchema()->baseDataTable($this->orphanedSearchClass);
foreach($orphanIDs as $id) {
/** @var SiteTree $stageRecord */
$stageRecord = Versioned::get_one_by_stage(
$this->orphanedSearchClass,
'Stage',
array("\"$orphanBaseClass\".\"ID\"" => $id)
Versioned::DRAFT,
array("\"$orphanBaseTable\".\"ID\"" => $id)
);
if($stageRecord) {
$removedOrphans[$stageRecord->ID] = sprintf('Removed %s (#%d) from Stage', $stageRecord->Title, $stageRecord->ID);
@ -230,8 +228,8 @@ in the other stage:<br />
/** @var SiteTree $liveRecord */
$liveRecord = Versioned::get_one_by_stage(
$this->orphanedSearchClass,
'Live',
array("\"$orphanBaseClass\".\"ID\"" => $id)
Versioned::LIVE,
array("\"$orphanBaseTable\".\"ID\"" => $id)
);
if($liveRecord) {
$removedOrphans[$liveRecord->ID] = sprintf('Removed %s (#%d) from Live', $liveRecord->Title, $liveRecord->ID);
@ -257,12 +255,12 @@ in the other stage:<br />
$holder->write();
$removedOrphans = array();
$orphanBaseClass = ClassInfo::baseDataClass($this->orphanedSearchClass);
$orphanBaseTable = DataObject::getSchema()->baseDataTable($this->orphanedSearchClass);
foreach($orphanIDs as $id) {
$stageRecord = Versioned::get_one_by_stage(
$this->orphanedSearchClass,
'Stage',
array("\"$orphanBaseClass\".\"ID\"" => $id)
array("\"$orphanBaseTable\".\"ID\"" => $id)
);
if($stageRecord) {
$removedOrphans[$stageRecord->ID] = sprintf('Rebased %s (#%d)', $stageRecord->Title, $stageRecord->ID);
@ -277,7 +275,7 @@ in the other stage:<br />
$liveRecord = Versioned::get_one_by_stage(
$this->orphanedSearchClass,
'Live',
array("\"$orphanBaseClass\".\"ID\"" => $id)
array("\"$orphanBaseTable\".\"ID\"" => $id)
);
if($liveRecord) {
$removedOrphans[$liveRecord->ID] = sprintf('Rebased %s (#%d)', $liveRecord->Title, $liveRecord->ID);
@ -318,9 +316,8 @@ in the other stage:<br />
$orphans = new ArrayList();
foreach(array(Versioned::DRAFT, Versioned::LIVE) as $stage) {
$joinByStage = $join;
$table = $class;
$table .= ($stage == 'Live') ? '_Live' : '';
$table = DataObject::getSchema()->tableName($class);
$table .= ($stage == Versioned::LIVE) ? '_Live' : '';
$stageOrphans = Versioned::get_by_stage(
$class,
$stage,

View File

@ -168,8 +168,8 @@ class CMSSiteTreeFilterTest extends SapphireTest {
$deletedPageID = $deletedPage->ID;
// Can't use straight $blah->delete() as that blows it away completely and test fails
$deletedPage->deleteFromStage('Live');
$deletedPage->deleteFromStage('Draft');
$deletedPage->deleteFromStage(Versioned::LIVE);
$deletedPage->deleteFromStage(Versioned::DRAFT);
$checkParentExists = Versioned::get_latest_version('SiteTree', $deletedPageID);
// Check deleted page is included