Automatically provide simple importers for each log type.

This commit is contained in:
Michael Stenta 2017-07-04 11:02:52 -04:00
parent cd33745feb
commit 691b4ab68c
2 changed files with 272 additions and 46 deletions

View File

@ -18,12 +18,26 @@ function farm_import_feeds_importer_default() {
$importer = farm_import_asset_importer($type);
// Add common field mappings.
farm_import_add_asset_fields($bundle, $importer);
farm_import_add_importer_fields('farm_asset', $bundle, $importer);
// Add it to the list.
$export['farm_asset_' . $bundle] = $importer;
}
// Generate an importer for each log type.
$log_types = log_types();
foreach ($log_types as $bundle => $type) {
// Generate importer.
$importer = farm_import_log_importer($type);
// Add common field mappings.
farm_import_add_importer_fields('log', $bundle, $importer);
// Add it to the list.
$export['log_' . $bundle] = $importer;
}
return $export;
}
@ -39,13 +53,111 @@ function farm_import_feeds_importer_default() {
* @see farm_import_feeds_importer_default()
*/
function farm_import_asset_importer($asset_type) {
// Start with our common base importer.
$importer = farm_import_base_importer();
// Add the necessary information.
$importer->id = 'farm_asset_' . $asset_type->type;
$importer->config['name'] = 'Asset: ' . $asset_type->label;
$importer->config['description'] = 'Import ' . $asset_type->label . ' assets from CSV files.';
$importer->config['processor']['plugin_key'] = 'FarmAssetProcessor';
$importer->config['processor']['config']['bundle'] = $asset_type->type;
$importer->config['processor']['config']['mappings'] = array(
array(
'source' => 'Asset ID',
'target' => 'id',
'unique' => 1,
'language' => 'und',
),
array(
'source' => 'Name',
'target' => 'name',
'unique' => FALSE,
'language' => 'und',
),
array(
'source' => 'Created',
'target' => 'created',
'unique' => FALSE,
'language' => 'und',
),
array(
'source' => 'Active',
'target' => 'active',
'unique' => FALSE,
'language' => 'und',
),
);
// Return the importer.
return $importer;
}
/**
* Helper function for generating a log importer.
*
* @param $log_type
* The log type entity.
*
* @return object
* Returns a feeds importer configuration object.
*
* @see farm_import_feeds_importer_default()
*/
function farm_import_log_importer($log_type) {
// Start with our common base importer.
$importer = farm_import_base_importer();
// Add the necessary information.
$importer->id = 'log_' . $log_type->type;
$importer->config['name'] = 'Log: ' . $log_type->label;
$importer->config['description'] = 'Import ' . $log_type->label . ' logs from CSV files.';
$importer->config['processor']['plugin_key'] = 'LogProcessor';
$importer->config['processor']['config']['bundle'] = $log_type->type;
$importer->config['processor']['config']['mappings'] = array(
array(
'source' => 'Log ID',
'target' => 'id',
'unique' => 1,
'language' => 'und',
),
array(
'source' => 'Done',
'target' => 'done',
'unique' => FALSE,
'language' => 'und',
),
array(
'source' => 'Date',
'target' => 'timestamp',
'unique' => FALSE,
'language' => 'und',
),
array(
'source' => 'Name',
'target' => 'name',
'unique' => FALSE,
'language' => 'und',
),
);
// Return the importer.
return $importer;
}
/**
* Helper function for building a Feeds importer.
*
* @return object
* Returns a feeds importer object skeleton.
*/
function farm_import_base_importer() {
$feeds_importer = new stdClass();
$feeds_importer->disabled = FALSE; /* Edit this to true to make a default feeds_importer disabled initially */
$feeds_importer->api_version = 1;
$feeds_importer->id = 'farm_asset_' . $asset_type->type;
$feeds_importer->config = array(
'name' => 'Farm Asset: ' . $asset_type->label,
'description' => 'Import ' . $asset_type->label . ' assets from CSV files.',
'fetcher' => array(
'plugin_key' => 'FeedsFileFetcher',
'config' => array(
@ -68,42 +180,14 @@ function farm_import_asset_importer($asset_type) {
),
),
'processor' => array(
'plugin_key' => 'FarmAssetProcessor',
'config' => array(
'author' => 0,
'authorize' => 1,
'mappings' => array(
array(
'source' => 'Asset ID',
'target' => 'id',
'unique' => 1,
'language' => 'und',
),
array(
'source' => 'Name',
'target' => 'name',
'unique' => FALSE,
'language' => 'und',
),
array(
'source' => 'Created',
'target' => 'created',
'unique' => FALSE,
'language' => 'und',
),
array(
'source' => 'Active',
'target' => 'active',
'unique' => FALSE,
'language' => 'und',
),
),
'insert_new' => '1',
'update_existing' => '2',
'update_non_existent' => 'skip',
'input_format' => 'plain_text',
'skip_hash_check' => 0,
'bundle' => $asset_type->type,
'language' => 'und',
),
),
@ -118,26 +202,56 @@ function farm_import_asset_importer($asset_type) {
}
/**
* Add common field mappings to asset importers.
* Add common field mappings to importers.
*
* @param $type
* The entity type.
* @param $bundle
* The asset bundle.
* @param $importer
* The importer configuration object that will be altered.
*/
function farm_import_add_asset_fields($bundle, $importer) {
function farm_import_add_importer_fields($type, $bundle, $importer) {
// If the importer doesn't have a mapping array for some reason, bail.
if (empty($importer->config['processor']['config']['mappings'])) {
return;
}
// Define the field mappings that will be added.
$fields = array(
// Get field mappings, depending on the type.
switch ($type) {
case 'farm_asset':
$mappings = farm_import_asset_field_mappings();
break;
case 'log':
$mappings = farm_import_log_field_mappings();
break;
default:
$mappings = array();
}
// Add fields, if they exist on the bundle.
foreach ($mappings as $field => $mapping) {
if (!empty(field_info_instance($type, $field, $bundle))) {
$importer->config['processor']['config']['mappings'][] = $mapping;
}
}
}
/**
* Define field mapping that will be added to assets.
*
* @return array
* Returns an array of feeds importer mapping information, keyed by field
* machine names.
*/
function farm_import_asset_field_mappings() {
return array(
'field_farm_description' => array(
'source' => 'Description',
'target' => 'field_farm_description',
'format' => 'farm_format',
'unique' => FALSE,
'language' => 'und',
),
'field_farm_parent' => array(
@ -147,11 +261,42 @@ function farm_import_add_asset_fields($bundle, $importer) {
'language' => 'und',
),
);
// Add fields, if they exist on the bundle.
foreach ($fields as $field => $mapping) {
if (!empty(field_info_instance('farm_asset', $field, $bundle))) {
$importer->config['processor']['config']['mappings'][] = $mapping;
}
}
}
/**
* Define field mapping that will be added to logs.
*
* @return array
* Returns an array of feeds importer mapping information, keyed by field
* machine names.
*/
function farm_import_log_field_mappings() {
return array(
'field_farm_asset' => array(
'source' => 'Asset IDs',
'target' => 'field_farm_asset:etid',
'unique' => FALSE,
'language' => 'und',
),
'field_farm_area' => array(
'source' => 'Area names',
'target' => 'field_farm_area',
'term_search' => '0',
'autocreate' => 1,
'language' => 'und',
),
'field_farm_notes' => array(
'source' => 'Notes',
'target' => 'field_farm_notes',
'format' => 'farm_format',
'unique' => FALSE,
'language' => 'und',
),
'field_farm_log_category' => array(
'source' => 'Category names',
'target' => 'field_farm_log_category',
'unique' => FALSE,
'language' => 'und',
),
);
}

View File

@ -13,8 +13,10 @@ function farm_import_feeds_tamper_default() {
// Load asset types.
$asset_types = farm_asset_types();
// Make asset name field required.
// Add asset tamper plugins.
foreach ($asset_types as $bundle => $type) {
// Make asset name field required.
$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;
@ -29,9 +31,7 @@ function farm_import_feeds_tamper_default() {
$feeds_tamper->weight = 0;
$feeds_tamper->description = 'Required field';
$export['farm_asset_' . $bundle . '-name-required'] = $feeds_tamper;
}
foreach ($asset_types as $bundle => $type) {
// If a "Parent IDs" field exists, explode it into an array of IDs.
if (!empty(field_info_instance('farm_asset', 'field_farm_parent', $bundle))) {
$feeds_tamper = new stdClass();
@ -52,5 +52,86 @@ function farm_import_feeds_tamper_default() {
}
}
// Load log types.
$log_types = log_types();
// Add log tamper plugins.
foreach ($log_types as $bundle => $type) {
// Make date field required.
$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 = 'log_' . $bundle . '-date-required';
$feeds_tamper->importer = 'log_' . $bundle;
$feeds_tamper->source = 'Date';
$feeds_tamper->plugin_id = 'required';
$feeds_tamper->settings = array(
'invert' => 0,
'log' => 0,
);
$feeds_tamper->weight = 0;
$feeds_tamper->description = 'Required field';
$export['log_' . $bundle . '-date-required'] = $feeds_tamper;
// If an "Asset IDs" field exists, explode it into an array of IDs.
if (!empty(field_info_instance('log', 'field_farm_asset', $bundle))) {
$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 = 'log_' . $bundle . '-asset_ids-explode';
$feeds_tamper->importer = 'log_' . $bundle;
$feeds_tamper->source = 'Asset IDs';
$feeds_tamper->plugin_id = 'explode';
$feeds_tamper->settings = array(
'separator' => ',',
'limit' => '',
'real_separator' => ',',
);
$feeds_tamper->weight = 0;
$feeds_tamper->description = 'Explode';
$export['log_' . $bundle . '-asset_ids-explode'] = $feeds_tamper;
}
// If an "Area names" field exists, explode it into an array of area names.
if (!empty(field_info_instance('log', 'field_farm_area', $bundle))) {
$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 = 'log_' . $bundle . '-area_names-explode';
$feeds_tamper->importer = 'log_' . $bundle;
$feeds_tamper->source = 'Area names';
$feeds_tamper->plugin_id = 'explode';
$feeds_tamper->settings = array(
'separator' => ',',
'limit' => '',
'real_separator' => ',',
);
$feeds_tamper->weight = 0;
$feeds_tamper->description = 'Explode';
$export['log_' . $bundle . '-area_names-explode'] = $feeds_tamper;
}
// If a "Category names" field exists, explode it into an array of category
// names.
if (!empty(field_info_instance('log', 'field_farm_log_category', $bundle))) {
$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 = 'log_' . $bundle . '-category_names-explode';
$feeds_tamper->importer = 'log_' . $bundle;
$feeds_tamper->source = 'Category names';
$feeds_tamper->plugin_id = 'explode';
$feeds_tamper->settings = array(
'separator' => ',',
'limit' => '',
'real_separator' => ',',
);
$feeds_tamper->weight = 0;
$feeds_tamper->description = 'Explode';
$export['log_' . $bundle . '-category_names-explode'] = $feeds_tamper;
}
}
return $export;
}