123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- <?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);
- }
|