twitter_actions.rules.inc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * @file
  4. * Provide better intergration into the rules module
  5. */
  6. /**
  7. * Implements hook_rules_action_info() on behalf of the twitter module.
  8. */
  9. function twitter_actions_rules_action_info() {
  10. return array(
  11. 'rules_core_twitter_actions_set_status_action' => array(
  12. 'label' => t('Post a message to Twitter'),
  13. 'group' => t('Twitter'),
  14. 'parameter' => array(
  15. 'message' => array(
  16. 'type' => 'text',
  17. 'label' => t('Message'),
  18. 'description' => t("The content of the tweet."),
  19. ),
  20. 'sender' => array(
  21. 'type' => 'user',
  22. 'label' => t('Sender'),
  23. 'description' => t("User whose Twitter account will be used."),
  24. ),
  25. ),
  26. 'base' => 'twitter_actions_set_status',
  27. 'access callback' => 'rules_twitter_actions_access_callback',
  28. ),
  29. );
  30. }
  31. /**
  32. * Fetches Twitter account info and submits with the message to the Twitter API
  33. *
  34. * @param $message
  35. * The message to post
  36. * @param $sender
  37. * The Drupal user that has a Twitter account
  38. */
  39. function twitter_actions_set_status($message, $sender) {
  40. if ($twitter_uid = db_query("SELECT twitter_uid FROM {twitter_account} WHERE uid = :uid", array(':uid' => $sender->uid))->fetchField()) {
  41. module_load_include('inc', 'twitter');
  42. $twitter_account = twitter_account_load($twitter_uid);
  43. twitter_set_status($twitter_account, $message);
  44. }
  45. else {
  46. watchdog('twitter', 'Twitter authentication failed. Please check your account name and try again.', array(), WATCHDOG_ERROR);
  47. }
  48. }
  49. /**
  50. * Implements hook_rules_condition_info().
  51. */
  52. function twitter_actions_rules_condition_info() {
  53. $defaults = array(
  54. 'group' => t('Twitter'),
  55. 'parameter' => array(
  56. 'user' => array(
  57. 'type' => 'user',
  58. 'label' => t('User'),
  59. 'description' => t('The user to be checked for.'),
  60. ),
  61. ),
  62. 'named parameter' => TRUE,
  63. 'access callback' => 'rules_twitter_actions_access_callback',
  64. );
  65. $items['rules_core_twitter_conditions_user_has_linked'] = $defaults + array(
  66. 'label' => t('User has linked Twitter account'),
  67. 'help' => t('Evaluates to TRUE in case there is a record in the twitter_account for the provided user.'),
  68. 'base' => 'twitter_actions_has_linked',
  69. );
  70. $items['rules_core_twitter_conditions_text_is_under_140'] = array(
  71. 'group' => t('Twitter'),
  72. 'named parameter' => TRUE,
  73. 'parameter' => array(
  74. 'text' => array(
  75. 'type' => 'text',
  76. 'label' => t('Text to check'),
  77. ),
  78. ),
  79. 'label' => t('Text is under 140 characters'),
  80. 'help' => t('Returns TRUE if the length of the text is 140 or less.'),
  81. 'base' => 'twitter_actions_less_140',
  82. );
  83. return $items;
  84. }
  85. /**
  86. * The callback function for the Rules condition
  87. * @param $element
  88. * $element['user']: The user to be checked for.
  89. * @return
  90. * TRUE if the user has linked his/her Twitter account.
  91. */
  92. function twitter_actions_has_linked($element) {
  93. return db_query("SELECT twitter_uid FROM {twitter_account} WHERE uid = :uid", array(':uid' => $element['user']->uid))->fetchField() ? TRUE : FALSE;
  94. }
  95. /**
  96. * The callback function for the Rules condition
  97. * @param $element
  98. * $element['user']: The user to be checked for.
  99. * @return
  100. * TRUE if the user has linked his/her Twitter account.
  101. */
  102. function twitter_actions_less_140($element) {
  103. return strlen($element['text']) < 141;
  104. }
  105. /**
  106. * The callback function to access the condition
  107. */
  108. function rules_twitter_actions_access_callback($type, $name) {
  109. return user_access('add twitter accounts');
  110. }