Source of file FusionExtension.php
Size: 4,190 Bytes - Last Modified: 2021-12-23T10:06:08+00:00
/var/www/docs.ssmods.com/process/src/code/extensions/FusionExtension.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 | <?php /** * This extension will automatically be applied to existing and configuration defined tag types, and will help consolidate these into fusion tags. * @author Nathan Glasl <nathan@symbiote.com.au> */ class FusionExtension extends DataExtension { private static $has_one = array( 'FusionTag' => 'FusionTag' ); /** * Restrict access when deleting tags. */ public function canDelete($member) { return false; } /** * Hide the fusion tag. */ public function updateCMSFields(FieldList $fields) { $fields->removeByName('FusionTagID'); } /** * Confirm that the tag has been given a title and doesn't already exist. */ public function validate(ValidationResult $result) { // Determine the field to use, based on the configuration defined tag types. $validate = 'Title'; $class = $this->owner->ClassName; foreach(Config::inst()->get('FusionService', 'custom_tag_types') as $type => $field) { if($type === $class) { $validate = $field; } } // Confirm that the tag has been given a title and doesn't already exist. if($result->valid() && !$this->owner->$validate) { $result->error("\"{$validate}\" required!"); } // Allow extension. $this->owner->extend('validateFusionExtension', $result); return $result; } /** * Update the fusion tag to reflect the change. */ public function onAfterWrite() { parent::onAfterWrite(); // Determine the field to use, based on the configuration defined tag types. $write = 'Title'; $class = $this->owner->ClassName; foreach(Config::inst()->get('FusionService', 'custom_tag_types') as $type => $field) { if($type === $class) { $write = $field; } } // Determine whether there's an existing fusion tag. $changed = $this->owner->getChangedFields(); $existing = FusionTag::get()->filter('Title', $this->owner->$write)->first(); if(is_null($this->owner->FusionTagID) && !$existing) { // There is no fusion tag, therefore instantiate one using this tag. $fusion = FusionTag::create(); $fusion->Title = $this->owner->$write; $fusion->TagTypes = serialize(array( $class => $class )); $fusion->write(); // Update this tag to point to the fusion tag. $this->owner->FusionTagID = $fusion->ID; $this->owner->write(); } else if(is_null($this->owner->FusionTagID) && $existing) { // There is a fusion tag, therefore append this tag type. $types = unserialize($existing->TagTypes); $types[$class] = $class; $existing->TagTypes = serialize($types); $existing->write(); // Update this tag to point to the fusion tag. $this->owner->FusionTagID = $existing->ID; $this->owner->write(); } else if(isset($changed[$write]) && !isset($changed['FusionTagID']) && $existing && ($existing->ID != $this->owner->FusionTagID)) { // Update the fusion tag to remove this tag type. $fusion = FusionTag::get()->byID($this->owner->FusionTagID); $types = unserialize($fusion->TagTypes); unset($types[$this->owner->ClassName]); $fusion->TagTypes = !empty($types) ? serialize($types) : null; $fusion->write(); // There is an existing fusion tag, therefore append this tag type. $types = unserialize($existing->TagTypes); $types[$class] = $class; $existing->TagTypes = serialize($types); $existing->write(); // Update this tag to point to the new fusion tag. $this->owner->FusionTagID = $existing->ID; $this->owner->write(); } // Determine whether this tag has been updated. else if(isset($changed[$write]) && !isset($changed['FusionTagID']) && ($existing = FusionTag::get()->byID($this->owner->FusionTagID))) { // There is an update, therefore update the existing fusion tag to reflect the change. $existing->Title = $changed[$write]['after']; $existing->write(); } } /** * Update the fusion tag to remove this tag type. */ public function onAfterDelete() { parent::onAfterDelete(); $fusion = FusionTag::get()->byID($this->owner->FusionTagID); $types = unserialize($fusion->TagTypes); unset($types[$this->owner->ClassName]); $fusion->TagTypes = !empty($types) ? serialize($types) : null; $fusion->write(); } } |