From a6743b03f5cb1f830241856e8705f6bcef51217d Mon Sep 17 00:00:00 2001 From: Robbie Averill Date: Wed, 17 May 2017 17:24:50 +1200 Subject: [PATCH] NEW Allow shortcode handler key to be configurable --- _config.php | 2 +- code/DMS.php | 17 +++++++++++++++ code/model/DMSDocumentSet.php | 8 ++++++- code/tools/ShortCodeRelationFinder.php | 3 ++- docs/en/configuration.md | 23 ++++++++++++++++++-- javascript/DocumentHtmlEditorFieldToolbar.js | 7 ++++-- tests/DMSDocumentSetTest.php | 17 +++++++++++++++ tests/DMSTest.php | 9 ++++++++ tests/ShortCodeRelationFinderTest.php | 2 ++ 9 files changed, 81 insertions(+), 7 deletions(-) diff --git a/_config.php b/_config.php index 5d1c4b9..b7e7c67 100644 --- a/_config.php +++ b/_config.php @@ -11,7 +11,7 @@ if (!file_exists(BASE_PATH . DIRECTORY_SEPARATOR . DMS_DIR)) { CMSMenu::remove_menu_item('DMSDocumentAddController'); ShortcodeParser::get('default')->register( - 'dms_document_link', + $config->get('DMS', 'shortcode_handler_key'), array('DMSShortcodeHandler', 'handle') ); diff --git a/code/DMS.php b/code/DMS.php index 10b9348..3b0e81e 100644 --- a/code/DMS.php +++ b/code/DMS.php @@ -17,6 +17,13 @@ class DMS implements DMSInterface */ public static $dmsFolderSize = 1000; + /** + * The shortcode handler key. Can be changed by user code. + * + * @config + * @var string + */ + private static $shortcode_handler_key = 'dms_document_link'; /** * Factory method that returns an instance of the DMS. This could be any class that implements the DMSInterface. @@ -166,4 +173,14 @@ class DMS implements DMSInterface $folderName = intval($id / self::$dmsFolderSize); return $folderName; } + + /** + * Get the shortcode handler key + * + * @return string + */ + public function getShortcodeHandlerKey() + { + return (string) Config::inst()->get('DMS', 'shortcode_handler_key'); + } } diff --git a/code/model/DMSDocumentSet.php b/code/model/DMSDocumentSet.php index 332d5c9..38f7402 100644 --- a/code/model/DMSDocumentSet.php +++ b/code/model/DMSDocumentSet.php @@ -138,7 +138,13 @@ class DMSDocumentSet extends DataObject $addNewButton->setDocumentSetId($self->ID); $fields->removeByName('Documents'); - $fields->addFieldToTab('Root.Main', $gridField); + $fields->addFieldsToTab( + 'Root.Main', + array( + $gridField, + HiddenField::create('DMSShortcodeHandlerKey', false, DMS::inst()->getShortcodeHandlerKey()) + ) + ); $self->addQueryFields($fields); } }); diff --git a/code/tools/ShortCodeRelationFinder.php b/code/tools/ShortCodeRelationFinder.php index 645cfe8..9cddc50 100644 --- a/code/tools/ShortCodeRelationFinder.php +++ b/code/tools/ShortCodeRelationFinder.php @@ -46,12 +46,13 @@ class ShortCodeRelationFinder $list = DataList::create('SiteTree'); $where = array(); $fields = $this->getShortCodeFields('SiteTree'); + $shortcode = DMS::inst()->getShortcodeHandlerKey(); foreach ($fields as $ancClass => $ancFields) { foreach ($ancFields as $ancFieldName => $ancFieldSpec) { if ($ancClass != "SiteTree") { $list = $list->leftJoin($ancClass, '"'.$ancClass.'"."ID" = "SiteTree"."ID"'); } - $where[] = "\"$ancClass\".\"$ancFieldName\" LIKE '%[dms_document_link,id=$number]%'"; //."%s" LIKE ""', + $where[] = "\"$ancClass\".\"$ancFieldName\" LIKE '%[{$shortcode},id=$number]%'"; //."%s" LIKE ""', } } diff --git a/docs/en/configuration.md b/docs/en/configuration.md index a9a1bf8..dc4db25 100644 --- a/docs/en/configuration.md +++ b/docs/en/configuration.md @@ -31,7 +31,7 @@ DMSDocumentAddController: ## Adding fields to the Query Builder Query builder fields are read from the DMSDocument::searchable_fields property set in [querybuilder.yml](../../_config/querybuilder.yml). Some default fields are provided and can be customised -by modifying the field and/or filter properties of a field or adding a new field entirely. +by modifying the field and/or filter properties of a field or adding a new field entirely. [See here for information](https://docs.silverstripe.org/en/developer_guides/model/searchfilters/) on how to modify search filters and [see here for more information](https://docs.silverstripe.org/en/developer_guides/forms/field_types/common_subclasses/) on the field types available. @@ -55,4 +55,23 @@ DMSDocument: filter: 'ExactMatchFilter' Filename: title: 'File name' -``` \ No newline at end of file +``` + +## Change the shortcode handler + +If you need to change the `dms_document_link` shortcode handler for some reason, you can do so with YAML configuration +and some PHP: + +```yaml +DMS: + shortcode_handler_key: your_shortcode +``` + +And for example in `_config.php`: + +```php +ShortcodeParser::get('default')->register( + Config::inst()->get('DMS', 'shortcode_handler_key'), + array('DMSShortcodeHandler', 'handle') +); +``` diff --git a/javascript/DocumentHtmlEditorFieldToolbar.js b/javascript/DocumentHtmlEditorFieldToolbar.js index aa5e8a2..bc8044d 100755 --- a/javascript/DocumentHtmlEditorFieldToolbar.js +++ b/javascript/DocumentHtmlEditorFieldToolbar.js @@ -29,11 +29,14 @@ }); $('form.htmleditorfield-linkform').entwine({ + getShortcodeKey: function () { + return this.find(':input[name=DMSShortcodeHandlerKey]').val(); + }, insertLink: function () { var href, target = null; var checkedValue = this.find(':input[name=LinkType]:checked').val(); if (checkedValue === 'document') { - href = '[dms_document_link,id=' + this.find('.selected-document').data('document-id') + ']'; + href = '[' + this.getShortcodeKey() + ',id=' + this.find('.selected-document').data('document-id') + ']'; // Determine target if (this.find(':input[name=TargetBlank]').is(':checked')) { @@ -88,7 +91,7 @@ } //match a document or call the regular link handling - if (href.match(/^\[dms_document_link(\s*|%20|,)?id=([0-9]+)\]?$/i)) { + if (href.match(new RegExp("/^\[" + this.getShortcodeKey() + "(\s*|%20|,)?id=([0-9]+)\]?$/", "i"))) { var returnArray = { LinkType: 'document', DocumentID: RegExp.$2, diff --git a/tests/DMSDocumentSetTest.php b/tests/DMSDocumentSetTest.php index 22108e5..a813a1a 100644 --- a/tests/DMSDocumentSetTest.php +++ b/tests/DMSDocumentSetTest.php @@ -122,6 +122,23 @@ class DMSDocumentSetTest extends SapphireTest ); } + /** + * Ensure that the configurable shortcode handler key is a hidden field in the CMS + */ + public function testShortcodeHandlerKeyFieldExists() + { + Config::inst()->update('DMS', 'shortcode_handler_key', 'unit-test'); + + $set = DMSDocumentSet::create(); + $set->write(); + + $fields = $set->getCMSFields(); + $field = $fields->fieldByName('Root.Main.DMSShortcodeHandlerKey'); + + $this->assertInstanceOf('HiddenField', $field); + $this->assertSame('unit-test', $field->Value()); + } + /** * Test that extra documents are added after write */ diff --git a/tests/DMSTest.php b/tests/DMSTest.php index a79239f..6a5f4ce 100644 --- a/tests/DMSTest.php +++ b/tests/DMSTest.php @@ -184,6 +184,15 @@ class DMSTest extends FunctionalTest $this->assertContainsOnlyInstancesOf('DMSDocument', $documents); } + /** + * Ensure the shortcode handler key is configurable + */ + public function testShortcodeHandlerKeyIsConfigurable() + { + Config::inst()->update('DMS', 'shortcode_handler_key', 'testing'); + $this->assertSame('testing', DMS::inst()->getShortcodeHandlerKey()); + } + /** * Test that document sets can be retrieved for a given page */ diff --git a/tests/ShortCodeRelationFinderTest.php b/tests/ShortCodeRelationFinderTest.php index 5149679..71e1a22 100644 --- a/tests/ShortCodeRelationFinderTest.php +++ b/tests/ShortCodeRelationFinderTest.php @@ -5,6 +5,8 @@ class ShortCodeRelationFinderTest extends SapphireTest public function testFindInRate() { + Config::inst()->update('DMS', 'shortcode_handler_key', 'dms_document_link'); + $d1 = $this->objFromFixture('DMSDocument', 'd1'); $d2 = $this->objFromFixture('DMSDocument', 'd2');