farm_import.module in farmOS 7
Farm import module.
File
modules/farm/farm_import/farm_import.moduleView source
<?php
/**
* @file
* Farm import module.
*/
/**
* Implements hook_ctools_plugin_api().
*/
function farm_import_ctools_plugin_api($module = NULL, $api = NULL) {
$return = array();
if ($module == 'feeds' && $api == 'feeds_importer_default') {
$return['version'] = '1';
}
if ($module == 'feeds_tamper' && $api == 'feeds_tamper_default') {
$return['version'] = '2';
}
return $return;
}
/**
* Implements hook_ctools_plugin_directory().
*/
function farm_import_ctools_plugin_directory($owner, $plugin_type) {
if ($owner == 'feeds_tamper' && $plugin_type == 'plugins') {
return 'includes/feeds_tamper/plugins/';
}
}
/**
* Implements hook_farm_access_perms().
*/
function farm_import_farm_access_perms($role) {
$perms = array();
// Load the list of farm roles.
$roles = farm_access_roles();
// If this role has 'config' access, grant access to Feeds importers.
if (!empty($roles[$role]['access']['config'])) {
// Add permissions for asset importers.
$asset_types = farm_asset_types();
foreach ($asset_types as $type) {
$perms[] = 'import farm_asset_' . $type->type . ' feeds';
}
// Add permissions for log importers.
$log_types = log_types();
foreach ($log_types as $type) {
$perms[] = 'import log_' . $type->type . ' feeds';
}
}
return $perms;
}
/**
* Implements hook_farm_ui_actions().
*/
function farm_import_farm_ui_actions() {
$actions = array();
// Load entity UI information.
$ui_info = farm_ui_entities();
// Add action links to asset and log importers on listing pages.
$types = array(
'farm_asset',
'log',
);
foreach ($types as $type) {
if (!empty($ui_info[$type])) {
foreach ($ui_info[$type] as $bundle => $info) {
if (!empty($info['view'])) {
$actions[$bundle . '_import'] = array(
'title' => t('Import') . ' ' . strtolower($info['label_plural']),
'href' => 'import/' . $type . '_' . $bundle,
'views' => array(
$info['view'],
),
'weight' => 100,
);
}
}
}
}
return $actions;
}
/**
* Implements hook_feeds_processor_targets_alter().
*/
function farm_import_feeds_processor_targets_alter(&$targets, $type, $bundle) {
// Add a target for the "Archive assets" column on logs.
if ($type == 'log') {
$targets['farm_import_log_archive_assets'] = array(
'name' => t('Archive assets'),
'description' => t('Archives the assets referenced on the log.'),
'callback' => 'farm_import_log_archive_assets',
);
}
}
/**
* Callback for 'farm_import_log_archive_assets' target.
*/
function farm_import_log_archive_assets($source, $entity, $target, $value, $mapping) {
// Don't do anything if we weren't given any data.
if (empty($value)) {
return;
}
// In case the value arrives as an array.
if (is_array($value)) {
$value = reset($value);
}
// Do nothing if the archiving is set to any of the FALSE values.
if (empty($value)) {
return;
}
// Load log entity metadata wrapper.
$log_wrapper = entity_metadata_wrapper('log', $entity);
// Iterate through the assets and archive them (if they aren't already).
foreach ($log_wrapper->field_farm_asset as $asset_wrapper) {
$asset = $asset_wrapper
->value();
if (empty($asset->archived)) {
$asset->archived = REQUEST_TIME;
farm_asset_save($asset);
}
}
}
/**
* Helper function for generating standardized tamper plugin definitions.
*
* @param string $entity_type
* The entity type.
* @param string $bundle
* The entity bundle.
* @param string $source
* The name of the Feeds source (column header in CSV imports).
* @param string $plugin
* The Feeds Tamper plugin machine name.
* @param mixed $value
* A value to provide as input to certain plugins (ie: default_value).
*
* @return object
* Returns a Feeds Tamper plugin object.
*/
function farm_import_feeds_tamper_plugin($entity_type, $bundle, $source, $plugin, $value = NULL) {
// Build the importer machine name.
$importer = $entity_type . '_' . $bundle;
// Generate a machine name from the source.
$source_machine = feeds_tamper_make_machine($source);
// Build the tamper plugin ID.
$id = $importer . '-' . $source_machine . '-' . $plugin;
// Build the description and settings based on the plugin type.
$plugin_type = $plugin;
switch ($plugin_type) {
// Boolean.
case 'boolean_default_true':
$plugin_type = 'boolean_default';
$description = 'Convert to boolean (with default)';
$settings = array(
'default_value' => TRUE,
);
break;
case 'boolean_default_false':
$plugin_type = 'boolean_default';
$description = 'Convert to boolean (with default)';
$settings = array(
'default_value' => FALSE,
);
break;
// Explode.
case 'explode':
$description = 'Explode';
$settings = array(
'separator' => ',',
'limit' => '',
'real_separator' => ',',
);
break;
// Find and replace.
case 'find_replace':
$description = 'Find replace';
$settings = array(
'find' => !empty($value['find']) ? $value['find'] : '',
'replace' => !empty($value['replace']) ? $value['replace'] : '',
'case_sensitive' => 0,
'word_boundaries' => 0,
'whole' => 0,
'regex' => FALSE,
'func' => 'str_ireplace',
);
break;
// Required.
case 'required':
$description = 'Required field';
$settings = array(
'invert' => 0,
'log' => 1,
);
break;
// String to Unix timestamp.
case 'strtotime':
$description = 'String to Unix timestamp';
$settings = array();
break;
// Unix timestamp to date.
case 'timetodate':
$description = 'Unix timestamp to Date';
$settings = array(
'date_format' => 'Y-m-d',
);
break;
// Trim whitespace.
case 'trim':
$description = 'Trim';
$settings = array(
'mask' => '',
'side' => 'trim',
);
break;
// Empty defaults.
default:
$description = '';
$settings = array();
}
// Build the plugin.
$feeds_tamper = new stdClass();
$feeds_tamper->disabled = FALSE;
/* Edit this to true to make a default feeds_tamper disabled initially */
$feeds_tamper->api_version = 2;
$feeds_tamper->id = $id;
$feeds_tamper->importer = $importer;
$feeds_tamper->source = $source;
$feeds_tamper->plugin_id = $plugin_type;
$feeds_tamper->settings = $settings;
$feeds_tamper->weight = 0;
$feeds_tamper->description = $description;
// Return the plugin.
return $feeds_tamper;
}
Functions
Name | Description |
---|---|
farm_import_ctools_plugin_api | Implements hook_ctools_plugin_api(). |
farm_import_ctools_plugin_directory | Implements hook_ctools_plugin_directory(). |
farm_import_farm_access_perms | Implements hook_farm_access_perms(). |
farm_import_farm_ui_actions | Implements hook_farm_ui_actions(). |
farm_import_feeds_processor_targets_alter | Implements hook_feeds_processor_targets_alter(). |
farm_import_feeds_tamper_plugin | Helper function for generating standardized tamper plugin definitions. |
farm_import_log_archive_assets | Callback for 'farm_import_log_archive_assets' target. |