Source of file ComponentPageStructure.php
Size: 2,006 Bytes - Last Modified: 2021-12-23T10:07:48+00:00
/var/www/docs.ssmods.com/process/src/src/Model/ComponentPageStructure.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 | <?php namespace Symbiote\Frontend\LivingPage\Model; use SilverStripe\CMS\Model\SiteTree; use SilverStripe\Forms\TreeDropdownField; use SilverStripe\ORM\DataObject; /** * A template for a living page structure * * @author marcus */ class ComponentPageStructure extends DataObject { use ComponentTrait; private static $table_name = 'ComponentPageStructure'; private static $db = [ 'Title' => 'Varchar', 'Structure' => 'Text', ]; private static $has_one = [ 'SourcePage' => SiteTree::class ]; public function onBeforeWrite() { parent::onBeforeWrite(); if ($this->SourcePageID) { $page = $this->SourcePage(); if ($page && strlen($page->PageStructure)) { $components = json_decode($page->PageStructure, true); if (isset($components['content'])) { $newContent = []; foreach ($components['content'] as $key => $component) { $newContent[$key] = $this->cleanupComponent($component); } $components['content'] = $newContent; $this->Structure = json_encode($components, JSON_PRETTY_PRINT); } } $this->SourcePageID = 0; } } protected function cleanupComponent($component) { $newComponent = []; foreach ($component as $key => $val) { if ($key === 'id') { continue; } if (is_array($val)) { $val = $this->cleanupComponent($val); // $val = $newVal; } $newComponent[$key] = $val; } return $newComponent; } public function getCMSFields() { $fields = parent::getCMSFields(); $fields->replaceField('SourcePageID', TreeDropdownField::create('SourcePageID', 'Source page', SiteTree::class)); return $fields; } } |