Skip to content

Commit

Permalink
Added method duplicate() to DBField, Blocks and ContentElements
Browse files Browse the repository at this point in the history
  • Loading branch information
Zauberfisch committed Sep 21, 2017
1 parent 0506d36 commit a0fd668
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Model/Block/AbstractBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ public static function getPageBuilderEditPopupFields() {
return new \FieldList([]);
}

public function duplicate() {
return clone $this;
}

public function onAfterCreate() {
$this->setWidthDesktop(12);
$this->setWidthTablet(12);
Expand Down
14 changes: 14 additions & 0 deletions src/Model/Block/ContentElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function setContentElement($contentElement) {
$this->_contentElement = $contentElement;
if ($contentElement->hasField('ID') && $contentElement->ID) {
$this->setContentElementID($contentElement->ID);
$contentElement->setBlock($this);
}
return $this;
}
Expand All @@ -37,6 +38,7 @@ public function getContentElement() {
$obj = \PageBuilder_Model_ContentElement_AbstractContentElement::get()->byID($this->getContentElementID());
if ($obj && $obj->exists()) {
$this->_contentElement = $obj;
$obj->setBlock($this);
}
}
return $this->_contentElement;
Expand Down Expand Up @@ -84,6 +86,18 @@ public static function get_create_options() {
return $options;
}

public function duplicate() {
/** @var ContentElement $new */
$new = parent::duplicate();
$element = $this->getContentElement();
if ($element) {
$new->setContentElement($element->duplicate());
} else {
$new->setContentElementID(0);
}
return $new;
}

public function getPageBuilderFields($prefix, $pageBuilder, $blockPosition = 0, $parent = null) {
$return = parent::getPageBuilderFields($prefix, $pageBuilder, $blockPosition, $parent);
$obj = $this->getContentElement();
Expand Down
10 changes: 10 additions & 0 deletions src/Model/Block/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,14 @@ public function BlocksForTemplate() {
$this->extend('updateBlocksForTemplate', $blocks);
return new \ArrayList($blocks);
}

public function duplicate() {
$new = parent::duplicate();
$blocks = [];
foreach ($this->getBlocks() as $block) {
$blocks[] = $block->duplicate();
}
$this->setBlocks(new ArrayList($blocks));
return $new;
}
}
18 changes: 18 additions & 0 deletions src/Model/ContentElement/AbstractContentElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
*/
class PageBuilder_Model_ContentElement_AbstractContentElement extends \PersistentDataObject_Model_DataObject {
//class AbstractContentElement extends \DataObject {
protected $block;

public function getPageBuilderPopupFields() {
$fields = new \FieldList();
$this->extend('updatePageBuilderPopupFields', $fields);
Expand Down Expand Up @@ -49,4 +51,20 @@ public function i18n_singular_name() {
$class = $class[count($class) - 1];
return _t("{$this->class}.SINGULARNAME", FormField::name_to_label($class));
}

/**
* @param \zauberfisch\PageBuilder\Model\Block\ContentElement $block
* @return PageBuilder_Model_ContentElement_AbstractContentElement
*/
public function setBlock($block) {
$this->block = $block;
return $this;
}

/**
* @return \zauberfisch\PageBuilder\Model\Block\ContentElement
*/
public function getBlock() {
return $this->block;
}
}
6 changes: 6 additions & 0 deletions src/Model/DBField.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,10 @@ class DBField extends DataObjectField {
public function nullValue() {
return new Base();
}

public function duplicate() {
$new = new static();
$new->setValue($this->getValue()->duplicate());
return $new;
}
}

0 comments on commit a0fd668

Please sign in to comment.