Index: twitter.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/twitter/twitter.module,v
retrieving revision 1.1
diff -u -p -r1.1 twitter.module
--- twitter.module	23 Jan 2007 17:05:50 -0000	1.1
+++ twitter.module	10 Feb 2008 21:09:40 -0000
@@ -4,6 +4,129 @@
 define('TWITTER_URL', 'http://twitter.com/statuses/update.xml');
 
 /**
+ * Implementation of hook_menu
+ *
+ */
+
+function twitter_menu($may_cache) {
+  $items = array();
+    if ($may_cache) {
+      $items[] = array(
+        'path' => 'admin/settings/twitter',
+        'title' => t('Twitter configuration details'),
+        'description' => t('Set details like the site-wide twitter account.'),
+        'callback' => 'drupal_get_form',
+        'callback arguments' => array('twitter_admin_settings'),
+        'access' => user_access('administer site configuration'),
+     );  
+   return $items;  
+  } 
+}
+
+
+/**
+ * Implementation of hook_perm
+ *
+ */
+function twitter_perm() {
+  return array(
+    'broadcast to personal twitter',
+    'broadcast to sitewide twitter',
+  );
+}
+ 
+
+function twitter_admin_settings() {
+
+	$form['twitter'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Twitter site-wide settings.'),
+  );
+  $form['twitter']['twitter_site_user'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Site-wide twitter username.'),
+    '#default_value' => variable_get('twitter_site_user', ''),
+  );
+  $form['twitter']['twitter_site_pass'] = array(
+    '#type' => 'password',
+    '#title' => t('Password'),
+  );
+  $form['twitter']['twitter_site_text'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Text format'),
+    '#default_value' => variable_get('twitter_site_text', ''),
+    '#description' => t('Format of the text to submit. Use !title and !url for the post title and url respectively.')
+  );
+  
+  // todo, is there a better way to do this ala FAPI?
+  $edit = $_POST;
+  if ($edit) {
+    $form['twitter']['twitter_site_encrypted'] = array(
+      '#type' => 'value',
+      '#value' => base64_encode($edit['twitter_site_user'] . ':' . $edit['twitter_site_pass']),
+    );
+  }
+  return system_settings_form($form);
+}
+
+/**
+ * Implementation of hook_form_alter
+ * Hook into node type forms.
+ */
+function twitter_form_alter($form_id, &$form) {
+	switch($form_id) {
+		case 'node_type_form':
+			$form['workflow']['twitter'] = array(
+			  '#type' => 'radios',
+			  '#title' => t('Twitter broadcasting'),
+			  '#description' => t('Enable twitter broadcasting upon publishing of this node-type'),
+			  '#default_value' => variable_get('twitter_'. $form['#node_type']->type, 0),
+			  '#options' => array(
+			    t('Disabled'),
+			    t('Enabled')),
+			  '#weight' => 20,
+	    );
+	  break;
+		case $form['type']['#value'] .'_node_form':
+			if (user_access('broadcast to personal twitter') || user_access('broadcast to sitewide twitter')) {
+			  twitter_alter_node_form($form_id, $form);
+	    }
+	  break;
+	}
+}
+
+/**
+ * Add twitter checkboxes on node-edit form.
+ *
+ */
+
+function twitter_alter_node_form($form_id, &$form) {
+  $form['twitter'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Twitter settings'),
+    '#collapsible' => TRUE,
+    '#collapsed' => TRUE,
+    '#weight' => 30,
+  );
+  
+  $options = array();
+  if (user_access('broadcast to personal twitter')) {
+  	$options['personal'] = t('Send to personal twitter');
+  }
+  if (user_access('broadcast to personal twitter')) {
+  	$options['sitewide'] = t('Send to sitewide twitter');
+  }
+  $form['twitter']['twitter_options'] = array(
+    '#type' => 'checkboxes',
+    '#title' => t('Twitter broadcast options'),
+    '#default_value' => '',
+    '#options' => $options,
+    '#description' => t('Choose whether to send this node title / url to your personal and/or site-wide twitter feed.'),
+  );
+}
+
+
+/**
  * Implementation of hook_user()
  */
 function twitter_user($op, &$edit, &$account, $category = NULL) {
@@ -40,8 +163,14 @@ function twitter_nodeapi(&$node, $op) {
     case 'insert':
       global $user;
       if ($node->status == 1) {
-        twitter_post($node, $user);
+      	if ($node->twitter_options['personal']) {
+      		twitter_post($node, $user);
+      	}
+        if ($node->twitter_options['sitewide']) {
+        	twitter_post($node, $user, TRUE);
+        }
       }
+    break;
   }
 }
 
@@ -49,16 +178,26 @@ function twitter_nodeapi(&$node, $op) {
  * Implements the twitter posting API per:
  * http://twitter.com/help/api
  */
-function twitter_post($node, $account) {
-  if (empty($account->twitter_encrypted)) {
+function twitter_post($node, $account, $sitewide = FALSE) {
+  // Check if key is set, in the case of posting to user account or sitewide account.
+  // If it's false in either case, return false.
+	if (($sitewide == 0 && empty($account->twitter_encrypted)) || ($sitewide == 1 && !variable_get('twitter_site_encrypted', ''))) {
+		print 'did not pass.';
     return false;
   }
-
-  $text = ($account->twitter_text) ? $account->twitter_text : 'New post: !title (!url)';
+  
+  if (!$sitewide) {
+  	$text = ($account->twitter_text) ? $account->twitter_text : 'New post: !title (!url)';
+  	$auth = $account->twitter_encrypted;
+  }
+  elseif ($sitewide) {
+    $text = variable_get('twitter_site_text','') ? variable_get('twitter_site_text','') : 'New post: !title (!url)';
+    $auth = variable_get('twitter_site_encrypted','');
+  }
   $text = t($text, array('!title' => $node->title,
                          '!url' => url('node/' . $node->nid, NULL, NULL, TRUE)));
   
-  $headers = array('Authorization' => 'Basic '. $account->twitter_encrypted,
+  $headers = array('Authorization' => 'Basic '. $auth,
                    'Content-type' => 'application/x-www-form-urlencoded');
   $data = 'status='. urlencode($text);
 
