You are here

function salesforce_api_fieldmap_edit_form in Salesforce Suite 7.2

Same name and namespace in other branches
  1. 5.2 salesforce_api/salesforce_api.admin.inc \salesforce_api_fieldmap_edit_form()
  2. 6.2 salesforce_api/salesforce_api.admin.inc \salesforce_api_fieldmap_edit_form()
  3. 7 salesforce_api/salesforce_api.admin.inc \salesforce_api_fieldmap_edit_form()
2 string references to 'salesforce_api_fieldmap_edit_form'
salesforce_api_menu in salesforce_api/salesforce_api.module
Implements hook_menu().
sf_prematch_form_alter in sf_prematch/sf_prematch.module
Implements hook_form_alter().

File

salesforce_api/salesforce_api.admin.inc, line 601
Contains the admin page callbacks for the Salesforce module, including forms for general settings and fieldmap administration.

Code

function salesforce_api_fieldmap_edit_form($form, &$form_state, $map) {

  // Include the CSS and JS for the form.
  $path = drupal_get_path('module', 'salesforce_api');
  drupal_add_css($path . '/misc/salesforce_api.admin.css');
  drupal_add_js($path . '/misc/salesforce_api.admin.js');

  // Return to the admin page if the fieldmap did not exist.
  if (empty($map)) {
    drupal_set_message(t('That fieldmap does not exist.'), 'error');
    drupal_goto(SALESFORCE_PATH_FIELDMAPS);
  }
  $form = array();

  // Add the index to the form array.
  $form['fieldmap_index'] = array(
    '#type' => 'value',
    '#value' => $map->name,
  );

  // Add a description of the source fieldmap to the form array.
  $form['fieldmap_desc'] = array(
    '#value' => '<p>' . salesforce_api_fieldmap_description($map) . '</p>',
  );
  $form['description'] = array(
    '#type' => 'textfield',
    '#title' => 'Title or short description',
    '#description' => t('Enter a brief description of this fieldmap to distinguish it from potentially similar fieldmaps'),
    '#default_value' => $map->description,
  );

  // Fail with an error message if either the source or target object
  // definitions were not found.
  if (!salesforce_api_fieldmap_source_entity_enabled($map)) {
    drupal_set_message(t('This fieldmap cannot be imported, because the module which supports the Drupal entity "%entity" cannot be found. Please make sure you have required any modules with which this fieldmap was built.', array(
      '%entity' => $map->drupal_entity,
    )), 'error');
    drupal_goto(SALESFORCE_PATH_FIELDMAPS);
  }
  if (!salesforce_api_fieldmap_source_bundle_enabled($map)) {
    drupal_set_message(t('This fieldmap cannot be imported, because the module which supports the Drupal entity type "%bundle" cannot be found. Please make sure you have required any modules with which this fieldmap was built.', array(
      '%bundle' => $map->drupal_bundle,
    )), 'error');
    drupal_goto(SALESFORCE_PATH_FIELDMAPS);
  }
  if (!salesforce_api_fieldmap_target_enabled($map)) {
    drupal_set_message(t('This fieldmap cannot be edited because salesforce_api cannot find a definition for the Salesforce object "%sfobj". Please verify your Salesforce connection and settings.', array(
      '%sfobj' => $map->salesforce,
    )), 'error');
    drupal_goto(SALESFORCE_PATH_FIELDMAPS);
  }
  $source = salesforce_api_fieldmap_objects_load('drupal', $map->drupal_entity, $map->drupal_bundle);
  $target = salesforce_api_fieldmap_objects_load('salesforce', 'salesforce', $map->salesforce);
  $automatic = array(
    SALESFORCE_AUTO_SYNC_CREATE => $map->automatic & SALESFORCE_AUTO_SYNC_CREATE,
    SALESFORCE_AUTO_SYNC_UPDATE => $map->automatic & SALESFORCE_AUTO_SYNC_UPDATE,
    SALESFORCE_AUTO_SYNC_DELETE => $map->automatic & SALESFORCE_AUTO_SYNC_DELETE,
  );
  $form['drupal_sfapi_automatic'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Synchronize automatically with Salesforce on (check all that apply)'),
    '#options' => array(
      SALESFORCE_AUTO_SYNC_CREATE => t('Create'),
      SALESFORCE_AUTO_SYNC_UPDATE => t('Update'),
      SALESFORCE_AUTO_SYNC_DELETE => t('Delete'),
    ),
    '#return_value' => 1,
    '#default_value' => $automatic,
    '#theme' => 'salesforce_api_drupal_sfapi_automatic',
    '#description' => t('Please indicate how Salesforce records should be handled when Drupal records are created or updated.'),
  );

  // Determine what fields have not yet been mapped, so they can be selected for mapping.
  $unmapped_fields = _sf_fieldmap_edit_get_unmapped_fields($target['fields'], $map);

  // Provide a control for adding a new field.
  $form['add_field'] = array(
    '#type' => 'fieldset',
    '#title' => t('Map a Salesforce field'),
    'new_field' => array(
      '#type' => 'select',
      '#options' => $unmapped_fields,
      '#multiple' => FALSE,
      // Make this a list box to save on scrolling.
      '#attributes' => array(
        'size' => 10,
      ),
    ),
    'add_button' => array(
      '#type' => 'submit',
      '#value' => t('Add'),
    ),
  );

  // Add the data to the form for the required fields table.
  $form['fields'] = array(
    '#theme' => 'salesforce_api_fieldmap_edit_form_table',
  );
  $form['fields']['header'] = array(
    array(
      '#markup' => t('Target: @label', array(
        '@label' => $target['label'],
      )),
    ),
    array(
      '#markup' => t('Source: @label', array(
        '@label' => $source['label'],
      )),
    ),
  );

  // Adding fixed value and evaluate PHP options
  $options = salesforce_api_fieldmap_field_options($source);
  $options['Other']['fixed'] = t('Fixed value');
  $options['Other']['tokens'] = t('Token value');
  if (user_access('use php for salesforce fixed values')) {
    $options['Other']['php'] = t('Evaluate PHP');
  }

  // Loop through each of the target fields.
  $rows = array(
    'required' => array(),
    'optional' => array(),
  );
  $form['fake_required'] = array(
    '#type' => 'value',
    '#value' => array(),
  );
  foreach ($target['fields'] as $key => $value) {
    $sf_field_type = $value['type'];

    // Determine to which table this field should belong.
    if ($sf_field_type & SALESFORCE_FIELD_CREATEABLE && !($sf_field_type & (SALESFORCE_FIELD_NILLABLE | SALESFORCE_FIELD_DEFAULTEDONCREATE))) {

      // If the field is not nillable and not defaulted on create, then it must be required.
      $type = 'required';
      $required = ' <span class="form-required" title="' . t('This field is required.') . '">*</span>';
    }
    else {
      $type = 'optional';
      $required = '';
    }
    if ($sf_field_type & SALESFORCE_FIELD_CREATEABLE && !($sf_field_type & SALESFORCE_FIELD_UPDATEABLE)) {
      $type = 'optional';
      $required = ' <span class="form-required" title="' . t('This field will only be set for new records.') . '">' . t('Create-only') . '</span>';
    }
    elseif (!($sf_field_type & SALESFORCE_FIELD_CREATEABLE) && $sf_field_type & SALESFORCE_FIELD_UPDATEABLE) {
      $type = 'optional';
      $required = ' <span class="form-required" title="' . t('This field can only be set for existing records.') . '">' . t('Update-only') . '</span>';
    }
    elseif (!($sf_field_type & (SALESFORCE_FIELD_CREATEABLE | SALESFORCE_FIELD_UPDATEABLE))) {
      $type = 'optional';
      $required = ' <span class="form-required" title="' . t('This field will be available for imports only.') . '">' . t('Read-only') . '</span>';
    }
    if ($type == 'optional' && !isset($map->fields[$key])) {
      continue;
    }

    // Create a row for this field.
    $row = array(
      'target' => array(
        '#markup' => $value['label'] . $required,
      ),
    );
    if (isset($map->fields[$key]) && is_array($map->fields[$key])) {
      $default_key = $map->fields[$key]['type'];
      $default_value = $map->fields[$key]['value'];
    }
    elseif (isset($map->fields[$key])) {
      $default_key = $map->fields[$key];
      $default_value = NULL;
    }
    else {
      $options = array(
        '' => '',
      ) + $options;
      $default_key = NULL;
      $default_value = NULL;
    }

    // Add the select list for the associated target field.
    $row['source'][$key] = array(
      '#type' => 'select',
      '#title' => $value['label'],
      '#options' => $options,
      '#default_value' => $default_key,
      '#multiple' => FALSE,
      '#attributes' => array(
        'class' => array(
          'sf_fieldmap_options',
        ),
        'id' => 'sf-fieldmap-option-' . $key,
      ),
    );
    $row['source'][$key . "_extra"] = array(
      '#type' => 'textfield',
      '#title' => t('Value'),
      '#default_value' => $default_value,
      '#size' => 20,
      '#maxlength' => 512,
      '#prefix' => '<div id="' . $key . '-extra-hidden" class="fieldmap-extra-text">',
      '#suffix' => '</div>',
      '#description' => 'Omit &lt;?php ?&gt; tags. Return the value to set. Standard caveats apply.',
    );

    // If this field is not a required part of the fieldmap, add a remove link.
    // @todo: Make this an AJAX callback.
    if ($type != 'required') {
      $row['remove'][$key] = array(
        '#markup' => l(t('Remove'), SALESFORCE_PATH_FIELDMAPS . '/' . $map->name . '/remove/' . $key),
      );
    }
    else {
      $form['fake_required']['#value'][] = $key;
    }

    // Add the row to the correct rows array.
    $rows[$type][$key] = $row;
  }

  // Combine the rows arrays into one with required fields displayed first.
  $form['fields']['rows'] = array_merge($rows['required'], $rows['optional']);
  $form['fields']['rows']['#tree'] = TRUE;
  if (module_exists('token')) {

    // Show help on available tokens.
    $form['token_help'] = array(
      '#title' => t('Replacement patterns'),
      '#type' => 'fieldset',
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    );
    $form['token_help']['help'] = array(
      '#theme' => 'token_tree',
      '#token_types' => array(
        $map->drupal_entity,
      ),
      '#global_types' => TRUE,
    );
  }
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save changes'),
    '#suffix' => l(t('Cancel'), SALESFORCE_PATH_FIELDMAPS),
  );
  return $form;
}