mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Merged from branches/2.3
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@75903 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
8ca264b0b0
commit
f4eecd4d0d
@ -60,41 +60,6 @@ class ComponentSet extends DataObjectSet {
|
||||
$this->joinField = $joinField;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the extra field data for a single row of the relationship
|
||||
* join table, given the known child ID.
|
||||
*
|
||||
* @todo This should return casted fields, like Enum, Varchar, Date
|
||||
* instead of just the raw value of the field.
|
||||
*
|
||||
* @param string $componentName The name of the component
|
||||
* @param int $childID The ID of the child for the relationship
|
||||
* @param string|null $fieldName To get a specific extra data field, specify it here
|
||||
* @return array|string Array of field => value or single string of value
|
||||
*/
|
||||
function getExtraData($componentName, $childID, $fieldName = null) {
|
||||
$ownerObj = $this->ownerObj;
|
||||
$parentField = $this->ownerClass . 'ID';
|
||||
$childField = ($this->childClass == $this->ownerClass) ? 'ChildID' : ($this->childClass . 'ID');
|
||||
|
||||
if(!$componentName) return false;
|
||||
|
||||
$extraFields = $ownerObj->many_many_extraFields($componentName);
|
||||
if(!$extraFields) return false;
|
||||
|
||||
if($fieldName && !empty($extraFields[$fieldName])) {
|
||||
$query = DB::query("SELECT $fieldName FROM {$this->tableName} WHERE $parentField = '{$this->ownerObj->ID}' AND $childField = '{$childID}'");
|
||||
return $query->value();
|
||||
} else {
|
||||
$fields = array();
|
||||
foreach($extraFields as $fieldName => $fieldSpec) {
|
||||
$query = DB::query("SELECT $fieldName FROM {$this->tableName} WHERE $parentField = '{$this->ownerObj->ID}' AND $childField = '{$childID}'");
|
||||
$fields[$fieldName] = $query->value();
|
||||
}
|
||||
return $fields;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an array of all the IDs in this component set, where the keys are the same as the
|
||||
* values.
|
||||
|
@ -1031,6 +1031,13 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
|
||||
// Need to flush cache to avoid outdated versionnumber references
|
||||
$this->flushCache();
|
||||
|
||||
// Update any virtual pages that might need updating
|
||||
$linkedPages = DataObject::get("VirtualPage", "CopyContentFromID = $this->ID");
|
||||
if($linkedPages) foreach($linkedPages as $page) {
|
||||
$page->copyFrom($page->CopyContentFrom());
|
||||
$page->write();
|
||||
}
|
||||
|
||||
parent::onAfterWrite();
|
||||
}
|
||||
|
||||
@ -1446,6 +1453,13 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
|
||||
AND \"SiteTree_Live\".\"ParentID\" = " . sprintf('%d', $this->ParentID) );
|
||||
}
|
||||
|
||||
// Publish any virtual pages that might need publishing
|
||||
$linkedPages = DataObject::get("VirtualPage", "CopyContentFromID = $this->ID");
|
||||
if($linkedPages) foreach($linkedPages as $page) {
|
||||
$page->copyFrom($page->CopyContentFrom());
|
||||
$page->doPublish();
|
||||
}
|
||||
|
||||
// Handle activities undertaken by decorators
|
||||
$this->extend('onAfterPublish', $original);
|
||||
}
|
||||
|
@ -95,13 +95,6 @@ class Versioned extends DataObjectDecorator {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Temporary table mapping each database record to its version on the given date.
|
||||
* Created by requireArchiveTempTable().
|
||||
* @var array
|
||||
*/
|
||||
protected static $createdArchiveTempTable = array();
|
||||
|
||||
/**
|
||||
* Create a temporary table mapping each database record to its version on the given date.
|
||||
* This is used by the versioning system to return database content on that date.
|
||||
@ -113,14 +106,20 @@ class Versioned extends DataObjectDecorator {
|
||||
if(!isset(self::$createdArchiveTempTable[$baseTable])) {
|
||||
self::$createdArchiveTempTable[$baseTable] = true;
|
||||
|
||||
DB::query("CREATE TEMPORARY TABLE \"_Archive$baseTable\" (
|
||||
DB::query("CREATE TEMPORARY TABLE IF NOT EXISTS \"_Archive$baseTable\" (
|
||||
\"RecordID\" INT NOT NULL PRIMARY KEY,
|
||||
\"Version\" INT NOT NULL
|
||||
)");
|
||||
DB::query("INSERT INTO \"_Archive$baseTable\"
|
||||
SELECT \"RecordID\", max(\"Version\") FROM \"{$baseTable}_versions\"
|
||||
WHERE \"LastEdited\" <= '$date'
|
||||
GROUP BY \"RecordID\"");
|
||||
|
||||
if(!DB::query("SELECT COUNT(*) FROM \"_Archive$baseTable\"")->value()) {
|
||||
if($date) $dateClause = "WHERE \"LastEdited\" <= '$date'";
|
||||
else $dateClause = "";
|
||||
|
||||
DB::query("INSERT INTO \"_Archive$baseTable\"
|
||||
SELECT \"RecordID\", max(Version) FROM \"{$baseTable}_versions\"
|
||||
$dateClause
|
||||
GROUP BY \"RecordID\"");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -513,28 +513,6 @@ JS;
|
||||
|
||||
$detailFields = $this->getCustomFieldsFor($childData);
|
||||
|
||||
// Loading of extra field values for editing an existing record
|
||||
if($manyManyRelationName) {
|
||||
$manyManyComponentSet = $parentClass->getManyManyComponents($manyManyRelationName);
|
||||
$extraFieldsSpec = $parentClass->many_many_extraFields($this->name);
|
||||
|
||||
$extraData = null;
|
||||
if($childData && $childData->ID) {
|
||||
$extraData = $manyManyComponentSet->getExtraData($manyManyRelationName, $childData->ID);
|
||||
}
|
||||
|
||||
if($extraFieldsSpec) foreach($extraFieldsSpec as $fieldName => $fieldSpec) {
|
||||
// @todo Create the proper DBField type instead of hardcoding Varchar
|
||||
$fieldObj = new Varchar($fieldName);
|
||||
|
||||
if(isset($extraData[$fieldName])) {
|
||||
$fieldObj->setValue($extraData[$fieldName]);
|
||||
}
|
||||
|
||||
$detailFields->addFieldToTab('Root.Main', $fieldObj->scaffoldFormField($fieldName));
|
||||
}
|
||||
}
|
||||
|
||||
if($hasManyRelationName && $childData->ID) {
|
||||
$hasManyComponentSet = $parentClass->getComponents($hasManyRelationName);
|
||||
}
|
||||
@ -650,16 +628,8 @@ JS;
|
||||
if(isset($data['ctf']['manyManyRelation'])) {
|
||||
$parentRecord = DataObject::get_by_id($data['ctf']['parentClass'], (int) $data['ctf']['sourceID']);
|
||||
$relationName = $data['ctf']['manyManyRelation'];
|
||||
$extraFields = array();
|
||||
|
||||
if(isset($data['ctf']['extraFields'])) {
|
||||
foreach($data['ctf']['extraFields'] as $field => $value) {
|
||||
$extraFields[$field] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
$componentSet = $parentRecord->getManyManyComponents($relationName);
|
||||
$componentSet->add($childData, $extraFields);
|
||||
$componentSet->add($childData);
|
||||
}
|
||||
|
||||
if(isset($data['ctf']['hasManyRelation'])) {
|
||||
@ -829,16 +799,8 @@ class ComplexTableField_ItemRequest extends RequestHandler {
|
||||
if(isset($data['ctf']['manyManyRelation'])) {
|
||||
$parentRecord = DataObject::get_by_id($data['ctf']['parentClass'], (int) $data['ctf']['sourceID']);
|
||||
$relationName = $data['ctf']['manyManyRelation'];
|
||||
$extraFields = array();
|
||||
|
||||
if(isset($data['ctf']['extraFields'])) {
|
||||
foreach($data['ctf']['extraFields'] as $field => $value) {
|
||||
$extraFields[$field] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
$componentSet = $parentRecord->getManyManyComponents($relationName);
|
||||
$componentSet->add($dataObject, $extraFields);
|
||||
$componentSet->add($dataObject);
|
||||
}
|
||||
|
||||
$referrer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : null;
|
||||
|
@ -41,29 +41,12 @@ class ComplexTableFieldTest extends FunctionalTest {
|
||||
$this->assertEquals($field->Items()->Count(), 2, 'There are 2 CTF items in the DataObjectSet');
|
||||
}
|
||||
|
||||
function testDetailFormDisplaysWithCorrectFields() {
|
||||
$field = $this->manyManyForm->dataFieldByName('Players');
|
||||
$detailForm = $field->add();
|
||||
$parser = new CSSContentParser($detailForm);
|
||||
|
||||
/* There is a field called "Name", which is a text input */
|
||||
$this->assertEquals(count($parser->getBySelector('#Name input')), 1, 'There is a field called "Name", which is a text input');
|
||||
|
||||
/* There is a field called "Role" - this field is the extra field for $many_many_extraFields */
|
||||
$this->assertEquals(count($parser->getBySelector('#Role input')), 1, 'There is a field called "Role" - this field is the extra field for $many_many_extraFields');
|
||||
}
|
||||
|
||||
function testAddingManyManyNewPlayerWithExtraData() {
|
||||
function testAddingManyManyNewPlayer() {
|
||||
$team = DataObject::get_one('ComplexTableFieldTest_Team', "Name = 'The Awesome People'");
|
||||
|
||||
$this->post('ComplexTableFieldTest_Controller/ManyManyForm/field/Players/AddForm', array(
|
||||
'Name' => 'Bobby Joe',
|
||||
'ctf' => array(
|
||||
'extraFields' => array(
|
||||
'Role' => 'Goalie',
|
||||
'Position' => 'Player',
|
||||
'DateJoined' => '2008-10-10'
|
||||
),
|
||||
'ClassName' => 'ComplexTableFieldTest_Player',
|
||||
'manyManyRelation' => 'Players',
|
||||
'parentClass' => 'ComplexTableFieldTest_Team',
|
||||
@ -82,17 +65,6 @@ class ComplexTableFieldTest extends FunctionalTest {
|
||||
|
||||
/* Automatic many-many relation was set correctly on the new player */
|
||||
$this->assertEquals($teams->Count(), 1, 'Automatic many-many relation was set correctly on the new player');
|
||||
|
||||
/* The extra fields have the correct value */
|
||||
$extraFields = $teams->getExtraData('Teams', $team->ID);
|
||||
|
||||
/* There are 3 extra fields */
|
||||
$this->assertEquals(count($extraFields), 3, 'There are 3 extra fields');
|
||||
|
||||
/* The three extra fields have the correct values */
|
||||
$this->assertEquals($extraFields['Role'], 'Goalie', 'The extra field "Role" has the correct value');
|
||||
$this->assertEquals($extraFields['Position'], 'Player', 'The extra field "Position" has the correct value');
|
||||
$this->assertEquals($extraFields['DateJoined'], '2008-10-10', 'The extra field "DateJoined" has the correct value');
|
||||
}
|
||||
|
||||
function testAddingHasManyData() {
|
||||
|
50
tests/model/VirtualPageTest.php
Normal file
50
tests/model/VirtualPageTest.php
Normal file
@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
class VirtualPageTest extends SapphireTest {
|
||||
static $fixture_file = 'sapphire/tests/model/VirtualPageTest.yml';
|
||||
|
||||
/**
|
||||
* Test that, after you update the source page of a virtual page, all the virtual pages
|
||||
* are updated
|
||||
*/
|
||||
function testEditingSourcePageUpdatesVirtualPages() {
|
||||
$master = $this->objFromFixture('Page', 'master');
|
||||
$master->Title = "New title";
|
||||
$master->Content = "<p>New content</p>";
|
||||
$master->write();
|
||||
|
||||
$vp1 = $this->objFromFixture('VirtualPage', 'vp1');
|
||||
$vp2 = $this->objFromFixture('VirtualPage', 'vp2');
|
||||
|
||||
$this->assertEquals("New title", $vp1->Title);
|
||||
$this->assertEquals("New title", $vp2->Title);
|
||||
$this->assertEquals("<p>New content</p>", $vp1->Content);
|
||||
$this->assertEquals("<p>New content</p>", $vp2->Content);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that, after you publish the source page of a virtual page, all the virtual pages
|
||||
* are published
|
||||
*/
|
||||
function testPublishingSourcePagePublishesVirtualPages() {
|
||||
$master = $this->objFromFixture('Page', 'master');
|
||||
$master->Title = "New title";
|
||||
$master->Content = "<p>New content</p>";
|
||||
$master->write();
|
||||
$master->doPublish();
|
||||
|
||||
Versioned::reading_stage("Live");
|
||||
$vp1 = DataObject::get_by_id("VirtualPage", $this->idFromFixture('VirtualPage', 'vp1'));
|
||||
$vp2 = DataObject::get_by_id("VirtualPage", $this->idFromFixture('VirtualPage', 'vp2'));
|
||||
|
||||
$this->assertNotNull($vp1);
|
||||
$this->assertNotNull($vp2);
|
||||
|
||||
$this->assertEquals("New title", $vp1->Title);
|
||||
$this->assertEquals("New title", $vp2->Title);
|
||||
$this->assertEquals("<p>New content</p>", $vp1->Content);
|
||||
$this->assertEquals("<p>New content</p>", $vp2->Content);
|
||||
Versioned::reading_stage("Stage");
|
||||
}
|
||||
|
||||
}
|
15
tests/model/VirtualPageTest.yml
Normal file
15
tests/model/VirtualPageTest.yml
Normal file
@ -0,0 +1,15 @@
|
||||
Page:
|
||||
master:
|
||||
Title: My Page
|
||||
master2:
|
||||
Title: My Other Page
|
||||
holder:
|
||||
Title: Sub-pages
|
||||
VirtualPage:
|
||||
vp1:
|
||||
CopyContentFrom: =>Page.master
|
||||
Parent: =>Page.holder
|
||||
vp2:
|
||||
CopyContentFrom: =>Page.master
|
||||
Parent: =>Page.holder
|
||||
|
Loading…
Reference in New Issue
Block a user