array( 'type' => 'system', 'description' => t('Post a message to Twitter'), 'configurable' => TRUE, 'hooks' => array( 'nodeapi' => array('view', 'insert', 'update', 'delete'), 'comment' => array('view', 'insert', 'update', 'delete'), 'user' => array('view', 'insert', 'update', 'delete', 'login'), 'cron' => array('run'), ), ), ); } /** * Return a form definition so the Send email 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 = array()) { // Set default values for form. $context += array( 'account_id' => -1, 'screen_name' => '', 'message' => '', ); $form['screen_name'] = array( '#type' => 'textfield', '#title' => t('Twitter account name'), '#default_value' => $context['screen_name'], '#size' => 25, '#required' => TRUE, ); $form['message'] = array( '#type' => 'textarea', '#title' => t('Message'), '#default_value' => $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, %teaser, %body, %tinyurl. Not all variables will be available in all contexts.'), '#required' => TRUE, ); return $form; } function twitter_actions_set_status_action_validate($form, $form_state) { if (!_twitter_use_oauth()) { form_set_error('screen_name', t('Oath has not yet been setup.')); } if (!db_result(db_query("SELECT twitter_uid FROM {twitter_account} WHERE screen_name = '%s'", $form_state['values']['screen_name']))) { form_set_error('screen_name', t('Twitter authentication failed. Please check your account name and try again.')); } } function twitter_actions_set_status_action_submit($form, $form_state) { $form_values = $form_state['values']; $twitter_uid = db_result(db_query("SELECT twitter_uid FROM {twitter_account} WHERE screen_name = '%s'", $form_values['screen_name'])); // 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 Drupal action. * Sends an email. */ function twitter_actions_set_status_action($object, $context) { global $user; $variables['%site_name'] = variable_get('site_name', 'Drupal'); switch ($context['hook']) { case 'nodeapi': case 'workflow': // Because this is not an action of type 'node' the node // will not be passed as $object, but it will still be available // in $context. $node = $context['node']; break; // The comment hook provides nid, in $context. case 'comment': $comment = $context['comment']; $node = node_load($comment->nid); case 'user': // Because this is not an action of type 'user' the user // object is not passed as $object, but it will still be available // in $context. $account = $context['account']; if (isset($context['node'])) { $node = $context['node']; } elseif ($context['recipient'] == '%author') { // If we don't have a node, we don't have a node author. watchdog('error', 'Cannot use %author token in this context.'); return; } break; case 'taxonomy': $account = $user; $vocabulary = taxonomy_vocabulary_load($object->vid); $variables = array_merge($variables, array( '%term_name' => $object->name, '%term_description' => $object->description, '%term_id' => $object->tid, '%vocabulary_name' => $vocabulary->name, '%vocabulary_description' => $vocabulary->description, '%vocabulary_id' => $vocabulary->vid, ) ); break; default: // We are being called directly. $node = $object; } if (isset($node)) { if (!isset($account)) { $account = user_load(array('uid' => $node->uid)); } if ($recipient == '%author') { $recipient = $account->mail; } } $variables['%username'] = $account->name; // Node-based variable translation is only available if we have a node. if (isset($node) && is_object($node)) { $variables = array_merge($variables, array( '%uid' => $node->uid, '%node_url' => url('node/'. $node->nid, array('absolute' => TRUE)), '%node_type' => node_get_types('name', $node), '%title' => $node->title, '%teaser' => $node->teaser, '%body' => $node->body ) ); } // 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); }