You are here

function services_client_wizard_form_mapping_submit in Services Client 7.2

Submit handler; Build event and save it to DB.

File

./services_client.admin.inc, line 362
Administration pages for configuring services client module.

Code

function services_client_wizard_form_mapping_submit($form, &$form_state) {

  // Make shortcut to have more readable code.
  $object =& $_SESSION['services_client_wizard'];

  // Create new event object.
  ctools_include('export');
  $event = ctools_export_crud_new('services_client_connection_event');
  $event->connection = $object->connection;
  $event->name = $object->name;
  $event->title = $object->title;
  $event->entity_type = $object->local_entity;
  $event->event = 'save';
  $event->plugin = $object->handler;
  $handler = $event
    ->getHandler();

  // Add property condition
  if (!empty($object->local_bundle)) {
    $entity_info = entity_get_info($object->local_entity);
    $uuid = $handler
      ->addPlugin('condition', 'ServicesClientPropertyCondition');
    $handler
      ->setPluginConfig('condition', $uuid, array(
      'property' => $entity_info['bundle keys']['bundle'],
      'value' => $object->local_bundle,
      'condition' => 'equals',
    ));
  }

  // Add mapping
  foreach ($form_state['values']['mapping'] as $row) {
    $local = explode(':', $row['local']);
    $remote = explode(':', $row['remote']);
    $reader = $formatter = '';
    $reader_config = $formatter_config = array();

    // Field reader
    if ($local[0] == 'field') {
      $reader = 'ServicesClientFieldReader';
      $reader_config = array(
        'field' => $local[1],
        'property' => $local[2],
        'language' => LANGUAGE_NONE,
        'all_values' => TRUE,
      );
    }
    elseif ($local[0] == 'property') {
      $reader = 'ServicesClientPropertyReader';
      $reader_config = array(
        'property' => $local[1],
      );
    }

    // Field formatter
    if ($remote[0] == 'field') {
      $formatter = 'ServicesClientFieldFormatter';
      $formatter_config = array(
        'field' => $remote[1],
        'language' => LANGUAGE_NONE,
        'property' => $remote[2],
        'multivalue' => 'all_values',
        'empty' => 'null_field',
        'default_value' => '',
      );
    }
    elseif ($remote[0] == 'property') {
      $formatter = 'ServicesClientPropertyFormatter';
      $formatter_config = array(
        'property' => $remote[1],
        'multivalue' => 'force_single',
        'empty' => 'no_field',
        'default_value' => '',
      );
    }

    // If both are configured.
    if (!empty($reader) && !empty($formatter)) {
      $uuid = $handler
        ->addPlugin('mapping', 'ServicesClientMappingPlugin');
      $handler
        ->setPluginConfig('mapping', $uuid, array(
        'reader' => $reader,
        'reader_config' => $reader_config,
        'formatter' => $formatter,
        'formatter_config' => $formatter_config,
      ));
    }
  }

  // Save event and put to disabled state.
  $event = $handler
    ->save();
  $event = ctools_export_crud_load('services_client_connection_event', $event->name);
  $event->table = 'services_client_connection_event';
  ctools_export_crud_set_status($event->table, $event, TRUE);

  // Go to configuratin page.
  drupal_set_message(t('Event was saved, please review, finish configuration and enable event.'), 'status');
  $form_state['redirect'] = $handler
    ->getUrl('configure');
}