|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class TwitterAccount extends DataObject |
|
|
|
|
|
|
4
|
|
|
{ |
|
5
|
|
|
public static $dependencies = [ |
|
6
|
|
|
'twitterService' => '%$TwitterService', |
|
7
|
|
|
]; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @var TwitterService |
|
11
|
|
|
*/ |
|
12
|
|
|
public $twitterService; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var array |
|
16
|
|
|
*/ |
|
17
|
|
|
private static $db = [ |
|
|
|
|
|
|
18
|
|
|
'Title' => 'Varchar', |
|
19
|
|
|
'AccessToken' => 'Text', |
|
20
|
|
|
]; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @return FieldList |
|
24
|
|
|
*/ |
|
25
|
|
|
public function getCMSFields() |
|
26
|
|
|
{ |
|
27
|
|
|
$fields = parent::getCMSFields(); |
|
28
|
|
|
|
|
29
|
|
|
$fields->removeByName('AccessToken'); |
|
30
|
|
|
|
|
31
|
|
|
if ($this->isAuthorised()) { |
|
32
|
|
|
$usernameField = LiteralField::create( |
|
33
|
|
|
'Title', |
|
34
|
|
|
'<p class="message good">' . |
|
35
|
|
|
_t('Twitter.AccountAuthorisedMessage', 'This account has been authorised.') . |
|
36
|
|
|
'</p>' . |
|
37
|
|
|
'<div class="field">' . |
|
38
|
|
|
'<label class="left">' . |
|
39
|
|
|
_t('Twitter.FieldLabelTitle', 'Username') . |
|
40
|
|
|
'</label>' . |
|
41
|
|
|
'<div class="middleColumn" style="padding-top:8px;">' . |
|
42
|
|
|
'<a ' . |
|
43
|
|
|
"href='https://www.twitter.com/{$this->Title}' " . |
|
|
|
|
|
|
44
|
|
|
'title="View on Twitter" ' . |
|
45
|
|
|
'target="_blank">' . |
|
46
|
|
|
$this->Title . |
|
|
|
|
|
|
47
|
|
|
'</a>' . |
|
48
|
|
|
'</div>' . |
|
49
|
|
|
'</div>' |
|
50
|
|
|
); |
|
51
|
|
|
} else { |
|
52
|
|
|
$usernameField = Textfield::create('Title', _t('Twitter.FieldLabelTitle', 'Username')); |
|
53
|
|
|
$usernameField->setDescription( |
|
54
|
|
|
_t( |
|
55
|
|
|
'Twitter.FieldDescriptionTitle', |
|
56
|
|
|
'The Twitter account you want to pull media from.' |
|
57
|
|
|
) |
|
58
|
|
|
); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$fields->addFieldToTab('Root.Main', $usernameField); |
|
62
|
|
|
|
|
63
|
|
|
$this->extend('updateCMSFields', $fields); |
|
64
|
|
|
|
|
65
|
|
|
return $fields; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @return FieldList |
|
70
|
|
|
*/ |
|
71
|
|
|
public function getCMSActions() |
|
72
|
|
|
{ |
|
73
|
|
|
$actions = parent::getCMSActions(); |
|
74
|
|
|
|
|
75
|
|
|
if (!$this->getField('ID') || $this->isAuthorised()) { |
|
76
|
|
|
$this->extend('updateCMSActions', $actions); |
|
77
|
|
|
return $actions; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$token = $this->twitterService->getOAuthToken(); |
|
81
|
|
|
|
|
82
|
|
|
// Set the token in session so it can be accessed throughout the OAuth flow. |
|
83
|
|
|
$this->twitterService->setSessionOAuthToken($token); |
|
84
|
|
|
|
|
85
|
|
|
$actions->push( |
|
86
|
|
|
LiteralField::create( |
|
87
|
|
|
'OAuthLink', |
|
88
|
|
|
'<a class="ss-ui-button" href="' . $this->twitterService->getAuthoriseUrl($token) . '">' . |
|
89
|
|
|
_t('Twitter.ButtonLabelAuthoriseAccount', 'Authorise account') . |
|
90
|
|
|
'</a>' |
|
91
|
|
|
) |
|
92
|
|
|
); |
|
93
|
|
|
|
|
94
|
|
|
$this->extend('updateCMSActions', $actions); |
|
95
|
|
|
|
|
96
|
|
|
return $actions; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @return RequiredFields |
|
101
|
|
|
*/ |
|
102
|
|
|
public function getCMSValidator() |
|
103
|
|
|
{ |
|
104
|
|
|
return new RequiredFields('Title'); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Sets an OAuth token for the TwitterAccount. |
|
109
|
|
|
* |
|
110
|
|
|
* @param array $token |
|
111
|
|
|
*/ |
|
112
|
1 |
|
public function setAccessToken($token) |
|
113
|
|
|
{ |
|
114
|
1 |
|
$this->setField('AccessToken', serialize($token)); |
|
115
|
1 |
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Gets the TwitterAccount's OAuth token. |
|
119
|
|
|
* |
|
120
|
|
|
* @return array |
|
121
|
|
|
*/ |
|
122
|
1 |
|
public function getAccessToken() |
|
123
|
|
|
{ |
|
124
|
1 |
|
return unserialize($this->getField('AccessToken')); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Checks if the TwitterAccount has been authorised via OAuth. |
|
129
|
|
|
* |
|
130
|
|
|
* @return boolean |
|
131
|
|
|
*/ |
|
132
|
1 |
|
public function isAuthorised() |
|
133
|
|
|
{ |
|
134
|
1 |
|
return (bool) $this->getField('AccessToken'); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
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.