-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbigboard.module
286 lines (243 loc) · 7.65 KB
/
bigboard.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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<?php
/*
__ __ __ ___ __
/\ \__/\ \ /\ \__ /'___\ __/\ \__
\ \ ,_\ \ \___ __ ___ __ __\ \ ,_\/\ \__//\_\ \ ,_\
\ \ \/\ \ _ `\ /'__`\ / __`\/\ \/\ \\ \ \/\ \ ,__\/\ \ \ \/
\ \ \_\ \ \ \ \/\ __/ /\ \L\ \ \ \_\ \\ \ \_\ \ \_/\ \ \ \ \_
\ \__\\ \_\ \_\ \____\ \ \____/\ \____/ \ \__\\ \_\ \ \_\ \__\
\/__/ \/_/\/_/\/____/ \/___/ \/___/ \/__/ \/_/ \/_/\/__/
Description: BigBoard Module for Drupal
Developer: Michael Witwicki | The Outfit, Inc.
Website: fromtheoutfit.com
Contact: hello@fromtheoutfit.com / 617.459.4578
*/
/*
* @file
* A module which updates BigBoard.us when your content changes.
*/
/**
* Implements hook_help().
* Displays help and module information.
*
* @param path
* Which path of the site we're using to display help
* @param arg
* Array that holds the current path as returned from arg() function
* @return string
*/
function bigboard_help($path, $arg)
{
switch ($path)
{
case "admin/help#bigboard":
return '<p>' . t('Updates <a href="https://bigboard.us">BigBoard.us</a> when your content changes') . '</p>';
break;
}
}
/**
* Implements hook_menu().
*/
function bigboard_menu()
{
$items = array();
$items['admin/config/services/bigboard'] = array(
'title' => 'BigBoard Settings',
'description' => 'Configuration for BigBoard module',
'page callback' => 'drupal_get_form',
'page arguments' => array('bigboard_form'),
'access arguments' => array('access administration pages'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* Page callback: Current posts settings
*
* @see bigboard_menu()
*/
function bigboard_form($form, &$form_state)
{
$node_types = node_type_get_types();
$form['bigboard_access_token'] = array(
'#type' => 'textfield',
'#title' => t('Access Token'),
'#default_value' => variable_get('bigboard_access_token', ''),
'#size' => 50,
'#maxlength' => 50,
'#description' => t('To connect your site to BigBoard - go to Account -> Services in BigBoard and Connect a Custom Service. Paste your Access Token below'),
'#required' => TRUE,
);
foreach ($node_types as $node_type)
{
$form['bigboard_config_' . $node_type->type] = array(
'#type' => 'checkboxes',
'#title' => ucwords($node_type->name) . ' ' . t('Options'),
'#default_value' => variable_get('bigboard_config_' . $node_type->type, ''),
'#options' => array(
'created' => ucwords($node_type->name) . ' ' . t('Created'),
'updated' => ucwords($node_type->name) . ' ' . t('Updated'),
'commented' => ucwords($node_type->name) . ' ' . t('Commented'),
),
'#description' => t('Select when you would like this content type to update BigBoard.us'),
'#required' => FALSE,
);
}
return system_settings_form($form);
}
/**
* Implements hook_node_insert().
*/
function bigboard_node_insert($node)
{
// check the configuration to see if we should post this to bigboard
if (bigboard_submission_check($node->type, 'created'))
{
// globals
global $user;
// variables
$node_type = node_type_get_name($node);
// get the url
$url = bigboard_url($node);
// send it to bigboard
bigboard($user->mail, $node->title, ucwords($node_type) . ' ' . t('Created'), $url);
}
}
/**
* Implements hook_node_update().
*/
function bigboard_node_update($node)
{
// check the configuration to see if we should post this to bigboard
if (bigboard_submission_check($node->type, 'updated'))
{
// globals
global $user;
// variables
$node_type = node_type_get_name($node);
// get the url
$url = bigboard_url($node);
// send it to bigboard
bigboard($user->mail, $node->title, ucwords($node_type) . ' ' . t('Updated'), $url);
}
}
/**
* Implements hook_comment_insert().
*/
function bigboard_comment_insert($comment)
{
// globals
global $user;
// variables
$email = '';
// let's do our best to get the email address for this user
switch (TRUE)
{
case (isset($user->mail) && filter_var($user->mail, FILTER_VALIDATE_EMAIL)):
$email = $user->mail;
break;
case (isset($comment->mail) && filter_var($comment->mail, FILTER_VALIDATE_EMAIL)):
$email = $comment->mail;
break;
}
// We can only proceed if we have a valid email
if (strlen($email) > 0)
{
// variables
$node = node_load((int)$comment->nid);
$node_type = node_type_get_name($node);
// check the configuration to see if we should post this to bigboard
if (bigboard_submission_check($node->type, 'commented'))
{
// get the url
$url = bigboard_url($node);
// send it to bigboard
bigboard($email, $node->title, ucwords($node_type) . ' ' . t('Commented'), $url . '#comment-' . $comment->cid);
}
}
}
/**
* Call the BigBoard API
*
* @param string $email
* @param string $summary
* @param string $label
* @param string $url
* @return void
*/
function bigboard($email, $summary, $label, $url)
{
$access_token = variable_get('bigboard_access_token');
$endpoint = 'https://bigboard.us/api';
if (isset($access_token))
{
// set event information
$p['events'][0] = array(
'email' => $email,
'summary' => $summary,
'label' => $label,
'url' => $url,
'time' => time(),
);
// set headers
$headers[] = 'Content-Type: application/json';
$headers[] = 'Accept: application/json';
$headers[] = 'X-BigBoard-Token: ' . $access_token;
// get it on the board!
$ch = curl_init();
$options = array(
CURLOPT_POST => 1,
CURLOPT_URL => $endpoint,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_TIMEOUT => 60,
CURLOPT_SSL_VERIFYHOST => FALSE,
CURLOPT_SSL_VERIFYPEER => FALSE,
CURLOPT_POSTFIELDS => json_encode($p),
CURLOPT_USERAGENT => 'BigBoard (bigboard@fromtheoutfit.com)',
CURLOPT_FOLLOWLOCATION => TRUE
);
curl_setopt_array($ch, $options);
$data = json_decode(curl_exec($ch));
}
}
/**
* Returns the URL to post to BigBoard
*
* @param object $node
* @return string
*/
function bigboard_url($node)
{
global $base_url;
$url = rtrim($base_url, '/') . '/';
if (isset($node->path) && strlen($node->path['alias']) > 0)
{
$url .= $node->path['alias'];
}
else
{
$url .= 'node/' . $node->nid;
}
return $url;
}
/**
* Determines if a particular content type should update bigboard for the given hook
*
* @param string $node_type
* @param string $hook_type
* @return boolean
*/
function bigboard_submission_check($node_type = '', $hook_type = '')
{
$data = FALSE;
if (strlen($node_type) > 0 && strlen($hook_type) > 0)
{
$config = variable_get('bigboard_config_' . $node_type);
if (isset($config[$hook_type]) && (string)$config[$hook_type] == $hook_type)
{
$data = TRUE;
}
}
return $data;
}