You are here

function salesforce_api_fieldmap_edit_form in Salesforce Suite 5.2

Same name and namespace in other branches
  1. 6.2 salesforce_api/salesforce_api.admin.inc \salesforce_api_fieldmap_edit_form()
  2. 7 salesforce_api/salesforce_api.admin.inc \salesforce_api_fieldmap_edit_form()
  3. 7.2 salesforce_api/salesforce_api.admin.inc \salesforce_api_fieldmap_edit_form()
1 string reference to 'salesforce_api_fieldmap_edit_form'
salesforce_api_menu in salesforce_api/salesforce_api.module
Implementation of hook_menu().

File

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

Code

function salesforce_api_fieldmap_edit_form($index) {

  // Load the fieldmap from the database.
  $map = salesforce_api_fieldmap_load($index);

  // 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);
  }

  // Include the CSS file for the form.
  drupal_add_css(drupal_get_path('module', 'salesforce_api') . '/salesforce_api.admin.css');
  $form = array();

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

  // Add a description of the source fieldmap to the form array.
  $form['fieldmap_desc'] = array(
    '#value' => '<p>' . salesforce_api_fieldmap_description($map) . '</p>',
  );

  // Based on the action, set the source and target object definitions.
  if ($map['action'] == 'import') {

    // For import fieldmaps, the source is the Salesforce object and the target
    // is the Drupal object.
    $source = salesforce_api_fieldmap_objects_load('salesforce', $map['salesforce']);
    $target = salesforce_api_fieldmap_objects_load('drupal', $map['drupal']);
  }
  else {

    // For export fieldmaps, the source is the Drupal object and the target is
    // the Salesforce object.
    $source = salesforce_api_fieldmap_objects_load('drupal', $map['drupal']);
    $target = salesforce_api_fieldmap_objects_load('salesforce', $map['salesforce']);
  }

  // Fail with an error message if either the source or target object
  // definitions were not found.
  if (empty($source) || empty($target)) {
    drupal_set_message(t('This fieldmap cannot be edited, because an object definition could not be found.'), 'error');
    drupal_goto(SALESFORCE_PATH_FIELDMAPS);
  }

  // Add the data to the form for the required fields table.
  $form['fields'] = array(
    '#theme' => 'salesforce_api_fieldmap_edit_form_table',
    'rows' => array(),
  );
  $form['fields']['header'] = array(
    array(
      '#value' => t('Target: @label (@type)', array(
        '@label' => $target['label'],
        '@type' => $map['action'] == 'import' ? t('Drupal') : t('Salesforce'),
      )),
    ),
    array(
      '#value' => t('Source: @label (@type)', array(
        '@label' => $source['label'],
        '@type' => $map['action'] == 'import' ? t('Salesforce') : t('Drupal'),
      )),
    ),
  );

  // Loop through each of the target fields.
  foreach ($target['fields'] as $key => $value) {

    // Skip fields that are automatically assigned upon create for now.
    if ($value['type'] == SALESFORCE_FIELD_SOURCE_ONLY) {
      continue;
    }

    // Determine to which table this field should belong.
    if ($value['type'] == SALESFORCE_FIELD_REQUIRED) {
      $type = 'required';
      $required = ' <span class="form-required" title="' . t('This field is required.') . '">*</span>';
    }
    else {
      $type = 'optional';
      $required = '';
    }

    // Create a row for this field.
    $row = array(
      'target' => array(
        '#value' => $value['label'] . $required,
      ),
    );

    // Add the select list for the associated target field.
    $row['source'][$key] = array(
      '#type' => 'select',
      '#title' => $value['label'],
      '#options' => salesforce_api_fieldmap_field_options($source),
      '#default_value' => $map['fields'][$key],
      '#required' => $type == 'required',
    );

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

  // Combine the rows arrays into one with required fields displayed first.
  $form['fields']['rows'] = array_merge($rows['required'], $rows['optional']);
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save changes'),
    '#suffix' => l(t('Cancel'), SALESFORCE_PATH_FIELDMAPS),
  );
  return $form;
}