Completed
Pull Request — master (#6)
by Robbie
03:26 queued 52s
created

RichLinksExtension::RichLinks()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 33
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 16
nc 3
nop 0
1
<?php
2
3
namespace CWP\Core\Extension;
4
5
use SilverStripe\Core\Extension;
6
use SilverStripe\ORM\DataObject;
7
8
/**
9
 * Adds capability to augment links with extra attributes and meta information.
10
 *
11
 * Usage in the templates:
12
 *      $Content.RichLinks
13
 *
14
 * Note: will only work with content produced by HtmlEditorField.
15
 */
16
class RichLinksExtension extends Extension
17
{
18
19
    /**
20
     * @var array
21
     */
22
    private static $casting = [
0 ignored issues
show
introduced by
The private property $casting is not used, and could be removed.
Loading history...
23
        'RichLinks' => 'HTMLText'
24
    ];
25
26
    /**
27
     * @return string
28
     */
29
    public function RichLinks()
30
    {
31
        // Note:
32
        // Assume we can use Regexes because the link will always be formatted
33
        // in the same way coming from the CMS.
34
35
        $content = $this->owner->value;
36
37
        // Find all file links for processing.
38
        preg_match_all('/<a.*href="\[file_link,id=([0-9]+)\].*".*>.*<\/a>/U', $content, $matches);
39
40
        // Attach the file type and size to each of the links.
41
        for ($i = 0; $i < count($matches[0]); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
42
            $file = DataObject::get_by_id('File', $matches[1][$i]);
43
            if ($file) {
44
                $size = $file->getSize();
45
                $ext = strtoupper($file->getExtension());
46
                // Replace the closing </a> tag with the size span (and reattach the closing tag).
47
                $newLink = substr($matches[0][$i], 0, strlen($matches[0][$i]) - 4)
48
                    . "<span class='fileExt'> [$ext, $size]</span></a>";
49
                $content = str_replace($matches[0][$i], $newLink, $content);
50
            }
51
        }
52
53
        // Inject extra attributes into the external links.
54
        $pattern = '/(<a.*)(href=\"https?:\/\/[^\"]*\"[^>]*>.*)(<\/a>)/iU';
55
        $replacement = sprintf(
56
            '$1class="external" rel="external" title="%s" $2<span class="nonvisual-indicator">(external link)</span>$3',
57
            _t('RichLinks.OpenLinkTitle', 'Open external link')
58
        );
59
        $content = preg_replace($pattern, $replacement, $content, -1);
60
61
        return $content;
62
    }
63
}
64