Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #151: add feed previews #154

Merged
merged 14 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions css/feedspreview.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* @file
* CSS for Feeds Preview.
*/

.feeds-preview-table-source-column,
.feeds-preview-table-target-column {
width: 100px;
vertical-align: top;
}

.feeds-preview-table-value-column {
max-width: 400px;
overflow-x: auto;
}
.feeds-preview-table-value-column pre {
margin: 0;
}

.feeds-preview-table {
display: none;
}
.feeds-preview-table.active {
display: table;
}

.feeds-preview-long-content.collapsed {
height: 40px;
overflow: hidden;
}

.feeds-preview-controls-previous,
.feeds-preview-controls-next {
display: inline-block;
}

.feeds-preview-table-row-not-mapped td.feeds-preview-table-target-column {
background-image: var(--icon-warning);
background-repeat: no-repeat;
background-position: right;
background-size: 1em;
}
12 changes: 12 additions & 0 deletions feedspreview.info
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name = Feeds Import Preview
description = Preview the feeds source content before importing.
package = Feeds
backdrop = 1.x
type = module
dependencies[] = feeds

test_dependencies[] = feeds_tamper:feeds_tamper

files[] = src/FeedsPreviewSource.php
files[] = src/FeedsPreviewTable.php

62 changes: 62 additions & 0 deletions feedspreview.install
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

/**
* @file
* Install, update and uninstall functions for the Feeds Import Preview module.
*/

/**
* Implements hook_schema().
*/
function feedspreview_schema() {
$schema = array();

$schema['feedspreview_source'] = array(
'description' => 'Source definitions for feeds.',
'fields' => array(
'id' => array(
'type' => 'varchar',
'length' => 128,
'not null' => TRUE,
'default' => '',
'description' => 'Id of the feed configuration.',
),
'config' => array(
'type' => 'blob',
'size' => 'big',
'not null' => FALSE,
'description' => 'Configuration of the source.',
'serialize' => TRUE,
),
'source' => array(
'type' => 'text',
'not null' => TRUE,
'description' => 'Main source resource identifier. E. g. a path or a URL.',
),
'state' => array(
'type' => 'blob',
'size' => 'big',
'not null' => FALSE,
'description' => 'State of import or clearing batches.',
'serialize' => TRUE,
),
'fetcher_result' => array(
'type' => 'blob',
'size' => 'big',
'not null' => FALSE,
'description' => 'Cache for fetcher result.',
'serialize' => TRUE,
),
'imported' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'unsigned' => TRUE,
'description' => 'Timestamp when this source was imported last.',
),
),
'primary key' => array('id'),
);

return $schema;
}
90 changes: 90 additions & 0 deletions feedspreview.module
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

/**
* @file
* Feeds Import Preview module.
*
* Preview the feeds source content before importing.
*/

/**
* Location of Feeds importer id in path on feeds importer configuration pages.
*
* @var int
*/
define('FEEDSPREVIEW_IMPORTER_ARG', 3);

/**
* Location of active "menu item" in path on feeds importer configuration pages.
*
* @var int
*/
define('FEEDSPREVIEW_ACTIVE_ARG', 4);

/**
* Implements hook_autoload_info().
*/
function feedspreview_autoload_info() {
return array(
'FeedsPreviewSource' => 'src/FeedsPreviewSource.php',
'FeedsPreviewTable' => 'src/FeedsPreviewTable.php',
);
}

/**
* Implements hook_preprocess_HOOK() for feeds_ui_edit_page().
*
* Adds "Preview" section to Feeds edit page.
*/
function feedspreview_preprocess_feeds_ui_edit_page(&$variables) {
$importer = feeds_importer_load(arg(FEEDSPREVIEW_IMPORTER_ARG));
$active = arg(FEEDSPREVIEW_ACTIVE_ARG);
$path = 'admin/structure/feeds/' . $importer->id;

switch ($active) {
case 'preview':
module_load_include('pages.inc', 'feedspreview');
$variables['active']['title'] = t('Import preview');
$variables['active']['body'] = backdrop_get_form('feedspreview_form', $importer->id);
break;
}

$variables['info']['preview'] = array(
'class' => array('config-set'),
'title' => t('Preview'),
'body' => array(
array(
'body' => t('Show a preview of the import.'),
'actions' => array(
l(t('Import preview'), $path . '/preview'),
),
),
),
'actions' => array(),
);
}

/**
* Implements hook_form_FORM_ID_alter() for form feeds_import_form().
*
* Adds preview button to the form.
*/
function feedspreview_form_feeds_import_form_alter(&$form, &$form_state) {
// Load feedspreview.pages.inc along with the form.
form_load_include($form_state, 'inc', 'feedspreview', 'feedspreview.pages');

if (isset($form_state['feeds_parser_result'])) {
$form['feedspreview'] = feedspreview_show_result_on_form($form['#importer_id'], $form_state['feeds_parser_result']);

// Replace source form to support file fetcher.
$source = FeedsPreviewSource::instance($form['#importer_id'], 0);
$source_form = $source->configForm($form_state);
$form['feeds'] = array_merge($form['feeds'], $source_form);
}

$form['actions']['preview'] = array(
'#type' => 'submit',
'#value' => t('Preview'),
'#submit' => array('feedspreview_form_submit'),
);
}
135 changes: 135 additions & 0 deletions feedspreview.pages.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php

/**
* @file
* Page callbacks for Feeds Import Preview.
*/

/**
* Form constructor for the feeds import preview form.
*
* @see feedspreview_form_validate()
* @see feedspreview_form_submit()
*/
function feedspreview_form($form, &$form_state, $importer_id) {
$source = FeedsPreviewSource::instance($importer_id, 0);

// Set flag that this form is viewed in the admin interface.
$form_state['admin_preview'] = TRUE;

$form = array();
$form['#importer_id'] = $importer_id;
$form['#attributes']['enctype'] = 'multipart/form-data';

$form['help'] = array(
'#markup' => '<p>' . t('This will give you a brief look of what the parsed result looks like (thus before the data is processed). Note that the data may be modified during processing. If your browser supports it, you can use the left and right arrow keys on your keyboard to navigate between the results.') . '</p>',
);

if (isset($form_state['feeds_parser_result'])) {
$form['feedspreview_result'] = feedspreview_table($source, $form_state['feeds_parser_result']);
}

$source_form = $source->configForm($form_state);
if (!empty($source_form)) {
$form['feeds'] = array(
'#type' => 'fieldset',
'#title' => t('Import settings'),
'#tree' => TRUE,
'#collapsed' => isset($form_state['feeds_parser_result']),
'#collapsible' => isset($form_state['feeds_parser_result']),
) + $source_form;
}

$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Preview import'),
);

return $form;
}

/**
* Returns preview results array.
*
* @param string $importer_id
* The ID of the importer.
* @param FeedsParserResult $result
* The parser result.
*
* @return array
* Markup for previewing the results.
*/
function feedspreview_show_result_on_form($importer_id, $result) {
$field = array(
'#type' => 'fieldset',
'#title' => t('Preview'),
'#weight' => -50,
);

$field['help'] = array(
'#markup' => '<p>' . t('This will give you a brief look of what the parsed result looks like (thus before the data is processed). Note that the data may be modified during processing. If your browser supports it, you can use the left and right arrow keys on your keyboard to navigate between the results.') . '</p>',
);

$source = FeedsPreviewSource::instance($importer_id, 0);
$field['result'] = feedspreview_table($source, $result);

return $field;
}

/**
* Form validation handler for feedspreview_form().
*
* @see feedspreview_form()
* @see feedspreview_form_submit()
*/
function feedspreview_form_validate($form, &$form_state) {
FeedsPreviewSource::instance($form['#importer_id'], 0)->configFormValidate($form_state['values']['feeds']);
}

/**
* Form submission handler for feedspreview_form().
*
* @see feedspreview_form()
* @see feedspreview_form_validate()
*/
function feedspreview_form_submit($form, &$form_state) {
$source = FeedsPreviewSource::instance($form['#importer_id'], 0);

if (!empty($form_state['values']['feeds']) && is_array($form_state['values']['feeds'])) {
$source->addConfig($form_state['values']['feeds']);
if (!empty($form_state['admin_preview'])) {
// Save source only when using the preview form on the importer configuration.
$source->save();
}
}

try {
$form_state['feeds_parser_result'] = $source->preview();
}
catch (Exception $e) {
backdrop_set_message($e->getMessage(), 'error');
}
$form_state['rebuild'] = TRUE;
}

/**
* Builds a preview table.
*
* @param FeedsSource $feeds_source
* The import source.
* @param FeedsParserResult $result
* The parser result.
*
* @return array
* A build array in the format expected by backdrop_render().
*/
function feedspreview_table(FeedsSource $feeds_source, FeedsParserResult $result) {
// Check the result's source.
if (empty($result->items)) {
backdrop_set_message(t('No result.'));
return;
}

$table_builder = new FeedsPreviewTable($feeds_source);
return $table_builder->build($result);
}
Loading
Loading