<?php
/**
 * @copyright (c) Kufer Software GmbH, Loisachstrasse 4, D-84453 Mühldorf
 */


class WbPluginBasis
{
  
  private $settingsObject = false;

  
  private $settingsArray = array();

  
  private $curPlugin;

  
  private $settingsPath;

  
  public function __construct($pSettingsDir = false, $pPluginName = false)
  {
    if ($pPluginName) {
      $this->curPlugin = (string)$pPluginName;
    } else {
      $match = array();
      preg_match('/WbPlugin([A-Za-z_\\-]*)/', get_class($this), $match);
      $this->curPlugin = lcfirst($match[1]);
    }

    if ($pSettingsDir) {
      $this->settingsPath = WBPLUGIN_PATH . (string)$pSettingsDir . DIRECTORY_SEPARATOR . 'data.xml';
    } else {
      $this->settingsPath = PluginApi::getPluginDirByPId($this->curPlugin) . 'data.xml';
    }
  }

  
  public function getContent($pUrlParam)
  {
    return '';
  }

  public function getVarValue($pVarname, $pParams, $pDatamodul = null)
  {
    switch ($pVarname) {
      default:
        if ($pDatamodul != null) {
          return $pDatamodul->getVarValue($pVarname);
        } else {
          return 'Plugin Variable';
        }
      break;
    }
  }

  
  public function getSetting($pSettingName, $pDefaultValue = '')
  {
    return $this->getSettingByLfdNr(0, $pSettingName, $pDefaultValue);
  }

  
  public function getSettingByLfdNr($pIndex, $pSettingName, $pDefaultValue = '')
  {
    $data = $this->getSettings();
    if (key_exists($pIndex, $data) && key_exists($pSettingName, $data[$pIndex])) {
      return $this->settingsArray[$pIndex][$pSettingName];
    } else {
      return $pDefaultValue;
    }
  }

  
  public function getSettingById($pIndex, $pSettingName, $pDefaultValue = '')
  {
    if (!$this->settingsObject) {
      $this->getSettings();
    }

    $data = $this->settingsObject->xpath(sprintf('/data/element[@id="%s"]', $pIndex));
    if (($data = $this->settingsObject->xpath(sprintf('/data/element[@id="%s"]', $pIndex)))
          && (key_exists(0, $data)) && (isset($data[0]->{(string)$pSettingName}))) {
      return html_entity_decode((string)$data[0]->{(string)$pSettingName});
    } else {
      return $pDefaultValue;
    }
  }

  
  public function getSettings()
  {
    if (!$this->settingsObject && !$this->readSettings()) {
      Logger::logg(sprintf('Settings für das Plugin "%s" unter dem Pfad "%s"', $this->curPlugin, $this->settingsPath), 500);
    }

    return $this->settingsArray;
  }

  
  private function readSettings()
  {
    if ((!file_exists($this->settingsPath)) || (!$this->settingsObject = simplexml_load_file($this->settingsPath))) {
      return false;
    }

    $settingsArray = $this->simpleXmlToArray($this->settingsObject);
    $this->settingsArray = (key_exists('element', $settingsArray)) ? $settingsArray['element'] : array();

    foreach ($this->settingsArray as $key => $val) {
      if (!is_int($key)) {
        $tmp_setting = $this->settingsArray;
        unset($this->settingsArray);
        $this->settingsArray[] = $tmp_setting;
        break;
      }
    }


    return true;
  }

  
  private function simpleXmlToArray($pObject)
  {
    $out = array();

    foreach ((array)$pObject as $index => $node) {
      if ((string)$index !== '@attributes') {
        $out[$index] = ((is_object($node) || is_array($node)) && count($node) > 0) ? $this->simpleXmlToArray($node) : html_entity_decode($node);
      }
    }

    return $out;
  }

  
  public static function pluginExists($pPluginPath)
  {
    return file_exists(WBPLUGIN_PATH . $pPluginPath);
  }
}