You are here

instagram_feeds_plugins.module in Instagram Feeds 7

Code for the Instagram Feeds Plugin module.

File

modules/instagram_feeds_plugins/instagram_feeds_plugins.module
View source
<?php

/**
 * @file
 * Code for the Instagram Feeds Plugin module.
 */

/**
 * Implements hook_feeds_plugins().
 */
function instagram_feeds_plugins_feeds_plugins() {
  $info = array();
  $info['InstagramFeedsPluginsPager'] = array(
    'name' => t('Instagram Pager'),
    'description' => t('Fetch continuous pages returned by Instagram API feeds.'),
    'help' => t('This fetcher will use the data returned by Instagram API feeds to continue to the next page, and so on. During the first fetch, no paging will take place.'),
    'handler' => array(
      'parent' => 'FeedsHTTPFetcher',
      'class' => 'InstagramFeedsPluginsPager',
      'file' => 'InstagramFeedsPluginsPager.inc',
      'path' => drupal_get_path('module', 'instagram_feeds_plugins') . '/plugins/feeds',
    ),
  );
  $info['InstagramFeedsPluginsNodeProcessor'] = array(
    'name' => t('Instagram Feeds Node processor'),
    'description' => t('Create and update nodes regardless of source.'),
    'help' => t('Create and update nodes from parsed content regardless of source.'),
    'handler' => array(
      'parent' => 'FeedsNodeProcessor',
      'class' => 'InstagramFeedsPluginsNodeProcessor',
      'file' => 'InstagramFeedsPluginsNodeProcessor.inc',
      'path' => drupal_get_path('module', 'instagram_feeds_plugins') . '/plugins/feeds',
    ),
  );
  return $info;
}

/**
 * Implements multiple page importing of feed in one call without batch UI.
 *
 * @param string $importer_id
 *   Importer ID.
 */
function instagram_feeds_plugins_import_feed($importer_id) {
  $importer = feeds_importer($importer_id);
  $config = $importer
    ->getConfig();

  // Fetch all Instagram importer node nids.
  $nids = db_select('node', 'node')
    ->fields('node', array(
    'nid',
  ))
    ->condition('type', $config['content_type'])
    ->condition('status', 1)
    ->execute()
    ->fetchCol();

  // Iterate each importer.
  foreach ($nids as $nid) {

    // Load the Feed.
    if ($feed = feeds_source($importer_id, $nid)) {

      // Invoke the pre-import hooks.
      module_invoke_all('feeds_before_import', $feed);

      // Start the import batch in the background.
      $batch = array(
        'title' => t('Importing'),
        'operations' => array(
          array(
            'feeds_batch',
            array(
              'import',
              $feed->id,
              $feed->feed_nid,
            ),
          ),
        ),
        'progress_message' => '',
      );
      batch_set($batch);

      // This allows the batch to run without the UI.
      $batch =& batch_get();
      $batch['progressive'] = FALSE;
      batch_process();
    }
  }
}

/**
 * Implements hook_feeds_processor_targets_alter().
 */
function instagram_feeds_plugins_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) {
  foreach (field_info_instances($entity_type, $bundle_name) as $name => $instance) {
    $info = field_info_field($name);
    if (in_array($info['type'], array(
      'file',
      'image',
    ))) {
      $targets[$name] = array(
        'name' => check_plain($instance['label']),
        'callback' => 'instagram_feeds_plugins_file_feeds_set_target',
        'description' => t('The @label field of the node.', array(
          '@label' => $instance['label'],
        )),
      );
    }
  }
}

/**
 * Callback for mapping. Here is where the actual mapping happens.
 *
 * When the callback is invoked, $target contains the name of the field the
 * user has decided to map to and $value contains the value of the feed item
 * element the user has picked as a source.
 */
function instagram_feeds_plugins_file_feeds_set_target($source, $entity, $target, $value) {
  if (empty($value)) {
    return;
  }
  module_load_include('inc', 'file');

  // Make sure $value is an array of objects of type FeedsEnclosure.
  if (!is_array($value)) {
    $value = array(
      $value,
    );
  }
  foreach ($value as $k => $v) {
    if ('InstagramFeedsPluginsPager' == $source->importer->config['fetcher']['plugin_key']) {
      if (!$v instanceof InstagramFeedsPluginsEnclosure) {
        if (is_string($v)) {
          $value[$k] = new InstagramFeedsPluginsEnclosure($v, 'application/octet-stream');
        }
        else {
          unset($value[$k]);
        }
      }
    }
    else {
      if (!$v instanceof FeedsEnclosure) {
        if (is_string($v)) {
          $value[$k] = new FeedsEnclosure($v, 'application/octet-stream');
        }
        else {
          unset($value[$k]);
        }
      }
    }
  }
  if (empty($value)) {
    return;
  }

  // Determine file destination.
  // @todo This needs review and debugging.
  list($entity_id, $vid, $bundle_name) = entity_extract_ids($entity->feeds_item->entity_type, $entity);
  unset($vid, $entity_id);
  $instance_info = field_info_instance($entity->feeds_item->entity_type, $target, $bundle_name);
  $info = field_info_field($target);
  $data = array();
  if (!empty($entity->uid)) {
    $data[$entity->feeds_item->entity_type] = $entity;
  }
  $destination = file_field_widget_uri($info, $instance_info, $data);

  // Populate entity.
  $i = 0;
  $field = isset($entity->{$target}) ? $entity->{$target} : array();
  foreach ($value as $v) {
    if ($file = $v
      ->getFile($destination)) {
      $field[LANGUAGE_NONE][$i] = (array) $file;

      // @todo: Figure out how to properly populate this field.
      $field[LANGUAGE_NONE][$i]['display'] = 1;
      if ($info['cardinality'] == 1) {
        break;
      }
      $i++;
    }
  }
  $entity->{$target} = $field;
}

/**
 * Invoked before a feed item is saved.
 *
 * @param object $source
 *   FeedsSource object that describes the source that is being imported.
 * @param object $entity
 *   The entity object.
 */
function instagram_feeds_plugins_feeds_presave(FeedsSource $source, $entity) {
  if ('InstagramFeedsPluginsPager' == $source->importer->config['fetcher']['plugin_key']) {
    if (!isset($source
      ->state(FEEDS_FETCH)->item_count[$source->config['InstagramFeedsPluginsPager']['source']])) {
      $source
        ->state(FEEDS_FETCH)->item_count = array(
        $source->config['InstagramFeedsPluginsPager']['source'] => 0,
      );
    }
    $source
      ->state(FEEDS_FETCH)->item_count[$source->config['InstagramFeedsPluginsPager']['source']]++;
  }
}

/**
 * Helper function to receive fields mappings.
 *
 * Helper function to receive fields mappings for filtering Instagram images
 * by usernames etc.
 */
function _instagram_feeds_plugins_get_fields_mappings($source) {
  $source_mappings = $source->importer->config['processor'];
  $result =& drupal_static(__FUNCTION__, array());
  if (!isset($result[$source->id])) {
    $mappings = array();
    foreach ($source_mappings['config']['mappings'] as $field) {
      if (isset($field['unique']) && $field['unique']) {
        $mappings['unique'] = $field;
      }
      switch ($field['target']) {
        case 'field_instf_user':
          $mappings['user'] = $field;
          break;
        case 'field_instf_hash_tags':
          $mappings['tag'] = $field;
          break;
        case 'field_instf_thumb_url:url':
          $mappings['thumb_url'] = $field;
          break;
        case 'field_instf_image_url:url':
          $mappings['img_url'] = $field;
          break;
      }
    }
    if (!isset($mappings['unique'])) {
      $mappings['unique'] = $source_mappings['config']['mappings'][0];
    }
    $result[$source->id] = $mappings;
  }
  return $result[$source->id];
}

Functions

Namesort descending Description
instagram_feeds_plugins_feeds_plugins Implements hook_feeds_plugins().
instagram_feeds_plugins_feeds_presave Invoked before a feed item is saved.
instagram_feeds_plugins_feeds_processor_targets_alter Implements hook_feeds_processor_targets_alter().
instagram_feeds_plugins_file_feeds_set_target Callback for mapping. Here is where the actual mapping happens.
instagram_feeds_plugins_import_feed Implements multiple page importing of feed in one call without batch UI.
_instagram_feeds_plugins_get_fields_mappings Helper function to receive fields mappings.