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

SocialNetworksSTE::updateCMSFields()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
namespace SunnysideUp\ShareThis;
4
5
use SilverStripe\Forms\FieldList;
6
use SilverStripe\Forms\HeaderField;
7
use SilverStripe\Forms\CheckboxField;
8
use SilverStripe\Forms\LiteralField;
9
use SilverStripe\View\Requirements;
10
use SilverStripe\Core\Config\Config;
11
use SunnysideUp\ShareThis\SocialNetworkingLinksDataObject;
12
use SilverStripe\CMS\Model\SiteTreeExtension;
13
14
/**
15
 * Add a field to each SiteTree object and it's subclasses to enable "follow us on ...", this can be a blog, twitter, facebook or whatever else.
16
 * it uses the SocialNetworkingLinksDataObject to get a list of icons.
17
 * @author nicolaas [at] sunnysideup.co.nz
18
 * @todo fix populateDefaults to make sure SiteConfig table is built first
19
 */
20
class SocialNetworksSTE extends SiteTreeExtension
21
{
22
23
    /**
24
     * Use the font-awesome icon collection?
25
     * @var Boolean
26
     */
27
    private static $use_font_awesome = false;
28
29
    /**
30
     * list of sitetree extending classnames where
31
     * the ShareThis functionality should be included
32
     * @var Array
33
     */
34
    private static $always_include_in = [];
35
36
    /**
37
     * list of sitetree extending classnames where
38
     * the ShareThis functionality should NEVER be included
39
     * @var Array
40
     */
41
    private static $never_include_in = [];
42
43
    /**
44
     * @var array
45
     */
46
    private static $db = [
47
        'HasSocialNetworkingLinks' => 'Boolean'
48
    ];
49
50
    /**
51
     * @param  FieldList $fields
52
     *
53
     * @return FieldList $fields
54
     */
55
    public function updateCMSFields(FieldList $fields)
56
    {
57
        if ($this->applyToOwnerClass()) {
58
            $config = $this->owner->getSiteConfig();
0 ignored issues
show
Bug introduced by
The method getSiteConfig() does not exist on SilverStripe\ORM\DataObject. Did you maybe mean config()?

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...
59
            if (! $config->AlwaysIncludeSocialNetworkingLinks) {
60
                $fields->addFieldToTab('Root.SocialMedia', HeaderField::create('SocialNetworksHeader', 'Ask visitors to JOIN YOU on your social media'));
61
                $fields->addFieldToTab('Root.SocialMedia', CheckboxField::create('HasSocialNetworkingLinks', 'Show Join Us on our Social Networks Links on this Page (e.g. follow us on Twitter) - make sure to specify social networking links!'));
62
            }
63
            $fields->addFieldToTab('Root.SocialMedia', LiteralField::create('LinkToSiteConfigSocialMedia', "<p>There are more social media settings in the <a href=\"{$config->CMSEditLink()}\">Site Config</a>.</p>"));
64
        }
65
        return $fields;
66
    }
67
68
    /**
69
     * @return boolean
70
     */
71
    public function ShowSocialNetworks()
72
    {
73
        if ($this->applyToOwnerClass()) {
74
            $config = $this->owner->getSiteConfig();
0 ignored issues
show
Bug introduced by
The method getSiteConfig() does not exist on SilverStripe\ORM\DataObject. Did you maybe mean config()?

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...
75
            if ($config->AlwaysIncludeSocialNetworkingLinks) {
76
                return true;
77
            }
78
            return $this->owner->HasSocialNetworkingLinks;
79
        }
80
        return false;
81
    }
82
83
    /**
84
     * @return SocialNetworkingLinksDataObject
0 ignored issues
show
Documentation introduced by
Should the return type not be \SilverStripe\ORM\DataList?

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...
85
     */
86
    public function SocialNetworks()
87
    {
88
        Requirements::themedCSS('SocialNetworking', "sharethis");
89
90
        if (Config::inst()->get(SocialNetworksSTE::class, "use_font_awesome")) {
91
            Requirements::css("//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css");
92
        }
93
        return SocialNetworkingLinksDataObject::get();
94
    }
95
96
    /**
97
     * @return boolean
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean|null?

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...
98
     */
99
    private function applyToOwnerClass()
100
    {
101
        $always = Config::inst()->get(SocialNetworksSTE::class, "always_include_in");
102
        $never = Config::inst()->get(SocialNetworksSTE::class, "never_include_in");
103
        if (count($always) == 0 && count($never) == 0) {
104
            return true;
105
        }
106 View Code Duplication
        if (count($never) && count($always) == 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
107
            if (in_array($this->owner->ClassName, $never)) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return !in_array($this->...er->ClassName, $never);.
Loading history...
108
                return false;
109
            }
110
            return true;
111
        }
112 View Code Duplication
        if (count($always) && count($never) == 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
113
            if (in_array($this->owner->ClassName, $always)) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return in_array($this->o...r->ClassName, $always);.
Loading history...
114
                return true;
115
            }
116
            return false;
117
        }
118 View Code Duplication
        if (count($never) && count($always)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
119
            if (in_array($this->owner->ClassName, $never)) {
120
                return false;
121
            }
122
            if (in_array($this->owner->ClassName, $always)) {
0 ignored issues
show
Unused Code introduced by
This if statement and the following return statement are superfluous as you return always true.
Loading history...
123
                return true;
124
            }
125
            //exception... if dev sets both always and never
126
            //then the ones not set will be included by default.
127
            return true;
128
        }
129
    }
130
}
131