You are here

Frx.inc in Forena Reports 7.5

Same filename and directory in other branches
  1. 7.3 Frx.inc
  2. 7.4 Frx.inc

Frx.incL General Forena Reporting Class

File

Frx.inc
View source
<?php

/**
 * @file Frx.incL
 * General Forena Reporting Class
 */
define('FRX_TOKEN_EXP', '/\\{[^\\n^\\r^}]+}/');
define('FRX_SQL_REWRITE_EXP', '/\\[[^\\n^\\r^\\]]+\\]/');
define('FRX_SQL_TOKEN', '/(?<!(:|[a-zA-Z]|[0-9]|[_\\.])):[a-zA-Z]([a-zA-Z]|[0-9]|[_\\.])+/');
use Drupal\forena\Context\DataContext;
use Drupal\forena\Token\TokenReplacerBase;
use Drupal\forena\Skin;
use Drupal\forena\Editor\BlockEditor;
use Drupal\forena\Editor\ReportEditor;
use Drupal\forena\ReportFields;
use Drupal\forena\DataManager;
use Drupal\forena\Menu;
class Frx {

  /**
   * Helper method to return current url parameters.
   * @return unknown
   */
  public static function parms() {
    $parms = $_GET;
    unset($parms['q']);
    return $parms;
  }

  /**
   * Temporary dom object used for for fragment importing and manipulation.
   * We use a singleton here to reduce the memory footprint.
   * @return DOMDocument
   */
  public static function tempDOM() {
    static $o = '';
    if (!$o) {
      $o = new DOMDocument('1.0', 'UTF-8');
      @$o
        ->load('<?xml version="1.0" encoding="UTF-8"?>
         <!DOCTYPE root [
         <!ENTITY nbsp "&#160;">
         ]>');
    }
    return $o;
  }

  /**
   * Skin Factory
   * @return Skin
   */
  public static function Skin() {
    static $o = '';
    if (!$o) {
      $o = new Skin();
    }
    return $o;
  }

  /**
   * Data Factory
   */
  public static function Data() {
    static $o = '';
    if (!$o) {
      $o = new DataContext();
    }
    return $o;
  }

  /**
   * File singleton factory
   * @return ReportFile
   */
  public static function File() {
    static $o = '';
    if (!$o) {
      $o = new \Drupal\forena\File\ReportFile();
    }
    return $o;
  }

  /**
   * File singleton factory
   * @return FrxDataFile
   */
  public static function DataFile() {
    static $o = '';
    if (!$o) {
      $o = new \Drupal\forena\File\DataFile();
    }
    return $o;
  }

  /**
   * File singleton factory
   * @return Menu
   */
  public static function Menu() {
    static $o = '';
    if (!$o) {
      $o = new Menu();
    }
    return $o;
  }

  /**
   *
   * @param  $config array of field configuration;
   * @return ReportFields
   */
  public static function Fields($config = array()) {
    $o = new ReportFields($config);
    return $o;
  }

  /**
   *
   * Enter description here ...
   * @param unknown_type $type
   * @return DocumentTypeBase
   */
  public static function Document($type = 'web') {
    static $doc_types = '';
    static $objects = '';

    // Invoke doc_type hook to see which document types are there.
    if (!$doc_types) {
      $doc_types = module_invoke_all('forena_document_types');
    }
    if (!$type) {
      $type = 'web';
    }
    if (isset($doc_types[$type]) && class_exists(@$doc_types[$type]['class'])) {
      if (!@$doc_types[$type]['object']) {
        $class = $doc_types[$type]['class'];
        $o = new $class();
        $o->format = $type;
        $doc_types[$type]['object'] = $o;
      }
      else {
        $o = $doc_types[$type]['object'];
      }
      return $o;
    }
    else {
      drupal_set_message(t('Unsupported document type: %s', array(
        '%s' => $type,
      )), 'error');
      drupal_not_found();
      exit;
    }
  }

  /**
   * Returns list of document types supported.
   * @param unknown_type $all
   */
  public static function documentTypes($all = FALSE) {
    static $supported_doctypes = '';
    if (!$supported_doctypes || $all) {
      $doc_types = module_invoke_all('forena_document_types');
      if (!$all) {
        unset($doc_types['web']);
      }
      $supported_doctypes = array();
      foreach ($doc_types as $type => $doc) {
        $supported_doctypes[$type] = $type;
      }
    }
    return $supported_doctypes;
  }

  /**
   * Retreive the classes from other modules.
   * @param string $class
   * @return Ambigous <string, unknown>
   */
  public static function Controls($class = '') {
    static $instances = '';
    if (!$instances) {
      foreach (module_list() as $module) {
        $function = $module . '_forena_controls';
        if (function_exists($function)) {
          $returned_controls = $function();
          if ($returned_controls) {
            foreach ((array) $returned_controls as $k => $c) {
              $c['module'] = $module;
              if (isset($c['file'])) {
                $c['file'] = drupal_get_path('module', $c['module']) . '/' . trim($c['file'], '/');
                include_once $c['file'];
              }
              if (class_exists($c['class'])) {
                $instances[$k] = new $c['class']();
              }
            }
          }
        }
      }
    }

    // Return class if we asked for one.
    if ($class) {
      return @$instances[$class];
    }
    return $instances;
  }

  /**
   * Returns an object of the template class
   * that has a method named templates.
   * @return RendererBase
   */
  public static function Template($class) {
    return Frx::Controls($class);
  }

  /**
   * Forena Repository manager
   * Class factory
   * @return DataManager
   */
  public static function DataManager() {
    static $o = '';
    if (!$o) {
      $o = new DataManager();
    }
    return $o;
  }

  /**
   * General wrapper procedure for reporting erros
   *
   * @param string $short_message Message that will be displayed to the users
   * @param string $log Message that will be recorded in the logs.
   */
  public static function error($short_message = '', $log = '') {
    if ($short_message) {
      drupal_set_message(check_markup($short_message), 'error', FALSE);
    }
    if ($log) {
      watchdog('forena', $log, NULL, WATCHDOG_ERROR);
    }
  }

  /**
   * Debug handler
   * Enter description here ...
   * @param unknown_type $short_message
   * @param unknown_type $log
   */
  public static function debug($short_message = '', $log = '') {
    if ($log) {
      watchdog('forena debug', $log, NULL);
    }
    if ($short_message) {
      drupal_set_message(check_markup($short_message));
    }
  }

  /**
   * Factory for token replacement syntax engine object
   * @param $regexp string regular expression for finding tokens
   * @param $trim string to trim off the end of tokens.
   * @return TokenReplacerBase
   */
  public static function SyntaxEngine($regexp, $trim) {
    return new TokenReplacerBase($regexp, $trim);
  }

  /**
   * Factory method to return instance object
   * @param unknown_type $parent
   * @param unknown_type $key
   * @return object
   */
  public static function getDriver($parent, $key) {
    static $objects = '';
    $o = NULL;
    if (!$objects) {
      $objects = array();
    }
    $plugins = Frx::drivers($parent);
    if (isset($plugins[$key])) {
      $class = @$plugins[$key]['class'];
      if (!isset($objects[$class])) {
        if (class_exists($class)) {
          $objects[$class] = new $class();
        }
      }
      $o = $objects[$class];
    }
    return $o;
  }

  /**
   * Factory object to get the context based on a name.  IF the context doesn't
   * exist we assume the default data context (FrxData).
   * @param $context string
   * @return FrxContext
   */
  public static function Context($context) {
    $o = NULL;

    // Instantiate the class if it exists
    if ($context) {
      $o = Frx::getDriver('FrxContext', $context);
    }

    // Otherwise instantiate the data class.
    if (!$o) {
      $o = Frx::Data();
    }
    return $o;
  }

  /**
   * Loads all of the include files that
   */
  public static function drivers($parent = '') {
    static $drivers = '';
    if (!$drivers) {
      $drivers = array();
      foreach (module_list() as $module) {
        $function = $module . '_forena_plugins';
        if (function_exists($function)) {
          $returned_plugins = $function();
          if ($returned_plugins) {
            foreach ((array) $returned_plugins as $key => $p) {
              $p['module'] = $module;
              if (isset($p['file'])) {
                if (@$p['path']) {
                  $p['file'] = rtrim($p['path'], '/') . '/' . $p['file'];
                }
                else {
                  $p['file'] = drupal_get_path('module', $p['module']) . '/' . $p['file'];
                }
              }
              if (is_int($key)) {
                $drivers[] = $p;
              }
              else {
                $drivers[$key] = $p;
              }
            }
          }
        }
      }
      foreach ($drivers as $p) {
        if (isset($p['file'])) {
          include_once trim($p['file'], '/');
        }
      }
    }

    // Return the plugins if a parent was requested.
    $ret_plugins = array();
    if ($parent) {
      foreach ($drivers as $key => $p) {
        if (@$p[parent] == $parent) {
          $ret_plugins[$key] = $p;
        }
      }
    }
    return $ret_plugins;
  }

  /**
   *
   * @param  $report_name
   * @return ReportEditor <string, ReportEditor>
   */
  public static function Editor($report_name = '', $edit = TRUE) {
    static $o = '';
    if (!$o || !$edit) {
      $o = new ReportEditor($report_name, $edit);
    }
    else {
      if ($report_name) {
        $o
          ->load($report_name, $edit);
      }
    }
    return $o;
  }

  /**
   *
   * @param $block_name
   * @return BlockEditor
   */
  public static function BlockEditor($block_name = '', $edit = TRUE) {
    static $o = '';
    if (!$o) {
      $o = new BlockEditor($block_name, $edit);
    }
    else {
      if ($block_name) {
        $o
          ->load($block_name, $edit);
      }
    }
    return $o;
  }
  static function supportedFormats() {
    $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;
  }

  /**
   * Load the formatters for all initialized repositories.
   *
   */
  public static function formatter($fkey) {

    // Get all repositories
    $repos = Frx::DataManager()->repositories;
    $formats = array();
    foreach ($repos as $k => $r) {
      $provider = isset($r['data']) ? $r['data'] : NULL;
      if ($provider && method_exists($provider, 'formats')) {
        $f = $provider
          ->formats();
        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();
        if (isset($f[$fkey]) && method_exists($provider, $fkey)) {

          // We found an object with the advertised method return it
          return $provider;
        }
      }
    }
    return $formats;
  }
  public static function format($value, $format, $format_str, $teng = '') {
    $fo = Frx::formatter($format);
    if ($fo) {
      $ret = $fo
        ->{$format}($value, $format_str, $teng);
    }
    else {
      $ret = $value;
    }
    return $ret;
  }

}

Constants

Namesort descending Description
FRX_SQL_REWRITE_EXP
FRX_SQL_TOKEN
FRX_TOKEN_EXP @file Frx.incL General Forena Reporting Class

Classes

Namesort descending Description
Frx