/** * The process to automatically consolidate existing and configuration defined tag types, executed on project build. */ public function requireDefaultRecords() { parent::requireDefaultRecords(); // Retrieve existing and configuration defined tag types that have not been consolidated. foreach ($this->service->getFusionTagTypes() as $type => $field) { if (($tags = $type::get()->filter('FusionTagID', 0)) && $tags->exists()) { foreach ($tags as $tag) { // Determine whether there's an existing fusion tag. if (!($existing = FusionTag::get()->filter('Title', $tag->{$field})->first())) { // There is no fusion tag, therefore instantiate one using the current tag. $fusion = FusionTag::create(); $fusion->Title = $tag->{$field}; $fusion->TagTypes = serialize(array($tag->ClassName => $tag->ClassName)); $fusion->write(); $fusionID = $fusion->ID; } else { // There is a fusion tag, therefore append the current tag type. $types = unserialize($existing->TagTypes); $types[$tag->ClassName] = $tag->ClassName; $existing->TagTypes = serialize($types); $existing->write(); $fusionID = $existing->ID; } // Update the current tag to point to this. $tag->FusionTagID = $fusionID; $tag->write(); DB::alteration_message("\"{$tag->{$field}}\" Fusion Tag", 'created'); } } } }
/** * Update the page filtering, allowing CMS searchable content tagging. */ public function updateSearchForm($form) { // Update the page filtering, allowing multiple tags. Requirements::javascript(FUSION_PATH . '/javascript/fusion.js'); // Instantiate a field containing the existing tags. $form->Fields()->insertBefore(ListboxField::create('q[Tagging]', 'Tags', FusionTag::get()->map('Title', 'Title')->toArray(), ($filtering = $this->owner->getRequest()->getVar('q')) && isset($filtering['Tagging']) ? $filtering['Tagging'] : array(), null, true), 'q[Term]'); // Allow extension. $this->owner->extend('updateCMSMainTaggingExtensionSearchForm', $form); }
/** * Display the appropriate tagging field. */ public function updateCMSFields(FieldList $fields) { $fields->removeByName('FusionTags'); // Determine whether consolidated tags are found in the existing relationships. $types = array(); foreach (singleton('FusionService')->getFusionTagTypes() as $type => $field) { $types[$type] = $type; } $types = array_intersect($this->owner->many_many(), $types); if (empty($types)) { // There are no consolidated tags found, therefore instantiate a tagging field. $fields->addFieldToTab('Root.Tagging', ListboxField::create('FusionTags', 'Tags', FusionTag::get()->map()->toArray())->setMultiple(true)); } // Allow extension. $this->owner->extend('updateTaggingExtensionCMSFields', $fields); }
/** * 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(); }
/** * The process to automatically consolidate existing and configuration defined tag types, executed on project build. */ public function requireDefaultRecords() { parent::requireDefaultRecords(); // Retrieve existing and configuration defined tag types that have not been consolidated. $wrapped = array(); foreach ($this->service->getFusionTagTypes() as $type => $field) { if (($tags = $type::get()->filter('FusionTagID', 0)) && $tags->exists()) { foreach ($tags as $tag) { // Determine whether there's an existing fusion tag. if (!($existing = FusionTag::get()->filter('Title', $tag->{$field})->first())) { // There is no fusion tag, therefore instantiate one using the current tag. $fusion = FusionTag::create(); $fusion->Title = $tag->{$field}; $fusion->TagTypes = serialize(array($tag->ClassName => $tag->ClassName)); $fusion->write(); $fusionID = $fusion->ID; DB::alteration_message("\"{$tag->{$field}}\" Fusion Tag", 'created'); } else { // There is a fusion tag, therefore append the current tag type. $types = unserialize($existing->TagTypes); $types[$tag->ClassName] = $tag->ClassName; $existing->TagTypes = serialize($types); $existing->write(); $fusionID = $existing->ID; } // Update the current tag to point to this. $tag->FusionTagID = $fusionID; $tag->write(); } } // The existing and configuration defined tag types need to be wrapped for serialised partial matching. $wrapped[] = "\"{$type}\""; } // Determine whether tag type exclusions have caused any fusion tags to become redundant. $tags = FusionTag::get()->where('TagTypes IS NOT NULL'); if (count($wrapped)) { // Determine the fusion tags that only contain tag type exclusions. $tags = $tags->exclude('TagTypes:PartialMatch', $wrapped); } if ($tags->exists()) { // Determine any data objects with the tagging extension. $objects = array(); $configuration = Config::inst(); $classes = ClassInfo::subclassesFor('DataObject'); unset($classes['DataObject']); foreach ($classes as $class) { // Determine the specific data object extensions. $extensions = $configuration->get($class, 'extensions', Config::UNINHERITED); if (is_array($extensions) && in_array('TaggingExtension', $extensions)) { $objects[] = $class; } } // Determine whether these fusion tags are now redundant. $mode = Versioned::get_reading_mode(); Versioned::reading_stage('Stage'); $types = array(); $where = array(); foreach ($tags as $tag) { // Determine whether this fusion tag is being used. $ID = $tag->ID; foreach ($objects as $object) { if ($object::get()->filter('FusionTags.ID', $ID)->exists()) { // This fusion tag is being used, so keep it. continue 2; } } // These fusion tags are now redundant. $types = array_merge($types, unserialize($tag->TagTypes)); $where[] = array('FusionTagID' => $ID); $tag->delete(); DB::alteration_message("\"{$tag->Title}\" Fusion Tag", 'deleted'); } Versioned::set_reading_mode($mode); // The tag type exclusions need updating to reflect this. foreach ($types as $exclusion) { $query = new SQLUpdate($exclusion, array('FusionTagID' => 0), $where); $query->useDisjunction(); $query->execute(); } } }