From 43b7871f013a2d06c61af191ad2eb781b288d98f Mon Sep 17 00:00:00 2001
From: Peter <aegir@mysite.com>
Date: Thu, 2 Dec 2010 16:36:30 -0600
Subject: [PATCH] added rules integration

---
 smsbulk.module |  132 ++++++++++++++++++++++++++++++++++++++++++--------------
 1 files changed, 100 insertions(+), 32 deletions(-)

diff --git a/smsbulk.module b/smsbulk.module
index 9e529ed..f9a49bc 100755
--- a/smsbulk.module
+++ b/smsbulk.module
@@ -98,41 +98,103 @@ function _send_sms_action_form($context) {
   $limit = variable_get('smsbulk_max_chars', SMSBULK_MAX_CHARS);
   drupal_add_js(drupal_get_path('module', 'smsbulk') .'/smsbulk.js');
   $options = array(t('- select -'));
-  $view = views_get_current_view();
-  foreach ($view->field as $field) {
-    $options += array($field->field_alias => $field->options['label']);
-  }
-  $form['sms_field'] = array(
-    '#type' => 'select',
-    '#options' => $options,
-    '#title' => t('Number field'),
-    '#description' => t('Select the field that acts as mobile phone number'),
-    '#required' => TRUE,
-  );
-  $form['sms_text'] = array(
-    '#type'  => 'textarea',
-    '#title' => 'SMS Text',
-    '#required' => TRUE,
-  );
-  $form['sms_limit'] = array(
-    '#type'   => 'markup',
-    '#prefix' => '<div id="keystrokes" class="description" limit="'. $limit .'"><span></span>',
-    '#value'  => t('@max chars max', array('@max' => $limit)),
-    '#suffix' => ' ('. t('if you are using tokens then this value is just a hint') .')</div>',
-  );
+  //added checking for use with rules
+  if (!isset($context['view']->field)) {
+    $view = views_get_view($context['view']->name);
+    $view = $context['view']->display['default']->display_options;
+    if(!isset($context['view_vid'])){
+      foreach ($view['fields'] as $field) {
+	$field_alias = $field['table'] .'_' . $field['field'];
+	$options += array($field_alias => $field['label']);
 
-  $form += sms_bulk_get_gateway_options();
+      }
+    }
+    else {
+      $options += array($context['sms_field'] => $context['sms_field']);
+    }
+  }
+  else {
+    $view = views_get_current_view();
+    foreach ($view->field as $field) {
+      $options += array($field->field_alias => $field->options['label']);
+    }
+  }
+  if(arg(1) == 'rules' || !isset($context['view']->field)){
+    $form['sms_field'] = array(
+      '#type' => 'select',
+      '#options' => $options,
+      '#title' => t('Number field'),
+      '#default_value' => isset($context['sms_field']) ? $context['sms_field'] : '', 
+      '#description' => t('Select the field that acts as mobile phone number'),
+      '#required' => TRUE,
+    );
+    if(arg(1) == 'rules'){
+      $form['rules_warning'] = array(
+      '#type'   => 'markup',
+      '#value'  => t('To see the other field options, you will need to switch Operation from "Send SMS to contact list" and then back.  You will loose all your other settings on this page by doing that.'),
+      );
+    }
+    $form['sms_text'] = array(
+      '#type'  => 'textarea',
+      '#title' => 'SMS Text',
+      '#default_value' => isset($context['sms_text']) ? $context['sms_text'] : '', 
+      '#required' => TRUE,
+    );
+    $form += sms_bulk_get_gateway_options();
+    $form['gateway']['country']['#default_value'] = isset($context['gateway_options']) ? $context['gateway_options'] : '1'; 
 
-  if (module_exists('token')) {
-    $form['view']['token_help'] = array(
-      '#title' => t('Replacement patterns'),
-      '#type' => 'fieldset',
-      '#collapsible' => TRUE,
-      '#collapsed' => TRUE,
+    if (module_exists('token')) {
+      //drupal_add_js('misc/collapse.js');
+      $form['view']['token_help'] = array(
+	'#title' => t('Replacement patterns'),
+	'#type' => 'fieldset',
+	'#collapsible' => TRUE,
+	'#collapsed' => TRUE,
+      );
+      $form['view']['token_help']['help'] = array(
+	'#value' => theme('token_help', 'node'),
+      );
+    }
+      $form['sms_rulesform'] = array(
+      '#type'   => 'value',
+      '#value'  => TRUE,
+      );
+  }
+  else {
+    $form['sms_field'] = array(
+      '#type' => 'select',
+      '#options' => $options,
+      '#title' => t('Number field'),
+      '#default_value' => variable_get('sms_bulk_sms_field', ''), 
+      '#description' => t('Select the field that acts as mobile phone number'),
+      //'#required' => TRUE,
+    );
+    $form['sms_text'] = array(
+      '#type'  => 'textarea',
+      '#title' => 'SMS Text',
+      '#default_value' => variable_get('sms_bulk_sms_text', ''), 
     );
-    $form['view']['token_help']['help'] = array(
-      '#value' => theme('token_help', 'smsbulk_row'),
+    $form['sms_limit'] = array(
+      '#type'   => 'markup',
+      '#prefix' => '<div id="keystrokes" class="description" limit="'. $limit .'"><span></span>',
+      '#value'  => t('@max chars max', array('@max' => $limit)),
+      '#suffix' => ' ('. t('if you are using tokens then this value is just a hint') .')</div>',
     );
+
+    $form += sms_bulk_get_gateway_options();
+
+
+    if (module_exists('token')) {
+      $form['view']['token_help'] = array(
+	'#title' => t('Replacement patterns'),
+	'#type' => 'fieldset',
+	'#collapsible' => TRUE,
+	'#collapsed' => TRUE,
+      );
+      $form['view']['token_help']['help'] = array(
+	'#value' => theme('token_help', 'smsbulk_row'),
+      );
+    }
   }
   return $form;
 }
@@ -148,6 +210,7 @@ function _send_sms_action_submit($form, $form_state) {
     'sms_text'          => $form_state['values']['sms_text'],
     'sms_field'         => $form_state['values']['sms_field'],
     'gateway_options'   => $form_state['values']['gateway'],
+    'sms_rulesform'	=> $form_state['values']['sms_rulesform'],
   );
 }
 
@@ -181,7 +244,12 @@ function _smsbulk_send(&$object, &$context, $oid) {
     }
     else {
       if (module_exists('token')) {
-        $sms_text = token_replace($context['sms_text'], 'smsbulk_row', $row);
+	if ($context['sms_rulesform']) {
+	  $sms_text = token_replace($context['sms_text'], 'node', $object);
+	}
+	else {
+	  $sms_text = token_replace($context['sms_text'], 'smsbulk_row', $row);
+	}
       }
       else {
         $sms_text = $context['sms_text'];
-- 
1.5.4.3

