You are here

class Skin in Forena Reports 8

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

Class Skin A skin is a collectio of css and js files that need to get used by an application or reports. Skins are idntified by .fri files contained in the report directory.

Skins can specify external JS Libraries as well as

Used to be called a "form"

Hierarchy

Expanded class hierarchy of Skin

3 files declare their use of Skin
DocumentBase.php in src/FrxPlugin/Document/DocumentBase.php
DocumentBase.inc Given a report, render the appropriate output given the document format. @author davidmetzler
ReportFileSystem.php in src/File/ReportFileSystem.php
SkinTest.php in tests/src/Unit/SkinTest.php
1 string reference to 'Skin'
forena_report_layout_form in ./forena.report.inc

File

src/Skin.php, line 23
Implements \Drupal\forena\Skins

Namespace

Drupal\forena
View source
class Skin {
  use FrxAPI;
  protected static $skins = [];
  public $name = '';
  public $library = [];
  public $stylesheets = [];
  public $scripts = [];
  public $dependencies = [];
  public $default_skin = '';
  public $info = [];
  protected $replacer;

  /**
   * Ojbect factory for Skins.
   * @param string $skin
   *   Name of skin to be loaded
   * @return \Drupal\forena\Skin
   *   Skin object factory.
   */
  public static function instance($skin) {
    if ($skin && !isset(static::$skins[$skin])) {
      static::$skins[$skin] = new Skin($skin);
    }
    return static::$skins[$skin];
  }

  /**
   * @param string $skin
   *   Load the skin
   */
  public function __construct($skin) {
    $this->replacer = new ReportReplacer();
    $this
      ->load($skin);
  }

  /**
   * Add CSS Files
   * @param $type
   * @param $sheet
   */
  public function addCSS($type, $sheet) {
    if (strpos($sheet, 'http:') === 0 || strpos($sheet, 'https:') === 0) {
      $this->stylesheets[$type][] = $sheet;
    }
    elseif (ReportFileSystem::instance()
      ->exists($sheet)) {
      $this->stylesheets[$type][] = ReportFileSystem::instance()
        ->path($sheet);
    }
    elseif (file_exists($sheet)) {
      $this->stylesheets[$type][] = $sheet;
    }
  }

  /**
   * Return Replaced skin definition.
   * @return array
   *   The token replaced skin definition
   */
  public function replacedInfo() {
    $info = $this->info;
    $this->replacer
      ->replaceNested($info);
    return $info;
  }

  /**
   * @param string $script
   *   The filename of the script to be added.
   */
  public function addJS($script) {
    if (strpos($script, 'http:') === 0 || strpos($script, 'https:') === 0) {
      $this->scripts[] = $script;
    }
    elseif ($this
      ->reportFileSystem()
      ->exists($script)) {
      $this->scripts[] = $this
        ->reportFileSystem()
        ->path($script);
    }
    elseif (file_exists('sites/all/libraries/' . $script)) {
      $this->scripts[] = 'sites/all/libraries/' . $script;
    }
    elseif (file_exists($script)) {
      $this->scripts[] = $script;
    }
  }

  /**
   * Load the skin based on the name.
   * @param string $name
   *   path/name of skin.
   * @return \Drupal\forena\Skin
   */
  public function load($name) {
    $fileSystem = $this
      ->reportFileSystem();
    $path_info = [];
    if (!$name) {
      $name = $this->default_skin;
    }
    if ($name) {

      //Check for an info file
      $this->info = [];
      if ($fileSystem
        ->exists($name . '.skin.yml')) {
        $this->info = Skin::parseYml($fileSystem
          ->contents($name . '.skin.yml'));
        $path_info = $fileSystem
          ->pathinfo($name . '.skin.yml');
      }

      // add and process sytlesheets
      if ($this->info) {
        $this->info['dir'] = '/' . $path_info['dirname'];
        $this
          ->dataService()
          ->setContext('skin', $this->info);
      }
    }

    // Replace tokens in css files based on paths.
    $this->library = [];
    if (isset($this->info['library'])) {
      $library = $this->info['library'];
      $new_library = $library;

      // Process CSS
      if (isset($library['css'])) {
        unset($new_library['css']);
        foreach ($library['css'] as $level => $files) {
          $new_library[$level] = [];
          foreach ($files as $file => $options) {
            $new_file = $this->replacer
              ->replace($file, TRUE);
            $new_library['css'][$level][$new_file] = $options;
          }
        }
      }

      // Process JS
      if (isset($library['js'])) {
        unset($new_library['js']);
        foreach ($library['js'] as $file => $options) {
          $new_file = $this->replacer
            ->replace($file, TRUE);
          $new_library['js'][$new_file] = $options;
        }
      }
      $this->library = $new_library;
    }
    return $this;
  }

  /**
   * Adds on report specific skin files to
   * @param string $name
   *   name of report to add.
   */
  public function loadSkinFiles($name) {
    $this
      ->addCSS('all', $name . '.css');
    foreach (AppService::instance()->doc_formats as $ext) {
      $this
        ->addCSS($ext, $name . '-' . $ext . '.css');
    }
    $this
      ->addJS($name . '.js');
  }

  /**
   * @param string $data Data to be parsed.
   * @return array
   *   Parsed YML data.
   */
  public static function parseYml($data) {
    $parser = new Parser();
    return $parser
      ->parse($data);
  }

  /**
   * @param string $data Data to be parsed
   */
  public static function parseJSON($data) {
    $parsed = json_decode($data, TRUE);
    return $parsed;
  }

  /**
   * Merge definitions
   * @param array $definition
   *   Skein definition to be merged.
   */
  public function merge($definition) {
    if ($definition) {
      foreach ($definition as $key => $value) {
        if (isset($this->info[$key])) {
          $this->info[$key] = array_merge($this->info[$key], $value);
        }
        else {
          $this->info[$key] = $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.
Skin::$default_skin public property
Skin::$dependencies public property
Skin::$info public property
Skin::$library public property
Skin::$name public property
Skin::$replacer protected property
Skin::$scripts public property
Skin::$skins protected static property
Skin::$stylesheets public property
Skin::addCSS public function Add CSS Files
Skin::addJS public function
Skin::instance public static function Ojbect factory for Skins.
Skin::load public function Load the skin based on the name.
Skin::loadSkinFiles public function Adds on report specific skin files to
Skin::merge public function Merge definitions
Skin::parseJSON public static function
Skin::parseYml public static function
Skin::replacedInfo public function Return Replaced skin definition.
Skin::__construct public function