NEW Allow shortcode handler key to be configurable

This commit is contained in:
Robbie Averill 2017-05-17 17:24:50 +12:00
parent 961306ecc0
commit a6743b03f5
9 changed files with 81 additions and 7 deletions

View File

@ -11,7 +11,7 @@ if (!file_exists(BASE_PATH . DIRECTORY_SEPARATOR . DMS_DIR)) {
CMSMenu::remove_menu_item('DMSDocumentAddController'); CMSMenu::remove_menu_item('DMSDocumentAddController');
ShortcodeParser::get('default')->register( ShortcodeParser::get('default')->register(
'dms_document_link', $config->get('DMS', 'shortcode_handler_key'),
array('DMSShortcodeHandler', 'handle') array('DMSShortcodeHandler', 'handle')
); );

View File

@ -17,6 +17,13 @@ class DMS implements DMSInterface
*/ */
public static $dmsFolderSize = 1000; 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. * 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); $folderName = intval($id / self::$dmsFolderSize);
return $folderName; return $folderName;
} }
/**
* Get the shortcode handler key
*
* @return string
*/
public function getShortcodeHandlerKey()
{
return (string) Config::inst()->get('DMS', 'shortcode_handler_key');
}
} }

View File

@ -138,7 +138,13 @@ class DMSDocumentSet extends DataObject
$addNewButton->setDocumentSetId($self->ID); $addNewButton->setDocumentSetId($self->ID);
$fields->removeByName('Documents'); $fields->removeByName('Documents');
$fields->addFieldToTab('Root.Main', $gridField); $fields->addFieldsToTab(
'Root.Main',
array(
$gridField,
HiddenField::create('DMSShortcodeHandlerKey', false, DMS::inst()->getShortcodeHandlerKey())
)
);
$self->addQueryFields($fields); $self->addQueryFields($fields);
} }
}); });

View File

@ -46,12 +46,13 @@ class ShortCodeRelationFinder
$list = DataList::create('SiteTree'); $list = DataList::create('SiteTree');
$where = array(); $where = array();
$fields = $this->getShortCodeFields('SiteTree'); $fields = $this->getShortCodeFields('SiteTree');
$shortcode = DMS::inst()->getShortcodeHandlerKey();
foreach ($fields as $ancClass => $ancFields) { foreach ($fields as $ancClass => $ancFields) {
foreach ($ancFields as $ancFieldName => $ancFieldSpec) { foreach ($ancFields as $ancFieldName => $ancFieldSpec) {
if ($ancClass != "SiteTree") { if ($ancClass != "SiteTree") {
$list = $list->leftJoin($ancClass, '"'.$ancClass.'"."ID" = "SiteTree"."ID"'); $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 ""',
} }
} }

View File

@ -31,7 +31,7 @@ DMSDocumentAddController:
## Adding fields to the Query Builder ## 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 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/) [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. on the field types available.
@ -55,4 +55,23 @@ DMSDocument:
filter: 'ExactMatchFilter' filter: 'ExactMatchFilter'
Filename: Filename:
title: 'File name' title: 'File name'
``` ```
## 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')
);
```

View File

@ -29,11 +29,14 @@
}); });
$('form.htmleditorfield-linkform').entwine({ $('form.htmleditorfield-linkform').entwine({
getShortcodeKey: function () {
return this.find(':input[name=DMSShortcodeHandlerKey]').val();
},
insertLink: function () { insertLink: function () {
var href, target = null; var href, target = null;
var checkedValue = this.find(':input[name=LinkType]:checked').val(); var checkedValue = this.find(':input[name=LinkType]:checked').val();
if (checkedValue === 'document') { 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 // Determine target
if (this.find(':input[name=TargetBlank]').is(':checked')) { if (this.find(':input[name=TargetBlank]').is(':checked')) {
@ -88,7 +91,7 @@
} }
//match a document or call the regular link handling //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 = { var returnArray = {
LinkType: 'document', LinkType: 'document',
DocumentID: RegExp.$2, DocumentID: RegExp.$2,

View File

@ -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 * Test that extra documents are added after write
*/ */

View File

@ -184,6 +184,15 @@ class DMSTest extends FunctionalTest
$this->assertContainsOnlyInstancesOf('DMSDocument', $documents); $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 * Test that document sets can be retrieved for a given page
*/ */

View File

@ -5,6 +5,8 @@ class ShortCodeRelationFinderTest extends SapphireTest
public function testFindInRate() public function testFindInRate()
{ {
Config::inst()->update('DMS', 'shortcode_handler_key', 'dms_document_link');
$d1 = $this->objFromFixture('DMSDocument', 'd1'); $d1 = $this->objFromFixture('DMSDocument', 'd1');
$d2 = $this->objFromFixture('DMSDocument', 'd2'); $d2 = $this->objFromFixture('DMSDocument', 'd2');