You are here

function gathercontent_upload_process in GatherContent 7.3

Same name and namespace in other branches
  1. 8.5 gathercontent_upload/gathercontent_upload.module \gathercontent_upload_process()
  2. 8.3 gathercontent.module \gathercontent_upload_process()

Batch operation callback.

We are doing upload operation here.

Parameters

object $node: Object of content we want to upload.

1 string reference to 'gathercontent_upload_process'
gathercontent_views_vbo_upload in ./gathercontent.module
@TODO: function comment!!!

File

./gathercontent.module, line 1338

Code

function gathercontent_upload_process($entity, $uuid, &$context) {

  // 1. Load template from remote
  // 2. Compare local and remote template
  // 3. If templates are same, load node from remote.
  // 4. Set values based on mapping.
  $mapping = entity_load('gathercontent_mapping', array(
    $entity->gathercontent_mapping_id,
  ));
  $mapping = reset($mapping);
  $tmp_obj = new GatherContent\Template();
  $remote_template = $tmp_obj
    ->getTemplate($mapping->gathercontent_template_id);
  $cont_obj = new GatherContent\Content();
  $remote_node = $cont_obj
    ->getContent($entity->gathercontent_id);
  $config = $remote_node->config;
  $mapping_data = unserialize($mapping->data);
  $operation_item = entity_create('gathercontent_operation_item', array(
    'operation_uuid' => $uuid,
    'item_status' => $remote_node->status->data->name,
    'item_status_color' => $remote_node->status->data->color,
    'template_name' => $remote_template->name,
    'item_name' => $remote_node->name,
    'gathercontent_id' => $entity->gathercontent_id,
    'nid' => $entity->nid,
  ));
  if ($remote_template->config == unserialize($mapping->template)->config) {
    $node = entity_metadata_wrapper('node', $entity);
    try {
      foreach ($config as &$pane) {
        $is_translatable = module_exists('entity_translation') && entity_translation_node_supported_type($mapping->content_type) && $mapping_data[$pane->name]['language'] != LANGUAGE_NONE;
        if ($is_translatable) {
          $language = $mapping_data[$pane->name]['language'];
        }
        else {
          $language = LANGUAGE_NONE;
        }
        foreach ($pane->elements as &$field) {
          if (isset($mapping_data[$pane->name]['elements'][$field->name]) && !empty($mapping_data[$pane->name]['elements'][$field->name])) {
            $local_field_name = $mapping_data[$pane->name]['elements'][$field->name];
            if ($mapping_data[$pane->name]['type'] === 'content') {
              $field_info = field_info_field($local_field_name);
              $is_translatable = $is_translatable && $field_info['translatable'];
              switch ($field->type) {
                case 'files':

                  // There is currently no API for manipulating with files.
                  break;
                case 'choice_radio':
                  $option_names = array();
                  foreach ($field->options as &$option) {

                    // Set selected to false for each option.
                    $option->selected = FALSE;
                    $option_names[] = $option->name;
                  }

                  // Fetch local selected option.
                  if ($is_translatable) {
                    $selected = $node
                      ->language($language)->{$local_field_name}
                      ->value();
                  }
                  else {
                    $selected = $node->{$local_field_name}
                      ->value();
                  }
                  if (!in_array($selected, $option_names)) {

                    // If it's other, then find that option in remote.
                    foreach ($field->options as &$option) {
                      if (isset($option->value)) {
                        $option->selected = TRUE;
                        $option->value = $selected;
                      }
                    }
                  }
                  else {

                    // If it's checkbox, find it by remote option name,
                    // which should be same.
                    foreach ($field->options as &$option) {
                      if ($option->name == $selected) {
                        $option->selected = TRUE;
                      }
                    }
                  }
                  break;
                case 'choice_checkbox':
                  foreach ($field->options as &$option) {

                    // Set selected to false for each option.
                    $option->selected = FALSE;
                  }

                  // Fetch local selected option.
                  if ($is_translatable) {
                    $selected = $node
                      ->language($language)->{$local_field_name}
                      ->value();
                  }
                  else {
                    $selected = $node->{$local_field_name}
                      ->value();
                  }

                  // If it's checkbox, find it by remote option name,
                  // which should be same.
                  foreach ($field->options as &$option) {
                    if (isset($selected[$option->name])) {
                      $option->selected = TRUE;
                    }
                  }
                  break;
                case 'section':

                  // We don't upload this because this field shouldn't be
                  // edited.
                  break;
                default:
                  if ($local_field_name === 'title') {
                    $field->value = $node->title
                      ->value();
                  }
                  else {
                    if ($is_translatable) {
                      if (module_exists('title') && title_field_replacement_enabled('node', $mapping->content_type, 'title') && title_field_replacement_info('node', 'title')['field']['field_name'] === $local_field_name) {
                        $field->value = $node
                          ->language($language)->{$local_field_name}
                          ->value();
                      }
                      else {
                        $field->value = $node
                          ->language($language)->{$local_field_name}->value
                          ->value();
                      }
                    }
                    else {
                      $field->value = $node->{$local_field_name}->value
                        ->value();
                    }
                  }
                  break;
              }
            }
            elseif ($mapping_data[$pane->name]['type'] === 'metatag') {
              if (module_exists('metatag') && metatag_entity_supports_metatags('node', $mapping->content_type)) {
                if ($is_translatable) {
                  $field->value = $node
                    ->language($language)->metatags->{$local_field_name}
                    ->value();
                }
                else {
                  $field->value = $node->metatags->{$local_field_name}
                    ->value();
                }
              }
              else {
                throw new Exception("Metatag module not enabled or entity doesn't support \n    metatags while trying to map values woth metatag content.");
              }
            }
          }
          else {
            $operation_item->status = "System error, please contact you administrator.";
            entity_save('gathercontent_operation_item', $operation_item);
          }
        }
      }
      drupal_alter('gathercontent_pre_node_upload', $entity, $config);
      if ($cont_obj
        ->postContent($entity->gathercontent_id, $config)) {
        $operation_item->status = "Success";
        entity_save('gathercontent_operation_item', $operation_item);
      }
      else {
        $operation_item->status = 'Mapping doesn\'t match';
        entity_save('gathercontent_operation_item', $operation_item);
        module_invoke_all('gathercontent_post_node_upload', $entity, $config);
      }
    } catch (\Exception $e) {
      watchdog('gathercontent_import', print_r($e, TRUE), array(), WATCHDOG_ERROR);
      $operation_item->status = 'Mapping doesn\'t match';
      entity_save('gathercontent_operation_item', $operation_item);
    }
  }
  else {
    $operation_item->status = 'Mapping doesn\'t match';
    entity_save('gathercontent_operation_item', $operation_item);
  }
  $context['results']['uuid'] = $uuid;
}