Source of file DMSDocumentExtension.php
Size: 2,576 Bytes - Last Modified: 2021-12-23T10:55:49+00:00
/var/www/docs.ssmods.com/process/src/code/extensions/DMSDocumentExtension.php
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 | <?php class DMSDocumentExtension extends DataExtension { public static $db = array( 'ShowTagsFrontend' => 'Boolean(0)', ); public function updateCMSFields(FieldList $fields) { if ($this->owner->ID) { $srcTags = function () { $tags = array(); foreach (DMSTag::get() as $t) { $tags[$t->ID] = $t->Category . ($t->isValueDefined() ? ' -> ' . $t->Value : ''); } return $tags; }; $selectTags = ListboxField::create( 'DocumentTags', _t('DMSDocumentExtension.Tags', 'Tags'), $srcTags(), implode(',', $this->owner->Tags()->column()), null, true )->useAddNew('DMSTag', $srcTags, FieldList::create( TextField::create('Category', _t('DMSDocumentExtension.Category', 'Category *')), TextField::create('Value', _t('DMSDocumentExtension.Value', 'Value')), HiddenField::create('MultiValue', null, 1) )); $fields->insertAfter($selectTags, 'Description'); $fields->insertAfter(CheckboxField::create('ShowTagsFrontend', _t('DMSDocumentExtension.ShowTagsFrontend', 'Show document tags in frontend?')), 'DocumentTags'); } } public function onBeforeWrite() { $changedFields = $this->owner->getChangedFields(false, 1); if (array_key_exists("DocumentTags", $changedFields)) { $currentTags = explode(',', $this->owner->getField('DocumentTags')); $oldTags = DMSTag::get() ->innerJoin("DMSDocument_Tags", "\"DMSDocument_Tags\".\"DMSTagID\" = \"DMSTag\".\"ID\" AND \"DMSDocument_Tags\".\"DMSDocumentID\" = " . $this->owner->ID)->column(); // delete the tags foreach (array_diff($oldTags, $currentTags) as $idTag) { $tag = DMSTag::get()->byID($idTag); if ($tag) { $this->owner->removeTag($tag->Category, $tag->Value ? $tag->Value : null); } } // add the tags foreach (array_diff($currentTags, $oldTags) as $idTag) { $tag = DMSTag::get()->byID($idTag); if ($tag) { $tag->Documents()->add($this->owner); } } } } public function onBeforeDelete() { $this->owner->removeAllTags(); } } |