| 1 |
<?php
|
| 2 |
// $Id: jott.module,v 1.9 2008/02/13 21:34:15 coltrane Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Implementation of hook_menu().
|
| 6 |
*/
|
| 7 |
function jott_menu() {
|
| 8 |
$items = array();
|
| 9 |
|
| 10 |
$items['jottlink'] = array(
|
| 11 |
'title' => 'Jott',
|
| 12 |
'page callback' => '_jott_link',
|
| 13 |
'access arguments' => array('access content'),
|
| 14 |
'type' => MENU_CALLBACK,
|
| 15 |
);
|
| 16 |
$items['jottsetup'] = array(
|
| 17 |
'title' => 'Jott',
|
| 18 |
'page callback' => '_jott_setup',
|
| 19 |
'access arguments' => array('access content'),
|
| 20 |
'type' => MENU_CALLBACK,
|
| 21 |
);
|
| 22 |
$items['admin/settings/jott'] = array(
|
| 23 |
'title' => 'Jott',
|
| 24 |
'description' => 'Administer Jott settings',
|
| 25 |
'page callback' => 'drupal_get_form',
|
| 26 |
'page arguments' => array('jott_admin_settings'),
|
| 27 |
'access arguments' => array('administer jott'),
|
| 28 |
);
|
| 29 |
|
| 30 |
return $items;
|
| 31 |
}
|
| 32 |
|
| 33 |
/**
|
| 34 |
* Menu callback Jott admin settings.
|
| 35 |
*/
|
| 36 |
function jott_admin_settings() {
|
| 37 |
$form['jott']['jott_post_low_confidence'] = array(
|
| 38 |
'#type' => 'checkbox',
|
| 39 |
'#title' => t('Post low confidence messages'),
|
| 40 |
'#description' => t('Jotts with confidence 2 means the audio was not transcribed well enough and the body sent to the Jott Link will be "Please listen". Check this box if you want to post these jotts.'),
|
| 41 |
'#default_value' => variable_get('jott_post_low_confidence', 0)
|
| 42 |
);
|
| 43 |
$form['jott']['jott_parse_message'] = array(
|
| 44 |
'#type' => 'checkbox',
|
| 45 |
'#title' => t('Parse message'),
|
| 46 |
'#description' => t('Parse the Jott message using the separator below.'),
|
| 47 |
'#default_value' => variable_get('jott_parse_message', 1)
|
| 48 |
);
|
| 49 |
$form['jott']['jott_parse_separator'] = array(
|
| 50 |
'#type' => 'textfield',
|
| 51 |
'#title' => t('Parse separator'),
|
| 52 |
'#description' => t('A character, word, or short phrase to separate the title and body by. This must be in your jott for the message to be parsed. Example, "body" or "end title".'),
|
| 53 |
'#default_value' => variable_get('jott_parse_separator', 'body')
|
| 54 |
);
|
| 55 |
|
| 56 |
/*$form['jott_advanced'] = array(
|
| 57 |
'#type' => 'fieldset',
|
| 58 |
'#title' => t('Advanced parse settings'),
|
| 59 |
'#collapsible' => TRUE,
|
| 60 |
'#collapsed' => TRUE
|
| 61 |
);
|
| 62 |
$form['jott_advanced']['jott_parse_pattern'] = array(
|
| 63 |
'#type' => 'textfield',
|
| 64 |
'#title' => t('Parse pattern'),
|
| 65 |
'#description' => t('Set the regular expression pattern to use for parsing the message. You must have two, and only two, groups in the pattern because they are used and expected to pull the title and the body.'),
|
| 66 |
'#default_value' => variable_get('jott_parse_pattern', '/^(.*)body(.*)/i')
|
| 67 |
);*/
|
| 68 |
|
| 69 |
return system_settings_form($form);
|
| 70 |
}
|
| 71 |
|
| 72 |
/**
|
| 73 |
* Extracts the Jott post.
|
| 74 |
* ?userKey=0d0e1e62-4122-9b-b1028e5651bf&message=Hello+world.&listen=http%3a%2f%2fwww.jott.com%2fShow.aspx%3fid%3d3d24485b-2ef9-46ac-b1b3-b6e057d&creationdate=1%2f5%2f2008+2%3a50%3a08+PM&creationdateutc=1%2f5%2f2008+9%3a50%3a08+PM&confidence=3&audio=1
|
| 75 |
*/
|
| 76 |
function _jott_get_post() {
|
| 77 |
$post = array();
|
| 78 |
|
| 79 |
if (!empty($_GET['userKey']) && !empty($_GET['message'])) {
|
| 80 |
$post['userKey'] = urldecode($_GET['userKey']);
|
| 81 |
$post['message'] = urldecode($_GET['message']);
|
| 82 |
$post['listen'] = urldecode($_GET['listen']);
|
| 83 |
$post['creationdate'] = urldecode($_GET['creationdate']);
|
| 84 |
$post['confidence'] = urldecode($_GET['confidence']);
|
| 85 |
$post['audio'] = urldecode($_GET['audio']);
|
| 86 |
}
|
| 87 |
|
| 88 |
return $post;
|
| 89 |
}
|
| 90 |
|
| 91 |
/**
|
| 92 |
* Extracts the Jott post.
|
| 93 |
* userKey=0d0e1e62-4122-9b92b2e651bf&postbackurl=http%3A%2F%2Fjott.com%2FLinkPostBack.ashx%3Fendpointid%3D4392558%26userkey%3D0d0e1e62
|
| 94 |
*/
|
| 95 |
function _jott_get_config() {
|
| 96 |
$config = array();
|
| 97 |
|
| 98 |
if (!empty($_GET['userKey']) && !empty($_GET['postbackurl'])) {
|
| 99 |
$config['userKey'] = urldecode($_GET['userKey']);
|
| 100 |
$config['postbackurl'] = urldecode($_GET['postbackurl']);
|
| 101 |
}
|
| 102 |
|
| 103 |
return $config;
|
| 104 |
}
|
| 105 |
|
| 106 |
/**
|
| 107 |
* Callback for Jott Link.
|
| 108 |
*/
|
| 109 |
function _jott_link() {
|
| 110 |
global $user;
|
| 111 |
|
| 112 |
$post = _jott_get_post();
|
| 113 |
if (empty($post)) {
|
| 114 |
_jott_post_return('Jott.com post failed.', TRUE);
|
| 115 |
}
|
| 116 |
|
| 117 |
$jotter = _is_jotter($post['userKey']);
|
| 118 |
if ($jotter === FALSE) {
|
| 119 |
watchdog('jott', t('Jott userkey not found.'), WATCHDOG_NOTICE);
|
| 120 |
return drupal_access_denied();
|
| 121 |
}
|
| 122 |
|
| 123 |
if ($post['audio'] == 1 && $post['confidence'] <= 2 && !variable_get('jott_post_low_confidence', 0)) {
|
| 124 |
_jott_post_return('Confidence too low.', TRUE);
|
| 125 |
}
|
| 126 |
|
| 127 |
$ret = _jott_save($jotter, $post);
|
| 128 |
|
| 129 |
if ($ret !== FALSE) {
|
| 130 |
_jott_post_return('Jott received. '. $ret['title']);
|
| 131 |
}
|
| 132 |
else {
|
| 133 |
_jott_post_return('Error, your jott was not processed correctly.', TRUE);
|
| 134 |
}
|
| 135 |
}
|
| 136 |
|
| 137 |
/**
|
| 138 |
* Save the jott.
|
| 139 |
*/
|
| 140 |
function _jott_save($jotter, $post) {
|
| 141 |
$node = new stdClass();
|
| 142 |
$node->type = 'jott';
|
| 143 |
$node->uid = $jotter;
|
| 144 |
$parse = variable_get('jott_parse_message', 1);
|
| 145 |
if ($parse) {
|
| 146 |
$data = _jott_parse($post['message']);
|
| 147 |
if ($data !== FALSE) {
|
| 148 |
$node->title = $data['title'];
|
| 149 |
$node->body = $data['body'];
|
| 150 |
}
|
| 151 |
}
|
| 152 |
if (!$parse || $data === FALSE) {
|
| 153 |
$node->title = trim(truncate_utf8($post['message'], 29, TRUE));
|
| 154 |
$node->body = $post['message'];
|
| 155 |
}
|
| 156 |
$node->listen = $post['listen'];
|
| 157 |
$node->confidence = $post['confidence'];
|
| 158 |
$options = variable_get('node_options_jott', array('status'));
|
| 159 |
foreach ($options as $option) {
|
| 160 |
$node->$option = 1;
|
| 161 |
}
|
| 162 |
$comment = variable_get('comment_jott', '2');
|
| 163 |
$node->comment = $comment;
|
| 164 |
|
| 165 |
node_save($node);
|
| 166 |
|
| 167 |
if (!empty($node->nid)) {
|
| 168 |
return array('nid' => $node->nid, 'title' => $node->title);
|
| 169 |
}
|
| 170 |
else {
|
| 171 |
return FALSE;
|
| 172 |
}
|
| 173 |
}
|
| 174 |
|
| 175 |
/**
|
| 176 |
* Parse the jott message.
|
| 177 |
*/
|
| 178 |
function _jott_parse($message) {
|
| 179 |
//$pattern = variable_get('jott_parse_pattern', '/^(.*)body(.*)/i');
|
| 180 |
$pattern = '/^(.*)'. variable_get('jott_parse_separator', 'body') .'(.*)/i';
|
| 181 |
preg_match($pattern, $message, $matches);
|
| 182 |
if (!empty($matches) && count($matches) == 3) {
|
| 183 |
return array('title' => $matches[1], 'body' => $matches[2]);
|
| 184 |
}
|
| 185 |
else {
|
| 186 |
return FALSE;
|
| 187 |
}
|
| 188 |
}
|
| 189 |
|
| 190 |
/**
|
| 191 |
* Callback for Jott setup.
|
| 192 |
*/
|
| 193 |
function _jott_setup() {
|
| 194 |
global $user;
|
| 195 |
|
| 196 |
$config = _jott_get_config();
|
| 197 |
if (empty($config) && empty($_SESSION['userKey'])) {
|
| 198 |
return 'Failed to receive Jott post.';
|
| 199 |
}
|
| 200 |
else if (!empty($_SESSION['userKey'])) {
|
| 201 |
$config['userKey'] = $_SESSION['userKey'];
|
| 202 |
$config['postbackurl'] = $_SESSION['postbackurl'];
|
| 203 |
}
|
| 204 |
|
| 205 |
if ($user->uid === 0) {
|
| 206 |
drupal_set_message(t('You must be logged in to set up Jott. After logging in submit the Jott Link on Jott.com again.'));
|
| 207 |
drupal_goto('user');
|
| 208 |
}
|
| 209 |
|
| 210 |
if (!user_access('create jotts')) {
|
| 211 |
return drupal_access_denied();
|
| 212 |
}
|
| 213 |
|
| 214 |
if (_is_jotter($config['userKey'])) {
|
| 215 |
return 'Your Jott Link has already been created.';
|
| 216 |
}
|
| 217 |
|
| 218 |
db_query("DELETE FROM {jott_user} WHERE uid = %d", $user->uid);
|
| 219 |
db_query("INSERT INTO {jott_user} (uid, userkey, postbackurl) VALUES (%d, '%s', '%s')", $user->uid, $config['userKey'], $config['postbackurl']);
|
| 220 |
|
| 221 |
return 'Your Jott Link is now active.';
|
| 222 |
}
|
| 223 |
|
| 224 |
/**
|
| 225 |
* Check if there is already a record for a userKey.
|
| 226 |
*/
|
| 227 |
function _is_jotter($user_key) {
|
| 228 |
$result = db_fetch_object(db_query("SELECT uid FROM {jott_user} WHERE userkey = '%s'", $user_key));
|
| 229 |
|
| 230 |
if ($result) {
|
| 231 |
return $result->uid;
|
| 232 |
}
|
| 233 |
else {
|
| 234 |
return FALSE;
|
| 235 |
}
|
| 236 |
}
|
| 237 |
|
| 238 |
function _jott_post_return($msg = 'Failed', $fail = FALSE) {
|
| 239 |
if ($fail) {
|
| 240 |
watchdog('jott', t('@msg : !url', array('@msg' => $msg, '!url' => check_url(request_uri()))), NULL, WATCHDOG_ERROR);
|
| 241 |
}
|
| 242 |
print t($msg);
|
| 243 |
// We exit because Jott returns our output via SMS or email and we don't want to send a full drupal page.
|
| 244 |
exit;
|
| 245 |
}
|
| 246 |
|
| 247 |
/**
|
| 248 |
* Implementation of hook_node_info().
|
| 249 |
*/
|
| 250 |
function jott_node_info() {
|
| 251 |
return array(
|
| 252 |
'jott' => array(
|
| 253 |
'name' => t('Jott'),
|
| 254 |
'module' => 'jott',
|
| 255 |
'description' => t("A jott transcribed from Jott.com"),
|
| 256 |
)
|
| 257 |
);
|
| 258 |
}
|
| 259 |
|
| 260 |
/**
|
| 261 |
* Implementation of hook_perm().
|
| 262 |
*/
|
| 263 |
function jott_perm() {
|
| 264 |
return array('create jotts', 'edit own jotts', 'administer jott');
|
| 265 |
}
|
| 266 |
|
| 267 |
/**
|
| 268 |
* Implementation of hook_access().
|
| 269 |
*/
|
| 270 |
function jott_access($op, $node) {
|
| 271 |
global $user;
|
| 272 |
|
| 273 |
if ($op == 'create') {
|
| 274 |
return user_access('create jotts');
|
| 275 |
}
|
| 276 |
|
| 277 |
if ($op == 'update' || $op == 'delete') {
|
| 278 |
if (user_access('edit own jotts') && ($user->uid == $node->uid)) {
|
| 279 |
return TRUE;
|
| 280 |
}
|
| 281 |
}
|
| 282 |
}
|
| 283 |
|
| 284 |
/**
|
| 285 |
* Implementation of hook_form().
|
| 286 |
*/
|
| 287 |
function jott_form(&$node) {
|
| 288 |
$type = node_get_types('type', $node);
|
| 289 |
|
| 290 |
// We need to define form elements for the node's title and body.
|
| 291 |
$form['title'] = array(
|
| 292 |
'#type' => 'textfield',
|
| 293 |
'#title' => check_plain($type->title_label),
|
| 294 |
'#required' => TRUE,
|
| 295 |
'#default_value' => $node->title,
|
| 296 |
'#weight' => -5
|
| 297 |
);
|
| 298 |
$form['body_filter']['body'] = array(
|
| 299 |
'#type' => 'textarea',
|
| 300 |
'#title' => check_plain($type->body_label),
|
| 301 |
'#default_value' => $node->body,
|
| 302 |
'#required' => FALSE
|
| 303 |
);
|
| 304 |
$form['body_filter']['filter'] = filter_form($node->format);
|
| 305 |
|
| 306 |
return $form;
|
| 307 |
}
|
| 308 |
|
| 309 |
/**
|
| 310 |
* Implementation of hook_insert().
|
| 311 |
*/
|
| 312 |
function jott_insert($node) {
|
| 313 |
if (!empty($node->listen) && !empty($node->confidence)) {
|
| 314 |
db_query("INSERT INTO {jott_node} (nid, listen, confidence) VALUES (%d, '%s', %d)", $node->nid, $node->listen, $node->confidence);
|
| 315 |
}
|
| 316 |
}
|
| 317 |
|
| 318 |
/**
|
| 319 |
* Implementation of hook_delete().
|
| 320 |
*/
|
| 321 |
function jott_delete($node) {
|
| 322 |
db_query('DELETE FROM {jott_node} WHERE nid = %d', $node->nid);
|
| 323 |
}
|
| 324 |
|
| 325 |
/**
|
| 326 |
* Implementation of hook_load().
|
| 327 |
*/
|
| 328 |
function jott_load($node) {
|
| 329 |
$additions = db_fetch_object(db_query('SELECT listen, confidence FROM {jott_node} WHERE nid = %d', $node->nid));
|
| 330 |
return $additions;
|
| 331 |
}
|
| 332 |
|
| 333 |
/**
|
| 334 |
* Implementation of hook_view().
|
| 335 |
*/
|
| 336 |
function jott_view($node) {
|
| 337 |
$node = node_prepare($node, $teaser);
|
| 338 |
if (!empty($node->confidence)) {
|
| 339 |
$node->content['jott'] = array(
|
| 340 |
'#value' => t('!jott with confidence of @confidence', array('!jott' => l('Via Jott.com', 'http://jott.com'), '@confidence' => $node->confidence)),
|
| 341 |
'#weight' => 10,
|
| 342 |
);
|
| 343 |
}
|
| 344 |
|
| 345 |
return $node;
|
| 346 |
}
|
| 347 |
|
| 348 |
/**
|
| 349 |
* Implementation of hook_link().
|
| 350 |
*/
|
| 351 |
function jott_link($type, $object = NULL, $teaser = FALSE) {
|
| 352 |
$links = array();
|
| 353 |
|
| 354 |
if ($type == 'node' && $object->type == 'jott' && !empty($object->listen)) {
|
| 355 |
$links['jott_listen'] = array(
|
| 356 |
'title' => t('Listen to this post'),
|
| 357 |
'href' => check_url($object->listen),
|
| 358 |
'attributes' => array('title' => t('Listen to this post on Jott.com')),
|
| 359 |
);
|
| 360 |
}
|
| 361 |
|
| 362 |
return $links;
|
| 363 |
}
|