Merge pull request #35 from ajshort/pull-3

Add data-ext and data-size attributes to DMS links.
This commit is contained in:
Stephen Shkardoon 2013-10-09 15:26:31 -07:00
commit e6925ef912
4 changed files with 65 additions and 30 deletions

View File

@ -13,7 +13,9 @@ if (!file_exists(BASE_PATH . DIRECTORY_SEPARATOR . DMS_DIR)) user_error("DMS dir
CMSMenu::remove_menu_item('DMSDocumentAddController');
ShortcodeParser::get('default')->register('dms_document_link', array('DMSDocument_Controller', 'dms_link_shortcode_handler'));
ShortcodeParser::get('default')->register(
'dms_document_link', array('DMSShortcodeHandler', 'handle')
);
if ($config->get('DMSDocument_versions', 'enable_versions')) {
//using the same db relations for the versioned documents, as for the actual documents

View File

@ -916,34 +916,5 @@ class DMSDocument_Controller extends ContentController {
$this->httpError(404, 'This asset does not exist.');
}
/**
* Handles dms_document_link shortcode
* @return string
*/
public static function dms_link_shortcode_handler($arguments, $content = null, $parser = null) {
$linkText = null;
if (!empty($arguments['id'])) {
$document = DMSDocument::get()->filter(array('ID' => $arguments['id']))->First();
if ($document && !$document->isHidden()) {
if (!empty($content)) {
$linkText = sprintf('<a href="%s">%s</a>', $document->Link(), $parser->parse($content));
} else {
$linkText = $document->Link();
}
}
}
if (empty($linkText)) {
$errorPage = ErrorPage::get()->filter(array('ErrorCode' => '404'))->First();
if ($errorPage) {
$linkText = $errorPage->Link();
}
}
return $linkText;
}
}

View File

@ -0,0 +1,39 @@
<?php
/**
* Handles replacing `dms_document_link` shortcodes with links to the actual
* document.
*/
class DMSShortcodeHandler {
public static function handle(
$arguments, $content, ShortcodeParser $parser, $tag, array $extra = array()
) {
if(!empty($arguments['id'])) {
$document = DMSDocument::get()->byID($arguments['id']);
if($document && !$document->isHidden()) {
if($content) {
return sprintf(
'<a href="%s">%s</a>', $document->Link(), $parser->parse($content)
);
} else {
if(isset($extra['element'])) {
$extra['element']->setAttribute('data-ext', $document->getExtension());
$extra['element']->setAttribute('data-size', $document->getFileSizeFormatted());
}
return $document->Link();
}
}
}
$error = ErrorPage::get()->filter('ErrorCode', '404')->First();
if($error) {
return $error->Link();
}
return '';
}
}

View File

@ -0,0 +1,23 @@
<?php
/**
* Tests DMS shortcode linking functionality.
*/
class DMSShortcodeTest extends SapphireTest {
public function testShortcodeOperation() {
$file = 'dms/tests/DMS-test-lorum-file.pdf';
$document = DMS::inst()->storeDocument($file);
$result = ShortcodeParser::get('default')->parse(sprintf(
'<p><a href="[dms_document_link,id=\'%d\']">Document</a></p>', $document->ID
));
$value = Injector::inst()->create('HTMLValue', $result);
$link = $value->query('//a')->item(0);
$this->assertEquals("/dmsdocument/$document->ID", $link->getAttribute('href'));
$this->assertEquals($document->getExtension(), $link->getAttribute('data-ext'));
$this->assertEquals($document->getFileSizeFormatted(), $link->getAttribute('data-size'));
}
}