You are here

public function SocialContent::import in Social Content 7.2

Do the import.

Will get all social content and create nodes from them.

Parameters

array $settings_override: (Optional) Pass through settings in a key => value array to override instance and global settings.

Return value

array Stats about the import process.

File

./social_content.class.inc, line 544
Social Content class.

Class

SocialContent
TODO: Table names should be a property for ease of change Separate this class into smaller classes.

Code

public function import($settings_override = array()) {
  $import_stats = array(
    'skipped' => 0,
    'imported' => 0,
    'processed' => 0,
  );
  $field_mappings = $this
    ->getFieldMappings();

  // Override instance settings first, as these can be used elsewhere.
  $this->settings['instance'] = array_merge($this->settings['instance'], $settings_override);
  $settings = array_merge($this->settings['global'], $this->settings['instance']);
  $last_id = NULL;
  if (!isset($settings['skip_last_id']) || $settings['skip_last_id'] == FALSE) {
    $last_id = self::getLastImportedExternalID($settings['id']);
  }

  // Determine which language the imoprted content should be in.
  global $language;
  $langcode = $settings['language'] == $this->currentLanguageKey ? $language->language : $settings['language'];

  // The rows which to imort.
  $rows = $this
    ->getRows($last_id);
  if ($rows) {

    // We know how many rows we will process for this import.
    $import_stats['processed'] = count($rows);
    foreach ($rows as $row) {

      // We are not importing a row if it is not an object
      // or doesn't have an id
      // or prepareRow returns FALSE.
      if (!is_object($row) && !isset($row->id) || $this
        ->prepareRow($row) === FALSE) {
        $import_stats['skipped']++;
        continue;
      }

      // The path will be machine name plus row id.
      $path = $this
        ->getMachineName() . '/' . $row->id;

      // Basic attributes of a node.
      $values = array(
        'type' => $settings['content_type'],
        'status' => $settings['auto_publish'],
        'promote' => 0,
        'language' => $langcode,
        'created' => isset($row->created) ? $row->created : time(),
        'path' => array(
          'alias' => $path,
        ),
      );

      // If an author has not yet been set, default to uid 1 (this is for
      // legacy reasons, everything used to always be imported as uid 1).
      if (!isset($settings['author'])) {
        $values['uid'] = 1;
      }
      elseif (empty($settings['author'])) {
        $values['uid'] = 0;
      }
      else {
        $values['uid'] = $settings['author'];
      }

      // Using entity_metadata_wrapper as it's easier to set fields.
      $entity = entity_create('node', $values);
      $wrapper = entity_metadata_wrapper('node', $entity);

      // We only want to set properties in this array on the $wrapper
      // or else it will throw an exception.
      // Set the title.
      $property_info = $wrapper
        ->getPropertyInfo();
      if (isset($property_info['title'])) {
        if (empty($row->title)) {
          $wrapper->title
            ->set($this
            ->getLabel() . ' : ' . $row->id);
        }
        else {
          $wrapper->title
            ->set($row->title);
          unset($row->title);
        }
      }

      // Attach the rest of the fields.
      $this
        ->attachFields($field_mappings, $wrapper, $row);

      // Let other modules alter wrapper given full context.
      $context = array(
        'settings' => $settings,
        'row' => $row,
      );
      drupal_alter('social_content_import', $wrapper, $context);

      // Finally save the node.
      try {
        $wrapper
          ->save();
        if (module_exists('entity_translation') && !empty($settings['translate'])) {
          $this
            ->translate($wrapper
            ->value(), $langcode, array_filter($settings['translate']));
        }
      } catch (Exception $e) {
        watchdog('social_content', 'Exception caught: %message', array(
          '%message' => $e
            ->getMessage(),
        ));
        $import_stats['skipped']++;
        continue;
      }

      // Log the import of this row to history.
      $this
        ->logHistory($wrapper, $row);

      // Increment the import counter.
      $import_stats['imported']++;
    }
  }
  return $import_stats;
}