<?php
/**
 * @copyright (c) Kufer Software GmbH, Oderstraße 7, D-84453 Mühldorf
 */

namespace Kufer\Web\Classes\Addfunctions;

class PflichtfelderOperations
{
  private function __construct()
  {
  }

  
  public static function getPflichtFelder(string $pPrefix = '', string $pVorlage = '', array $nonIfVarnames = array(), $pVarnamenMapping = null, array $pKnrAry = array()): array
  {
    if (trim($pVorlage ?? '') === '' && empty($nonIfVarnames)) {
      return array();
    }

    if (is_null($pVarnamenMapping)) {
      $pVarnamenMapping = \ConstantOperations::getVarnamenMapping();
    }
    $prefix = ($pPrefix != '') ? $pPrefix : '';
    $prefixLength = \StringOperations::strlen($prefix);
    $pflichtfelderAusVariablen = array();
    $pattern = '/' . $prefix . '_[a-zA-Z0-9_\\-{}%]*_pflichtfeld/';
    $replaceKursId = static function($pMatch) { return str_replace('_{%k_id%}', '', $pMatch); };
    foreach ($nonIfVarnames as $varname) {
      if (preg_match($pattern, $varname, $matches) === 1) {
        $pflichtfelderAusVariablen[] = $replaceKursId($matches[0]);
      }
    }

    $matches = array();
    
    if (preg_match_all($pattern, $pVorlage, $matches) > 0) {
      $pflichtfelderAusVariablen = array_merge($pflichtfelderAusVariablen, array_map($replaceKursId, array_values($matches[0])));
    }

    $matches = array();
    
    $pattern = '/name=[\'"][a-zA-Z0-9_]*(' . $prefix . '_[a-zA-Z0-9_\\-]*_pflichtfeld)[\'"] .* value=[\'"][^\'"]*[\'"]/';
    if (preg_match_all($pattern, $pVorlage, $matches)) {
      
      $pflichtfelderAusVariablen = array_merge($pflichtfelderAusVariablen, $matches[1]);
    }
    if (count($pflichtfelderAusVariablen) == 0) {
      return array();
    }
    
    $pflichtfelderAusVariablen = array_unique($pflichtfelderAusVariablen);
    $pflichtfeldVariablennamen = array();
    
    foreach ($pflichtfelderAusVariablen as $pflichtfeld) {
      $varname = \StringOperations::substr($pflichtfeld, 1 + $prefixLength, -12);
      if (isset($pVarnamenMapping[$varname])) {
        $pflichtfeldVariablennamen[$varname] = $pVarnamenMapping[$varname];
      } else {
        
        
        
        

        
        $varnameorig = $varname;
        if (strpos($varname, 'geschlecht') === 0) {
          $varname = 'geschlecht';
        }
        $pflichtfeldVariablennamen[$varnameorig] = $varname;
      }
    }

    
    
    
    if (count($pKnrAry) > 0) {
      $tmpAry = unserialize(serialize($pflichtfeldVariablennamen));
      $pflichtfeldVariablennamen = array();
      foreach ($tmpAry as $key => $value) {
        $pflichtfeldVariablennamen[$key] = array();
        foreach ($pKnrAry as $knr) {
          $tmpValue = str_replace('input_freifeld', 'beleghistorie', $value);
          $tmpValue .= '_' . $knr;
          $pflichtfeldVariablennamen[$key][] = $tmpValue;
        }
      }
    }

    return $pflichtfeldVariablennamen;
  }

  
  public static function getEditedVars(string $pPrefix = '', string $pVorlage = '', ?array $pVarnamenMapping = null): array
  {
    if ($pVorlage == '') {
      return array();
    }
    if (is_null($pVarnamenMapping)) {
      $pVarnamenMapping = \ConstantOperations::getVarnamenMapping();
    }

    $prefix = ($pPrefix != '') ? $pPrefix : '';
    $pattern = '/{%' . $prefix . '_(.*)(%}|:|_pflichtfeld)/U';

    $matches = array();
    
    preg_match_all($pattern, $pVorlage, $matches);
    $felder = array_values(array_unique(array_filter($matches[1])));

    $result = array();
    foreach ($felder as $feld) {
      if (isset($pVarnamenMapping[$feld])) {
        $result[] = $pVarnamenMapping[$feld];
      } else {
        
        $varnameorig = $feld;
        if (strpos($feld, 'geschlecht') === 0) {
          $feld = 'geschlecht';
        }
        $result[$varnameorig] = $feld;
      }
    }

    return $result;
  }

  
  public static function getPflichtfeldFehlerMeldung(string $pVarname, string $pVorlage, array $nonIfVarnames = array()): string
  {
    foreach ($nonIfVarnames as $varname) {
      if (preg_match('_' . $pVarname . '_pflichtfeld:(.*?)', $varname, $matches) === 1) {
        
        $part = explode(TRENNER_VORBELEGUNG, $matches[1]);
        $part = explode(TRENNER_AUSWAHL, $part[0]);
        
        
        return $part[0];
      }
    }

    
    
    $pattern = '/_' . $pVarname . '_pflichtfeld:(.*?)%}/';
    $matches = array();
    if (preg_match($pattern, $pVorlage, $matches)) {
      
      $part = explode(TRENNER_VORBELEGUNG, $matches[1]);
      $part = explode(TRENNER_AUSWAHL, $part[0]);
      
      
      return $part[0];
    }

    $pattern = '/_' . $pVarname . ':(.*?)%}/';
    $matches = array();
    if (preg_match($pattern, $pVorlage, $matches)) {
      
      $part = explode(TRENNER_VORBELEGUNG, $matches[1]);
      $part = explode(TRENNER_AUSWAHL, $part[0]);
      
      
      return $part[0];
    }

    
    $pattern = '/name=[\'"][a-zA-Z0-9_]*' . $pVarname
      . '(_{%k_id%}_pflichtfeld|_pflichtfeld)[\'"] .* ?value=[\'"]([^\'"]*)[\'"]/';
    if (preg_match($pattern, $pVorlage, $matches)) {
      
      $part = explode(TRENNER_VORBELEGUNG, $matches[2]);
      $part = explode(TRENNER_AUSWAHL, $part[0]);
      
      
      return urldecode($part[0]);
    }

    
    $pattern = '/<input.*? name=[\'"][a-zA-Z_]*' . preg_quote($pVarname, '/') . '[\'"] .*? placeholder=[\'"](.*?)[\'"]/';
    if (preg_match($pattern, $pVorlage, $matches)) {
      
      $part = explode(TRENNER_VORBELEGUNG, $matches[1]);
      $part = explode(TRENNER_AUSWAHL, $part[0]);
      
      
      return $part[0];
    }

    $part = explode(TRENNER_VORBELEGUNG, $pVarname);
    $part = explode(TRENNER_AUSWAHL, $part[0]);

    return $part[0];
  }
}