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

class LibConfigurable
{
  protected $configDir;

  
  protected function sortConfigFiles($pFiles)
  {
    $cnt = count($pFiles);
    for ($i = 0; $i < $cnt; $i++) {
      for ($j = 0; $j < ($cnt - 1); $j++) {
        $first = $pFiles[$j];
        $second = $pFiles[$j + 1];
        
        if ($first->sortId == $second->sortId) {
          if ($first->fileName > $second->fileName) {
            $pFiles[$j] = $second;
            $pFiles[$j + 1] = $first;
          }
          
        } elseif ($first->sortId > $second->sortId) {
          $pFiles[$j] = $second;
          $pFiles[$j + 1] = $first;
        }
      }
    }

    return $pFiles;
  }

  
  protected function getConfigFile($pFileName)
  {
    $cf = new ConfigFile();
    $str = StringOperations::substr($this->configDir, 0, strrpos($this->configDir, '/'));
    $cf->folderName = StringOperations::substr($str, strrpos($str, '/') + 1);
    $cf->fileName = $pFileName;
    $cf->absoluteFileName = $this->configDir . $cf->fileName . '.php';
    include $cf->absoluteFileName;

    if (isset($longName)) {
      $cf->longName = $longName;
      unset($longName);
    } else {
      $cf->longName = str_replace('_', ' ', $cf->fileName);
      $cf->longName = \StringOperations::ucwords($cf->longName);
      $cf->longName = str_replace(' ', '/', $cf->longName);
      $cf->longName = str_replace('ue', '&uuml;', $cf->longName);
      $cf->longName = str_replace('ae', '&auml;', $cf->longName);
      $cf->longName = str_replace('oe', '&ouml;', $cf->longName);
    }

    if (isset($tool_id)) {
      $cf->toolId = $tool_id;
    }

    if (isset($sortId)) {
      $cf->sortId = $sortId;
      unset($sortId);
    }

    if (isset($settingsArray) && is_array($settingsArray)) {
      $cf->settingsCount = count($settingsArray);
    }

    if (isset($sightsArray) && is_array($sightsArray)) {
      $cf->sightsCount = count($sightsArray);
    }

    return $cf;
  }

  
  public function getConfigFiles()
  {
    $files = array();
    $d = dir($this->configDir);
    while (false !== ($entry = $d->read())) {
      if ($entry != '.' && $entry != '..') {
        $matches = array();
        preg_match('/(\\S*)\\.php/', $entry, $matches);
        if (isset($matches[1]) && (trim($matches[1]) != '')) {
          $cf = $this->getConfigFile($matches[1]);
          if ($cf->settingsCount > 0 || $cf->sightsCount > 0) {
            array_push($files, $cf);
          }
        }
      }
    }

    return $this->sortConfigFiles($files);
  }

  
  public function getAllConfigFilesRecursive($pRestrictFoldersTo = array(), $pDir = '')
  {
    
    $tmpDir = $this->configDir;
    $configFiles = array();
    $pDir == '' ?: $this->configDir = $pDir . '/';
    $dir = dir($this->configDir);
    while (false !== ($file = $dir->read())) {
      $pathToFile = $this->configDir . $file;
      if ($file != '.' && $file != '..') {
        $matches = array();
        if (preg_match('/(\\S*)\\.php/', $file, $matches) === 0) {
          if (is_dir($pathToFile) && (empty($pRestrictFoldersTo) || in_array($file, $pRestrictFoldersTo))) {
            $configFiles = array_merge($configFiles, $this->getAllConfigFilesRecursive($pRestrictFoldersTo, $pathToFile));
          }
        } else {
          if (isset($matches[1]) && (trim($matches[1]) != '')) {
            $configFile = $this->getConfigFile($matches[1]);
            array_push($configFiles, $configFile);
          }
        }
      }
    }
    
    $this->configDir = $tmpDir;

    return $configFiles;
  }
}

class ConfigFile
{
  public $fileName;
  public $absoluteFileName;
  public $longName;
  public $sortId = 50;
  public $folderName;
  public $settingsObjects;
  public $toolId = 0;
  public $settingsCount = 0;
  public $sightsCount = 0;
}