You are here

function salesforce_api_fieldmap_edit_form in Salesforce Suite 7

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.2 salesforce_api/salesforce_api.admin.inc \salesforce_api_fieldmap_edit_form()

Displays the edit form for adding field associations to a fieldmap.

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
Implementation of hook_form_alter().

File

salesforce_api/salesforce_api.admin.inc, line 304
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, $fieldmap) {

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

  // 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.
  $path = drupal_get_path('module', 'salesforce_api');
  drupal_add_css($path . '/misc/salesforce_api.admin.css');
  $form = array();

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

  // Add a description of the source fieldmap to the form array.
  $form['fieldmap_desc'] = array(
    '#markup' => '<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 fielmap to distinguish it from potentially similar fieldmaps'),
    '#default_value' => $map['description'],
  );
  $source = salesforce_api_fieldmap_objects_load('drupal', $map['drupal_entity'], $map['drupal_bundle']);
  $target = salesforce_api_fieldmap_objects_load('salesforce', '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);
  }
  $form['drupal_sfapi_automatic'] = array(
    '#type' => 'checkbox',
    '#title' => t('Automatically Populate Salesforce?'),
    '#return_value' => 1,
    '#default_value' => $map['automatic'],
    '#description' => t('Automatically create and link new salesforce objects when Drupal objects are created?'),
  );

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

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

    // Determine to which table this field should belong.
    if (!($value['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 ($value['type'] & SALESFORCE_FIELD_CREATEABLE && !($value['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 (!($value['type'] & SALESFORCE_FIELD_CREATEABLE) && $value['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 (!($value['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>';
    }

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

    // Add the select list for the associated target field.
    $row['source'][$key] = array(
      '#type' => 'select',
      '#title' => check_plain($value['label']),
      '#options' => salesforce_api_fieldmap_field_options($source),
      '#default_value' => !empty($map['fields'][$key]) ? $map['fields'][$key] : NULL,
      '#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;
}