function salesforce_api_fieldmap_edit_form in Salesforce Suite 6.2
Same name and namespace in other branches
- 5.2 salesforce_api/salesforce_api.admin.inc \salesforce_api_fieldmap_edit_form()
- 7 salesforce_api/salesforce_api.admin.inc \salesforce_api_fieldmap_edit_form()
- 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 - Implementation of 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 551 - 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_state, $fieldmap) {
// 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");
// Load the fieldmap from the database.
$map = salesforce_api_fieldmap_load($fieldmap, TRUE);
// 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');
drupal_add_js($path . '/misc/salesforce_api.admin.js');
$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(
'#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 fielmap 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_enabled($map)) {
drupal_set_message(t('This fieldmap cannot be edited 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,
)), '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);
$target = salesforce_api_fieldmap_objects_load('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.'),
);
// Get a list of Salesforce-side fields which haven't been set yet.
asort($target['fields']);
$not_set = array(
'',
);
foreach ($target['fields'] as $key => $field) {
if (!isset($map->fields[$key])) {
$not_set[$key] = $field['label'];
}
}
// Provide a control for setting a new field
$form['add_field'] = array(
'#type' => 'fieldset',
'#title' => t('Add a field'),
'new_field' => array(
'#type' => 'select',
'#options' => $not_set,
),
'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(
'#value' => t('Target: @label', array(
'@label' => $target['label'],
)),
),
array(
'#value' => t('Source: @label', array(
'@label' => $source['label'],
)),
),
);
// Adding fixed value option
$options = salesforce_api_fieldmap_field_options($source);
$options['Other']['fixed'] = t('Fixed 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) {
// Determine to which table this field should belong.
if ($value['type'] & SALESFORCE_FIELD_CREATEABLE && !($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>';
}
if ($type == 'optional' && !isset($map->fields[$key])) {
continue;
}
// Create a row for this field.
$row = array(
'target' => array(
'#value' => $value['label'] . $required,
),
);
if (is_array($map->fields[$key])) {
$default_key = $map->fields[$key]['type'];
$default_value = $map->fields[$key]['value'];
}
else {
$default_key = $map->fields[$key];
$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,
'#attributes' => array(
'class' => '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 <?php ?> tags. Return the value to set. Standard caveats apply.',
);
if ($type != 'required') {
$row['remove'][$key] = array(
'#type' => 'markup',
'#value' => 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;
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save changes'),
'#suffix' => l(t('Cancel'), SALESFORCE_PATH_FIELDMAPS),
);
return $form;
}