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 = array(); |
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 = array(); |
42
|
|
|
|
43
|
|
|
private static $db = array( |
44
|
|
|
'HasSocialNetworkingLinks' => 'Boolean' |
45
|
|
|
); |
46
|
|
|
|
47
|
|
|
public function updateCMSFields(FieldList $fields) |
48
|
|
|
{ |
49
|
|
|
if ($this->applyToOwnerClass()) { |
50
|
|
|
$config = $this->owner->getSiteConfig(); |
51
|
|
|
if (! $config->AlwaysIncludeSocialNetworkingLinks) { |
52
|
|
|
$fields->addFieldToTab('Root.SocialMedia', new HeaderField('SocialNetworksHeader', 'Ask visitors to JOIN YOU on your social media')); |
53
|
|
|
$fields->addFieldToTab('Root.SocialMedia', new CheckboxField('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!')); |
54
|
|
|
} |
55
|
|
|
$fields->addFieldToTab('Root.SocialMedia', new LiteralField('LinkToSiteConfigSocialMedia', "<p>There are more social media settings in the <a href=\"{$config->CMSEditLink()}\">Site Config</a>.</p>")); |
56
|
|
|
} |
57
|
|
|
return $fields; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function ShowSocialNetworks() |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
if ($this->applyToOwnerClass()) { |
63
|
|
|
$config = $this->owner->getSiteConfig(); |
64
|
|
|
if ($config->AlwaysIncludeSocialNetworkingLinks) { |
65
|
|
|
return true; |
66
|
|
|
} |
67
|
|
|
return $this->owner->HasSocialNetworkingLinks; |
68
|
|
|
} |
69
|
|
|
return false; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function SocialNetworks() |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
Requirements::themedCSS('SocialNetworking', "sharethis"); |
75
|
|
|
if (Config::inst()->get(SocialNetworksSTE::class, "use_font_awesome")) { |
76
|
|
|
Requirements::css("//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css"); |
77
|
|
|
} |
78
|
|
|
return SocialNetworkingLinksDataObject::get(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
private function applyToOwnerClass() |
82
|
|
|
{ |
83
|
|
|
$always = Config::inst()->get(SocialNetworksSTE::class, "always_include_in"); |
84
|
|
|
$never = Config::inst()->get(SocialNetworksSTE::class, "never_include_in"); |
85
|
|
|
if (count($always) == 0 && count($never) == 0) { |
86
|
|
|
return true; |
87
|
|
|
} |
88
|
|
View Code Duplication |
if (count($never) && count($always) == 0) { |
|
|
|
|
89
|
|
|
if (in_array($this->owner->ClassName, $never)) { |
|
|
|
|
90
|
|
|
return false; |
91
|
|
|
} |
92
|
|
|
return true; |
93
|
|
|
} |
94
|
|
View Code Duplication |
if (count($always) && count($never) == 0) { |
|
|
|
|
95
|
|
|
if (in_array($this->owner->ClassName, $always)) { |
|
|
|
|
96
|
|
|
return true; |
97
|
|
|
} |
98
|
|
|
return false; |
99
|
|
|
} |
100
|
|
View Code Duplication |
if (count($never) && count($always)) { |
|
|
|
|
101
|
|
|
if (in_array($this->owner->ClassName, $never)) { |
102
|
|
|
return false; |
103
|
|
|
} |
104
|
|
|
if (in_array($this->owner->ClassName, $always)) { |
|
|
|
|
105
|
|
|
return true; |
106
|
|
|
} |
107
|
|
|
//exception... if dev sets both always and never |
108
|
|
|
//then the ones not set will be included by default. |
109
|
|
|
return true; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
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.