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(' ', $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(' ', $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(' ', $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();
$xml .= ''."\n";
$xml .= ''."\n";
$xml .=''."\n";
foreach ($nodes as $node) {
$xml .= ' '."\n";
foreach ($view->field as $field) {
if ($fields[$field['id']]['visible'] !== false) {
$label = views_xml_strip_illegal_chars($field['label'] ? $field['label'] : $fields[$field['fullname']]['name']);
// Added strip_tags by Stephen Musgrave @ Capellic - 2008-08-01
$value = views_xml_strip_illegal_chars(strip_tags(views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view)));
$label = preg_replace('/\W/', '', $label); // atrip any non-word character
$label = str_replace("Profile", '', $label); //strip out Profile: from profile fields
if (strtotime($value))
$value = date(DATE_ISO8601, strtotime($value));
if (is_null($value) || ($value === '')) continue;
$xml .= " <$label>$value$label>\n";
}
}
$xml .= ' '."\n";
}
$xml .=''."\n";
drupal_set_header('Content-Type: text/xml');
print $xml;
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();
$xml .= ''."\n";
$xml .= ''."\n";
$xml .= ''."\n";
$xml .= ''."\n";
$xml .= ' '.variable_get('site_name', 'drupal').'-'.$view->name.''."\n";
$xml .= ' '.$user->name.''."\n";
$xml .= ' '.$user->mail.''."\n";
$xml .= ''."\n";
$xml .= ''.date(DATE_ISO8601, time()).''."\n";
foreach ($nodes as $node) {
$xml .= ' field as $field) {
if ($fields[$field['id']]['visible'] !== false) {
$field_count++;
$label = views_xml_strip_illegal_chars($field['label'] ? $field['label'] : $fields[$field['fullname']]['name']);
$value = views_xml_strip_illegal_chars(views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view));
$label = preg_replace('/\W/', '', $label); // atrip any non-word character
$label = str_replace("Profile", '', $label); //strip out Profile: from profile fields
if (strtotime($value))
$value = date(DATE_ISO8601, strtotime($value));
if (is_null($value) || ($value === '')) continue;
$xml .= $label.'="'.$value.'" ';
}
}
$xml .= " />"."\n";
}
$xml .= " ";
drupal_set_header('Content-Type: text/xml');
//drupal_set_header('Content-Disposition: attachment; filename="view-'. $view->name .'.csv"');
print $xml;
module_invoke_all('exit');
exit;
}
/**
* Strips illegal Unicode characters and encodes entities in string
*
* @param string $input
* @return string
*/
function views_xml_strip_illegal_chars($input) {
$output = preg_replace('/[\x{80}-\x{A0}'. // Non-printable ISO-8859-1 + NBSP
'\x{01}-\x{1F}'. //Non-printable ASCII characters
'\x{AD}'. // Soft-hyphen
'\x{2000}-\x{200F}'. // Various space characters
'\x{2028}-\x{202F}'. // Bidirectional text overrides
'\x{205F}-\x{206F}'. // Various text hinting characters
'\x{FEFF}'. // Byte order mark
'\x{FF01}-\x{FF60}'. // Full-width latin
'\x{FFF9}-\x{FFFD}'. // Replacement characters
'\x{0}]/u', // NULL byte
'', $input);
$output = str_replace('"', '"', $output); //encode quote
$output = str_replace('&', '&', $output); //encode ampersand
$output = str_replace("'", '&pos;', $output); //encode apostrophe
$output = str_replace('<', '<', $output); //encode left-angled bracket
$output = str_replace('>', '&rt;', $output); //encode right-angled bracket
// Removed by Stephen Musgrave @ Capellic - 2008-08-01
//$output = str_replace('.', '', $output); //replace . to avoid string being parsed as date - ugly hack
return $output;
}