| 1 |
<?php
|
| 2 |
// $Id: twitter_actions.module,v 1.1.2.3 2009/05/25 19:35:38 walkah Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Exposes Drupal actions for sending Twitter messages.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Implementation of hook_action_info().
|
| 11 |
*/
|
| 12 |
function twitter_actions_action_info() {
|
| 13 |
return array(
|
| 14 |
'twitter_actions_set_status_action' => array(
|
| 15 |
'type' => 'system',
|
| 16 |
'description' => t('Post a message to Twitter'),
|
| 17 |
'configurable' => TRUE,
|
| 18 |
'hooks' => array(
|
| 19 |
'nodeapi' => array('view', 'insert', 'update', 'delete'),
|
| 20 |
'comment' => array('view', 'insert', 'update', 'delete'),
|
| 21 |
'user' => array('view', 'insert', 'update', 'delete', 'login'),
|
| 22 |
'cron' => array('run'),
|
| 23 |
),
|
| 24 |
),
|
| 25 |
);
|
| 26 |
}
|
| 27 |
|
| 28 |
/**
|
| 29 |
* Return a form definition so the Send email action can be configured.
|
| 30 |
*
|
| 31 |
* @param $context
|
| 32 |
* Default values (if we are editing an existing action instance).
|
| 33 |
* @return
|
| 34 |
* Form definition.
|
| 35 |
*/
|
| 36 |
function twitter_actions_set_status_action_form($context = array()) {
|
| 37 |
// Set default values for form.
|
| 38 |
$context += array(
|
| 39 |
'screen_name' => '',
|
| 40 |
'password' => '',
|
| 41 |
'message' => '',
|
| 42 |
);
|
| 43 |
|
| 44 |
$form['screen_name'] = array(
|
| 45 |
'#type' => 'textfield',
|
| 46 |
'#title' => t('Twitter account name'),
|
| 47 |
'#default_value' => $context['screen_name'],
|
| 48 |
'#size' => 25,
|
| 49 |
'#required' => TRUE,
|
| 50 |
);
|
| 51 |
|
| 52 |
$form['password'] = array(
|
| 53 |
'#title' => t('Twitter password'),
|
| 54 |
'#type' => 'password',
|
| 55 |
'#size' => 25,
|
| 56 |
'#required' => TRUE,
|
| 57 |
);
|
| 58 |
|
| 59 |
$form['message'] = array(
|
| 60 |
'#type' => 'textarea',
|
| 61 |
'#title' => t('Message'),
|
| 62 |
'#default_value' => $context['message'],
|
| 63 |
'#cols' => '80',
|
| 64 |
'#rows' => '3',
|
| 65 |
'#description' => t('The message that should be sent. You may include the following variables: %site_name, %username, %node_url, %node_type, %title, %teaser, %body. Not all variables will be available in all contexts.'),
|
| 66 |
'#required' => TRUE,
|
| 67 |
);
|
| 68 |
return $form;
|
| 69 |
}
|
| 70 |
|
| 71 |
function twitter_actions_set_status_action_validate($form, $form_state) {
|
| 72 |
module_load_include('inc', 'twitter');
|
| 73 |
$verify = FALSE;
|
| 74 |
|
| 75 |
$pass = $form_state['values']['password'];
|
| 76 |
$name = $form_state['values']['screen_name'];
|
| 77 |
|
| 78 |
module_load_include('inc', 'twitter');
|
| 79 |
|
| 80 |
$valid = twitter_authenticate($name, $pass);
|
| 81 |
if (!$valid) {
|
| 82 |
form_set_error('password', t('Twitter authentication failed. Please check your account name and try again.'));
|
| 83 |
}
|
| 84 |
}
|
| 85 |
|
| 86 |
function twitter_actions_set_status_action_submit($form, $form_state) {
|
| 87 |
$form_values = $form_state['values'];
|
| 88 |
// Process the HTML form to store configuration. The keyed array that
|
| 89 |
// we return will be serialized to the database.
|
| 90 |
$params = array(
|
| 91 |
'screen_name' => $form_values['screen_name'],
|
| 92 |
'password' => $form_values['password'],
|
| 93 |
'message' => $form_values['message'],
|
| 94 |
);
|
| 95 |
return $params;
|
| 96 |
}
|
| 97 |
|
| 98 |
/**
|
| 99 |
* Implementation of a configurable Drupal action.
|
| 100 |
* Sends an email.
|
| 101 |
*/
|
| 102 |
function twitter_actions_set_status_action($object, $context) {
|
| 103 |
global $user;
|
| 104 |
$variables['%site_name'] = variable_get('site_name', 'Drupal');
|
| 105 |
|
| 106 |
switch ($context['hook']) {
|
| 107 |
case 'nodeapi':
|
| 108 |
// Because this is not an action of type 'node' the node
|
| 109 |
// will not be passed as $object, but it will still be available
|
| 110 |
// in $context.
|
| 111 |
$node = $context['node'];
|
| 112 |
break;
|
| 113 |
// The comment hook provides nid, in $context.
|
| 114 |
case 'comment':
|
| 115 |
$comment = $context['comment'];
|
| 116 |
$node = node_load($comment->nid);
|
| 117 |
case 'user':
|
| 118 |
// Because this is not an action of type 'user' the user
|
| 119 |
// object is not passed as $object, but it will still be available
|
| 120 |
// in $context.
|
| 121 |
$account = $context['account'];
|
| 122 |
if (isset($context['node'])) {
|
| 123 |
$node = $context['node'];
|
| 124 |
}
|
| 125 |
elseif ($context['recipient'] == '%author') {
|
| 126 |
// If we don't have a node, we don't have a node author.
|
| 127 |
watchdog('error', 'Cannot use %author token in this context.');
|
| 128 |
return;
|
| 129 |
}
|
| 130 |
break;
|
| 131 |
case 'taxonomy':
|
| 132 |
$account = $user;
|
| 133 |
$vocabulary = taxonomy_vocabulary_load($object->vid);
|
| 134 |
$variables = array_merge($variables, array(
|
| 135 |
'%term_name' => $object->name,
|
| 136 |
'%term_description' => $object->description,
|
| 137 |
'%term_id' => $object->tid,
|
| 138 |
'%vocabulary_name' => $vocabulary->name,
|
| 139 |
'%vocabulary_description' => $vocabulary->description,
|
| 140 |
'%vocabulary_id' => $vocabulary->vid,
|
| 141 |
)
|
| 142 |
);
|
| 143 |
break;
|
| 144 |
default:
|
| 145 |
// We are being called directly.
|
| 146 |
$node = $object;
|
| 147 |
}
|
| 148 |
|
| 149 |
if (isset($node)) {
|
| 150 |
if (!isset($account)) {
|
| 151 |
$account = user_load(array('uid' => $node->uid));
|
| 152 |
}
|
| 153 |
if ($recipient == '%author') {
|
| 154 |
$recipient = $account->mail;
|
| 155 |
}
|
| 156 |
}
|
| 157 |
|
| 158 |
$variables['%username'] = $account->name;
|
| 159 |
|
| 160 |
// Node-based variable translation is only available if we have a node.
|
| 161 |
if (isset($node) && is_object($node)) {
|
| 162 |
$variables = array_merge($variables, array(
|
| 163 |
'%uid' => $node->uid,
|
| 164 |
'%node_url' => url('node/'. $node->nid, array('absolute' => TRUE)),
|
| 165 |
'%node_type' => node_get_types('name', $node),
|
| 166 |
'%title' => $node->title,
|
| 167 |
'%teaser' => $node->teaser,
|
| 168 |
'%body' => $node->body
|
| 169 |
)
|
| 170 |
);
|
| 171 |
}
|
| 172 |
|
| 173 |
$message = strtr($context['message'], $variables);
|
| 174 |
|
| 175 |
module_load_include('inc', 'twitter');
|
| 176 |
twitter_set_status($context['screen_name'], $context['password'], $message);
|
| 177 |
}
|