-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathextension.driver.php
253 lines (218 loc) · 7.52 KB
/
extension.driver.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<?php
require_once(TOOLKIT . '/class.sectionmanager.php');
require_once(TOOLKIT . '/class.entrymanager.php');
require_once(TOOLKIT . '/class.authormanager.php');
require_once(EXTENSIONS . '/twitternotifier/lib/twitteroauth/twitteroauth.php');
class Extension_TwitterNotifier extends Extension
{
public $table = "tbl_authors_twitter_accounts";
public function about()
{
return array
(
'name' => 'Twitter Notifier',
'version' => '1.0.2',
'release-date' => '2011-08-12',
'author' => array(
'name' => 'John Porter',
'website' => 'http://designermonkey.co.uk/',
'email' => 'contact@designermonkey.co.uk'
),
'description' => 'Notify Twitter when you create an entry.'
);
}
public function uninstall()
{
Symphony::Database()->query("
DROP TABLE `".$this->table."`
");
return Symphony::Configuration()->remove('twitter-notifier');
}
public function install()
{
return Symphony::Database()->query("
CREATE TABLE IF NOT EXISTS `".$this->table."` (
`id` int(10) unsigned NOT NULL auto_increment,
`screen_name` varchar(100) NOT NULL,
`user_id` varchar(100),
`oauth_token` text,
`oauth_token_secret` text,
`authorised` enum('yes','no') DEFAULT 'no',
`section` int(10) unsigned NOT NULL,
`field_param` int(10) unsigned NOT NULL,
`field_msg` int(10) unsigned NOT NULL,
`page` int(10) NOT NULL,
`params` varchar(250) NOT NULL,
`authors` varchar(250) NOT NULL,
`status` varchar(250) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM;
");
}
public function getSubscribedDelegates()
{
return array(
array(
'page' => '/publish/new/',
'delegate' => 'EntryPostCreate',
'callback' => 'sendTwitterNotification'
),
array(
'page' => '/backend/',
'delegate' => 'InitaliseAdminPageHead',
'callback' => 'initialiseAdminPageHead'
),
array(
'page' => '/system/preferences/',
'delegate' => 'AddCustomPreferenceFieldsets',
'callback' => 'appendToPreferences'
),
array(
'page' => '/system/preferences/',
'delegate' => 'Save',
'callback' => 'savePreferences'
),
array(
'page' => '/system/preferences/success/',
'delegate' => 'Save',
'callback' => 'savePreferences'
)
);
}
public function fetchNavigation()
{
return array(
array(
'location' => 'System',
'name' => __('Twitter Accounts'),
'link' => '/accounts/'
)
);
}
/**
* Add the JavaScript to the head of the 'accounts' page
*/
public function initialiseAdminPageHead($context)
{
$page = $context['parent']->Page;
if($page instanceof contentExtensionTwitterNotifierAccounts){
$page->addStylesheetToHead(URL . '/extensions/twitternotifier/assets/twitternotifier.css');
$page->addScriptToHead(URL . '/extensions/twitternotifier/assets/jquery.oauthpopup.js', null, false);
$page->addScriptToHead(URL . '/extensions/twitternotifier/assets/twitternotifier.js', null, false);
}
}
private function _checkConsumerDetails()
{
if(Symphony::Configuration()->get('consumer-secret', 'twitter-notifier') && Symphony::Configuration()->get('consumer-key', 'twitter-notifier'))
{
return true;
}
return false;
}
public function savePreferences($context)
{
if(empty($context['settings']['twitter-notifier']['consumer-key']) || empty($context['settings']['twitter-notifier']['consumer-secret']))
{
if($this->_checkConsumerDetails() == true && $context['settings']['twitter-notifier']['remove-details'] == 'yes')
{
$context['settings']['twitter-notifier']['consumer-key'] = $this->getConsumerKey();
$context['settings']['twitter-notifier']['consumer-secret'] = $this->getConsumerSecret();
}
}
}
public function appendToPreferences($context)
{
$fieldset = new XMLElement('fieldset');
$fieldset->setAttribute('class', 'settings');
$fieldset->appendChild(new XMLElement('legend', __('Twitter API')));
$status = new XMLElement('div');
if($this->_checkConsumerDetails() == true)
{
$status->appendChild(new XMLElement('p', __('Your Consumer details are saved in the preferences, to change them, please re-enter them here.')));
}
else
{
// Add info
$fieldset->appendChild(new XMLElement('p',__('An <a href="http://dev.twitter.com/login" title="Login at Twitter Developers site">application will need registering</a> with a Twitter account to get the details required here.')));
$status->setAttribute('class', 'invalid');
$p = new XMLElement('p', __('Your Consumer details are not saved in the preferences enter them to save.'));
$p->setAttribute('style', 'padding-top:0.75em');
$status->appendChild($p);
}
$fieldset->appendChild($status);
$div = new XMLElement('div');
$div->setAttribute('class','group');
// Add Consumer Key field
$label = Widget::Label(__('Consumer Key'));
$label->appendChild(Widget::Input('settings[twitter-notifier][consumer-key]', null, 'password'));
$div->appendChild($label);
// Add Consumer Secret field
$label = Widget::Label(__('Consumer Secret'));
$label->appendChild(Widget::Input('settings[twitter-notifier][consumer-secret]', null, 'password'));
$div->appendChild($label);
$fieldset->appendChild($div);
// Add sub help
$fieldset->appendChild(new XMLElement('p',__('Your Consumer details are required to allow Author accounts access to notify Twitter.'), array('class' => 'help')));
$context['wrapper']->appendChild($fieldset);
}
/**
* Sends the notification to Twitter
*/
public function sendTwitterNotification($context)
{
// Get any accounts that relate to this section
$accounts = Symphony::Database()->fetch("
SELECT * FROM `" . $this->table . "` WHERE `section` = " . (int)$context['section']->get('id') . ";
");
$author_id = $context['entry']->get('author_id');
foreach($accounts as $account)
{
$account['authors'] = explode(',',$account['authors']);
if(!in_array($author_id, $account['authors'])) continue;
$page = Symphony::Database()->fetch("
SELECT * FROM `tbl_pages` WHERE `id` = " . $account['page'] . ";
");
$page = current($page);
$url = URL . '/' . ($page['path'] ? $page['path'] . '/' : '') . $page['handle'] . '/' . $account['params'] . '/';
$url_handle = $context['entry']->getData($account['field_param']);
$url = str_replace('$field', $url_handle['handle'], $url);
$msg = $context['entry']->getData($account['field_msg']);
$TwitterOAuth = new TwitterOAuth(
$this->getConsumerKey(),
$this->getConsumerSecret(),
$account['oauth_token'],
$account['oauth_token_secret']
);
$reserve = $TwitterOAuth->get('help/configuration')->short_url_length;
$msg_append = '... ' . __('Read more') . ' ';
$reserve += count($msg_append);
$msg = preg_replace('/\s+?(\S+)?$/', '', substr($msg['value'], 0, (141 - $reserve)));
$tweet = $msg . $msg_append . $url;
$result = $TwitterOAuth->post('statuses/update', array(
'status' => $tweet,
'wrap_links' => 'true',
'trim_user' => 'true',
'include_entities' => 'true'
));
// I've requested entities to be returned for future implemntation of Tracker support
}
}
/**
* Helper Function - Get the consumer key from the Configuration object
*
* @return String Twitter Consumer Key
*/
public function getConsumerKey()
{
return Symphony::Configuration()->get('consumer-key', 'twitter-notifier');
}
/**
* Helper Function - Get the consumer secret from the Configuration object
*
* @return String Twitter Consumer Secret
*/
public function getConsumerSecret()
{
return Symphony::Configuration()->get('consumer-secret', 'twitter-notifier');
}
}