first import
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
name = Twitter actions
|
||||
description = Exposes Drupal actions to send Twitter messages.
|
||||
core = 7.x
|
||||
dependencies[] = twitter
|
||||
dependencies[] = oauth_common
|
||||
|
||||
; Information added by drupal.org packaging script on 2011-12-03
|
||||
version = "7.x-3.0-beta4"
|
||||
core = "7.x"
|
||||
project = "twitter"
|
||||
datestamp = "1322940643"
|
||||
|
188
sites/all/modules/twitter/twitter_actions/twitter_actions.module
Normal file
188
sites/all/modules/twitter/twitter_actions/twitter_actions.module
Normal file
@@ -0,0 +1,188 @@
|
||||
<?php
|
||||
/**
|
||||
* @file
|
||||
* Exposes Drupal actions for sending Twitter messages.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Implements hook_action_info().
|
||||
*/
|
||||
function twitter_actions_action_info() {
|
||||
return array(
|
||||
'twitter_actions_set_status_action' => array(
|
||||
'type' => 'system',
|
||||
'label' => t('Post a message to Twitter'),
|
||||
'configurable' => TRUE,
|
||||
'triggers' => array(
|
||||
'node_view',
|
||||
'node_insert',
|
||||
'node_update',
|
||||
'node_delete',
|
||||
'comment_view',
|
||||
'comment_insert',
|
||||
'comment_update',
|
||||
'comment_delete',
|
||||
'user_view',
|
||||
'user_insert',
|
||||
'user_update',
|
||||
'user_delete',
|
||||
'user_login',
|
||||
'cron',
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a form definition so the Twitter action can be configured.
|
||||
*
|
||||
* @param $context
|
||||
* Default values (if we are editing an existing action instance).
|
||||
* @return
|
||||
* Form definition.
|
||||
*/
|
||||
function twitter_actions_set_status_action_form($context) {
|
||||
$options = array();
|
||||
$results = db_query("SELECT screen_name FROM {twitter_account}");
|
||||
foreach ($results as $result) {
|
||||
$options[$result->screen_name] = $result->screen_name;
|
||||
}
|
||||
// Set default values for form.
|
||||
$form['screen_name'] = array(
|
||||
'#type' => 'select',
|
||||
'#title' => t('Twitter account name'),
|
||||
'#options' => $options,
|
||||
'#default_value' => isset($context['screen_name']) ? $context['screen_name'] : '',
|
||||
'#required' => TRUE,
|
||||
);
|
||||
|
||||
if (!count($options)) {
|
||||
$form['screen_name']['#description'] = t('You first need to add a Twitter account to one of ' .
|
||||
'your users with rights for posting to Twitter.');
|
||||
}
|
||||
|
||||
$form['message'] = array(
|
||||
'#type' => 'textarea',
|
||||
'#title' => t('Message'),
|
||||
'#default_value' => isset($context['message']) ? $context['message'] : '',
|
||||
'#cols' => '80',
|
||||
'#rows' => '3',
|
||||
'#description' => t('The message that should be sent. You may include the following variables: ' .
|
||||
'%site_name, %username, %node_url, %node_type, %title, %summary, %body, ' .
|
||||
'%tinyurl. Not all variables will be available in all contexts.'),
|
||||
'#required' => TRUE,
|
||||
);
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies if Oauth module has been setup and also checks the Twitter
|
||||
* authentication against the provided screen name.
|
||||
*/
|
||||
function twitter_actions_set_status_action_validate($form, $form_state) {
|
||||
if (!_twitter_use_oauth()) {
|
||||
form_set_error('screen_name', t('Oauth has not been setup yet. Please go to !link and follow steps.',
|
||||
array('!link' => l(t('Twitter settings'), 'admin/settings/twitter'))));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Submits the form and sets the twitter account pulling the data from the
|
||||
* twitter_account table.
|
||||
*/
|
||||
function twitter_actions_set_status_action_submit($form, $form_state) {
|
||||
$form_values = $form_state['values'];
|
||||
$twitter_uid = db_query("SELECT twitter_uid FROM {twitter_account} WHERE screen_name = :screen_name", array(':screen_name' => $form_values['screen_name']))->fetchField();
|
||||
// Process the HTML form to store configuration. The keyed array that
|
||||
// we return will be serialized to the database.
|
||||
$params = array(
|
||||
'twitter_uid' => $twitter_uid,
|
||||
'screen_name' => $form_values['screen_name'],
|
||||
'message' => $form_values['message'],
|
||||
);
|
||||
|
||||
return $params;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of a configurable Twitter actions.
|
||||
* @todo Implementation for language negotiation for the body and sumary. Also
|
||||
* need implementation for bodies with multiple values. Right now it is hard
|
||||
* coded and it will only get body and summary for 'und' language and only
|
||||
* the first value of the body field.
|
||||
* If the final message is over 140 chars, there is no feedback to the user.
|
||||
*/
|
||||
function twitter_actions_set_status_action($object, $context) {
|
||||
global $user;
|
||||
$variables['%site_name'] = variable_get('site_name', 'Drupal');
|
||||
// Seting variables array depending on action's group
|
||||
switch ($context['group']) {
|
||||
case 'node':
|
||||
$node = $context['node'];
|
||||
if (isset($node)) {
|
||||
$variables = array_merge($variables, array(
|
||||
'%uid' => $node->uid,
|
||||
'%username' => $node->name,
|
||||
'%node_url' => url('node/' . $node->nid, array('absolute' => TRUE)),
|
||||
'%node_type' => node_type_get_name($node),
|
||||
'%title' => $node->title,
|
||||
'%summary' => isset($node->body['und'][0]['value']) ? $node->body['und'][0]['summary'] : '',
|
||||
'%body' => isset($node->body['und'][0]['value']) ? $node->body['und'][0]['value'] : '',
|
||||
)
|
||||
);
|
||||
}
|
||||
break;
|
||||
|
||||
case 'comment':
|
||||
$node = node_load($context['comment']->nid);
|
||||
if (isset($node)) {
|
||||
$variables = array_merge($variables, array(
|
||||
'%uid' => $context['comment']->uid,
|
||||
'%username' => $context['comment']->name,
|
||||
'%node_url' => url('node/' . $node->nid, array('absolute' => TRUE)),
|
||||
'%node_type' => node_type_get_name($node),
|
||||
'%title' => $node->title,
|
||||
'%summary' => isset($node->body['und'][0]['value']) ? $node->body['und'][0]['summary'] : '',
|
||||
'%body' => isset($node->body['und'][0]['value']) ? $node->body['und'][0]['value'] : '',
|
||||
)
|
||||
);
|
||||
}
|
||||
break;
|
||||
|
||||
case 'user':
|
||||
$variables['%username'] = $context['user']->name;
|
||||
break;
|
||||
|
||||
case 'cron':
|
||||
break;
|
||||
|
||||
default:
|
||||
// We are being called directly.
|
||||
$node = $object;
|
||||
if (isset($node) && is_object($node)) {
|
||||
$variables = array_merge($variables, array(
|
||||
'%uid' => $node->uid,
|
||||
'%username' => $node->name,
|
||||
'%node_url' => url('node/' . $node->nid, array('absolute' => TRUE)),
|
||||
'%node_type' => node_type_get_name($node),
|
||||
'%title' => $node->title,
|
||||
'%summary' => isset($node->body['und'][0]['value']) ? $node->body['und'][0]['summary'] : '',
|
||||
'%body' => isset($node->body['und'][0]['value']) ? $node->body['und'][0]['value'] : '',
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Only make a tinyurl if it was asked for.
|
||||
if (strstr($context['message'], '%tinyurl') !== FALSE) {
|
||||
$variables = array_merge($variables, array(
|
||||
'%tinyurl' => twitter_shorten_url(url('node/' . $node->nid, array('absolute' => TRUE))),
|
||||
));
|
||||
}
|
||||
|
||||
$message = strtr($context['message'], $variables);
|
||||
module_load_include('inc', 'twitter');
|
||||
$twitter_account = twitter_account_load($context['twitter_uid']);
|
||||
twitter_set_status($twitter_account, $message);
|
||||
}
|
@@ -0,0 +1,118 @@
|
||||
<?php
|
||||
/**
|
||||
* @file
|
||||
* Provide better intergration into the rules module
|
||||
*/
|
||||
|
||||
/**
|
||||
* Implements hook_rules_action_info() on behalf of the twitter module.
|
||||
*/
|
||||
function twitter_actions_rules_action_info() {
|
||||
return array(
|
||||
'rules_core_twitter_actions_set_status_action' => array(
|
||||
'label' => t('Post a message to Twitter'),
|
||||
'group' => t('Twitter'),
|
||||
'parameter' => array(
|
||||
'message' => array(
|
||||
'type' => 'text',
|
||||
'label' => t('Message'),
|
||||
'description' => t("The content of the tweet."),
|
||||
),
|
||||
'sender' => array(
|
||||
'type' => 'user',
|
||||
'label' => t('Sender'),
|
||||
'description' => t("User whose Twitter account will be used."),
|
||||
),
|
||||
),
|
||||
'base' => 'twitter_actions_set_status',
|
||||
'access callback' => 'rules_twitter_actions_access_callback',
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches Twitter account info and submits with the message to the Twitter API
|
||||
*
|
||||
* @param $message
|
||||
* The message to post
|
||||
* @param $sender
|
||||
* The Drupal user that has a Twitter account
|
||||
*/
|
||||
function twitter_actions_set_status($message, $sender) {
|
||||
if ($twitter_uid = db_query("SELECT twitter_uid FROM {twitter_account} WHERE uid = :uid", array(':uid' => $sender->uid))->fetchField()) {
|
||||
module_load_include('inc', 'twitter');
|
||||
$twitter_account = twitter_account_load($twitter_uid);
|
||||
twitter_set_status($twitter_account, $message);
|
||||
}
|
||||
else {
|
||||
watchdog('twitter', 'Twitter authentication failed. Please check your account name and try again.', array(), WATCHDOG_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_rules_condition_info().
|
||||
*/
|
||||
function twitter_actions_rules_condition_info() {
|
||||
$defaults = array(
|
||||
'group' => t('Twitter'),
|
||||
'parameter' => array(
|
||||
'user' => array(
|
||||
'type' => 'user',
|
||||
'label' => t('User'),
|
||||
'description' => t('The user to be checked for.'),
|
||||
),
|
||||
),
|
||||
'named parameter' => TRUE,
|
||||
'access callback' => 'rules_twitter_actions_access_callback',
|
||||
);
|
||||
$items['rules_core_twitter_conditions_user_has_linked'] = $defaults + array(
|
||||
'label' => t('User has linked Twitter account'),
|
||||
'help' => t('Evaluates to TRUE in case there is a record in the twitter_account for the provided user.'),
|
||||
'base' => 'twitter_actions_has_linked',
|
||||
);
|
||||
|
||||
$items['rules_core_twitter_conditions_text_is_under_140'] = array(
|
||||
'group' => t('Twitter'),
|
||||
'named parameter' => TRUE,
|
||||
'parameter' => array(
|
||||
'text' => array(
|
||||
'type' => 'text',
|
||||
'label' => t('Text to check'),
|
||||
),
|
||||
),
|
||||
'label' => t('Text is under 140 characters'),
|
||||
'help' => t('Returns TRUE if the length of the text is 140 or less.'),
|
||||
'base' => 'twitter_actions_less_140',
|
||||
);
|
||||
|
||||
return $items;
|
||||
}
|
||||
|
||||
/**
|
||||
* The callback function for the Rules condition
|
||||
* @param $element
|
||||
* $element['user']: The user to be checked for.
|
||||
* @return
|
||||
* TRUE if the user has linked his/her Twitter account.
|
||||
*/
|
||||
function twitter_actions_has_linked($element) {
|
||||
return db_query("SELECT twitter_uid FROM {twitter_account} WHERE uid = :uid", array(':uid' => $element['user']->uid))->fetchField() ? TRUE : FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* The callback function for the Rules condition
|
||||
* @param $element
|
||||
* $element['user']: The user to be checked for.
|
||||
* @return
|
||||
* TRUE if the user has linked his/her Twitter account.
|
||||
*/
|
||||
function twitter_actions_less_140($element) {
|
||||
return strlen($element['text']) < 141;
|
||||
}
|
||||
|
||||
/**
|
||||
* The callback function to access the condition
|
||||
*/
|
||||
function rules_twitter_actions_access_callback($type, $name) {
|
||||
return user_access('add twitter accounts');
|
||||
}
|
Reference in New Issue
Block a user