Source of file SyncroableExtension.php
Size: 2,150 Bytes - Last Modified: 2021-12-23T10:35:22+00:00
/var/www/docs.ssmods.com/process/src/code/extensions/SyncroableExtension.php
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 | <?php /** * Add this extension to items that you want to allow other nodes to sync down * * You will probably want to * * ALTER TABLE `DataObjectThisIsAppliedTo` ADD INDEX ( `LastEditedUTC` ) ; * ALTER TABLE `DataObjectThisIsAppliedTo` ADD INDEX ( `UpdatedUTC` ) ; * * depending on whether you're doing UpdatedUTC updates or LastEditedUTC updates * * @author marcus@silverstripe.com.au * @license BSD License http://silverstripe.org/bsd-license/ */ class SyncroableExtension extends DataObjectDecorator { public function extraStatics() { return array( 'db' => array( 'MasterNode' => 'Varchar(128)', 'ContentID' => 'Varchar(128)', 'CreatedUTC' => 'SS_Datetime', // create time on master node 'LastEditedUTC' => 'SS_Datetime', // utc last edited time on master node 'UpdatedUTC' => 'SS_Datetime', // utc last edited time on any node 'OriginalID' => 'Int', ) ); } public function onBeforeWrite() { $config = SiteConfig::current_site_config(); if (!$this->owner->MasterNode) { $this->owner->MasterNode = $config->getSyncroIdentifier(); } if (!$this->owner->ContentID) { $this->owner->ContentID = Uuid::get(); } $nowUTC = gmdate('Y-m-d H:i:s'); $this->owner->UpdatedUTC = $nowUTC; // if we're updating on the master node, change the lasteditedUTC and created UTC if needbe if ($this->owner->MasterNode == $config->getSyncroIdentifier()) { $this->owner->LastEditedUTC = $nowUTC; if (!$this->owner->CreatedUTC) { $this->owner->CreatedUTC = $nowUTC; } } } public function onAfterWrite() { if (!$this->owner->OriginalID) { $this->owner->OriginalID = $this->owner->ID; $this->owner->write(); } } public function updateFrontendFields(FieldSet $fields) { $fields->removeByName('MasterNode'); $fields->removeByName('ContentID'); $fields->removeByName('OriginalID'); $fields->removeByName('LastEditedUTC'); } public function onAfterDelete() { parent::onAfterDelete(); if ($this->owner->MasterNode == SiteConfig::current_site_config()->SystemID) { SyncroDelete::record_delete($this->owner); } } } |