You are here

public function FrxDrupal::sqlData in Forena Reports 8

Get data based on file data block in the repository.

Parameters

string $sql: Query to execute

array $options: Options array containing type information for query.

Return value

array|\SimpleXMLElement Data returned from query execution.

File

src/FrxPlugin/Driver/FrxDrupal.php, line 134
Provides data blocks for native drupal connections using the default drupal connections.

Class

FrxDrupal
Class FrxDrupal

Namespace

Drupal\forena\FrxPlugin\Driver

Code

public function sqlData($sql, $options = array()) {
  $rs = $this
    ->query($sql, $options);
  $entity_map = array();
  $select_fields = array();
  $rownum = 0;
  if (!$rs) {
    return NULL;
  }
  if (@$options['return_type'] == 'raw') {
    return $rs;
  }
  $xml = new \SimpleXMLElement('<table/>');
  if ($rs && $rs
    ->columnCount()) {
    foreach ($rs as $data) {
      $rownum++;
      $row_node = $xml
        ->addChild('row');
      $row_node['num'] = $rownum;
      foreach ($data as $key => $value) {
        $row_node
          ->addChild($key, htmlspecialchars($value));
      }

      /* If we are querying entities, we will store away IDs
       * for later querying and XML processing in the
       * loadEntitys method
       */
      if (@$options['entity_type'] && @$options['entity_id']) {
        $display = @$options['entity_display'];
        $id_key = $options['entity_id'];
        $type = $options['entity_type'];
        $id = $data->{$id_key};
        if ($id) {
          $entity_map[$id][] = $row_node;
          $select_fields[$id] = $data;
        }
      }
    }
  }
  if ($entity_map) {
    $this
      ->loadEntities($type, $entity_map, $select_fields, $display);
  }
  if ($this->debug) {
    $d = $xml ? htmlspecialchars($xml
      ->asXML()) : '';
    $this
      ->app()
      ->debug('SQL: ' . $sql, '<pre> SQL:' . $sql . "\n XML: " . $d . "\n</pre>");
  }
  return $xml;
}