mirror of
https://github.com/silverstripe/silverstripe-dms
synced 2024-10-22 14:05:56 +02:00
NEW Add friendly URL segments for DMS documents
This commit is contained in:
parent
2ddd20cd6c
commit
80e36c3350
@ -260,7 +260,8 @@ class DMSDocument extends DataObject implements DMSDocumentInterface
|
||||
*/
|
||||
public function getLink()
|
||||
{
|
||||
$result = Controller::join_links(Director::baseURL(), 'dmsdocument/' . $this->ID);
|
||||
$urlSegment = sprintf('%d-%s', $this->ID, URLSegmentFilter::create()->filter($this->getTitle()));
|
||||
$result = Controller::join_links(Director::baseURL(), 'dmsdocument/' . $urlSegment);
|
||||
if (!$this->canView()) {
|
||||
$result = sprintf("javascript:alert('%s')", $this->getPermissionDeniedReason());
|
||||
}
|
||||
@ -823,7 +824,7 @@ class DMSDocument extends DataObject implements DMSDocumentInterface
|
||||
->setDisplayFields(Config::inst()->get('DMSDocument_versions', 'display_fields'))
|
||||
->setFieldFormatting(
|
||||
array(
|
||||
'FilenameWithoutID' => '<a target=\'_blank\' class=\'file-url\' href=\'$Link\'>'
|
||||
'FilenameWithoutID' => '<a target="_blank" class="file-url" href="$Link">'
|
||||
. '$FilenameWithoutID</a>'
|
||||
)
|
||||
);
|
||||
|
@ -31,21 +31,37 @@ class DMSDocument_Controller extends Controller
|
||||
$doc = null;
|
||||
|
||||
$id = Convert::raw2sql($request->param('ID'));
|
||||
|
||||
if (strpos($id, 'version') === 0) {
|
||||
// Versioned document
|
||||
$id = str_replace('version', '', $id);
|
||||
$id = $this->getDocumentIdFromSlug(str_replace('version', '', $id));
|
||||
$doc = DataObject::get_by_id('DMSDocument_versions', $id);
|
||||
$this->extend('updateVersionFromID', $doc, $request);
|
||||
} else {
|
||||
// Normal document
|
||||
$doc = DataObject::get_by_id('DMSDocument', $id);
|
||||
$doc = DataObject::get_by_id('DMSDocument', $this->getDocumentIdFromSlug($id));
|
||||
$this->extend('updateDocumentFromID', $doc, $request);
|
||||
}
|
||||
|
||||
return $doc;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a document's ID from a "friendly" URL slug containing a numeric ID and slugged title
|
||||
*
|
||||
* @param string $slug
|
||||
* @return int
|
||||
* @throws InvalidArgumentException if an invalid format is provided
|
||||
*/
|
||||
protected function getDocumentIdFromSlug($slug)
|
||||
{
|
||||
$parts = (array) sscanf($slug, '%d-%s');
|
||||
$id = array_shift($parts);
|
||||
if (is_numeric($id)) {
|
||||
return (int) $id;
|
||||
}
|
||||
throw new InvalidArgumentException($slug . ' is not a valid DMSDocument URL');
|
||||
}
|
||||
|
||||
/**
|
||||
* Access the file download without redirecting user, so we can block direct
|
||||
* access to documents.
|
||||
@ -54,7 +70,6 @@ class DMSDocument_Controller extends Controller
|
||||
{
|
||||
$doc = $this->getDocumentFromID($request);
|
||||
|
||||
|
||||
if (!empty($doc)) {
|
||||
$canView = $doc->canView();
|
||||
|
||||
|
@ -7,6 +7,29 @@ class DMSDocumentControllerTest extends SapphireTest
|
||||
{
|
||||
protected static $fixture_file = 'dmstest.yml';
|
||||
|
||||
/**
|
||||
* @var DMSDocument_Controller
|
||||
*/
|
||||
protected $controller;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
Config::inst()->update('DMS', 'folder_name', 'assets/_unit-test-123');
|
||||
$this->logInWithPermission('ADMIN');
|
||||
|
||||
$this->controller = $this->getMockBuilder('DMSDocument_Controller')
|
||||
->setMethods(array('sendFile'))
|
||||
->getMock();
|
||||
}
|
||||
|
||||
public function tearDown()
|
||||
{
|
||||
DMSFilesystemTestHelper::delete('assets/_unit-test-123');
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that the download behaviour is either "open" or "download"
|
||||
*
|
||||
@ -16,16 +39,8 @@ class DMSDocumentControllerTest extends SapphireTest
|
||||
*/
|
||||
public function testDownloadBehaviourOpen($behaviour, $expectedDisposition)
|
||||
{
|
||||
Config::inst()->update('DMS', 'folder_name', 'assets/_unit-test-123');
|
||||
|
||||
$this->logInWithPermission('ADMIN');
|
||||
|
||||
/** @var DMSDocument_Controller $controller */
|
||||
$controller = $this->getMockBuilder('DMSDocument_Controller')
|
||||
->setMethods(array('sendFile'))->getMock();
|
||||
|
||||
$self = $this;
|
||||
$controller->expects($this->once())
|
||||
$this->controller->expects($this->once())
|
||||
->method('sendFile')
|
||||
->will(
|
||||
$this->returnCallback(function ($path, $mime, $name, $disposition) use ($self, $expectedDisposition) {
|
||||
@ -38,11 +53,9 @@ class DMSDocumentControllerTest extends SapphireTest
|
||||
$openDoc->clearEmbargo(false);
|
||||
$openDoc->write();
|
||||
|
||||
$request = new SS_HTTPRequest('GET', 'index/' . $openDoc->ID);
|
||||
$request->match('index/$ID');
|
||||
$controller->index($request);
|
||||
|
||||
DMSFilesystemTestHelper::delete('assets/_unit-test-123');
|
||||
$request = new SS_HTTPRequest('GET', $openDoc->Link());
|
||||
$request->match('dmsdocument/$ID');
|
||||
$this->controller->index($request);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -264,6 +264,20 @@ class DMSDocumentTest extends SapphireTest
|
||||
DMSFilesystemTestHelper::delete('assets/_unit-tests');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that the link contains an ID and URL slug
|
||||
*/
|
||||
public function testGetLink()
|
||||
{
|
||||
Config::inst()->update('DMS', 'folder_name', 'assets/_unit-tests');
|
||||
|
||||
$document = DMS::inst()->storeDocument('dms/tests/DMS-test-lorum-file.pdf');
|
||||
|
||||
$expected = '/dmsdocument/' . $document->ID . '-dms-test-lorum-file-pdf';
|
||||
$this->assertSame($expected, $document->Link());
|
||||
$this->assertSame($expected, $document->getLink());
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure that the description can be returned in HTML format
|
||||
*/
|
||||
|
@ -22,7 +22,10 @@ class DMSShortcodeTest extends SapphireTest
|
||||
$value = Injector::inst()->create('HTMLValue', $result);
|
||||
$link = $value->query('//a')->item(0);
|
||||
|
||||
$this->assertStringEndsWith("/dmsdocument/$document->ID", $link->getAttribute('href'));
|
||||
$this->assertStringEndsWith(
|
||||
'/dmsdocument/' . $document->ID . '-dms-test-lorum-file-pdf',
|
||||
$link->getAttribute('href')
|
||||
);
|
||||
$this->assertEquals($document->getExtension(), $link->getAttribute('data-ext'));
|
||||
$this->assertEquals($document->getFileSizeFormatted(), $link->getAttribute('data-size'));
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user