function views_xml_backend_plugin_query_xml::parse in Views XML Backend 6
Same name and namespace in other branches
- 7 views_xml_backend_plugin_query_xml.inc \views_xml_backend_plugin_query_xml::parse()
1 call to views_xml_backend_plugin_query_xml::parse()
- views_xml_backend_plugin_query_xml::execute in ./
views_xml_backend_plugin_query_xml.inc - Executes the query and fills the associated view object with according values.
File
- ./
views_xml_backend_plugin_query_xml.inc, line 144 - Query plugin for views_xml_backend.
Class
- views_xml_backend_plugin_query_xml
- @file Query plugin for views_xml_backend.
Code
function parse(&$view, $data) {
$doc = new DOMDocument();
$success = $doc
->loadXML($data->contents);
// If the file fails to load, bail. The appropriate error messages will be
// displayed automatically.
if (!$success) {
return;
}
$xpath = new DOMXPath($doc);
// Create a simplexml object so that we can use
// SimpleXMLElement::getNamespaces().
// Does anyone know a better way to do it?
$simple = simplexml_import_dom($doc);
if (!$simple) {
return;
}
$namespaces = $simple
->getNamespaces(TRUE);
// Register namespaces. Allow for overriding the default namespace.
foreach ($namespaces as $prefix => $namespace) {
if ($prefix === '') {
if (empty($this->options['default_namespace'])) {
$prefix = 'default';
}
else {
$prefix = $this->options['default_namespace'];
}
}
$xpath
->registerNamespace($prefix, $namespace);
}
try {
if ($this->pager
->use_count_query() || !empty($view->get_total_rows)) {
// $this->pager->execute_count_query($count_query);
// Hackish execute_count_query implementation.
$this->pager->total_items = $xpath
->evaluate($view->build_info['count_query']);
if (!empty($this->pager->options['offset'])) {
$this->pager->total_items -= $this->pager->options['offset'];
}
$this->pager
->update_page_info();
}
// Let the pager modify the query to add limits.
//$this->pager->pre_execute($query);
if (!empty($this->limit) || !empty($this->offset)) {
$view->build_info['query'] = '(' . $view->build_info['query'] . ')';
$offset = intval(!empty($this->offset) ? $this->offset : 0);
if (!empty($this->limit)) {
$limit = intval($this->limit) + $offset;
$view->build_info['query'] .= "[position() > {$offset} and not(position() > {$limit})]";
}
else {
$view->build_info['query'] .= "[position() > {$offset}]";
}
}
// Get the rows.
$rows = $xpath
->query($view->build_info['query']);
$result = array();
foreach ($rows as $row) {
$item = new stdClass();
// Query each field per row.
foreach ($this->fields as $field) {
$field_key = $field['field'];
if (!$field_key) {
continue;
}
$node_list = $xpath
->evaluate($field_key, $row);
if ($node_list) {
// Allow multiple values in a field.
if ($field['multiple']) {
$item->{$field_key} = array();
foreach ($node_list as $node) {
$item->{$field_key}[] = $node->nodeValue;
}
}
else {
$item->{$field_key} = $node_list
->item(0)->nodeValue;
}
}
else {
// Make sure all of the fields are set. Allows us to do less error
// checking later on.
$item->{$field_key} = NULL;
}
}
$result[] = $item;
}
if (!empty($this->orderby)) {
// Array reverse, because the most specific are first.
foreach (array_reverse($this->orderby) as $orderby) {
$orderby
->sort($result);
}
}
$view->result = $result;
$view->total_rows = count($result);
$this->pager
->post_execute($view->result);
} catch (Exception $e) {
$view->result = array();
if (!empty($view->live_preview)) {
drupal_set_message(time());
drupal_set_message($e
->getMessage(), 'error');
}
else {
debug($e
->getMessage(), 'Views XML Backend');
}
}
}