twitter_actions.module 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /**
  3. * @file
  4. * Exposes Drupal actions for sending Twitter messages.
  5. */
  6. /**
  7. * Implements hook_action_info().
  8. */
  9. function twitter_actions_action_info() {
  10. return array(
  11. 'twitter_actions_set_status_action' => array(
  12. 'type' => 'system',
  13. 'label' => t('Post a message to Twitter'),
  14. 'configurable' => TRUE,
  15. 'triggers' => array(
  16. 'node_view',
  17. 'node_insert',
  18. 'node_update',
  19. 'node_delete',
  20. 'comment_view',
  21. 'comment_insert',
  22. 'comment_update',
  23. 'comment_delete',
  24. 'user_view',
  25. 'user_insert',
  26. 'user_update',
  27. 'user_delete',
  28. 'user_login',
  29. 'cron',
  30. ),
  31. ),
  32. );
  33. }
  34. /**
  35. * Returns a form definition so the Twitter action can be configured.
  36. *
  37. * @param $context
  38. * Default values (if we are editing an existing action instance).
  39. * @return
  40. * Form definition.
  41. */
  42. function twitter_actions_set_status_action_form($context) {
  43. $options = array();
  44. $results = db_query("SELECT screen_name FROM {twitter_account}");
  45. foreach ($results as $result) {
  46. $options[$result->screen_name] = $result->screen_name;
  47. }
  48. // Set default values for form.
  49. $form['screen_name'] = array(
  50. '#type' => 'select',
  51. '#title' => t('Twitter account name'),
  52. '#options' => $options,
  53. '#default_value' => isset($context['screen_name']) ? $context['screen_name'] : '',
  54. '#required' => TRUE,
  55. );
  56. if (!count($options)) {
  57. $form['screen_name']['#description'] = t('You first need to add a Twitter account to one of ' .
  58. 'your users with rights for posting to Twitter.');
  59. }
  60. $form['message'] = array(
  61. '#type' => 'textarea',
  62. '#title' => t('Message'),
  63. '#default_value' => isset($context['message']) ? $context['message'] : '',
  64. '#cols' => '80',
  65. '#rows' => '3',
  66. '#description' => t('The message that should be sent. You may include the following variables: ' .
  67. '%site_name, %username, %node_url, %node_type, %title, %summary, %body, ' .
  68. '%tinyurl. Not all variables will be available in all contexts.'),
  69. '#required' => TRUE,
  70. );
  71. return $form;
  72. }
  73. /**
  74. * Verifies if Oauth module has been setup and also checks the Twitter
  75. * authentication against the provided screen name.
  76. */
  77. function twitter_actions_set_status_action_validate($form, $form_state) {
  78. if (!_twitter_use_oauth()) {
  79. form_set_error('screen_name', t('Oauth has not been setup yet. Please go to !link and follow steps.',
  80. array('!link' => l(t('Twitter settings'), 'admin/settings/twitter'))));
  81. }
  82. }
  83. /**
  84. * Submits the form and sets the twitter account pulling the data from the
  85. * twitter_account table.
  86. */
  87. function twitter_actions_set_status_action_submit($form, $form_state) {
  88. $form_values = $form_state['values'];
  89. $twitter_uid = db_query("SELECT twitter_uid FROM {twitter_account} WHERE screen_name = :screen_name", array(':screen_name' => $form_values['screen_name']))->fetchField();
  90. // Process the HTML form to store configuration. The keyed array that
  91. // we return will be serialized to the database.
  92. $params = array(
  93. 'twitter_uid' => $twitter_uid,
  94. 'screen_name' => $form_values['screen_name'],
  95. 'message' => $form_values['message'],
  96. );
  97. return $params;
  98. }
  99. /**
  100. * Implementation of a configurable Twitter actions.
  101. * @todo Implementation for language negotiation for the body and sumary. Also
  102. * need implementation for bodies with multiple values. Right now it is hard
  103. * coded and it will only get body and summary for 'und' language and only
  104. * the first value of the body field.
  105. * If the final message is over 140 chars, there is no feedback to the user.
  106. */
  107. function twitter_actions_set_status_action($object, $context) {
  108. global $user;
  109. $variables['%site_name'] = variable_get('site_name', 'Drupal');
  110. // Seting variables array depending on action's group
  111. switch ($context['group']) {
  112. case 'node':
  113. $node = $context['node'];
  114. if (isset($node)) {
  115. $variables = array_merge($variables, array(
  116. '%uid' => $node->uid,
  117. '%username' => $node->name,
  118. '%node_url' => url('node/' . $node->nid, array('absolute' => TRUE)),
  119. '%node_type' => node_type_get_name($node),
  120. '%title' => $node->title,
  121. '%summary' => isset($node->body['und'][0]['value']) ? $node->body['und'][0]['summary'] : '',
  122. '%body' => isset($node->body['und'][0]['value']) ? $node->body['und'][0]['value'] : '',
  123. )
  124. );
  125. }
  126. break;
  127. case 'comment':
  128. $node = node_load($context['comment']->nid);
  129. if (isset($node)) {
  130. $variables = array_merge($variables, array(
  131. '%uid' => $context['comment']->uid,
  132. '%username' => $context['comment']->name,
  133. '%node_url' => url('node/' . $node->nid, array('absolute' => TRUE)),
  134. '%node_type' => node_type_get_name($node),
  135. '%title' => $node->title,
  136. '%summary' => isset($node->body['und'][0]['value']) ? $node->body['und'][0]['summary'] : '',
  137. '%body' => isset($node->body['und'][0]['value']) ? $node->body['und'][0]['value'] : '',
  138. )
  139. );
  140. }
  141. break;
  142. case 'user':
  143. $variables['%username'] = $context['user']->name;
  144. break;
  145. case 'cron':
  146. break;
  147. default:
  148. // We are being called directly.
  149. $node = $object;
  150. if (isset($node) && is_object($node)) {
  151. $variables = array_merge($variables, array(
  152. '%uid' => $node->uid,
  153. '%username' => $node->name,
  154. '%node_url' => url('node/' . $node->nid, array('absolute' => TRUE)),
  155. '%node_type' => node_type_get_name($node),
  156. '%title' => $node->title,
  157. '%summary' => isset($node->body['und'][0]['value']) ? $node->body['und'][0]['summary'] : '',
  158. '%body' => isset($node->body['und'][0]['value']) ? $node->body['und'][0]['value'] : '',
  159. )
  160. );
  161. }
  162. }
  163. // Only make a tinyurl if it was asked for.
  164. if (strstr($context['message'], '%tinyurl') !== FALSE) {
  165. $variables = array_merge($variables, array(
  166. '%tinyurl' => twitter_shorten_url(url('node/' . $node->nid, array('absolute' => TRUE))),
  167. ));
  168. }
  169. $message = strtr($context['message'], $variables);
  170. module_load_include('inc', 'twitter');
  171. $twitter_account = twitter_account_load($context['twitter_uid']);
  172. twitter_set_status($twitter_account, $message);
  173. }