forked from joesb/Oxf.am-Shurly-Integration
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shurly_integration.module
225 lines (206 loc) · 6.15 KB
/
shurly_integration.module
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
<?php // $Id: shurly_integration.module $
/**
* @file
* Tools for integrating with the Oxfam Short URL service, oxf.am.
*/
/**
* Implementation of hook_menu().
*/
function shurly_integration_menu() {
$items = array();
$items['admin/settings/oxfam-shurly'] = array(
'title' => 'Oxf.am short URL service settings',
'description' => 'Set up integration with the oxf.am short URL service.',
'page callback' => 'drupal_get_form',
'page arguments' => array('shurly_integration_admin_form'),
'access arguments' => array('administer site configuration'),
'file' => 'admin/shurly_integration.admin.inc',
);
$items['user/%user_category/edit/oxfam-shurly'] = array(
'title' => 'Oxf.am account',
'page callback' => 'shurly_integration_user_settings',
'page arguments' => array(1),
'access arguments' => array('add own oxf.am account'),
'load arguments' => array('%map', '%index'),
'weight' => 10,
'file' => 'shurly_integration.pages.inc',
'type' => MENU_LOCAL_TASK,
);
return $items;
}
/**
* Implementation of hook_perm()
*/
function shurly_integration_perm() {
return array('add own oxf.am account');
}
/**
* Implementation of hook_user().
*/
function shurly_integration_user($op, &$edit, &$account, $category = NULL) {
switch ($op) {
case 'load':
if ($oxf_am_account = shurly_integration_oxfam_account_get($account)) {
$account->oxf_am_api_key = $oxf_am_account;
}
break;
case 'categories':
return array(
array(
'name' => 'oxfam-shurly',
'title' => 'Oxf.am account',
'weight' => 3,
),
);
}
}
/**
* Implementation of hook_block().
*
* @return void
*/
function shurly_integration_block($op = 'list', $delta = 0, $edit = array()) {
switch ($op) {
case 'list':
$blocks['0'] = array('info' => t('Oxf.am: Node permalink'));
$blocks['1'] = array('info' => t('Oxf.am: Tweet this'));
return $blocks;
break;
case 'view':
$node = menu_get_object();
if (!$node->shurl['short_url']) {
return FALSE;
}
switch ($delta) {
case '0':
$block['subject'] = t('Permalink');
$block['content'] = theme('shurly_permalink', $node);
return $block;
break;
case '1':
$block['subject'] = t('Tweet this');
$block['content'] = theme('shurly_tweet_this', $node);
return $block;
break;
}
break;
}
}
/**
* Implementation of hook_theme().
*/
function shurly_integration_theme($existing, $type, $theme, $path) {
return array(
'shurly_tweet_this' => array(
'arguments' => array('node' => NULL),
'file' => 'shurly_integration.theme.inc',
),
'shurly_permalink' => array(
'arguments' => array('node' => NULL),
'file' => 'shurly_integration.theme.inc',
),
);
}
/**
* Implementation of hook_nodeapi().
*/
function shurly_integration_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
$node_types = variable_get('oxf_am_node_types', '');
if (!empty($node_types[$node->type]) && variable_get('oxf_am_do_shurl', TRUE)) {
module_load_include('inc', 'shurly_integration', 'shurly_integration');
switch ($op) {
case 'insert':
if (variable_get('oxf_am_do_shurl', TRUE)) {
shurly_integration_shurl_set($node);
}
break;
case 'update':
if (variable_get('oxf_am_do_shurl', TRUE)) {
$url = url('node/'. $node->nid, array('absolute' => TRUE));
$shurl = shurly_integration_shurl_get($node);
if ($shurl->long_url != $url) {
shurly_integration_shurl_set($node);
}
}
break;
case 'load':
if (variable_get('oxf_am_do_shurl', TRUE)) {
if ($shurl = shurly_integration_shurl_get($node)) {
$node->shurl = (array) $shurl;
}
}
break;
}
}
}
/**
* Get oxf.am account details for the given user.
*
* @return oxf.am account API key.
*/
function shurly_integration_oxfam_account_get($drupal_user) {
$result = db_query("SELECT * FROM {oxfam_shurl_user} WHERE uid = %d", $drupal_user->uid);
if ($account = db_fetch_object($result)) {
return $account->api_key;
}
return;
}
/**
* Set oxf.am account details for the given user.
*
* @return void
*/
function shurly_integration_oxfam_account_set($account = array()) {
if (db_result(db_query("SELECT 1 FROM {oxfam_shurl_user} WHERE uid = %d AND api_key = '%s'", $account['uid'], $account['api_key']))) {
drupal_write_record('oxfam_shurl_user', $account, array('uid', 'api_key')); }
else {
drupal_write_record('oxfam_shurl_user', $account);
}
}
/**
* Delete oxf.am account details for the given user.
*
* @return void
*/
function shurly_integration_oxfam_account_delete($account = array()) {
db_query("DELETE FROM {oxfam_shurl_user} WHERE uid = %d", $account['uid']);
}
/**
* Get oxf.am short url for a node.
*
* @return $shurl short url object
*/
function shurly_integration_shurl_get($node) {
if ($shurl = db_fetch_object(db_query("SELECT * FROM {oxfam_shurl} WHERE nid = %d", $node->nid))) {
return $shurl;
}
else {
return;
}
}
/**
* Set oxf.am short url for a node.
*
* @return void
*/
function shurly_integration_shurl_set($node) {
$url = url('node/'. $node->nid, array('absolute' => TRUE));
$service = variable_get('oxf_am_api_service', 'json');
$function = 'shurly_integration_shurl_get_'. $service;
$data = $function($url);
if ($data->shortUrl) {
$object['nid'] = $node->nid;
$object['short_url'] = $data->shortUrl;
$object['long_url'] = $data->longUrl;
if ($short_url = db_result(db_query("SELECT 1 FROM {oxfam_shurl} WHERE nid = %d", $node->nid))) {
if (drupal_write_record('oxfam_shurl', $object, array('nid')) != FALSE) {
drupal_set_message(t('Short URL updated for %node_title to !shurl.', array('%node_title' => $node->title, '!shurl' => $object['short_url'])));
}
}
else {
if (drupal_write_record('oxfam_shurl', $object) != FALSE){
drupal_set_message(t('Short URL created for %node_title.', array('%node_title' => $node->title)));
}
}
}
}