Completed
Pull Request — master (#7)
by
unknown
13:44
created

SocialNetworkingLinksDataObject::IconHTML()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SunnySideUp\ShareThis;
4
5
use SilverStripe\Assets\Image;
6
use SilverStripe\Security\Permission;
7
use SilverStripe\ORM\FieldType\DBField;
8
use SilverStripe\CMS\Model\SiteTree;
9
use SilverStripe\Forms\LiteralField;
10
use SilverStripe\Forms\TreeDropdownField;
11
use SilverStripe\ORM\DataObject;
12
13
/**
14
 *
15
 *@author nicolaas[at]sunnysideup.co.nz
16
 *@description: creates a list of places where people can follow you (e.g. twitter, your blog, etc...)
17
 *
18
 */
19
class SocialNetworkingLinksDataObject extends DataObject
20
{
21
    private static $db = array(
22
        'URL' => 'Varchar(255)',
23
        'Title' => 'Varchar(255)',
24
        'Sort' => 'Int'
25
    );
26
27
    private static $casting = array(
28
        'Code' => 'Varchar(255)',
29
        'Link' => 'Varchar(255)',
30
        'IconHTML' => 'HTMLText'
31
    );
32
33
    private static $has_one = array(
34
        'Icon' => Image::class,
35
        'InternalLink' => 'Page'
36
    );
37
38
    private static $searchable_fields = array(
39
        'Title' => 'PartialMatchFilter'
40
    );
41
42
    private static $field_labels = array(
43
        'InternalLink' => 'Internal Link',
44
        'URL' => 'OR External Link (e.g. http://twitter.com/myname/) - will override internal link',
45
        'Title' => 'Title',
46
        'Sort' => 'Sort Index (lower numbers shown first)',
47
        'IconID' => 'Icon (preferably something like 32pixels by 32pixels)'
48
    );
49
50
    private static $summary_fields = array(
51
        'Title' => 'Title',
52
        'IconHTML' => 'HTMLText'
53
    );
54
55
    private static $default_sort = 'Sort ASC, Title ASC';
56
57
    private static $singular_name = 'Join Us link';
58
59
    private static $plural_name = 'Join Us links';
60
61
    public function canView($member = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
62
    {
63
        return Permission::checkMember($member, 'SOCIAL_MEDIA');
64
    }
65
66
    public function canCreate($member = null, $context = [])
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
67
    {
68
        return Permission::checkMember($member, 'SOCIAL_MEDIA');
69
    }
70
71
    public function canEdit($member = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
72
    {
73
        return Permission::checkMember($member, 'SOCIAL_MEDIA');
74
    }
75
76
    public function canDelete($member = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
77
    {
78
        return Permission::checkMember($member, 'SOCIAL_MEDIA');
79
    }
80
81
    /**
82
     * @return String - returns the title with all non-alphanumeric + spaces removed.
83
     */
84
    public function Code()
85
    {
86
        return strtolower(preg_replace("/[^a-zA-Z0-9]/", '', $this->Title));
87
    }
88
89
    public function IconHTML()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
90
    {
91
        return $this->getIconHTML();
92
    }
93
    public function getIconHTML()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
94
    {
95
        $icon = $this->Icon();
96
        if ($icon && $icon->exists()) {
97
            $html = $icon->SetHeight(32);
98
        } else {
99
            $html = DBField::create_field("HTMLText", '<img src="/' . SS_SHARETHIS_DIR . "/images/icons/{$this->Code}.png\" alt=\"{$this->Code}\"/>");
100
        }
101
        return  $html;
102
    }
103
104
    public function Link()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
105
    {
106
        if ($this->URL) {
107
            return $this->URL;
108
        } elseif ($this->InternalLinkID) {
109
            $page = SiteTree::get()->byID($this->InternalLinkID);
110
            if ($page->exists()) {
111
                return $page->Link();
112
            }
113
        }
114
    }
115
116
    public function getCMSFields()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
117
    {
118
        $fields = parent::getCMSFields();
119
        if ($this->ID) {
120
            $fields->addFieldToTab('Root.Main', new LiteralField('Code', "<p>Code: {$this->Code()}</p>"));
121
            $fields->addFieldToTab('Root.Main', new LiteralField('Link', "<p>Link: <a href=\"{$this->Link()}\">{$this->Link()}</a></p>"));
122
            $fields->addFieldToTab('Root.Main', new LiteralField('Link', "<p>{$this->IconHTML()}</p>"));
123
        }
124
        $fields->removeFieldFromTab('Root.Main', 'InternalLinkID');
125
        $fields->addFieldToTab('Root.Main', new TreeDropdownField('InternalLinkID', 'Internal Link', SiteTree::class), 'URL');
126
        return $fields;
127
    }
128
}
129