You are here

commerce_pricelist_feeds.module in Commerce Pricelist 7

Implements the basic functionality required to create and display an entity.

This example does not use the Entity API module, which is used by many entity implementations and is recommended by many.

An example of use of creating and managing entities using the Entity API module is provided in the Model Entity module.

@todo: Reference the ronald_istos article series @todo: Reference the Drupal module development book @todo: Add a single field

File

commerce_pricelist_feeds/commerce_pricelist_feeds.module
View source
<?php

/**
 * @file
 * Implements the basic functionality required to create and display an entity.
 *
 * This example does not use the
 * @link http://drupal.org/project/entity Entity API module @endlink, which is
 * used by many entity implementations and is recommended by many.
 *
 * An example of use of creating and managing entities using the Entity API
 * module is provided in the
 * @link http://drupal.org/project/model Model Entity module @endlink.
 *
 * @todo: Reference the ronald_istos article series
 * @todo: Reference the Drupal module development book
 * @todo: Add a single field
 */

/**
 * Example of a CTools plugin hook that needs to be implemented to make
 * hook_feeds_plugins() discoverable by CTools and Feeds. The hook specifies
 * that the hook_feeds_plugins() returns Feeds Plugin API version 1 style
 * plugins.
 */
function commerce_pricelist_feeds_ctools_plugin_api($owner, $api) {
  if ($owner == 'feeds' && $api == 'plugins') {
    return array(
      'version' => 1,
    );
  }
}

/**
 * Implements hook_feeds_plugins().
 */
function commerce_pricelist_feeds_feeds_plugins() {
  $path = drupal_get_path('module', 'commerce_pricelist_feeds') . '/plugins';
  $info = array();
  $info['FeedsCommercePricelistItemProcessor'] = array(
    'name' => 'Commerce Price list item processor',
    'description' => 'Create and update commerce price list items.',
    'help' => 'Create and update commerce price list items from parsed content.',
    'handler' => array(
      'parent' => 'FeedsProcessor',
      'class' => 'FeedsCommercePricelistItemProcessor',
      'file' => 'FeedsCommercePricelistItemProcessor.inc',
      'path' => $path,
    ),
  );
  return $info;
}

/**
 * Implements hook_form_BASE_FORM_ID_alter().
 */
function commerce_pricelist_feeds_form_commerce_pricelist_list_form_alter(&$form, $form_state) {
  $feeds_info = system_get_info('module', 'feeds');
  $feeds_is_alpha = strpos($feeds_info['version'], '7.x-2.0-alpha') === 0;
  if ($feeds_is_alpha) {
    $importer = commerce_pricelist_feeds_get_importer_id();
    drupal_set_message('You are using an older alpha version of the Feeds module, support for this version will be removed in future versions of Commerce Pricelist. Please upgrade.', 'warning');
  }
  else {
    $importer = commerce_pricelist_feeds_get_importer();
  }
  if ($importer) {
    module_load_include('inc', 'feeds', 'feeds.pages');
    $entity = $form['entity']['#value'];
    $fake_nid = _commerce_pricelist_feeds_fake_nid($entity);
    $feeds_form = array(
      'nid' => array(
        '#value' => $fake_nid,
      ),
    );
    $feeds_form = feeds_import_form($feeds_form, $form_state, $importer);
    $form += $feeds_form;
    $form['feeds']['#weight'] = 100;
    $form['#validate'][] = 'commerce_pricelist_feeds_form_validate';
  }
}

/**
 * @param $form
 * @param $form_state
 */
function commerce_pricelist_feeds_form_validate($form, &$form_state) {
  if ($form_state['triggering_element']['#id'] !== 'edit-delete') {

    // Check if we have a file
    $has_file = !empty($_FILES['files']['name']['feeds']);
    if ($has_file) {
      $importer_id = $form['#importer_id'];
      $last_feeds =& drupal_static('commerce_pricelist_feeds_commerce_pricelist_list_last_feeds');

      //$entity = $form['entity']['#value'];
      $source = feeds_source($importer_id);

      // entity module magically moved $form['feeds'] to $entity->feeds :P.
      // configFormValidate may modify $last_feed, smuggle it to update/insert stage
      // through a static variable.
      $last_feeds = $form_state['values']['feeds'];

      //$source->configFormValidate($last_feeds);
      $source
        ->configFormValidate($last_feeds);

      //feeds_import_form_validate($form, $form_state);
    }
  }
}

/**
 * Implements hook_entity_presave().
 */
function commerce_pricelist_feeds_entity_presave($entity, $type) {

  // Populate $entity->feed from result of validation phase.
  if ($type == 'commerce_pricelist_list') {
    $last_feeds =& drupal_static('commerce_pricelist_feeds_commerce_pricelist_list_last_feeds');
    if (!empty($last_feeds)) {
      $entity->feeds = $last_feeds;
    }
    $last_feeds = NULL;
  }
}

/**
 * Implements hook_entity_insert().
 */
function commerce_pricelist_feeds_entity_insert($entity, $type) {

  // Source attached to entity.
  if ($type == 'commerce_pricelist_list') {
    if (isset($entity->feeds) && ($importer_id = commerce_pricelist_feeds_get_importer_id())) {
      commerce_pricelist_feeds_entity_update($entity, $type);
    }
  }
}

/**
 * Implements hook_entity_update().
 */
function commerce_pricelist_feeds_entity_update($entity, $type) {

  // Source attached to entity.
  if ($type == 'commerce_pricelist_list') {
    if (isset($entity->feeds) && ($importer_id = commerce_pricelist_feeds_get_importer_id())) {
      $fake_nid = _commerce_pricelist_feeds_fake_nid($entity);
      $config = array(
        'FeedsCommercePricelistItemProcessor' => array(
          'list_id' => $entity->list_id,
        ),
      );
      $source = feeds_source($importer_id, $fake_nid);
      $source
        ->addConfig($entity->feeds);
      $source
        ->addConfig($config);
      $source
        ->save();

      // Start import if requested.
      if (feeds_importer($importer_id)->config['import_on_create'] && !isset($entity->feeds['suppress_import'])) {
        $source
          ->startImport();
      }

      // Schedule source and importer.
      $source
        ->schedule();

      //feeds_importer($importer_id)->schedule();
    }
  }
}

/**
 * Implements hook_entity_delete().
 */
function commerce_pricelist_feeds_entity_delete($entity, $type) {

  // Source attached to entity.
  // Make sure we don't leave any orphans behind: Do not use
  // feeds_get_importer_id() to determine importer id as the importer may have
  // been deleted.
  if ($type == 'commerce_pricelist_list') {
    $fake_nid = _commerce_pricelist_feeds_fake_nid($entity);
    if ($importer_id = db_query("SELECT id FROM {feeds_source} WHERE feed_nid = :nid", array(
      ':nid' => $fake_nid,
    ))
      ->fetchField()) {
      feeds_source($importer_id, $fake_nid)
        ->delete();
    }
  }
}

/**
 * Get the first importer configured to be attached to price lists.
 *
 * @return bool|int|string
 */
function commerce_pricelist_feeds_get_importer() {
  $importers = feeds_importer_load_all();

  // Check if any one importer is configured as attached
  foreach ($importers as $id => $importer) {
    $conf = $importer->processor->config;
    if ($conf['bundle'] == 'commerce_pricelist_item' && $conf['is_attached']) {
      return $importer;
    }
  }
  return FALSE;
}

/**
 * Get the first importer configured to be attached to price lists.
 *
 * @return bool|int|string
 */
function commerce_pricelist_feeds_get_importer_id() {
  $importer = commerce_pricelist_feeds_get_importer();
  return $importer ? $importer->id : FALSE;
}

/**
 * @param $entity
 * @return int
 */
function _commerce_pricelist_feeds_fake_nid($entity) {

  // Feeds have support for attaching to nodes but not generic entities,
  // we fake a nid and hope we don't get into trouble...
  return 100000 + $entity->list_id;
}

/**
 * Callback for mapping. Here is where the actual mapping happens.
 */
function commerce_pricelist_feeds_set_target($source, $entity, $target, $search_value, $mapping = array()) {
  $query = new EntityFieldQuery();
  $query
    ->entityCondition('entity_type', 'commerce_pricelist_list')
    ->range(0, 1);
  switch ($target) {

    // Lookup by name.
    case 'pricelist_title':
      $name_query = clone $query;
      $result = $name_query
        ->propertyCondition('title', $search_value)
        ->execute();
      break;

    // Lookup by UUID.
    case 'pricelist_uuid':
      $uuid_query = clone $query;
      $result = $uuid_query
        ->propertyCondition('uuid', $search_value)
        ->execute();
      break;
  }
  $list_id = FALSE;
  if (isset($result['commerce_pricelist_list'])) {
    $list_id = reset($result['commerce_pricelist_list'])->list_id;
  }
  $entity->pricelist_id = $list_id;
}

Functions

Namesort descending Description
commerce_pricelist_feeds_ctools_plugin_api Example of a CTools plugin hook that needs to be implemented to make hook_feeds_plugins() discoverable by CTools and Feeds. The hook specifies that the hook_feeds_plugins() returns Feeds Plugin API version 1 style plugins.
commerce_pricelist_feeds_entity_delete Implements hook_entity_delete().
commerce_pricelist_feeds_entity_insert Implements hook_entity_insert().
commerce_pricelist_feeds_entity_presave Implements hook_entity_presave().
commerce_pricelist_feeds_entity_update Implements hook_entity_update().
commerce_pricelist_feeds_feeds_plugins Implements hook_feeds_plugins().
commerce_pricelist_feeds_form_commerce_pricelist_list_form_alter Implements hook_form_BASE_FORM_ID_alter().
commerce_pricelist_feeds_form_validate _state
commerce_pricelist_feeds_get_importer Get the first importer configured to be attached to price lists.
commerce_pricelist_feeds_get_importer_id Get the first importer configured to be attached to price lists.
commerce_pricelist_feeds_set_target Callback for mapping. Here is where the actual mapping happens.
_commerce_pricelist_feeds_fake_nid