sunnysideup /
silverstripe-sharethis
| 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; |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 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 = []; |
||
|
0 ignored issues
–
show
|
|||
| 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 = []; |
||
|
0 ignored issues
–
show
|
|||
| 42 | |||
| 43 | /** |
||
| 44 | * @var array |
||
| 45 | */ |
||
| 46 | private static $db = [ |
||
|
0 ignored issues
–
show
|
|||
| 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(); |
||
| 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(); |
||
| 75 | if ($config->AlwaysIncludeSocialNetworkingLinks) { |
||
| 76 | return true; |
||
| 77 | } |
||
| 78 | return $this->owner->HasSocialNetworkingLinks; |
||
| 79 | } |
||
| 80 | return false; |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @return SocialNetworkingLinksDataObject |
||
| 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 |
||
| 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 | if (count($never) && count($always) == 0) { |
||
| 107 | if (in_array($this->owner->ClassName, $never)) { |
||
| 108 | return false; |
||
| 109 | } |
||
| 110 | return true; |
||
| 111 | } |
||
| 112 | if (count($always) && count($never) == 0) { |
||
| 113 | if (in_array($this->owner->ClassName, $always)) { |
||
| 114 | return true; |
||
| 115 | } |
||
| 116 | return false; |
||
| 117 | } |
||
| 118 | if (count($never) && count($always)) { |
||
| 119 | if (in_array($this->owner->ClassName, $never)) { |
||
| 120 | return false; |
||
| 121 | } |
||
| 122 | if (in_array($this->owner->ClassName, $always)) { |
||
| 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 |