1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SunnySideUp\ShareThis; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Assets\Image; |
6
|
|
|
use SilverStripe\Core\Config\Config; |
7
|
|
|
use SilverStripe\Security\Permission; |
8
|
|
|
use SilverStripe\ORM\FieldType\DBField; |
9
|
|
|
use SilverStripe\Forms\LiteralField; |
10
|
|
|
use SunnySideUp\ShareThis\ShareThisOptions; |
11
|
|
|
use SunnySideUp\ShareThis\ShareThisSTE; |
12
|
|
|
use SilverStripe\ORM\DB; |
13
|
|
|
use SilverStripe\ORM\DataObject; |
14
|
|
|
use SilverStripe\Security\PermissionProvider; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @author nicolaas[at]sunnysideup.co.nz |
18
|
|
|
* @description: list of Share This Options that can be shown |
19
|
|
|
* @todo finish onAfterWrite and delete objects |
20
|
|
|
*/ |
21
|
|
|
class ShareThisDataObject extends DataObject implements PermissionProvider |
22
|
|
|
{ |
23
|
|
|
private static $permission_framework = array( |
24
|
|
|
"SOCIAL_MEDIA" => array( |
25
|
|
|
'name' => "Social Media Management", |
26
|
|
|
'category' => "Social Media", |
27
|
|
|
'help' => 'Edit relationships, links and data of various social media platforms.', |
28
|
|
|
'sort' => 0 |
29
|
|
|
) |
30
|
|
|
); |
31
|
|
|
|
32
|
|
|
private static $db = array( |
33
|
|
|
'Title' => 'Varchar(20)', |
34
|
|
|
'IncludeThisIcon' => 'Boolean', |
35
|
|
|
'IncludeThisIconInExtendedList' => 'Boolean', |
36
|
|
|
'Sort' => 'Int' |
37
|
|
|
); |
38
|
|
|
|
39
|
|
|
private static $has_one = array( |
40
|
|
|
'AlternativeIcon' => Image::class |
41
|
|
|
); |
42
|
|
|
|
43
|
|
|
private static $casting = array( |
44
|
|
|
'Icon' => 'HTMLText', |
45
|
|
|
'IncludeThisIconNice' => 'Varchar', |
46
|
|
|
'IncludeThisIconInExtendedListNice' => 'IncludeThisIconInExtendedList' |
47
|
|
|
); |
48
|
|
|
|
49
|
|
|
private static $field_labels = array( |
50
|
|
|
'Title' => 'Name', |
51
|
|
|
'IncludeThisIcon' => 'Include in main list', |
52
|
|
|
'IncludeThisIconNice' => 'Include in primary list', |
53
|
|
|
'IncludeThisIconInExtendedList' => 'Include in secondary list', |
54
|
|
|
'IncludeThisIconInExtendedListNice' => 'Include in secondary list', |
55
|
|
|
'Sort' => 'Sort Index (lower numbers shown first)', |
56
|
|
|
'AlternativeIcon' => 'Optional Alternative Icon (can be any size, a 32px by 32px square is recommended)' |
57
|
|
|
); |
58
|
|
|
|
59
|
|
|
private static $summary_fields = array( |
60
|
|
|
'Icon' => 'Icon', |
61
|
|
|
'Title' => 'Name', |
62
|
|
|
'IncludeThisIconNice' => 'IncludeThisIcon' |
63
|
|
|
//'IncludeThisIconInExtendedListNice' => 'IncludeThisIconInExtendedList' |
64
|
|
|
); |
65
|
|
|
|
66
|
|
|
private static $singular_name = 'Icon to share this page'; |
67
|
|
|
|
68
|
|
|
private static $plural_name = 'Icons to share this page'; |
69
|
|
|
|
70
|
|
|
private static $default_sort = 'IncludeThisIcon DESC, IncludeThisIconInExtendedList ASC, Sort ASC, Title ASC'; |
71
|
|
|
|
72
|
|
|
public function providePermissions() |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
return Config::inst()->get(ShareThisDataObject::class, "permission_framework"); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function canView($member = null) |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
return Permission::checkMember($member, 'SOCIAL_MEDIA'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function canCreate($member = null, $context = []) |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
return Permission::checkMember($member, 'SOCIAL_MEDIA'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function canEdit($member = null) |
|
|
|
|
88
|
|
|
{ |
89
|
|
|
return Permission::checkMember($member, 'SOCIAL_MEDIA'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function canDelete($member = null) |
|
|
|
|
93
|
|
|
{ |
94
|
|
|
return Permission::checkMember($member, 'SOCIAL_MEDIA'); |
95
|
|
|
} |
96
|
|
|
public function IncludeThisIconNice() |
97
|
|
|
{ |
98
|
|
|
return $this->getIncludeThisIconNice(); |
99
|
|
|
} |
100
|
|
|
public function getIncludeThisIconNice() |
101
|
|
|
{ |
102
|
|
|
return $this->IncludeThisIcon ? "YES" : "NO" ; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function IncludeThisIconInExtendedListNice() |
106
|
|
|
{ |
107
|
|
|
return $this->getIncludeThisIconInExtendedListNice(); |
108
|
|
|
} |
109
|
|
|
public function getIncludeThisIconInExtendedListNice() |
110
|
|
|
{ |
111
|
|
|
return $this->IncludeThisIconInExtendedList ? "YES" : "NO" ; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function Icon() |
|
|
|
|
115
|
|
|
{ |
116
|
|
|
return $this->getIcon(); |
117
|
|
|
} |
118
|
|
|
public function getIcon() |
|
|
|
|
119
|
|
|
{ |
120
|
|
|
$icon = $this->AlternativeIcon(); |
121
|
|
|
if ($icon->exists()) { |
122
|
|
|
return $icon->SetHeight(16); |
123
|
|
|
} |
124
|
|
|
$html = '<img src="' . SS_SHARETHIS_DIR . '/images/icons/' . strtolower($this->Title) . ".png\" alt=\"{$this->Title}\"/>"; |
125
|
|
|
return DBField::create_field("HTMLText", $html); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function getCMSFields() |
|
|
|
|
129
|
|
|
{ |
130
|
|
|
$fields = parent::getCMSFields(); |
131
|
|
|
if (class_exists("DataObjectSorterDOD")) { |
132
|
|
|
$fields->addFieldToTab("Root.Sort", new LiteralField("SortShortList", $this->dataObjectSorterPopupLink("IncludeThisIcon", 1, "<h3>Sort Main Icons</h3>"))); |
133
|
|
|
} |
134
|
|
|
//$fields->replaceField('Title', new LiteralField('Title', "<p>{$this->Icon}<span>{$this->Title}</span></p>")); |
135
|
|
|
return $fields; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function onAfterWrite() |
139
|
|
|
{ |
140
|
|
|
parent::onAfterWrite(); |
141
|
|
|
$objects = ShareThisDataObject::get()->filter('Title', $this->Title)->exclude('ID', $this->ID); |
|
|
|
|
142
|
|
|
//$objects->delete(); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function validate() |
|
|
|
|
146
|
|
|
{ |
147
|
|
|
$result = parent::validate(); |
148
|
|
|
$bookmarks = ShareThisOptions::get_page_specific_data("", "", ""); |
149
|
|
|
if (!isset($bookmarks[$this->Title])) { |
150
|
|
|
$result->error(sprintf( |
151
|
|
|
_t( |
152
|
|
|
'ShareThisDataObject.NON_EXISTING_TITLE', |
153
|
|
|
'This social plaform "%s" does not exist. Please change / delete the this entry.' |
154
|
|
|
), |
155
|
|
|
$this->Title |
156
|
|
|
)); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return $result; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function requireDefaultRecords() |
163
|
|
|
{ |
164
|
|
|
parent::requireDefaultRecords(); |
165
|
|
|
$actualArray = ShareThisOptions::get_general_data(); |
166
|
|
|
Config::inst()->update(ShareThisSTE::class, "included_icons", array()); |
167
|
|
|
Config::inst()->update(ShareThisSTE::class, "excluded_icons", array()); |
168
|
|
|
ShareThisOptions::set_general_data(null); |
169
|
|
|
$fullArray = ShareThisOptions::get_general_data(); |
170
|
|
|
foreach ($fullArray as $key) { |
171
|
|
|
$object = ShareThisDataObject::get()->filter('Title', $key); |
172
|
|
|
if (! $object->exists()) { |
173
|
|
|
$object = new ShareThisDataObject(); |
174
|
|
|
$object->Title = $key; |
175
|
|
|
$style = 'excluded'; |
176
|
|
|
$object->IncludeThisIcon = false; |
177
|
|
|
if (in_array($key, $actualArray)) { |
178
|
|
|
$object->IncludeThisIcon = true; |
179
|
|
|
$style = 'included'; |
180
|
|
|
} |
181
|
|
|
$object->write(); |
182
|
|
|
DB::alteration_message("Added Bookmark Icon for $key ($style)", 'created'); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
$inc = Config::inst()->get(ShareThisSTE::class, "included_icons"); |
186
|
|
View Code Duplication |
foreach ($inc as $key) { |
|
|
|
|
187
|
|
|
$object = ShareThisDataObject::get()->filter(array('Title' => $key, 'IncludeThisIcon' => 0)); |
188
|
|
|
if ($object->exists()) { |
189
|
|
|
$object = $object->first(); |
190
|
|
|
$object->IncludeThisIcon = true; |
191
|
|
|
$object->write(); |
192
|
|
|
DB::alteration_message("Updated inclusion for $key", 'created'); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
$exc = Config::inst()->get(ShareThisSTE::class, "excluded_icons"); |
196
|
|
View Code Duplication |
foreach ($exc as $key) { |
|
|
|
|
197
|
|
|
$object = ShareThisDataObject::get()->filter(array('Title' => $key, 'IncludeThisIcon' => 1)); |
198
|
|
|
if ($object->exists()) { |
199
|
|
|
$object = $object->first(); |
200
|
|
|
$object->IncludeThisIcon = false; |
201
|
|
|
$object->write(); |
202
|
|
|
DB::alteration_message("Updated inclusion for $key", 'created'); |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
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.