ShareThisSTE::IncludeShareAll()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
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\Dev\Debug;
6
use SilverStripe\Forms\FieldList;
7
use SilverStripe\Forms\HeaderField;
8
use SilverStripe\Forms\CheckboxField;
9
use SilverStripe\Forms\LiteralField;
10
use SunnysideUp\ShareThis\ShareThisOptions;
11
use SilverStripe\View\Requirements;
12
use SilverStripe\Core\Config\Config;
13
use SilverStripe\Core\Convert;
14
use SilverStripe\View\ArrayData;
15
use SilverStripe\ORM\ArrayList;
16
use SunnysideUp\ShareThis\ShareThisDataObject;
17
use SilverStripe\CMS\Model\SiteTreeExtension;
18
19
/**
20
 * Add a field to each SiteTree object and it's subclasses to enable Share icons.
21
 * @author nicolaas [at] sunnysideup.co.nz
22
 * @inspiration: Silverstripe Original Module - full credits to them.  We made our own to improve their module
23
 * @todo fix populateDefaults to make sure SiteConfig table is built first
24
 */
25
class ShareThisSTE extends SiteTreeExtension
26
{
27
28
    /**
29
     * Use the font-awesome icon collection?
30
     * @var Boolean
31
     */
32
    private static $use_font_awesome = true;
0 ignored issues
show
introduced by
The private property $use_font_awesome is not used, and could be removed.
Loading history...
33
34
    /**
35
     * list of sitetree extending classnames where
36
     * the ShareThis functionality should be included
37
     * @var Array
38
     */
39
    private static $always_include_in = [];
0 ignored issues
show
introduced by
The private property $always_include_in is not used, and could be removed.
Loading history...
40
41
    /**
42
     * list of sitetree extending classnames where
43
     * the ShareThis functionality should NEVER be included
44
     * @var Array
45
     */
46
    private static $never_include_in = [];
0 ignored issues
show
introduced by
The private property $never_include_in is not used, and could be removed.
Loading history...
47
48
    /**
49
    * use BW icons
50
    * @var boolean
51
    */
52
    private static $use_bw_effect = false;
0 ignored issues
show
introduced by
The private property $use_bw_effect is not used, and could be removed.
Loading history...
53
54
    /**
55
    * specify icons to be included, if left empty, this variable will be ignored
56
    * We have this variable so that you can setup a bunch of default icons
57
    * @var array
58
    */
59
    private static $included_icons = [];
0 ignored issues
show
introduced by
The private property $included_icons is not used, and could be removed.
Loading history...
60
61
    /**
62
    * specify icons to be excluded, if left empty, this variable will be ignored
63
    * We have this variable so that you can setup a bunch of default icons
64
    * @var array
65
    */
66
    private static $excluded_icons = [];
0 ignored issues
show
introduced by
The private property $excluded_icons is not used, and could be removed.
Loading history...
67
68
    /**
69
     * standard SS method
70
     * @var Array
71
     **/
72
    private static $db = array(
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
73
        'ShareIcons' => 'Boolean'
74
    );
75
76
    /**
77
     * @param  FieldList $fields
78
     *
79
     * @return FieldList $fields
80
     */
81
    public function updateCMSFields(FieldList $fields)
82
    {
83
        if ($this->applyToOwnerClass()) {
84
            $config = $this->owner->getSiteConfig();
85
86
            if (! $config->AlwaysIncludeShareThisLinks) {
87
                $fields->addFieldToTab('Root.SocialMedia', HeaderField::create('ShareThisHeader', 'Allow users to share this page'));
88
89
                $fields->addFieldToTab('Root.SocialMedia', CheckboxField::create('ShareIcons', 'Show Share Icons on this page', $config->IncludeByDefault));
90
91
                $fields->addFieldToTab('Root.SocialMedia', LiteralField::create('LinkToSiteConfigSocialMedia', "<p>Note: make sure to review the social media settings in the <a href=\"{$config->CMSEditLink()}\">Site Config</a>.</p>"));
92
            }
93
94
            $list = ShareThisOptions::get_all_options($this->owner->Title, $this->owner->Link(), $this->owner->MetaDescription);
95
96
            $fields->addFieldToTab('Root.SocialMedia', HeaderField::create('ShareThisNow', 'Share this page on your favourite social media sites...'));
97
98
            $html = "<div><p>Click on any of the icons below to share the '<i>{$this->owner->Title}</i>' page. Any click will open a new tab/window where you will need to enter your login details.</p>";
99
100
            foreach ($list as $key => $innerArray) {
101
                if (! isset($innerArray['click'])) {
102
                    $html .= "<span><a href=\"{$innerArray['url']}\" target=\"_blank\" style=\"whitespace: nowrap; display: inline-block;\"><img src=\"" . SS_SHARETHIS_DIR . "/images/icons/$key.png\" alt=\"$key\"/>{$innerArray['title']}</a></span>&nbsp;&nbsp;";
103
                }
104
            }
105
106
            $html .= '</div>';
107
            $fields->addFieldToTab('Root.SocialMedia', LiteralField::create('ShareNow', $html));
108
        }
109
110
        return $fields;
111
    }
112
113
    /**
114
     * Show the sharing icons
115
     */
116
    public function getShowShareIcons()
117
    {
118
        if ($this->applyToOwnerClass()) {
119
            $config = $this->owner->getSiteConfig();
120
            if ($config->AlwaysIncludeShareThisLinks) {
121
                return true;
122
            }
123
            return $this->owner->ShareIcons;
124
        }
125
    }
126
127
    /**
128
     * Get the sharing icons
129
     */
130
    public function getShareIcons()
131
    {
132
        $bookmarks = $this->makeBookmarks('IncludeThisIcon');
133
        return $this->makeShareIcons($bookmarks);
134
    }
135
136
    /**
137
     * Grabbing front end dependencies for the expanded sharing list with some extra
138
     * functionality
139
     */
140
    public function ShareAllExpandedList()
141
    {
142
        Requirements::javascript('silverstripe/admin: thirdparty/jquery/jquery.min.js');
143
        Requirements::javascript('sunnysideup/sharethis: javascript/ShareAllExpandedList.js');
144
        $bookmarks = $this->makeBookmarks('IncludeThisIconInExtendedList');
145
        return $this->makeShareIcons($bookmarks);
146
    }
147
148
    /**
149
     * Include share all
150
     */
151
    public function IncludeShareAll()
152
    {
153
        $config = $this->owner->getSiteConfig();
154
        return $config->ShareThisAllInOne;
155
    }
156
157
    /**
158
     * @return boolean
159
     */
160
    public function getShareAll()
161
    {
162
        if ($this->IncludeShareAll()) {
163
            return ShareThisOptions::get_share_all();
164
        }
165
    }
166
167
    /**
168
     * @return array
169
     */
170
    protected function makeShareIcons($bookmarks)
171
    {
172
        $icons = [];
173
        if ($bookmarks) {
174
            $useFontAwesome = Config::inst()->get(ShareThisSTE::class, "use_font_awesome");
175
            Requirements::themedCSS('SocialNetworking', "sharethis"); // ALSO  added in template
176
177
            if ($useFontAwesome) {
178
                Requirements::css("//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css");
179
            }
180
181
            Requirements::javascript('sunnysideup/sharethis: javascript/shareThis.js');
182
183
            if (Config::inst()->get(ShareThisSTE::class, "use_bw_effect")) {
184
                Requirements::customScript('sharethis.set_use_BW(true);', 'ShareThisBWEffect');
185
            }
186
187
            foreach ($bookmarks as $key => $bookmark) {
188
                if (isset($bookmark['title']) && isset($bookmark['url'])) {
189
                    $icon = array(
190
                        'Title' => Convert::raw2att($bookmark['title']),
191
                        'URL' => $bookmark['url'],
192
                        'Key' => $key,
193
                        'ImageSource' => "sharethis/images/icons/$key.png",
194
                        'FAIcon' => $bookmark["faicon"],
195
                        'UseStandardImage' => true
196
                    );
197
198
                    if (isset($bookmark['click'])) {
199
                        $icon['OnClick'] = $bookmark['click'];
200
                    }
201
202
                    if ($useFontAwesome) {
203
                        $icon['ImageSource'] = null;
204
                        $icon['UseStandardImage'] = false;
205
                        $icon['FAIcon'] = $bookmark["faicon"];
206
                    }
207
208
                    if (isset($bookmark['icon'])) {
209
                        $icon['ImageSource'] = $bookmark['icon'];
210
                        $icon['UseStandardImage'] = false;
211
                        $icon['FAIcon'] = null;
212
                    }
213
214
                    $icons[] = new ArrayData($icon);
215
                } else {
216
                    Debug::show("Title of url not defined for $key");
217
                }
218
            }
219
        }
220
221
        return new ArrayList($icons);
222
    }
223
224
    /**
225
     * Creating the bookmarks
226
     */
227
    protected function makeBookmarks($field)
228
    {
229
        $finalBookmarks = [];
230
231
        $bookmarks = ShareThisOptions::get_page_specific_data($this->owner->Title, $this->owner->Link(), $this->owner->MetaDescription);
232
233
        $objects = ShareThisDataObject::get()
234
            ->filter($field, 1)
235
            ->sort(array('Sort' => 'ASC', 'Title' => 'ASC'));
236
        if ($objects->count()) {
237
            foreach ($objects as $obj) {
238
                if (isset($bookmarks[$obj->Title])) {
239
                    $finalBookmarks[$obj->Title] = $bookmarks[$obj->Title];
240
241
                    if ($obj->AlternativeIconID && $obj->AlternativeIcon()->exists()) {
242
                        $finalBookmarks[$obj->Title]['icon'] = $obj->AlternativeIcon()->Link();
243
                    }
244
                }
245
            }
246
        } else {
247
            $finalBookmarks = $bookmarks;
248
        }
249
250
        return $finalBookmarks;
251
    }
252
253
    /**
254
     * @return boolean
255
     */
256
    private function applyToOwnerClass()
257
    {
258
        $always = Config::inst()->get(ShareThisSTE::class, "always_include_in");
259
        $never = Config::inst()->get(ShareThisSTE::class, "never_include_in");
260
        if (count($always) == 0 && count($never) == 0) {
261
            return true;
262
        } elseif (count($never) && count($always) == 0) {
263
            if (in_array($this->owner->ClassName, $never)) {
264
                return false;
265
            }
266
267
            return true;
268
        } elseif (count($always) && count($never) == 0) {
269
            if (in_array($this->owner->ClassName, $always)) {
270
                return true;
271
            }
272
273
            return false;
274
        } elseif (count($never) && count($always)) {
275
            if (in_array($this->owner->ClassName, $never)) {
276
                return false;
277
            }
278
279
            if (in_array($this->owner->ClassName, $always)) {
280
                return true;
281
            }
282
283
            //exception... if dev sets both always and never
284
            //then the ones not set will be included by default.
285
            return true;
286
        } else {
287
            user_error("Strange condition!", E_USER_NOTICE);
288
        }
289
    }
290
}
291