Completed
Pull Request — master (#9)
by
unknown
08:34
created

SocialNetworkingLinksDataObject::getIconHTML()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 0
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace SunnysideUp\ShareThis;
4
5
use \Page;
6
use SilverStripe\Assets\Image;
7
use SilverStripe\Security\Permission;
8
use SilverStripe\ORM\FieldType\DBField;
9
use SilverStripe\ORM\Filters\PartialMatchFilter;
10
use SilverStripe\CMS\Model\SiteTree;
11
use SilverStripe\Forms\LiteralField;
12
use SilverStripe\Forms\TreeDropdownField;
13
use SilverStripe\ORM\DataObject;
14
15
/**
16
 *
17
 *@author nicolaas[at]sunnysideup.co.nz
18
 *@description: creates a list of places where people can follow you (e.g. twitter, your blog, etc...)
19
 *
20
 */
21
class SocialNetworkingLinksDataObject extends DataObject
22
{
23
    /**
24
     * @var string
25
     */
26
    private static $table_name = 'SocialNetworkingLinksDataObject';
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
27
28
    /**
29
     * @var array
30
     */
31
    private static $db = [
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
32
        'URL' => 'Varchar(255)',
33
        'Title' => 'Varchar(255)',
34
        'Sort' => 'Int'
35
    ];
36
37
    /**
38
     * @var array
39
     */
40
    private static $casting = [
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
41
        'Code' => 'Varchar(255)',
42
        'Link' => 'Varchar(255)',
43
        'IconHTML' => 'HTMLText'
44
    ];
45
46
    /**
47
     * @var array
48
     */
49
    private static $has_one = [
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
50
        'Icon' => Image::class,
51
        'InternalLink' => Page::class
52
    ];
53
54
    /**
55
     * @var array
56
     */
57
    private static $searchable_fields = [
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
58
        'Title' => PartialMatchFilter::class
59
    ];
60
61
    /**
62
     * @return array
63
     */
64
    private static $field_labels = [
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
65
        'InternalLink' => 'Internal Link',
66
        'URL' => 'External Link (e.g. http://twitter.com/myname/) - will override internal link',
67
        'Title' => 'Title',
68
        'Sort' => 'Sort Index (lower numbers shown first)',
69
        'IconID' => 'Icon (preferably 32px X 32px)'
70
    ];
71
72
    /**
73
     * @var array
74
     */
75
    private static $summary_fields = [
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
76
        'Title' => 'Title',
77
        'IconHTML' => 'Icon'
78
    ];
79
80
    /**
81
     * @var string
82
     */
83
    private static $default_sort = 'Sort ASC, Title ASC';
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
84
85
    /**
86
     * @var string
87
     */
88
    private static $singular_name = 'Join Us link';
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
89
90
    /**
91
     * @var string
92
     */
93
    private static $plural_name = 'Join Us links';
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
94
95
    /**
96
     * @return boolean
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean|string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
97
     */
98
    public function canView($member = null)
99
    {
100
        return Permission::checkMember($member, 'SOCIAL_MEDIA');
101
    }
102
103
    /**
104
     * @return boolean
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean|string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
105
     */
106
    public function canCreate($member = null, $context = [])
107
    {
108
        return Permission::checkMember($member, 'SOCIAL_MEDIA');
109
    }
110
111
    /**
112
     * @return boolean
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean|string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
113
     */
114
    public function canEdit($member = null)
115
    {
116
        return Permission::checkMember($member, 'SOCIAL_MEDIA');
117
    }
118
119
    /**
120
     * @return boolean
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean|string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
121
     */
122
    public function canDelete($member = null)
123
    {
124
        return Permission::checkMember($member, 'SOCIAL_MEDIA');
125
    }
126
127
    /**
128
     * @return String - returns the title with all non-alphanumeric + spaces removed.
129
     */
130
    public function Code()
131
    {
132
        return strtolower(preg_replace("/[^a-zA-Z0-9]/", '', $this->Title));
133
    }
134
135
    /**
136
     * @return DBField
137
     */
138
    public function IconHTML()
139
    {
140
        return $this->getIconHTML();
141
    }
142
143
    /**
144
     * @return DBField / icon
145
     */
146
    public function getIconHTML()
147
    {
148
        $icon = $this->Icon();
0 ignored issues
show
Documentation Bug introduced by
The method Icon does not exist on object<SunnysideUp\Share...workingLinksDataObject>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
149
        if ($icon && $icon->exists()) {
150
            $html = $icon->ScaleHeight(32);
151
        } else {
152
            $html = DBField::create_field("HTMLText", '<img src="/' . SS_SHARETHIS_DIR . "/images/icons/{$this->Code}.png\" alt=\"{$this->Code}\"/>");
0 ignored issues
show
Documentation introduced by
The property Code does not exist on object<SunnysideUp\Share...workingLinksDataObject>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
153
        }
154
        return  $html;
155
    }
156
157
    /**
158
     * Link
159
     *
160
     * @return string
161
     */
162
    public function Link()
163
    {
164
        if ($this->URL) {
0 ignored issues
show
Documentation introduced by
The property URL does not exist on object<SunnysideUp\Share...workingLinksDataObject>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
165
            return $this->URL;
0 ignored issues
show
Documentation introduced by
The property URL does not exist on object<SunnysideUp\Share...workingLinksDataObject>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
166
        } elseif ($this->InternalLinkID) {
0 ignored issues
show
Documentation introduced by
The property InternalLinkID does not exist on object<SunnysideUp\Share...workingLinksDataObject>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
167
            $page = SiteTree::get()->byID($this->InternalLinkID);
0 ignored issues
show
Documentation introduced by
The property InternalLinkID does not exist on object<SunnysideUp\Share...workingLinksDataObject>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
168
            if ($page->exists()) {
169
                return $page->Link();
170
            }
171
        }
172
    }
173
174
    /**
175
     * @return FieldList $fields
0 ignored issues
show
Documentation introduced by
Should the return type not be \SilverStripe\Forms\FieldList?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
176
     */
177
    public function getCMSFields()
178
    {
179
        $fields = parent::getCMSFields();
180
181
        if ($this->ID) {
182
            $fields->addFieldToTab('Root.Main', LiteralField::create('Code', "<p>Code: {$this->Code()}</p>"));
183
            $fields->addFieldToTab('Root.Main', LiteralField::create('Link', "<p>Link: <a href=\"{$this->Link()}\">{$this->Link()}</a></p>"));
184
            $fields->addFieldToTab('Root.Main', LiteralField::create('Link', "<p>{$this->IconHTML()}</p>"));
185
        }
186
187
        $fields->removeFieldFromTab('Root.Main', 'InternalLinkID');
188
        $fields->addFieldToTab('Root.Main', TreeDropdownField::create('InternalLinkID', 'Internal Link', SiteTree::class), 'URL');
189
190
        return $fields;
191
    }
192
}
193