<?php

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


class BasisEvent
{
  
  public function onCheck($datamodul)
  {
    return true;
  }
}


class ColorEvent extends BasisEvent
{
  
  public $colors;
  
  public $contents;
  
  public $fieldname;
  
  public $datamodulname;

  
  public function __construct($colors, $contents, $fieldname, $datamodulname)
  {
    $this->colors = $this->getColorArry($colors);
    $this->contents = $contents;
    $this->fieldname = $fieldname;
    $this->datamodulname = $datamodulname;
  }

  
  private function getColorArry($pColors)
  {
    if (!empty($pColors)) {
      
      
      preg_match_all('/([^:= ]+)=([^:= ]+)|([^:= ]+)/', $pColors, $matches);

      
      if ($matches[1][0] != '') {
        $matchResult = array_combine($matches[1], $matches[2]);
        $resetedArryKeys = array_values($matchResult);
      }
      
      else {
        $resetedArryKeys = $matches[0];
      }

      return $resetedArryKeys;
    }
  }

  

  
  public function getTextColor($pDatamodul)
  {
    if (\is_null($this->colors)) {
      return true;
    }

    $datamodulValue = $pDatamodul->getVarValueEx($this->fieldname, $this->datamodulname);

    
    
    
    for ($k = 0; $k < StringOperations::strlen($this->contents); $k++) {
      $char = \StringOperations::substr($this->contents, $k, 1);
      if ($char === '-') {
        if ($datamodulValue == '' && isset($this->colors[$k])) {
          return $this->colors[$k];
        }
      } elseif ($char == $datamodulValue) {
        return $this->colors[$k];
      }
    }

    return true;
  }
}