Index: sugarondrupal.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/sugarondrupal/Attic/sugarondrupal.module,v
retrieving revision 1.1.2.27
diff -u -r1.1.2.27 sugarondrupal.module
--- sugarondrupal.module	25 Dec 2010 22:19:00 -0000	1.1.2.27
+++ sugarondrupal.module	26 Dec 2010 12:20:37 -0000
@@ -96,6 +96,263 @@
 }
 
 /**
+ * SugarCRM Action definition
+ */
+
+
+/**
+ * Implementation of hook_action_info().
+ */
+function sugarondrupal_action_info() {
+  return array(
+    'sugarondrupal_set_entry_action' => array(
+      'description'   => t('Create a SugarCRM entry'),
+      'type'          => 'system',
+      'configurable'  => TRUE,
+      'hooks'         => array('any' => TRUE),
+    ),
+  );
+}
+
+/**
+ * Configure SugarCRM Set Entry advanced action.
+ *
+ * @param $context array for context of this advanced action.
+ *
+ * @return
+ *   a configuration Form array for the advanced action.
+ */
+function sugarondrupal_set_entry_action_form($context) {
+  $form = array();
+
+  // Make $context available for hook_form_alter functions.
+  $form['sugarondrupal_context'] = array(
+    '#type'        => 'value',
+    '#value'       => $context,
+  );
+
+  // Make the user select one module.
+  $modules = sugarondrupal_get_available_modules();
+  foreach ($modules as $name) {
+    $module_names[$name] = $name;
+  }
+  $form['sugarondrupal_module_name'] = array(
+    '#type'           => 'select',
+    '#title'          => t('Entry type'),
+    '#options'        => $module_names,
+    '#description'    => t('Select the entry type to be created in SugarCRM. Please, note that only one entry of one type will be created. If you want to provide a Contact with custom Account (a different module information) you need to use additional SugaronDrupal configuration options.'),
+    '#default_value'  => isset($context['sugarondrupal_module_name']) ? $context['sugarondrupal_module_name'] : '',
+    '#weight'         => -1,
+  );
+
+  return $form;
+}
+
+/**
+ * Implementation of hook_form_system_actions_configure_alter().
+ */
+function sugarondrupal_form_system_actions_configure_alter($form, $form_state) {
+  // Only process sugarondrupal advanced actions.
+  if (!isset($form['sugarondrupal_module_name'])) {
+    return; 
+  }
+
+  // Retrieve advanced action context.
+  $context = $form['sugarondrupal_context']['#value'];
+
+  if (isset($form_state['post']['sugarondrupal_module_name']) || !empty($form['sugarondrupal_module_name']['#default_value'])) {
+    // sugarondrupal_module_name field might not be set yet, so use POST information.
+    $params = array(
+      'module' => (isset($form['sugarondrupal_module_name']['#default_value']) && !empty($form['sugarondrupal_module_name']['#default_value'])) ? $form['sugarondrupal_module_name']['#default_value'] : $form_state['post']['sugarondrupal_module_name'],
+      'fields' => array(),
+    );
+    $fields = sugarondrupal_get_module_fields($params);
+
+    // Create a container.
+    $form['sugarondrupal_fields'] = array(
+      '#type'   => 'fieldset',
+      '#title'  => t('Entry fields'),
+      '#weight' => 0,
+    );
+
+    // Provide some help about tokens.
+    $form['sugarondrupal_fields']['token_help'] = array(
+      '#title' => t('Replacement patterns'),
+      '#type' => 'fieldset',
+      '#collapsible' => TRUE,
+      '#collapsed' => TRUE,
+    );
+
+    if (module_exists('token')) {
+      $form['sugarondrupal_fields']['token_help']['help'] = array(
+        '#value' => theme('token_help', 'all'),
+      );
+    }
+    else {
+      $form['sugarondrupal_fields']['token_help']['help'] = array(
+        '#value' => t('Predefined object attributes can be used to populate these fields. If you are assigning this action for object based events, you can put attributes enclosed in the form [attribute_name] to populate these fields. i.e., if you are assigning this action to an User event, you may use [mail] to populate the field with $user->mail attribute. It is recommended to install Token module to gain advantage of the token replacement.'),
+      );
+      $form['sugarondrupal_fields']['token_help']['#collapsed'] = FALSE;
+    }
+
+    // Attach SugarCRM fields.
+    foreach ($fields['module_fields'] as $name => $field) {
+      $form['sugarondrupal_fields'][$name] = _sugarondrupal_build_form_element($field, $context);
+    }
+  }
+  // The form has not been submitted, make initial alterations.
+  else {
+    $form['buttons']['submit']['#value'] = t('Continue');
+  }
+
+  return $form;
+}
+
+function _sugarondrupal_build_form_element($field, $context) {
+  $element = array();
+
+  // Convert SugarCRM entry fields into Drupal Form API fields.
+  switch ($field['type']) {
+    case 'relate':
+      return NULL;
+    case 'name':
+    case 'varchar':
+    case 'full_name':
+    case 'phone':
+    case 'email':
+      $element['#type'] = 'textfield';
+      break;
+    case 'datetime':
+      $element['#type'] = 'textfield';
+      $element['#description'] = t('Use SugarCRM datetime format.');
+      break;
+    case 'date':
+      $element['#type'] = 'textfield';
+      $element['#description'] = t('Use SugarCRM date format.');
+      break;
+    case 'assigned_user_name':
+      $element['#type'] = 'textfield';
+      $element['#description'] = t('Use SugarCRM assigned user name format.');
+      break;
+    case 'id':
+      $element['#type'] = 'textfield';
+      $element['#description'] = t('Use SugarCRM id format.');
+      break;
+    case 'text':
+      $element['#type'] = 'textarea';
+      break;
+    case 'bool':
+      $element['#type'] = 'checkbox';
+      break;
+    case 'enum':
+      $element['#type'] = 'select';
+      $element['#options'] = _sugarondrupal_name_value_list_to_array($field['options']);
+      break;
+  }
+
+  $element['#title']    = str_replace(':', '', $field['label']);
+  $element['#name']     = $field['name'];
+  $element['#required'] = $field['required'];
+  $element['#default_value'] = isset($context['sugarondrupal_module_fields'][$field['name']]) ? $context['sugarondrupal_module_fields'][$field['name']] : NULL;
+
+  // Avoid special case for entry ID field that is required. This is only
+  // required for entry updates.
+  if ($field['name'] == 'id') {
+    $element['#required'] = FALSE;
+  }
+
+  return $element;
+}
+
+
+
+
+/**
+ * Validation handler for SugarCRM Set Entry advanced action configuration form.
+ */
+function sugarondrupal_set_entry_action_validate($form, $form_state) {
+  // Create a multistep process in this form.
+  if ($form_state['clicked_button']['#post']['op'] == t('Continue')) {
+    form_set_error('', '');
+  }
+}
+
+/**
+ * Submit handler for SugarCRM Set Entry advanced action configuration form.
+ */
+function sugarondrupal_set_entry_action_submit($form, $form_state) {
+  $context = array();
+
+  // Save the SugarCRM module that will hold the new entry.
+  $context['sugarondrupal_module_name'] = $form_state['values']['sugarondrupal_module_name'];
+
+  // Save the fields that will be populated in the new entry.
+  foreach ($form['sugarondrupal_fields'] as $name => $field) {
+    if (!empty($form_state['values'][$name])) {
+      $context['sugarondrupal_module_fields'][$name] = $form_state['values'][$name];
+    }
+  }
+
+  return $context;
+}
+
+/**
+ * Execute SugarCRM Set Entry advanced action.
+ *
+ * @param $object any of User, Node, Comment or system.
+ * @param $context array of configuration for this advanced action.
+ */
+function sugarondrupal_set_entry_action($object, $context) {
+  $type = 'global';
+
+  // Guess entry type by hook and set object to actual object.
+  switch ($context['hook']) {
+    case 'nodeapi':
+      $object = $context['node'];
+      $type = 'node';
+      break;
+    case 'user':
+      $object = $context['account'];
+      $type = 'user';
+      break;
+    case 'comment':
+      $object = $context['comment'];
+      $type = 'comment';
+      break;
+    case 'taxonomy':
+      $type = 'taxonomy';
+      break;
+  }
+
+  // Perform entry fields replacement.
+  $fields = array();
+  foreach($context['sugarondrupal_module_fields'] as $name => $value) {
+    $fields[$name] = (module_exists('token')) ? token_replace($value, $type, $object) : sugarondrupal_replace_token($value, $object);
+  }
+
+  // Submit the entry to SugarCRM
+  $params = array(
+    'module'          => $context['sugarondrupal_module_name'],
+    'name_value_list' => _sugarondrupal_array_to_name_value_list($fields),
+  );
+  sugarondrupal_set_entry($params);
+}
+
+
+/**
+ * Perform very primitive [token] substitution.
+ */
+function sugarondrupal_replace_token($value, $object) {
+  if (preg_match('/\[(.*)\]/', $value, $matches)) {
+    $attrib = $matches[1];
+    $value = (is_object($object) && isset($object->$attrib)) ? $object->$attrib : $value;
+  }
+
+  return $value;
+}
+
+
+/**
  * SugarOnDrupal helper functions.
  */
 

