? twitter-539400.patch
Index: twitter.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/twitter/twitter.module,v
retrieving revision 1.3.2.14
diff -u -p -r1.3.2.14 twitter.module
--- twitter.module	2 Jul 2009 14:54:18 -0000	1.3.2.14
+++ twitter.module	6 Aug 2009 08:49:11 -0000
@@ -27,6 +27,14 @@ function twitter_menu() {
     'type' => MENU_LOCAL_TASK,
   );
 
+  $items['twitter/tweet'] = array(
+    'title' => 'Tweet',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('twitter_tweet_form'),
+    'access arguments' => array('add twitter accounts'),
+    'type' => MENU_CALLBACK
+  );
+  
   return $items;
 }
 
@@ -313,3 +321,89 @@ function twitter_twitter_accounts($drupa
 function twitter_views_api() {
   return array('api' => 2);
 }
+
+/**
+ * Implementaiton for tweeting from Drupal itslef
+ */
+
+/**
+ * Function to render the form for tweeting.
+ * @return unknown_type
+ */
+  
+function twitter_tweet_form($context) {
+  module_load_include('inc', 'twitter');
+  global $user;
+  
+  $form['tweet'] = array(
+      '#type' => 'textarea',
+      '#title' => t('Tweet'),
+      '#size' => 50,
+      '#required' => TRUE,
+      '#description' => t('What are you doing right now?.'),
+  );
+  
+  $twitter_accounts = twitter_get_user_accounts($user->uid);
+  $names = array();
+  foreach($twitter_accounts as $twitter_account){
+    $names[][$twitter_account['screen_name']] = $twitter_account['screen_name'];
+  }
+  $form['twitter_account'] = array(
+    '#type' => 'select',
+    '#title' => t("Select Account"),
+    '#options' => $names,
+    '#description' =>  t('Select Twitter account')
+  );
+  
+  $form['submit'] = array(
+   '#type' => 'submit',
+   '#value' => t('Post to Twitter'),
+  );
+ 
+  
+  return $form;
+   
+}
+
+
+/**
+ * Implementation of 
+ */
+   
+function twitter_tweet_form_validate($form, &$form_state) {
+  $form_values = $form_state['values'];
+  
+  if(strlen($form_values['tweet']) > 140){
+    form_set_error("tweet", "Tweet cannot be more than 140 characters");
+  }
+  
+}
+
+
+function twitter_tweet_form_submit($form, &$form_state) {
+  module_load_include('inc', 'twitter');
+  
+  $form_values = $form_state['values'];
+  $twitter_username = mysql_escape_string($form_values['twitter_account']);
+  $sql = "SELECT ta.*, tu.uid, tu.password, tu.import FROM {twitter_user} tu LEFT JOIN {twitter_account} ta ON (tu.screen_name = ta.screen_name) WHERE ta.screen_name = '%s'";
+  $args = array($twitter_username);
+  $results = db_query($sql, $args);
+  $account = db_fetch_array($results);
+
+  if(count($account == 1)){
+    $pass = $account['password'];
+  }
+  
+  $status = $form_values['tweet'];
+  
+  $result = twitter_set_status($twitter_username, $pass, $status);
+  if (_twitter_request_failure($result)) {
+    drupal_set_message(t('An error occurred when posting to twitter: %code %error',
+                         array('%code' => $result->code, '%error' => $result->error)), 'warning');
+  }
+  else {
+    drupal_set_message(t('Successfully posted to Twitter accont with username '.$twitter_username));
+    twitter_cron();
+  }
+
+}
