You are here

function gathercontent_mapping_edit_form_submit in GatherContent 7.3

Submit callback for edit mapping form.

@inheritdoc

File

forms/gathercontent.mapping-edit.inc, line 422
Multistep mapping form.

Code

function gathercontent_mapping_edit_form_submit($form, &$form_state) {
  if ($form_state['triggering_element']['#id'] == 'edit-submit') {
    $form_definition_elements = array(
      'return',
      'form_build_id',
      'form_token',
      'form_id',
      'op',
    );
    $non_data_elements = array_merge($form_definition_elements, array(
      'gathercontent_template',
      'content_type',
      'id',
      'updated',
      'submit',
      'cancel',
    ));
    $mapping_data = array();
    foreach ($form_state['values'] as $key => $value) {
      if (!in_array($key, $non_data_elements)) {
        $mapping_data[$key] = $value;
      }
    }
    $mapping_entity = entity_load('gathercontent_mapping', array(
      $form_state['values']['id'],
    ));
    $mapping = reset($mapping_entity);
    $new = empty($mapping->data);
    if ($new) {
      $mapping->content_type = $form_state['values']['content_type'];
    }
    $mapping->data = serialize($mapping_data);
    $mapping->updated_drupal = time();
    $mapping->updated_gathercontent = $form_state['values']['updated'];
    $tmp = new Template();
    $template = $tmp
      ->getTemplate($mapping->gathercontent_template_id);
    $mapping->template = serialize($template);
    $mapping
      ->save();

    // We need to modify field for checkboxes and field instance for radios.
    foreach ($template->config as $i => $fieldset) {
      if ($fieldset->hidden === FALSE) {
        foreach ($fieldset->elements as $gathercontent_field) {
          if ($gathercontent_field->type === 'choice_checkbox') {
            if (!empty($mapping_data[$gathercontent_field->name])) {
              $local_options = array();
              foreach ($gathercontent_field->options as $option) {
                $local_options[$option->name] = $option->label;
              }
              $field_data = array(
                'field_name' => $mapping_data[$gathercontent_field->name],
                'settings' => array(
                  'allowed_values' => $local_options,
                ),
              );
              try {
                field_update_field($field_data);
              } catch (Exception $e) {

                // Log something.
              }
            }
          }
          elseif ($gathercontent_field->type === 'choice_radio') {
            if (!empty($mapping_data[$gathercontent_field->name])) {
              $local_options = array();
              foreach ($gathercontent_field->options as $option) {
                if ($option != end($gathercontent_field->options)) {
                  $local_options[] = $option->name . "|" . $option->label;
                }
              }
              $instance = field_read_instance('node', $mapping_data[$gathercontent_field->name], $mapping->content_type);

              // Make the change.
              $instance['widget']['settings']['available_options'] = implode("\n", $local_options);

              // Save the instance.
              field_update_instance($instance);
            }
          }
        }
      }
    }
    if ($new) {
      drupal_set_message(t('Mapping has been created.', array(
        '@id' => $form_state['values']['id'],
      )));
    }
    else {
      drupal_set_message(t('Mapping has been updated.', array(
        '@id' => $form_state['values']['id'],
      )));
    }
  }
  drupal_goto('admin/config/gathercontent/mapping');
}