Passed
Push — master ( ff118d...a07925 )
by Thomas
03:31
created

SocialShareExtension::getSocialAbsoluteLink()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 10
rs 10
cc 3
nc 3
nop 1
1
<?php
2
3
namespace LeKoala\CommonExtensions;
4
5
use SilverStripe\Control\Controller;
6
use SilverStripe\Control\Director;
7
use SilverStripe\ORM\DataExtension;
8
9
/**
10
 * Easily add share urls based on Link method
11
 *
12
 * Typically you will need an AbsoluteLink method as well because it should
13
 * be available in sitemap
14
 *
15
 * https://github.com/wilr/silverstripe-googlesitemaps/blob/master/docs/en/index.md
16
 *
17
 * @link http://www.sharelinkgenerator.com/
18
 * @property \SilverStripe\CMS\Model\SiteTree $owner
19
 */
20
class SocialShareExtension extends DataExtension
21
{
22
    protected function getSocialAbsoluteLink($obj)
23
    {
24
        if ($obj->hasMethod('AbsoluteLink')) {
25
            $link = $obj->AbsoluteLink();
26
        } elseif ($obj->hasMethod('Link')) {
27
            $link = Director::absoluteURL($obj->Link());
28
        } else {
29
            $link = Controller::curr()->getRequest()->getURL();
30
        }
31
        return $link;
32
    }
33
34
    public function FacebookShareUrl()
35
    {
36
        $link = $this->getSocialAbsoluteLink($this->owner);
37
        return 'https://www.facebook.com/sharer/sharer.php?u=' . urlencode($link);
38
    }
39
40
    public function TwitterShareUrl()
41
    {
42
        $link = $this->getSocialAbsoluteLink($this->owner);
43
        return 'http://twitter.com/share?url=' . urlencode($link) . '&text=' . urlencode($this->owner->Title);
44
    }
45
46
    public function LinkedInShareUrl()
47
    {
48
        $link = $this->getSocialAbsoluteLink($this->owner);
49
        return 'https://www.linkedin.com/shareArticle?mini=true&url=' . urlencode($link);
50
    }
51
52
    public function EmailShareLink()
53
    {
54
        $link = $this->getSocialAbsoluteLink($this->owner);
55
        $body = _t('SocialExtension.DISCOVER', 'I discovered ') . ' "' . $this->owner->Title . '" \n' .
56
            _t('SocialExtension.SEE', 'You can see it here :') . ' ' . $link;
57
        return 'mailto:?subject=' . $this->owner->Title . '&body=' . htmlentities($body);
58
    }
59
}
60