diff --git a/core/model/ComponentSet.php b/core/model/ComponentSet.php index 9ea77ce4c..bc6833dd5 100755 --- a/core/model/ComponentSet.php +++ b/core/model/ComponentSet.php @@ -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. diff --git a/core/model/SiteTree.php b/core/model/SiteTree.php index 187608561..5a434a4d7 100644 --- a/core/model/SiteTree.php +++ b/core/model/SiteTree.php @@ -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); } diff --git a/core/model/Versioned.php b/core/model/Versioned.php index 82a825896..8beb69296 100755 --- a/core/model/Versioned.php +++ b/core/model/Versioned.php @@ -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\""); + } } } diff --git a/forms/ComplexTableField.php b/forms/ComplexTableField.php index c243c18f2..5f2e50e82 100755 --- a/forms/ComplexTableField.php +++ b/forms/ComplexTableField.php @@ -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; diff --git a/tests/forms/ComplexTableFieldTest.php b/tests/forms/ComplexTableFieldTest.php index 3a9d6e854..4c01a52f6 100644 --- a/tests/forms/ComplexTableFieldTest.php +++ b/tests/forms/ComplexTableFieldTest.php @@ -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() { diff --git a/tests/model/VirtualPageTest.php b/tests/model/VirtualPageTest.php new file mode 100644 index 000000000..e110c2687 --- /dev/null +++ b/tests/model/VirtualPageTest.php @@ -0,0 +1,50 @@ +objFromFixture('Page', 'master'); + $master->Title = "New title"; + $master->Content = "
New content
"; + $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("New content
", $vp1->Content); + $this->assertEquals("New content
", $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 = "New content
"; + $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("New content
", $vp1->Content); + $this->assertEquals("New content
", $vp2->Content); + Versioned::reading_stage("Stage"); + } + +} \ No newline at end of file diff --git a/tests/model/VirtualPageTest.yml b/tests/model/VirtualPageTest.yml new file mode 100644 index 000000000..26ef77f85 --- /dev/null +++ b/tests/model/VirtualPageTest.yml @@ -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 + \ No newline at end of file