You are here

class ReportReplacer in Forena Reports 8

Same name and namespace in other branches
  1. 7.5 src/Token/ReportReplacer.php \Drupal\forena\Token\ReportReplacer

Hierarchy

Expanded class hierarchy of ReportReplacer

6 files declare their use of ReportReplacer
AjaxCommandBase.php in src/FrxPlugin/AjaxCommand/AjaxCommandBase.php
AReportTokenTest.php in tests/src/Unit/AReportTokenTest.php
FieldFormatterTest.php in tests/src/Unit/FieldFormatterTest.php
Formatter.php in src/FrxPlugin/FieldFormatter/Formatter.php
contains various methods for extending report formating, layout, transformation and design.
Report.php in src/Report.php
Basic report provider. Controls the rendering of the report.

... See full list

File

src/Token/ReportReplacer.php, line 15

Namespace

Drupal\forena\Token
View source
class ReportReplacer extends TokenReplacerBase {
  public $fields = [];
  public $format_callbacks = [];
  public $formats = [];

  // Build the token replacer and set the base formatter.
  public function __construct() {
    parent::__construct('/\\{[^\\n^\\r^}]+}/', '{}');
    $this
      ->setFormatter($this);
    $formatters = AppService::instance()
      ->getFormatterPlugins();
    foreach ($formatters as $class) {

      /** @var FormatterInterface $formatter */
      $formatter = new $class();
      $formats = $formatter
        ->formats();
      foreach ($formats as $method => $label) {
        $this->format_callbacks[$method] = array(
          $formatter,
          $method,
        );
        $this->formats[$method] = $label;
      }
    }
  }

  /**
   * Replaces nested array data.
   * @param $data
   *   The array containing values to replace.
   * @param $raw
   *   TRUE implies no formatting of data.
   */
  public function replaceNested(&$data, $raw = TRUE) {
    if (is_array($data)) {
      $values = $data;
      foreach ($values as $k => $value) {

        // Replace key data
        $key = $k;
        if (strpos($k, '{') !== FALSE) {
          $key = $this
            ->replace($key);
          unset($data[$k]);
          $data[$key] = $value;
        }

        // Replace value data.
        if (is_array($value)) {
          $this
            ->replaceNested($data[$key], $raw);
        }
        else {
          $data[$key] = $this
            ->replace($value, $raw);
        }
      }
    }
    else {
      $data = $this
        ->replace($data, $raw);
    }
  }

  /**
   * Get the value from the data.
   * This is used by token_replace method to extract the data based on the path provided.
   * @param $data
   * @param $key
   * @return string|array
   */
  protected function get_value($key, $raw = FALSE) {
    $context = '';
    $raw_key = $key;
    $dataSvc = $this
      ->dataService();
    if ($key && strpos($key, '.')) {
      @(list($context, $key) = explode('.', $key, 2));
      $o = $this
        ->getDataContext($context);
    }
    else {
      $o = $this
        ->currentDataContext();
    }
    $value = htmlentities($dataSvc
      ->getValue($key, $context));
    if ($this->formatter) {
      $value = $this->formatter
        ->format($value, $raw_key, $raw);
    }
    return $value;
  }

  /**
   * Define the fields associated with the formatter.
   * @param string $key
   *   Field replacement id
   * @param $field
   *   Field definition.
   */
  public function defineField($key, $field) {
    $def = [];

    //Make sure attribute names don't have -
    foreach ($field as $attr => $value) {
      $def[$attr] = $value;
    }
    $this->fields[$key] = $def;
  }

  /*
   * Formatter used by the syntax engine to alter data that gets extracted.
   * This invokes the field translation
   */
  public function format($value, $key, $raw = FALSE) {

    // Determine if there is a field overide entry
    $default = '';
    $link = '';
    $format = '';
    $format_string = '';
    $calc = '';
    $context = '';
    $field = [];
    if (isset($this->fields[$key])) {
      $field = $this->fields[$key];
      extract($field);
      if (isset($field['format-string'])) {
        $format_string = $field['format-string'];
      }
    }

    // Evaluate any calculations first.
    if ($calc) {
      if ($context) {
        $context .= ".";
      }
      $calc = $this
        ->replace($calc, TRUE);
      if ($calc) {
        $value = $this
          ->replace('{' . $context . '=' . $calc . '}', TRUE);
      }
    }

    //@TODO: Figure out how to deal with formatters.
    if ($format && !$raw) {
      if (!empty($this->format_callbacks[$format])) {
        $value = call_user_func($this->format_callbacks[$format], $value, $format_string, $this, $default);
      }
      $value = trim($value);
    }
    if (is_array($value) && !$raw) {
      $value = implode(' ', $value);
    }

    // Default if specified
    if (!$value && $default) {
      $value = $default;
    }
    if ($link && !$raw) {
      $link_fields = array_merge(array_fill_keys([
        'link',
        'target',
        'rel',
        'class',
        'add_query',
      ], ''), $field);
      $this
        ->replaceNested($link_fields, TRUE);
      $value = AppService::instance()
        ->reportLink($value, $link_fields);
    }
    return $value;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FrxAPI::app public function Returns containing application service
FrxAPI::currentDataContext public function Get the current data context.
FrxAPI::currentDataContextArray public function
FrxAPI::dataManager public function Returns the data manager service
FrxAPI::dataService public function Return Data Service
FrxAPI::documentManager public function Returns the fornea document manager
FrxAPI::error public function Report an error
FrxAPI::getDataContext public function Get the context of a specific id.
FrxAPI::getDocument public function Get the current document
FrxAPI::getReportFileContents public function Load the contents of a file in the report file system.
FrxAPI::innerXML function Enter description here... 1
FrxAPI::popData public function Pop data off of the stack.
FrxAPI::pushData public function Push data onto the Stack
FrxAPI::report public function Run a report with a particular format. 1
FrxAPI::reportFileSystem public function Get the current report file system.
FrxAPI::setDataContext public function Set Data context by id.
FrxAPI::setDocument public function Change to a specific document type.
FrxAPI::skins public function Get list of skins.
ReportReplacer::$fields public property
ReportReplacer::$formats public property
ReportReplacer::$format_callbacks public property
ReportReplacer::defineField public function Define the fields associated with the formatter.
ReportReplacer::format public function
ReportReplacer::get_value protected function Get the value from the data. This is used by token_replace method to extract the data based on the path provided. Overrides TokenReplacerBase::get_value
ReportReplacer::replaceNested public function Replaces nested array data.
ReportReplacer::__construct public function Overrides TokenReplacerBase::__construct
TokenReplacerBase::$formatter protected property
TokenReplacerBase::$tpattern private property
TokenReplacerBase::$trim_chars protected property
TokenReplacerBase::object_to_array public function Convert an object into an array
TokenReplacerBase::replace public function Replace the text in a report. Overrides TokenReplacerInterface::replace
TokenReplacerBase::setFormatter public function
TokenReplacerBase::test public function Test for TRUE/FALSE for conditions that are able to be represented using bind parameters Note that & are used to separate the different conditions and these are to be OR'd together. Overrides TokenReplacerInterface::test
TokenReplacerBase::tokens public function List all of the tokens used in a piece of text, ignoring duplicates. Overrides TokenReplacerInterface::tokens