1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SunnysideUp\ShareThis; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Assets\Image; |
6
|
|
|
use SilverStripe\Core\Config\Config; |
7
|
|
|
use SilverStripe\Forms\LiteralField; |
8
|
|
|
use SilverStripe\ORM\DataObject; |
9
|
|
|
use SilverStripe\ORM\DB; |
10
|
|
|
use SilverStripe\ORM\FieldType\DBField; |
11
|
|
|
use SilverStripe\ORM\ValidationResult; |
12
|
|
|
use SilverStripe\Security\Permission; |
13
|
|
|
use SilverStripe\Security\PermissionProvider; |
14
|
|
|
use SunnysideUp\ShareThis\ShareThisOptions; |
15
|
|
|
use SunnysideUp\ShareThis\ShareThisSTE; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author nicolaas[at]sunnysideup.co.nz |
19
|
|
|
* @description: list of Share This Options that can be shown |
20
|
|
|
* @todo finish onAfterWrite and delete objects |
21
|
|
|
*/ |
22
|
|
|
// class ShareThisDataObject extends DataObject implements PermissionProvider |
23
|
|
|
class ShareThisDataObject extends DataObject |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private static $table_name = 'ShareThisDataObject'; |
|
|
|
|
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
private static $permission_framework = [ |
|
|
|
|
34
|
|
|
"SOCIAL_MEDIA" => [ |
35
|
|
|
'name' => "Social Media Management", |
36
|
|
|
'category' => "Social Media", |
37
|
|
|
'help' => 'Edit relationships, links and data of various social media platforms.', |
38
|
|
|
'sort' => 0 |
39
|
|
|
] |
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
private static $db = [ |
|
|
|
|
46
|
|
|
'Title' => 'Varchar(20)', |
47
|
|
|
'IncludeThisIcon' => 'Boolean', |
48
|
|
|
'IncludeThisIconInExtendedList' => 'Boolean', |
49
|
|
|
'Sort' => 'Int' |
50
|
|
|
]; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var array |
54
|
|
|
*/ |
55
|
|
|
private static $has_one = [ |
|
|
|
|
56
|
|
|
'AlternativeIcon' => Image::class |
57
|
|
|
]; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var array |
61
|
|
|
*/ |
62
|
|
|
private static $casting = [ |
|
|
|
|
63
|
|
|
'Icon' => 'HTMLText', |
64
|
|
|
'IncludeThisIconNice' => 'Varchar', |
65
|
|
|
'IncludeThisIconInExtendedListNice' => 'IncludeThisIconInExtendedList' |
66
|
|
|
]; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @var array |
70
|
|
|
*/ |
71
|
|
|
private static $field_labels = [ |
|
|
|
|
72
|
|
|
'Title' => 'Name', |
73
|
|
|
'IncludeThisIcon' => 'Include in main list', |
74
|
|
|
'IncludeThisIconNice' => 'Include in primary list', |
75
|
|
|
'IncludeThisIconInExtendedList' => 'Include in secondary list', |
76
|
|
|
'IncludeThisIconInExtendedListNice' => 'Include in secondary list', |
77
|
|
|
'Sort' => 'Sort Index (lower numbers shown first)', |
78
|
|
|
'AlternativeIcon' => 'Optional Alternative Icon (can be any size, a 32px by 32px square is recommended)' |
79
|
|
|
]; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @var array |
83
|
|
|
*/ |
84
|
|
|
private static $summary_fields = [ |
|
|
|
|
85
|
|
|
'Title' => 'Name', |
86
|
|
|
]; |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @var string |
90
|
|
|
*/ |
91
|
|
|
private static $singular_name = 'Icon to share this page'; |
|
|
|
|
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @var string |
95
|
|
|
*/ |
96
|
|
|
private static $plural_name = 'Icons to share this page'; |
|
|
|
|
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @var string |
100
|
|
|
*/ |
101
|
|
|
private static $default_sort = 'IncludeThisIcon DESC, IncludeThisIconInExtendedList ASC, Sort ASC, Title ASC'; |
|
|
|
|
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
|
|
public function providePermissions() |
107
|
|
|
{ |
108
|
|
|
return Config::inst()->get(ShareThisDataObject::class, "permission_framework"); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return boolean |
113
|
|
|
*/ |
114
|
|
|
public function canView($member = null) |
115
|
|
|
{ |
116
|
|
|
return Permission::checkMember($member, 'SOCIAL_MEDIA'); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return boolean |
121
|
|
|
*/ |
122
|
|
|
public function canCreate($member = null, $context = []) |
123
|
|
|
{ |
124
|
|
|
return Permission::checkMember($member, 'SOCIAL_MEDIA'); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @return boolean |
129
|
|
|
*/ |
130
|
|
|
public function canEdit($member = null) |
131
|
|
|
{ |
132
|
|
|
return Permission::checkMember($member, 'SOCIAL_MEDIA'); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return boolean |
137
|
|
|
*/ |
138
|
|
|
public function canDelete($member = null) |
139
|
|
|
{ |
140
|
|
|
return Permission::checkMember($member, 'SOCIAL_MEDIA'); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @return string |
145
|
|
|
*/ |
146
|
|
|
public function IncludeThisIconNice() |
147
|
|
|
{ |
148
|
|
|
return $this->getIncludeThisIconNice(); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @return string |
153
|
|
|
*/ |
154
|
|
|
public function getIncludeThisIconNice() |
155
|
|
|
{ |
156
|
|
|
return $this->IncludeThisIcon ? "Yes" : "No" ; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @return string |
161
|
|
|
*/ |
162
|
|
|
public function IncludeThisIconInExtendedListNice() |
163
|
|
|
{ |
164
|
|
|
return $this->getIncludeThisIconInExtendedListNice(); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @return string |
169
|
|
|
*/ |
170
|
|
|
public function getIncludeThisIconInExtendedListNice() |
171
|
|
|
{ |
172
|
|
|
return $this->IncludeThisIconInExtendedList ? "Yes" : "No" ; |
|
|
|
|
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Icon |
177
|
|
|
*/ |
178
|
|
|
public function Icon() |
179
|
|
|
{ |
180
|
|
|
return $this->getIcon(); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Get the icon |
185
|
|
|
* |
186
|
|
|
* @return DBField [<description>] |
187
|
|
|
*/ |
188
|
|
|
public function getIcon() |
189
|
|
|
{ |
190
|
|
|
$icon = $this->AlternativeIcon(); |
|
|
|
|
191
|
|
|
if ($icon->exists()) { |
192
|
|
|
return $icon->ScaleHeight(16); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
$html = '<img src="' . SS_SHARETHIS_DIR . '/images/icons/' . strtolower($this->Title) . ".png\" alt=\"{$this->Title}\"/>"; |
196
|
|
|
|
197
|
|
|
return DBField::create_field("HTMLText", $html); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @return FieldList $fields |
|
|
|
|
202
|
|
|
*/ |
203
|
|
|
public function getCMSFields() |
204
|
|
|
{ |
205
|
|
|
$fields = parent::getCMSFields(); |
206
|
|
|
if (class_exists("DataObjectSorterDOD")) { |
207
|
|
|
$fields->addFieldToTab("Root.Sort", LiteralField::create("SortShortList", $this->dataObjectSorterPopupLink("IncludeThisIcon", 1, "<h3>Sort Main Icons</h3>"))); |
|
|
|
|
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
return $fields; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @return void |
215
|
|
|
*/ |
216
|
|
|
public function onAfterWrite() |
217
|
|
|
{ |
218
|
|
|
parent::onAfterWrite(); |
219
|
|
|
$objects = ShareThisDataObject::get()->filter('Title', $this->Title)->exclude('ID', $this->ID); |
|
|
|
|
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @return ValidationResult $result |
224
|
|
|
*/ |
225
|
|
|
public function validate() |
226
|
|
|
{ |
227
|
|
|
$result = parent::validate(); |
228
|
|
|
$bookmarks = ShareThisOptions::get_page_specific_data("", "", ""); |
229
|
|
|
if (!isset($bookmarks[$this->Title])) { |
230
|
|
|
$result->addError(sprintf( |
231
|
|
|
_t( |
232
|
|
|
'ShareThisDataObject.NON_EXISTING_TITLE', |
233
|
|
|
'This social plaform "%s" does not exist. Please change / delete the this entry.' |
234
|
|
|
), |
235
|
|
|
$this->Title |
236
|
|
|
)); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
return $result; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Setting default records |
244
|
|
|
* |
245
|
|
|
* @return void |
246
|
|
|
*/ |
247
|
|
|
public function requireDefaultRecords() |
248
|
|
|
{ |
249
|
|
|
parent::requireDefaultRecords(); |
250
|
|
|
$actualArray = ShareThisOptions::get_general_data(); |
251
|
|
|
Config::inst()->update(ShareThisSTE::class, "included_icons", []); |
|
|
|
|
252
|
|
|
Config::inst()->update(ShareThisSTE::class, "excluded_icons", []); |
253
|
|
|
ShareThisOptions::set_general_data(null); |
|
|
|
|
254
|
|
|
$fullArray = ShareThisOptions::get_general_data(); |
255
|
|
|
|
256
|
|
|
foreach ($fullArray as $key) { |
257
|
|
|
$object = ShareThisDataObject::get()->filter('Title', $key); |
258
|
|
|
|
259
|
|
|
if (!$object->exists()) { |
260
|
|
|
$object = new ShareThisDataObject(); |
261
|
|
|
$object->Title = $key; |
262
|
|
|
$style = 'excluded'; |
263
|
|
|
$object->IncludeThisIcon = false; |
|
|
|
|
264
|
|
|
|
265
|
|
|
if (in_array($key, $actualArray)) { |
|
|
|
|
266
|
|
|
$object->IncludeThisIcon = true; |
267
|
|
|
$style = 'included'; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
$object->write(); |
271
|
|
|
DB::alteration_message("Added Bookmark Icon for $key ($style)", 'created'); |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
$inc = Config::inst()->get(ShareThisSTE::class, "included_icons"); |
276
|
|
|
|
277
|
|
|
foreach ($inc as $key) { |
278
|
|
|
$object = ShareThisDataObject::get()->filter(['Title' => $key, 'IncludeThisIcon' => 0]); |
279
|
|
|
|
280
|
|
|
if ($object->exists()) { |
281
|
|
|
$object = $object->first(); |
282
|
|
|
$object->IncludeThisIcon = true; |
283
|
|
|
$object->write(); |
284
|
|
|
DB::alteration_message("Updated inclusion for $key", 'created'); |
285
|
|
|
} |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
$exc = Config::inst()->get(ShareThisSTE::class, "excluded_icons"); |
289
|
|
|
|
290
|
|
|
foreach ($exc as $key) { |
291
|
|
|
$object = ShareThisDataObject::get()->filter(['Title' => $key, 'IncludeThisIcon' => 1]); |
292
|
|
|
|
293
|
|
|
if ($object->exists()) { |
294
|
|
|
$object = $object->first(); |
295
|
|
|
$object->IncludeThisIcon = false; |
296
|
|
|
$object->write(); |
297
|
|
|
DB::alteration_message("Updated inclusion for $key", 'created'); |
298
|
|
|
} |
299
|
|
|
} |
300
|
|
|
} |
301
|
|
|
} |
302
|
|
|
|