|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class TwitterAdmin extends ModelAdmin |
|
|
|
|
|
|
4
|
|
|
{ |
|
5
|
|
|
public static $dependencies = [ |
|
6
|
|
|
'twitterService' => '%$TwitterService', |
|
7
|
|
|
]; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @var TwitterService |
|
11
|
|
|
*/ |
|
12
|
|
|
public $twitterService; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
private static $url_segment = 'twitter'; |
|
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
private static $menu_icon = 'silverstripe-twitter/images/twitter-logo.png'; |
|
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
private static $menu_title = 'Twitter'; |
|
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var array |
|
31
|
|
|
*/ |
|
32
|
|
|
private static $managed_models = [ |
|
|
|
|
|
|
33
|
|
|
'TwitterAccount', |
|
34
|
|
|
]; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var array |
|
38
|
|
|
*/ |
|
39
|
|
|
private static $allowed_actions = [ |
|
|
|
|
|
|
40
|
|
|
'ImportForm', |
|
41
|
|
|
'SearchForm', |
|
42
|
|
|
'OAuth', |
|
43
|
|
|
]; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param int $id |
|
47
|
|
|
* |
|
48
|
|
|
* @param FieldList $fields |
|
49
|
|
|
* |
|
50
|
|
|
* @return Form |
|
51
|
|
|
*/ |
|
52
|
|
|
public function getEditForm($id = null, $fields = null) |
|
53
|
|
|
{ |
|
54
|
|
|
$form = parent::getEditForm($id, $fields); |
|
55
|
|
|
|
|
56
|
|
|
$gridFieldName = $this->sanitiseClassName($this->modelClass); |
|
57
|
|
|
$gridFieldConfig = $form->Fields()->fieldByName($gridFieldName)->getConfig(); |
|
58
|
|
|
|
|
59
|
|
|
$gridFieldConfig->removeComponentsByType('GridFieldPrintButton'); |
|
60
|
|
|
$gridFieldConfig->removeComponentsByType('GridFieldExportButton'); |
|
61
|
|
|
$gridFieldConfig |
|
62
|
|
|
->getComponentByType('GridFieldAddNewButton') |
|
63
|
|
|
->setButtonName(_t('Twitter.ButtonLabelAddAccount', 'Add account')); |
|
64
|
|
|
$gridFieldConfig |
|
65
|
|
|
->getComponentByType('GridFieldDetailForm') |
|
66
|
|
|
->setItemRequestClass('TwitterAdminRequestHandler'); |
|
67
|
|
|
|
|
68
|
|
|
return $form; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Handles failed OAuth attempts. |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $url |
|
75
|
|
|
* |
|
76
|
|
|
* @param Form $form |
|
77
|
|
|
* |
|
78
|
|
|
* @return Controller |
|
79
|
|
|
*/ |
|
80
|
|
|
public function handleOAuthError($url, $form, $message = null) |
|
81
|
|
|
{ |
|
82
|
|
|
$message = $message ? $message : _t( |
|
83
|
|
|
'Twitter.MessageOAuthErrorResponse', |
|
84
|
|
|
'Unable to authorise account. Please try again.' |
|
85
|
|
|
); |
|
86
|
|
|
|
|
87
|
|
|
$form->sessionMessage($message, 'bad'); |
|
88
|
|
|
|
|
89
|
|
|
user_error($message, E_USER_WARNING); |
|
90
|
|
|
|
|
91
|
|
|
return Controller::curr()->redirect($url); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* OAuth callback handler. |
|
96
|
|
|
* |
|
97
|
|
|
* @param SS_HTTPRequest $request |
|
98
|
|
|
*/ |
|
99
|
|
|
public function OAuth($request) |
|
100
|
|
|
{ |
|
101
|
|
|
$denied = $request->getVar('denied'); |
|
102
|
|
|
$token = $request->getVar('oauth_token'); |
|
103
|
|
|
$verifier = $request->getVar('oauth_verifier'); |
|
104
|
|
|
|
|
105
|
|
|
$sessionRequestToken = $this->twitterService->getSessionOAuthToken(); |
|
106
|
|
|
|
|
107
|
|
|
$this->twitterService->clearSessionOAuthToken(); |
|
108
|
|
|
|
|
109
|
|
|
$form = $this->getEditForm(); |
|
110
|
|
|
|
|
111
|
|
|
if ( |
|
112
|
|
|
$denied || |
|
113
|
|
|
!$token || |
|
114
|
|
|
!$verifier || |
|
115
|
|
|
!$sessionRequestToken || |
|
|
|
|
|
|
116
|
|
|
!array_key_exists('oauth_token', $sessionRequestToken) || |
|
117
|
|
|
!array_key_exists('oauth_token_secret', $sessionRequestToken) || |
|
118
|
|
|
$token !== $sessionRequestToken['oauth_token'] |
|
119
|
|
|
) { |
|
120
|
|
|
return $this->handleOAuthError($this->Link(), $form); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
$accessToken = $this->twitterService->getAccessToken( |
|
124
|
|
|
$sessionRequestToken['oauth_token'], |
|
125
|
|
|
$sessionRequestToken['oauth_token_secret'], |
|
126
|
|
|
$verifier |
|
127
|
|
|
); |
|
128
|
|
|
|
|
129
|
|
|
if (!$accessToken) { |
|
130
|
|
|
return $this->handleOAuthError($this->Link(), $form); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
$twitterAccount = TwitterAccount::get() |
|
134
|
|
|
->filter(['Title' => $accessToken['screen_name']]) |
|
135
|
|
|
->first(); |
|
136
|
|
|
|
|
137
|
|
|
if (!$twitterAccount) { |
|
138
|
|
|
return $this->handleOAuthError($this->Link(), $form); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
$twitterAccount->setAccessToken($accessToken); |
|
142
|
|
|
$twitterAccount->write(); |
|
143
|
|
|
|
|
144
|
|
|
$form->sessionMessage(_t('Twitter.MessageOAuthSuccess', 'Successfully authorised your account.'), 'good'); |
|
145
|
|
|
|
|
146
|
|
|
return Controller::curr()->redirect($this->Link()); |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
class TwitterAdminRequestHandler extends GridFieldDetailForm_ItemRequest |
|
|
|
|
|
|
151
|
|
|
{ |
|
152
|
|
|
private static $allowed_actions = [ |
|
|
|
|
|
|
153
|
|
|
'edit', |
|
154
|
|
|
'view', |
|
155
|
|
|
'ItemEditForm' |
|
156
|
|
|
]; |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @return Form |
|
160
|
|
|
*/ |
|
161
|
|
|
public function ItemEditForm() |
|
162
|
|
|
{ |
|
163
|
|
|
$form = parent::ItemEditForm(); |
|
164
|
|
|
|
|
165
|
|
|
$formActions = $form->Actions(); |
|
166
|
|
|
|
|
167
|
|
|
if ($actions = $this->record->getCMSActions()) { |
|
168
|
|
|
foreach ($actions as $action) { |
|
169
|
|
|
$formActions->push($action); |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
return $form; |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.