Skip to content

Commit

Permalink
Merge pull request silverstripe#10 from nyeholt/feature/rollback
Browse files Browse the repository at this point in the history
NEW Rollback elements on page rollback
  • Loading branch information
wilr committed Nov 24, 2015
2 parents 397ba43 + ec887fd commit 2c8c41a
Showing 1 changed file with 46 additions and 13 deletions.
59 changes: 46 additions & 13 deletions code/extensions/ElementPageExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,25 +116,28 @@ public function onBeforeWrite() {
}
}

$elements = $this->owner->ElementArea();
if ($this->owner->hasMethod('ElementArea')) {
$elements = $this->owner->ElementArea();

if(!$elements->isInDB()) {
$elements->write();
$this->owner->ElementAreaID = $elements->ID;
}
else {
// Copy widgets content to Content to enable search
$searchableContent = array();
if(!$elements->isInDB()) {
$elements->write();
$this->owner->ElementAreaID = $elements->ID;
}
else {
// Copy widgets content to Content to enable search
$searchableContent = array();

foreach ($elements->Items() as $element) {
foreach ($elements->Items() as $element) {

if($element->config()->exclude_from_content) continue;
array_push($searchableContent, strip_tags($element->Content(), '<a>'));
if($element->config()->exclude_from_content) continue;
array_push($searchableContent, strip_tags($element->Content(), '<a>'));

}
}

$this->owner->Content = implode(' ', $searchableContent);
$this->owner->Content = implode(' ', $searchableContent);
}
}


// set theme_enabled back to what it was
Config::inst()->update('SSViewer', 'theme_enabled', $originalThemeEnabled);
Expand Down Expand Up @@ -208,4 +211,34 @@ public function onAfterPublish() {
}
}
}

/**
* Roll back all changes if the parent page has a rollback event
*
* Only do rollback if it's the 'cancel draft changes' rollback, not a specific version
* rollback.
*
* @param string $version
* @return null
*/
public function onBeforeRollback($version) {
if ($version !== 'Live') {
// we don't yet have a smart way of rolling back to a specific version
return;
}
if($id = $this->owner->ElementAreaID) {
$widgets = Versioned::get_by_stage('BaseElement', 'Live', "ParentID = '$id'");
$staged = array();

foreach($widgets as $widget) {
$staged[] = $widget->ID;

$widget->invokeWithExtensions('onBeforeRollback', $widget);

$widget->publish("Live", "Stage", false);

$widget->invokeWithExtensions('onAfterRollback', $widget);
}
}
}
}

0 comments on commit 2c8c41a

Please sign in to comment.