Skip to content
Snippets Groups Projects
Commit 4da7fec5 authored by allisterbeharry's avatar allisterbeharry
Browse files

Committed alpha release of Views JXML to Drupal-5 branch.

parent 4d9e984c
No related branches found
No related tags found
No related merge requests found
opml32x32.png

32.7 KiB

<?php
// $Id $
/**
* @file views-view-json.tpl.php
* View template to render view fields as JSON. Currently only supports the Exhibit format.
*
* - $view: The view in use.
* - $rows: The raw result objects from the query, with all data it fetched.
* - $options: The options for the style passed in from the UI.
*
* @ingroup views_templates
* @see views_json.views.inc
*/
//print_r($view);
//print_r($rows);
//print_r($options);
$items = array();
foreach($rows as $row) {
// print_r($row).EOL;
$items[] = explode("|", trim($row));
}
//print_r($items);
//foreach ($items as $item) {
// print_r($item).PHP_EOL;
// foreach($item as $itemfield) {
// print($itemfield);
// $itemfieldarray = explode(":", $itemfield);
// print_r($itemfieldarray).PHP_EOL;
// $label = $itemfieldarray[0]; $value=$itemfieldarray[1];
// print $label." : ".$value;
// }
//}
if ($options['format'] == 'Exhibit') json_exhibit_render($items);
function json_exhibit_render($items) {
define('EXHIBIT_DATE_FORMAT', '%Y-%m-%d %H:%M:%S');
$json = "{\n".str_repeat(" ", 4).'"items"'." : ". " [";
foreach ($items as $item) {
$json.="\n".str_repeat(" ", 8)."{\n";
foreach ($item as $itemfield) {
$itemfieldarray = explode(":", $itemfield);
$label = $itemfieldarray[0]; $value=$itemfieldarray[1];
$value = preg_replace('/<.*?>/', '', $value); // strip html tags
$value = str_replace(array("\r", "\n", ','), ' ', $value); // strip line breaks and commas
$value = str_replace('"', '""', $value); // escape " characters
$value = decode_entities($value);
$json.=str_repeat(" ", 8).$label. " ".": ".'"'.$value.'"'."\n";
}
$json.=str_repeat(" ", 8)."},\n";
}
$json.=str_repeat(" ", 4)."]\n}";
/*
* The following will cause an error in a live view preview - comment out if
* debugging in there.
*/
drupal_set_header('Content-Type: text/javascript');
print $json;
module_invoke_all('exit');
exit;
}
<?php
// $Id $
/**
* @file views-view-row-unformatted.tpl.php
* Simple view template to view unformatted fields from the views query.
*
* - $view: The view in use.
* - $fields: an array of $field objects. Each one contains:
* - $field->content: The output of the field.
* - $field->raw: The raw data for the field, if it exists. This is NOT output safe.
* - $field->class: The safe class id to use.
* - $field->handler: The Views field handler object controlling this field. Do not use
* var_export to dump this object, as it can't handle the recursion.
* - $field->separator: an optional separator that may appear before a field.
* - $row: The raw result object from the query, with all data it fetched.
*
* @ingroup views_templates
* @see views_json.views.inc
*/
//print ('template');
//print_r($fields);
//print_r($row);
//print_r($options);
//foreach ($fields as $id => $field)
// print $id.":".$field->content."\n";
$field_separator = filter_xss_admin($options['separator']);
foreach($row as $field_label => $field_value)
$row_unformatted.= $field_label.":".(!is_null($field_value) ? $field_value: "").$field_separator;
print rtrim($row_unformatted, $field_separator).PHP_EOL;
\ No newline at end of file
; $Id $
; $Id$
name = Views JSON
description = "Views style plugin to render node content as JSON."
package = Views
......
<?php
//$Id $
/**
* @file views_json.views.inc
* Views style plugin to render nodes in the JSON data format.
* @see views-view-json.tpl.php views-view-row-unformatted.tpl.php
* @ingroup views_plugins
*/
/**
* Implementation of hook_views_plugin
*
*/
function views_json_views_plugins() {
return array(
'style' => array( //declare the views_json style plugin
'views_json' => array(
'title' => t('JSON data document'),
'theme' => 'views_view_json',
'help' => t('Displays nodes in the JSON data format.'),
'handler' => 'views_plugin_style_json',
'uses row plugin' => TRUE,
'uses fields' => TRUE,
'uses options' => TRUE,
'type' => 'normal',
),
),
'row' => array( //declare the unformatted row plugin
'unformatted' => array(
'title' => t('Unformatted'),
'help' => t('(Displays the unformatted data for each row from the views query with each row on a new line. Set as | for views_json.'),
'handler' => 'views_plugin_row_unformatted',
'theme' => 'views_view_row_unformatted',
'uses fields' => TRUE,
'uses options' => TRUE,
'type' => 'normal',
)
)
);
}
/**
* Implementation of views_plugin_style
*
*/
class views_plugin_style_json extends views_plugin_style {
/**
* Set default options
*/
function options(&$options) {
$options['format'] = 'Exhibit';
}
/**
* Provide a form for setting options.
*
* @param array $form
* @param array $form_state
*/
function options_form(&$form, &$form_state) {
$form['format'] = array(
'#type' => 'radios',
'#title' => t('JSON data format'),
'#options' => array('Exhibit' => t('MIT Simile/Exhibit'), 'Canonical' => t('Canonical'), 'JSONP' => t('JSONP')),
'#default_value' => $this->options['format'],
);
}
}
/**
* Theme preprocess function for views-view-json.tpl.php
*
* @param array $vars
*/
function template_preprocess_views_view_json(&$vars) {
$view = &$vars['view'];
$options = $view->style_handler->options;
$handler = $view->style_handler;
}
/**
* Implementation of views_row_plugin
*
*/
class views_plugin_row_unformatted extends views_plugin_row {
/**
* Set default options
*
* @param array $options
*/
function options(&$options) {
$options['separator'] = '|';
}
/**
* Provide a form for setting options.
*/
function options_form(&$form, &$form_state) {
$fields = $this->display->handler->get_option('fields');
$options = array();
foreach ($fields as $field => $info) {
$handler = views_get_handler($info['table'], $info['field'], 'field');
if ($handler) {
$options[$field] = $handler->ui_name();
}
}
$form['separator'] = array(
'#title' => t('Separator'),
'#type' => 'textfield',
'#size' => 10,
'#default_value' => isset($this->options['separator']) ? $this->options['separator'] : ',',
'#description' => t('The separator is placed between fields.'),
);
}
}
/**
* Theme preprocess function for views-view-row-unformatted.tpl.php
*/
function template_preprocess_views_view_row_unformatted(&$vars) {
$view = $vars['view'];
//print('preprocess');
// Loop through the fields for this view.
foreach ($view->field as $id => $field) {
if (!empty($field['handler']) && is_object($field['handler'])) {
$object = new stdClass();
$object->content = $field['handler']->theme($vars['row']);
if (isset($field['handler']->field_alias) && isset($vars['row']->{$field['handler']->field_alias})) {
$object->raw = $vars['row']->{$field['handler']->field_alias};
}
else {
$object->raw = NULL; // make sure it exists to reduce NOTICE
}
if (!empty($vars['options']['separator']) && $object->content) {
$object->separator = filter_xss_admin($vars['options']['separator']);
}
$object->handler = $field['handler'];
$object->class = views_css_safe($id);
$object->label = check_plain($field['handler']->label());
$vars['fields'][$id] = $object;
}
}
}
; $Id $
name = Views XML
description = "Views style plugin to render node content as XML."
package = Views
dependencies = views
\ No newline at end of file
<?php
//$Id$
/**
* @file
* views_xml.module - provides Views plugin for rendering node content as XML.
*/
/**
* Implementation of hook_views_style_plugins
*/
function views_xml_views_style_plugins() {
return array(
'views_xml_raw' => array(
'name' => t('Views XML: Raw XML data document'),
'theme' => 'views_xml_raw',
'needs_table_header' => TRUE,
'needs_fields' => TRUE,
'even_empty' => TRUE,
),
'views_xml_opml' => array(
'name' => t('Views XML: OPML XML data document'),
'theme' => 'views_xml_opml',
'needs_table_header' => TRUE,
'needs_fields' => TRUE,
'even_empty' => TRUE,
),
);
}
/*
* Implementation of hook_views_arguments to add the XML
* and OPML argument selectors.
* @returns array of view arguments
*/
function views_xml_views_arguments() {
$arguments = array(
'xml_raw' => array(
'name' => t('Views XML: raw XML data document selector'),
'handler' => 'views_xml_views_handler',
'option' => 'string',
'help' => t('This argument specifies a Raw XML data document selector; it will only provide a method for rendering the current view as raw XML.'),
),
'xml_opml' => array(
'name' => t('Views XML: OPML XML data document selector'),
'handler' => 'views_xml_views_handler',
'option' => 'string',
'help' => t('This argument specifies a OPML XML data document selector; it will only provide a method for rendering the current view as OPML.'),
),
);
return $arguments;
}
/**
* handler for our own raw-XML or OPML or structured-XML argument handler
*/
function views_xml_views_handler($op, &$query, $argtype, $arg='') {
if ($op == 'filter') {
views_xml_views_argument('argument', $GLOBALS['current_view'], $arg);
}
}
/**
* argument hook that will display the XML document or display export icons.
*/
function views_xml_views_argument($op, &$view, $arg) {
if ($op == 'argument' && ($arg == 'xml_raw' || $arg == 'xml_opml')) {
$view->page_type = 'views_'. $arg;
}
else if ($op == 'post_view' && $view->build_type != 'block') {
$args = views_post_view_make_args($view, $arg, $arg);
$url = views_get_url($view, $args);
$title = views_get_title($view, 'page', $args);
$links = array();
if ($arg == 'xml_opml') {
if (($image = theme('image', drupal_get_path('module', 'views_xml') .'/opml32x32.png', t('OPML'), t('Show @title as OPML.', array('@title' => $title))))) {
$links[] = l($image, $url, array('class' => 'xml-icon'), $url_filter, NULL, FALSE, TRUE);
return implode('&nbsp;&nbsp;', $links);
}
}
else if ($arg == 'xml_raw') {
if (($image = theme('image', drupal_get_path('module', 'views_xml') .'/xml32x36.png', t('Raw XML'), t('Show @title as raw XML', array('@title' => $title))))) {
$links[] = l($image, $url, array('class' => 'xml-icon'), $url_filter, NULL, FALSE, TRUE);
return implode('&nbsp;&nbsp;', $links);
}
}
else if ($arg == 'xml_structured') {
if (($image = theme('image', drupal_get_path('module', 'views_xml') .'/xml64x64.png', t('Structured XML'), t('Show @title as structured XML', array('@title' => $title))))) {
$links[] = l($image, $url, array('class' => 'xml-icon'), $url_filter, NULL, FALSE, TRUE);
return implode('&nbsp;&nbsp;', $links);
}
}
}
}
/*
* describes how to theme a raw XML view
*/
function theme_views_xml_raw($view, $nodes, $type) {
views_xml_raw_render($view->vid, $nodes, $type);
}
/*
* describes how to theme a OPML view
*/
function theme_views_xml_opml($view, $nodes, $type) {
views_xml_opml_render($view->vid, $nodes, $type);
}
/**
* post view to display the render icons
*/
function views_xml_views_post_view($view, $items, $output) {
$links = '';
foreach ($view->argument as $id => $argument) {
if ($argument['type'] == 'xml_raw' || $argument['type'] == 'xml_opml') {
$links .= views_xml_views_argument('post_view', $view, $argument['type'], '');
}
}
return $links;
}
function views_xml_raw_render($vid, $nodes, $type) {
$view = views_load_view($vid);
$result = views_build_view('items', $view);
$fields = _views_get_fields();
$xw = new xmlWriter();
$xw->openMemory();
$xw->startDocument('1.0','UTF-8');
$xw->startElement('nodes');
foreach ($nodes as $node) {
$xw->startElement ('node');
foreach ($view->field as $field) {
if ($fields[$field['id']]['visible'] !== false) {
$label = $field['label'] ? $field['label'] : $fields[$field['fullname']]['name'];
$label = preg_replace('/\W/', '', $label); // atrip any non-word character
$value = $field;
$value = views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view);
$value = preg_replace('/<.*?>/', '', $value); // strip html tags
$value = str_replace(array("\r", "\n", ','), ' ', $value); // strip line breaks and commas
$value = str_replace('"', '""', $value); // escape " characters
$value = decode_entities($value);
$xw->writeAttribute($label, $value);
}
}
$xw->endElement(); //node
}
$xw->endElement(); //nodes
$xw->endDocument;
drupal_set_header('Content-Type: text/xml');
//drupal_set_header('Content-Disposition: attachment; filename="view-'. $view->name .'.csv"');
print $xw->outputMemory(true);
module_invoke_all('exit');
exit;
}
function views_xml_opml_render($vid, $nodes, $type) {
global $user;
$view = views_load_view($vid);
$result = views_build_view('items', $view);
$fields = _views_get_fields();
$xw = new xmlWriter();
$xw->openMemory();
$xw->startDocument('1.0','UTF-8');
$xw->startElement('opml');$xw->writeAttribute('version', '1.0');
$xw->startElement('head'); //<opml version="1.0"><head>
$xw->writeElement('title', variable_get('site_name', 'drupal').'-'.$view->name);//<opml version="1.0"><head><title>..</title>
$xw->writeElement('ownerName', $user->name);//<opml version="1.0"><head><ownerName>..</ownerName>
$xw->writeElement('ownerEmail', $user->mail);//<opml version="1.0"><head><ownerName>..</ownerName>
$xw->endElement(); //head
$xw->writeElement('dateCreated', gmstrftime('%Y-%m-%d %H:%M:%S', time()));//<opml version="1.0"><head><dateCreated>..</dateCreated>
foreach ($nodes as $node) {
$xw->startElement ('outline');
$field_count = 0;
foreach ($view->field as $field) {
if ($fields[$field['id']]['visible'] !== false) {
$field_count++;
$label = $field['label'] ? $field['label'] : $fields[$field['fullname']]['name'];
$label = preg_replace('/\W/', '', $label); // atrip any non-word character
$value = $field;
$value = views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view);
$value = preg_replace('/<.*?>/', '', $value); // strip html tags
$value = str_replace(array("\r", "\n", ','), ' ', $value); // strip line breaks and commas
$value = str_replace('"', '', $value); // remove quote characters
$value = decode_entities($value);
$xw->writeAttribute($field_count == 1 ? 'text' : $label, $value);
}
}
$xw->endElement(); //outline
}
$xw->endElement(); //opml
$xw->endDocument;
drupal_set_header('Content-Type: text/xml');
//drupal_set_header('Content-Disposition: attachment; filename="view-'. $view->name .'.csv"');
print $xw->outputMemory(true);
module_invoke_all('exit');
exit;
}
xml32x36.png

2.23 KiB

0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment