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

if (!defined('BASEDIR')) {
  include_once '..' . DIRECTORY_SEPARATOR . 'constants.php';
}


class DBWorkTitel extends DBWorkGenerated
{
  public function __construct($pDBTyp = 1, $pNewInstance = false)
  {
    parent::__construct($pDBTyp, $pNewInstance);
    $this->select = 'id, name, ansprache, stellung';
    $this->_dataModulFields = array('name', 'ansprache', 'id', 'stellung');
    $this->orderby = 'name';
  }

  
  public function getNewObject()
  {
    return new Titel();
  }

  public function getTableName(): string
  {
    return 'titel';
  }

  protected function getAllQuery(): \SelectQueryBuilder
  {
    return $this->getSelectQueryBuilder()
      ->select($this->select)
      ->from($this->getTableName())
      ->orderByAsc('name');
  }

  
  public function getTitleNames(): array
  {
    $builder = $this->getAllQuery()
      ->select('name');

    $this->dboperation->clear();
    $this->dboperation->query($builder->getSQL());

    return $this->getAllRowsSingleColumn('name');
  }

  
  public function getTitles(): array
  {
    $builder = $this->getAllQuery();

    $this->dboperation->clear();
    $this->dboperation->query($builder->getSQL());

    return $this->getAllRows();
  }

  
  public function getTitlesBehind()
  {
    $sql = 'SELECT ' . $this->select . ' FROM titel WHERE ' . $this->dboperation->dbparser->isNull('stellung', 0)
            . '>0 ORDER BY ' . $this->dboperation->dbparser->length('name') . ' DESC';
    $this->dboperation->clear();
    $this->dboperation->query($sql);

    return $this->getAllRows();
  }

  
  public function getTitle4Doz($nameTitel, $pVorne = true, $pMitKomma = true)
  {
    $titlesBehind = $this->getTitlesBehind();

    return $this->getTitle4DozByTitles($titlesBehind, $nameTitel, $pVorne, $pMitKomma);
  }

  
  public function getTitle4DozByTitles($pTitlesBehind, $nameTitel, $pVorne = true, $pMitKomma = true)
  {
    $titlesBehind = $pTitlesBehind;
    $nameTitel = ' ' . $nameTitel . ' ';
    
    

    

    $front = $nameTitel;
    $behind = '';
    $withComma = false;
    foreach ($titlesBehind as $title) {
      if (StringOperations::strpos($nameTitel, ' ' . $title->name . ' ') !== false) { 
        $front = str_replace($title->name, ' ', $front);
        $behind .= ' ' . $title->name;
        if ($title->stellung == 2) {
          $withComma = true;
        }
      }
    }
    if ($pVorne) {
      $result = trim($front);
    } else {
      if ($pMitKomma) {
        $behind = trim($behind);
        if ($withComma) {
          $behind = ', ' . $behind;
        } elseif ($behind !== '') {
          $behind = ' ' . $behind;
        }
      }
      $result = $behind;
    }

    return str_replace('  ', ' ', $result);
  }

  
  public function fuegeTitelHinzu($name, $nameTitel)
  {
    static $titlesBehind = null;
    if (is_null($titlesBehind)) {
      $titlesBehind = $this->getTitlesBehind();
    }

    $result = $name;

    $vorne = $this->getTitle4DozByTitles($titlesBehind, $nameTitel, true);
    if (trim($vorne) !== '') {
      $result = trim($vorne) . ' ' . $result;
    }

    $hinten = $this->getTitle4DozByTitles($titlesBehind, $nameTitel, false);
    if (trim($hinten) !== '') {
      $result = $result . $hinten; 
    }

    return $result;
  }
}