You are here

FrxDrupal.inc in Forena Reports 7.2

Provides data blocks for native drupal connections using the default drupal connections.

File

plugins/FrxDrupal.inc
View source
<?php

// $Id$

/**
 * @file
 * Provides data blocks for native drupal connections using the default
 * drupal connections.
 *
 */
class FrxDrupal extends FrxDataProvider {

  /**
   * Implements hooks into the drupal applications
   */
  private $db;

  /**
   * Object constructor
   *
   * @param unknown_type $uri Database connection string.
   * @param string $repos_path Path to location of data block definitions
   */
  public function __construct($conf, $repos_path) {
    parent::__construct($conf, $repos_path);

    // Set up the stuff required to translate.
    $this->te = new FrxSyntaxEngine(FRX_SQL_TOKEN, ':', $this);
  }

  /**
   * Get data based on file data block in the repository.
   *
   * @param String $block_name
   * @param Array $parm_data
   * @param Query $subQuery
   */
  public function data($block_name, $params = array(), $clause = '') {

    // Load the block from the file
    $block = $this
      ->load_block($block_name, $clause);
    $xml = '';
    if (trim($block['source']) && $this
      ->access($block['access'])) {
      $sql = $block['source'];
      if ($clause) {
        $sql = 'SELECT * FROM (' . trim($sql, ' ;') . ') forena_table ' . $clause;
      }
      $sql = $this->te
        ->replace($sql);
      $rs = db_query($sql);
      $xml = new SimpleXMLElement('<table/>');
      foreach ($rs as $data) {
        $row_node = $xml
          ->addChild('row');
        foreach ($data as $key => $value) {
          $row_node
            ->addChild($key, htmlspecialchars($value));

          //$row_node->$key = $value;
        }
      }
    }
    return $xml;
  }

  /**
   * Implement custom SQL formatter to make sure that strings are properly escaped.
   * Ideally we'd replace this with something that handles prepared statements, but it
   * wouldn't work for
   *
   * @param unknown_type $value
   * @param unknown_type $key
   * @param unknown_type $data
   */
  public function format($value, $key, $data) {
    $db = Database::getConnection('default');
    if ($db) {
      if ($value == '') {
        $value = 'NULL';
      }
      else {
        if (is_array($value)) {
          if ($value == array()) {
            $value = 'NULL';
          }
          else {

            // Build a array of values string
            $i = 0;
            $val = '';
            foreach ($value as $v) {
              $i++;
              if ($i > 1) {
                $val .= ',';
              }
              $val .= $db
                ->quote($v);
            }
            $value = $val;
          }
        }
        else {
          $value = trim($value);
          $value = $db
            ->quote($value);
        }
      }
    }
    return $value;
  }

}

Classes

Namesort descending Description
FrxDrupal @file Provides data blocks for native drupal connections using the default drupal connections.