You are here

function drush_gathercontent_import in GatherContent 7.3

Same name and namespace in other branches
  1. 8.5 gathercontent.drush.inc \drush_gathercontent_import()
  2. 8.3 gathercontent.drush.inc \drush_gathercontent_import()
  3. 8.4 gathercontent.drush.inc \drush_gathercontent_import()

Implements drush_COMMAND().

File

./gathercontent.drush.inc, line 167
Drush command to cli config import.

Code

function drush_gathercontent_import($mapping_id = NULL, $node_update_method = 'always_update', $status_id = '', $parent_menu_item = NULL) {
  if ($mapping_id === NULL) {

    /** @var stdClass $gc_mappings */
    $gc_mappings = entity_load('gathercontent_mapping');
    $options = [];
    foreach ($gc_mappings as $gc_mapping) {
      $options[$gc_mapping->id] = $gc_mapping->id . ' | ' . $gc_mapping->gathercontent_project . ' | ' . $gc_mapping->gathercontent_template;
    }
    $mapping_id = drush_choice($options, dt('Select a mapping ID: '));
  }
  if (!$mapping_id) {
    return drush_set_error('gathercontent_unknown_mapping_id', dt('Unknown mapping ID.'));
  }
  $uuid = _gathercontent_uuid_generate();
  $operation = entity_create('gathercontent_operation', [
    'uuid' => $uuid,
    'type' => 'import',
  ]);
  entity_save('gathercontent_operation', $operation);
  $mapping = entity_load('gathercontent_mapping', [
    $mapping_id,
  ]);
  $project_id = $mapping[$mapping_id]->gathercontent_project_id;
  $template_id = $mapping[$mapping_id]->gathercontent_template_id;
  $content_obj = new Content();
  $content = $content_obj
    ->getContents($project_id);
  $publish = drush_get_option('publish', variable_get('node_default_status'));
  $publish = $publish ? '1' : '0';
  $operations = [];
  foreach ($content as $item) {
    if ($item->template_id == $template_id) {
      $operations[] = [
        'gathercontent_import_process',
        [
          $item->id,
          $status_id,
          $operation->uuid,
          $publish,
          $parent_menu_item,
          $node_update_method,
        ],
      ];
    }
  }
  $batch = [
    'title' => dt('Importing'),
    'init_message' => dt('Starting import'),
    'error_message' => dt('An unrecoverable error has occurred. You can find the error message below. It is advised to copy it to the clipboard for reference.'),
    'operations' => $operations,
    'file' => drupal_get_path('module', 'gathercontent') . '/gathercontent.import.inc',
    'finished' => 'gathercontent_drush_import_process_finished',
  ];
  batch_set($batch);
  drush_backend_batch_process();
  $mappings = [];
  $query = new EntityFieldQuery();
  $query
    ->entityCondition('entity_type', 'gathercontent_operation_item')
    ->propertyCondition('operation_uuid', $operation->uuid);
  $result = $query
    ->execute();
  if (!empty($result['gathercontent_operation_item'])) {
    $imported_items = entity_load('gathercontent_operation_item', array_keys($result['gathercontent_operation_item']));
    foreach ($imported_items as $imported_item) {
      $mappings[$imported_item->id] = [
        'id' => $imported_item->id,
        'item_name' => $imported_item->item_name,
        'node_status' => $imported_item->item_status,
        'import_status' => $imported_item->status,
      ];
    }
  }
  return $mappings;
}