Completed
Pull Request — master (#168)
by Franco
02:30
created

code/DMSShortcodeHandler.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * Handles replacing `dms_document_link` shortcodes with links to the actual
5
 * document.
6
 *
7
 * @package dms
8
 */
9
class DMSShortcodeHandler
10
{
11
    public static function handle($arguments, $content, ShortcodeParser $parser, $tag, array $extra = array())
12
    {
13
        if (!empty($arguments['id'])) {
14
            $document = DMSDocument::get()->byID($arguments['id']);
15
16
            if ($document && !$document->isHidden()) {
17
                if ($content) {
18
                    return sprintf(
19
                        '<a href="%s">%s</a>',
20
                        $document->Link(),
21
                        $parser->parse($content)
22
                    );
23
                }
24
25
                if (isset($extra['element'])) {
26
                    $extra['element']->setAttribute('data-ext', $document->getExtension());
0 ignored issues
show
The method getExtension() does not exist on DataObject. Did you maybe mean getExtensionInstance()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
27
                    $extra['element']->setAttribute('data-size', $document->getFileSizeFormatted());
28
                }
29
30
                return $document->Link();
31
            }
32
        }
33
34
        $error = ErrorPage::get()->filter('ErrorCode', '404')->First();
35
36
        if ($error) {
37
            return $error->Link();
38
        }
39
40
        return '';
41
    }
42
}
43