You are here

feeds_tamper_ui.module in Feeds Tamper 7

Same filename and directory in other branches
  1. 6 feeds_tamper_ui/feeds_tamper_ui.module

Feeds Tamper UI - Defines the user interface for Feeds Tamper.

File

feeds_tamper_ui/feeds_tamper_ui.module
View source
<?php

/**
 * @file
 * Feeds Tamper UI - Defines the user interface for Feeds Tamper.
 */

/**
 * Implements hook_menu().
 */
function feeds_tamper_ui_menu() {
  $defaults = array(
    'page callback' => 'drupal_get_form',
    'access callback' => 'feeds_tamper_access',
    'access arguments' => array(
      3,
    ),
    'title arguments' => array(
      3,
    ),
    'file' => 'feeds_tamper_ui.admin.inc',
    'type' => MENU_CALLBACK,
  );
  $items = array();
  $items['admin/structure/feeds/%feeds_importer/tamper'] = array(
    'title' => 'Tamper',
    'title arguments' => array(),
    'page arguments' => array(
      'feeds_tamper_ui_list_form',
      3,
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => 10,
  ) + $defaults;
  $items['admin/structure/feeds/%feeds_importer/tamper/manage'] = array(
    'title' => 'List',
    'type' => MENU_DEFAULT_LOCAL_TASK,
  );
  $items['admin/structure/feeds/%feeds_importer/tamper/export'] = array(
    'title' => 'Export',
    'title arguments' => array(),
    'page arguments' => array(
      'feeds_tamper_ui_export_form',
      3,
    ),
    'access callback' => 'user_access',
    'access arguments' => array(
      'administer feeds_tamper',
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => 10,
  ) + $defaults;
  $items['admin/structure/feeds/%feeds_importer/tamper/add/%feeds_tamper_ui_source'] = array(
    'title callback' => 'feeds_tamper_ui_add_title',
    'title arguments' => array(
      6,
    ),
    'page arguments' => array(
      'feeds_tamper_ui_add_plugin_form',
      3,
      6,
    ),
  ) + $defaults;
  $items['admin/structure/feeds/%feeds_importer/tamper/%feeds_tamper_plugin_instance/edit'] = array(
    'title callback' => 'feeds_tamper_ui_edit_title',
    'title arguments' => array(
      5,
    ),
    'page arguments' => array(
      'feeds_tamper_ui_edit_plugin_form',
      5,
    ),
    'access arguments' => array(
      NULL,
      5,
    ),
  ) + $defaults;
  $items['admin/structure/feeds/%feeds_importer/tamper/%feeds_tamper_plugin_instance/delete'] = array(
    'title callback' => 'feeds_tamper_ui_delete_title',
    'title arguments' => array(
      5,
    ),
    'page arguments' => array(
      'feeds_tamper_ui_delete_form',
      5,
    ),
    'access arguments' => array(
      NULL,
      5,
    ),
  ) + $defaults;
  return $items;
}

/**
 * Implements hook_theme().
 */
function feeds_tamper_ui_theme($existing, $type, $theme, $path) {
  return array(
    'feeds_tamper_ui_list_form' => array(
      'render element' => 'form',
      'file' => 'feeds_tamper_ui.admin.inc',
    ),
  );
}

/**
 * Menu loader callback for plugin instances.
 */
function feeds_tamper_plugin_instance_load($instance_id) {
  return feeds_tamper_load_instance($instance_id);
}

/**
 * Menu loader callback for grabbing the source from the URL.
 */
function feeds_tamper_ui_source_load($source) {

  // We've HEX encoded the source to allow all possible characters.
  return pack('H*', $source);
}

/**
 * Add plugin title callback.
 */
function feeds_tamper_ui_add_title($source) {

  // Title callbacks get check_plain'ed already.
  return t('Add plugin to: !source', array(
    '!source' => $source,
  ));
}

/**
 * Edit plugin title callback.
 */
function feeds_tamper_ui_edit_title($instance) {
  if ($instance->export_type == EXPORT_IN_DATABASE) {
    return t('Edit: @id', array(
      '@id' => $instance->description,
    ));
  }
  return t('Override: @id', array(
    '@id' => $instance->description,
  ));
}

/**
 * Delete plugin title callback.
 */
function feeds_tamper_ui_delete_title($instance) {
  if ($instance->export_type == EXPORT_IN_DATABASE) {
    return t('Delete plugin: @id', array(
      '@id',
      $instance->id,
    ));
  }
  return t('Revert plugin: @id', array(
    '@id',
    $instance->id,
  ));
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Modify feeds_ui_overview_form(), adding Tamper links if the user has access.
 */
function feeds_tamper_ui_form_feeds_ui_overview_form_alter(&$form, &$form_state) {
  if (!empty($form['enabled'])) {
    foreach ($form['enabled'] as $id => &$table) {
      if (feeds_tamper_access($id)) {
        $table['operations']['#markup'] .= ' | ' . l(t('Tamper'), "admin/structure/feeds/{$id}/tamper");
      }
    }
  }
}

/**
 * Calculate display name for source.
 *
 * @param stdClass $instance
 *   A plugin instance object.
 *
 * @return string
 *   The unsanitized name to display for a Feeds source.
 */
function feeds_tamper_ui_source_name(stdClass $instance) {
  $importer = feeds_importer($instance->importer);
  $sources = $importer->parser
    ->getMappingSources();
  return !empty($sources[$instance->source]['name']) ? $sources[$instance->source]['name'] : $instance->source;
}

/**
 * Implements hook_form_BASE_FORM_ID_alter().
 */
function feeds_tamper_form_feeds_ui_create_form_alter(array &$form, array &$form_state) {

  // Only make these alterations if we're trying to clone a feed.
  if (arg(4) !== 'clone') {
    return;
  }

  // Add checkbox to clone tamper plugins.
  $form['clone_tamper_plugins'] = array(
    '#type' => 'checkbox',
    '#title' => t('Clone Tamper Plugins'),
    '#description' => t('Check this box if you also want to clone the Feeds Tamper plugins for this importer.'),
    '#weight' => 1,
  );

  // Give submit button a higher weight so it shows up under the checkbox.
  $form['submit']['#weight'] = 2;
  $form['#submit'][] = '_feeds_tamper_clone_tamper_plugins';
}

/**
 * Additional submit handler for feeds_build_create_form().
 *
 * @see feeds_tamper_form_feeds_ui_create_form_alter()
 */
function _feeds_tamper_clone_tamper_plugins(array &$form, array &$form_state) {
  if (empty($form_state['values']['clone_tamper_plugins'])) {
    return;
  }
  $from_importer_id = isset($form['#from_importer']->id) ? $form['#from_importer']->id : '';
  if (!$from_importer_id) {
    return;
  }
  $tamper_plugins = feeds_tamper_load_by_importer($from_importer_id, FALSE);

  // Copy plugins to the new importer.
  foreach ($tamper_plugins as $tamper_plugin) {
    foreach ($tamper_plugin as $old_instance) {
      $new_instance = clone $old_instance;
      $id_parts = explode('-', $old_instance->id);

      // Normal id.
      if (count($id_parts) === 3) {
        $id_parts[0] = $form_state['values']['id'];
        $new_instance->id = implode('-', $id_parts);
      }
      else {
        $new_instance->id = $form_state['values']['id'] . '-' . implode('-', $id_parts);
      }
      $new_instance->importer = $form_state['values']['id'];
      $new_instance->export_type = NULL;
      $new_instance->type = t('Local');
      unset($new_instance->table);
      unset($new_instance->disabled);
      feeds_tamper_save_instance($new_instance);
    }
  }
}

Functions

Namesort descending Description
feeds_tamper_form_feeds_ui_create_form_alter Implements hook_form_BASE_FORM_ID_alter().
feeds_tamper_plugin_instance_load Menu loader callback for plugin instances.
feeds_tamper_ui_add_title Add plugin title callback.
feeds_tamper_ui_delete_title Delete plugin title callback.
feeds_tamper_ui_edit_title Edit plugin title callback.
feeds_tamper_ui_form_feeds_ui_overview_form_alter Implements hook_form_FORM_ID_alter().
feeds_tamper_ui_menu Implements hook_menu().
feeds_tamper_ui_source_load Menu loader callback for grabbing the source from the URL.
feeds_tamper_ui_source_name Calculate display name for source.
feeds_tamper_ui_theme Implements hook_theme().
_feeds_tamper_clone_tamper_plugins Additional submit handler for feeds_build_create_form().