Commit b8fe34fa authored by metzlerd's avatar metzlerd
Browse files

Initial working code regarding Feeds integration.

parent f953e0cd
......@@ -59,6 +59,17 @@ class Frx {
}
return $o;
}
/**
*
* @param $config array of field configuration;
* @return FrxFields
*/
public static function Fields($config = array()) {
require_once('FrxFields.inc');
$o = new FrxFields($config);
return $o;
}
/**
*
* Enter description here ...
......
<?php
/*
* Implement a field parser that handles token replacements
*/
class FrxFields {
public function __construct($fields = array()) {
$this->teng = new FrxSyntaxEngine(FRX_TOKEN_EXP, '{}', $this);
$this->fields = $fields;
}
/**
* REnder the fields from {} token syntax based on current data;
* @param $text The text to be replaced.
* @param $raw boolean should field formatting be skipped
*/
public function render($text, $raw=FALSE) {
return $this->teng->replace($text, $raw);
}
/*
* Formatter used by the syntax engine to alter data that gets extracted.
* This invokes the field translation
*/
public function format($value, $key, $data) {
// Determine if there is a field overide entry
$default='';
$link ='';
$format='';
$format_str = '';
$target = '';
$class = '';
$rel = '';
// Extract the formatter for this field;
if ($this->fields && isset($this->fields[$key])) {
extract($this->fields[$key]);
}
if ($format) {
$value = FrxReportGenerator::$instance->format_data($value, $format, $format_str, $this->teng);
}
if (is_array($value)) {
$value = implode(' ', $value);
}
// Default if specified
if (!$value && $default) {
$value = $default;
}
if ($link) {
$attributes = array();
$target = $this->teng->replace($target, TRUE);
// use the target attribute to open links in new tabs or as popups.
if (@strpos(strtolower($target), 'popup')===0) {
$opts = 'status=1';
$options = "status=1";
$attributes = array('onclick' =>
'window.open(this.href, \'' . $target . '\', "' . $options . '"); return false;');
}
else {
if ($target) $attributes['target'] = $target;
}
if ($rel) $attributes['rel'] = $this->teng->replace($rel, TRUE);
if ($class) $attributes['class'] = explode(' ', trim($this->teng->replace($class, TRUE)));
@list($url, $query) = explode('?', $link);
$url = $this->teng->replace($url, TRUE);
@list($query, $queryFrag) = explode('#', $query);
@list($url, $fragment) = explode('#', $url);
$fragment = $fragment . $queryFrag;
$data = array();
parse_str($query, $data);
if ($data) foreach ($data as $k => $v) {
$data[$k] = $this->teng->replace($v, TRUE);
}
if ($add_query) {
$parms = $_GET;
unset($parms['q']);
$data = array_merge($parms, $data);
}
if (trim($url)) $value = $this->link(
htmlspecialchars_decode($value),
$url,
array('fragment' => $fragment, 'query' => $data, 'attributes' => $attributes, 'absolute' => TRUE)
);
}
return $value;
}
}
\ No newline at end of file
<?php
// Data Fetcher
class FeedsForenaBlockFetcherResult extends FeedsFetcherResult {
// Constructor for fetcher result
public function __construct($block, $data) {
parent::__construct('');
$this->block = $block;
$this->data = $data;
}
/**
* Overrides parent::getRaw().
*/
public function getRaw() {
$xml = forena_xml($this->block, $this->data);
$raw = '';
if (is_object($xml) && method_exists($xml, 'asXML')) {
$raw = $xml->asXML();
}
return $this->sanitizeRaw($raw);
}
}
// Block Fetcher
class FeedsForenaBlockFetcher extends FeedsFetcher {
// Implement data block fetch.
public function fetch(FeedsSource $source) {
$source_config = $source->getConfigFor($this);
// Just return a Block result if this is a Block.
$r = Frx::RepoMan();
$block = $source_config['source'];
$data = @$source_config['data'];
if ($r->loadBlock($block)) {
return new FeedsForenaBlockFetcherResult($block, $data);
}
throw new Exception('Source is not a valid Forena Data Block');
}
public function sourceForm($source_config) {
$form = array();
$form['source'] = array(
'#type' => 'textfield',
'#autocomplete_path' => 'forena/data_block/autocomplete',
'#title' => 'Data Block',
'#required' => TRUE,
'#description' => t('Select an exising block.'),
);
$form['data'] = array(
'#type' => 'textfield',
'#title' => t('Data block parameters as defined in the block. '),
'#description' => t('URL Parameters'),
'#default_value' => isset($source_config['data']) ? $source_config['data'] : '',
'#maxlength' => NULL,
'#required' => FALSE,
);
return $form;
}
public function sourceFormValidate(&$values) {
$values['source'] = trim($values['source']);
$block = @Frx::RepoMan()->loadBlock($values['source']);
if (!$block) {
form_set_error('feeds][FeedsForenaBlockFetcher][source', t('The specified block does not exist.'));
}
}
}
\ No newline at end of file
<?php
class FeedsForenaXMLParser extends FeedsParser {
/**
* Implements FeedsParser::parse().
*/
public function parse(FeedsSource $source, FeedsFetcherResult $fetcher_result) {
$source_config = $source->getConfigFor($this);
$result = new FeedsParserResult();
$fields = @$source_config['fields'];
$parser = Frx::Fields($fields);
// Load and configure parser.
$xpath = @$source_config['xpath'] ? $source_config['xpath'] : '*';
$raw = $fetcher_result->getRaw();
$mappings = feeds_importer($this->id)->processor->config['mappings'];
if ($raw) {
$xml = new SimpleXMLElement($raw);
$nodes = $xml->xpath($xpath);
foreach($nodes as $node) {
Frx::Data()->push($node, 'row');
$item = array();
foreach($mappings as $field) {
$text = $field['source'];
$item[$text] = $parser->render($text);
}
$result->items[] = $item;
Frx::Data()->pop();
}
}
// Create a result object and return it.
return $result;
}
/**
* Define defaults.
*/
public function sourceDefaults() {
return array(
'xplath' => $this->config['xpath'],
);
}
/**
* Source form.
*
* Show mapping configuration as a guidance for import form users.
*/
public function sourceForm($source_config) {
$form = array();
$form['#weight'] = -10;
$mappings = feeds_importer($this->id)->processor->config['mappings'];
$sources = $uniques = array();
foreach ($mappings as $mapping) {
$sources[] = check_plain($mapping['source']);
if (!empty($mapping['unique'])) {
$uniques[] = check_plain($mapping['source']);
}
}
$items = array();
$items[] = format_plural(count($uniques), t('Column <strong>!column</strong> is mandatory and considered unique: only one item per !column value will be created.', array('!column' => implode(', ', $uniques))), t('Columns <strong>!columns</strong> are mandatory and values in these columns are considered unique: only one entry per value in one of these column will be created.', array('!columns' => implode(', ', $uniques))));
$form['help'] = array(
'list' => array(
'#theme' => 'item_list',
'#items' => $items,
),
);
$form['xpath'] = array(
'#type' => 'textfield',
'#title' => t('XPath Expression'),
'#description' => t('The character that delimits fields in the CSV file.'),
'#default_value' => isset($source_config['xpath']) ? $source_config['xpath'] : '*',
);
return $form;
}
/**
* Define default configuration.
*/
public function configDefaults() {
return array(
'xpath' => '*',
);
}
/**
* Build configuration form.
*/
public function configForm(&$form_state) {
$form = array();
$form['xpath'] = array(
'#type' => 'textfield',
'#title' => t('XPath Expression'),
'#description' => t('Xpath to used to parse rows'),
'#default_value' => $this->config['xpath'],
);
return $form;
}
}
<?php
function _forena_feeds_plugins() {
$path = drupal_get_path('module', 'forena') . '/feeds';
$info = array();
$info['FeedsForenaBlockFetcher'] = array(
'name' => 'Forena Block Fetcher',
'description' => 'Fetch any forena data as XML. Use with an XML Feed parser',
'handler' => array(
'parent' => 'FeedsFetcher',
'class' => 'FeedsForenaBlockFetcher',
'file' => 'FeedsForenaBlockFetcher.inc',
'path' => $path,
),
);
$info['FeedsForenaXMLParser'] = array(
'name' => 'Forena XML Parser',
'description' => 'Parse XML using the fornea report token syntax.',
'handler' => array(
'parent' => 'FeedsParser',
'class' => 'FeedsForenaXMLParser',
'file' => 'FeedsForenaXMLParser.inc',
'path' => $path,
),
);
return $info;
}
\ No newline at end of file
......@@ -1401,6 +1401,11 @@ function theme_forena_web_report ($variables) {
return $output;
}
/**
* Iemplents hook_views_api
* @return multitype:number The Ambigous <The, string>
*/
function forena_views_api() {
return array(
'api' => 3,
......@@ -1409,6 +1414,15 @@ function forena_views_api() {
);
}
/**
* Implements hook_feeds_plugins().
*/
function forena_feeds_plugins() {
module_load_include('inc', 'forena', 'feeds/forena_feeds');
return _forena_feeds_plugins();
}
/**
* Implements hook_bean_types_api_info().
*/
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment