Skip to content
FrxReportGenerator.inc 6.71 KiB
Newer Older
<?php
// $Id$
/**
 * @file
 * Common functions used throughout the project but loaded in this
 * file to keep the module file lean.
 */
// Include Report renderer.
require_once('FrxReport.inc');
require_once('FrxDataSource.inc');
metzlerd's avatar
metzlerd committed
  public $app;
  private $repositories = array();
  static $instance='';
  public $title;
  public $content;
  static public function instance() {
David Metzler's avatar
David Metzler committed
    global $_forena_application_class;
David Metzler's avatar
David Metzler committed
      if (!$_forena_application_class) {
        $forena_application_class = 'FrxDrupalApplication';
      }

David Metzler's avatar
David Metzler committed
      self::$instance = new FrxReportGenerator($_forena_application_class);
  public function __construct($application_class) {

    $app = $this->app = new $application_class();
  }

  /**
   * Clear session data because we want to reload.
   */
  public function clear_session() {
David Metzler's avatar
David Metzler committed
    if (isset($_SESSION['forena_access'])) unset($_SESSION['forena_access']);

  /**
   * Load the formatters for all initialized repositories.
   *
   */
  function get_formatter($fkey) {
    // Get all repositories
    $repos = Frx::RepoMan()->repositories;
    $formats = array();
    foreach ($repos as $k => $r) {
      $provider = isset($r['data']) ? $r['data'] : NULL;
      if ($provider && method_exists($provider, 'formats'))  {
        $f = $provider->formats();
David Metzler's avatar
David Metzler committed
        if (isset($f[$fkey]) && method_exists($provider, $fkey)) {
          // We found an object with the advertised method return it
          return $provider;
        }
      }
    }
    //Did not find the formater in the data provider
    //Look to see if it's in a control class
    $controls = Frx::Controls();
    foreach ($controls as $k => $r) {
      $provider = $r;
      if ($provider && method_exists($provider, 'formats'))  {
        $f = $provider->formats();
David Metzler's avatar
David Metzler committed
        if (isset($f[$fkey]) && method_exists($provider, $fkey)) {
          // We found an object with the advertised method return it
          return $provider;
        }
      }
    }
    return $formats;
  }



  /**
   * Accepts the name of a file
   *
David Metzler's avatar
David Metzler committed
   */
  function get_report($report_name, $data=array()) {
David Metzler's avatar
David Metzler committed
    $r=NULL;
    $this->alter_parameters($report_name, $data);
    if ($report_name) {
      $r_text = $this->app->load_report($report_name);
David Metzler's avatar
David Metzler committed
      if ($r_text) {
        $r = new FrxReport($r_text, $data);
      }
  function alter_parameters($report_name, &$data) {
David Metzler's avatar
David Metzler committed
    if (method_exists($this->app, 'alter_parameters')) {
      $this->app->alter_parameters($report_name, $data);

  /**
   * Enter description here...
   *
   * @param simplexml $xml
   * @param string $tag
   * @return string
   */
  function inner_xml($xml, $tag) {
    if (is_object($xml) && is_object($xml->$tag)) {
      $xml_data = $xml->$tag->asXML();
      $xml_data = preg_replace("/<\/?" . $tag . "(.|\s)*?>/", "", $xml_data);
    };
    return $xml_data;
  }

  /**
   * Accepts the name of the html tag, and the string the tag is in.
   *
   * Returns the string within the html tag name
   *
David Metzler's avatar
David Metzler committed
   */
  function get_html($tag, $r_text) {
    $open = strpos($r_text, $tag);
    $close = strpos($r_text, '>', $open);
    $next = strpos($r_text, '<', $close + 1);
    $str = substr($r_text, $close + 1, $next - ($close + 1));

    return $str;
  }

  function format_data($value, $format, $format_str, $teng='') {
    $fo = $this->get_formatter($format);
    if ($fo) {
      $ret = $fo->$format($value, $format_str, $teng);
    }
    else {
      $ret = $value;
    }
    return $ret;
  }



  /**
   * Returns an object of the template class
   * that has a method named templates.
   *
   * If it fails it returns a 0;
   */
  function get_templates($fkey) {
    $templates = $this->supported_templates();
    if (class_exists($fkey)) {
David Metzler's avatar
David Metzler committed
      return new $fkey();
    }
  }

  /**
   *
   * @return returns an array of supported templates
   *
   */
  function supported_templates() {
David Metzler's avatar
David Metzler committed
    require_once('templates/FrxTemplate.inc');
    require_once('templates/FrxTable.inc');
    require_once('templates/FrxFieldTable.inc');
    require_once('templates/FrxEmailMerge.inc');
    require_once('templates/FrxGraphTemplate.inc');
    require_once('templates/FrxRptInclude.inc');
    $templates = array(
      'FrxTable' => 'Table',
      'FrxTemplate' => 'Document',
      'FrxEmailMerge' => 'Email Merge',
      'FrxFieldTable' => 'Fields',
      'FrxGraphTemplate' => 'Graph or Chart',
    $controls = Frx::Controls();
    $supported_formats = array();
    $f = array();
    foreach ($controls as $k => $r) {
      $provider = $r;
      if ($provider && method_exists($provider, 'formats'))  {
        $f = $provider->formats();
        $supported_formats = array_merge($supported_formats, $f);
      }
    }
    return $supported_formats;
  }


David Metzler's avatar
David Metzler committed
  /**
   * Load and render a report based on a drupal path.
   * In this function the arglist is used to get the full path to the report.
   *
   * @return unknown
   */
  function report($name_in, $parms = array(), $print=TRUE) {
    $output = '';
    $desc = Frx::Menu()->parseURL($name_in);
    $name = $desc['name'];
    $format = isset($desc['format']) ? $desc['format'] : '';
    $filename = $desc['filename'];
    // Determine the data to get.
    if (!$parms) {
      $parms = array_merge($_GET, $_POST);
      unset($parms['q']);
David Metzler's avatar
David Metzler committed
    else $parms = (array)$parms;
David Metzler's avatar
David Metzler committed

    if ($name) {
      $r = @$this->get_report($name, $parms);
      if (!$r || !$r->rpt_xml) {
        $this->app->error('Could not load report. Check for invalid XML in ' . $name);
        return '';
David Metzler's avatar
David Metzler committed

      //check for default parameters
      $r->processParameters();
      Frx::Skin()->load($r->skin);
      Frx::Skin()->loadSkinFiles($name);
      $r->render($format);
      $o =  Frx::Document($format);
      if ($o) {
        $output .= $o->render($r, $format,  array());
        //$output = check_markup($output, variable_get('forena_input_format', 'full_html'));
        if ($print) {
          $printed = $o->output($output);
        }
        else {
          $printed = FALSE;
        }
        if (!$printed) {
          return  $output;
        }
David Metzler's avatar
David Metzler committed
  public function debug($short_message='', $log='') {
    $this->app->debug($short_message, $log);
  }
David Metzler's avatar
David Metzler committed
  public function error($short_message='', $log='') {
    $this->app->error($short_message, $log);
  }
David Metzler's avatar
David Metzler committed
  public function configuration($name) {
    return $this->app->configuration($name);
  }
David Metzler's avatar
David Metzler committed
  public function report_path() {
    return $this->app->configuration('report_repos');
  }
David Metzler's avatar
David Metzler committed
  public function url($path, $options = array()) {
    return $this->app->url($path, $options);
  }