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


class DBWorkHLSemester extends DBWorkHLBasis
{
  
  public function getNewObject()
  {
    return new Semester();
  }

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

  
  public function getCurrentRow($pRefreshCache = false)
  {
    $tmp = $this->getNewObject();

    $tmp->id = $this->getDatamodulValue('id');
    $tmp->sem = $this->getDatamodulValue('sem');
    $tmp->von = $this->getDatamodulValue('von');
    $tmp->bis = $this->getDatamodulValue('bis');

    return $tmp;
  }

  
  public function getAll()
  {
    $this->dboperation->clear();
    $this->dboperation->query('SELECT id, sem, von, bis FROM hlsemester ORDER BY von, bis, id ASC');

    return $this->getAllRows();
  }

  
  public function getSemesterBySemAry($pSemAry, $pMitZukuenftigeSemester = false)
  {
    $semAry = $this->dboperation->getStrings4Sql($pSemAry);
    $semList = implode('\', \'', $semAry);

    $sql = 'SELECT id, sem, von, bis '
             . 'FROM hlsemester ';

    if (!$pMitZukuenftigeSemester) {
      $sql .= 'WHERE sem IN (\'' . $semList . '\') ';
    } else {
      $sql .= 'WHERE von >= '
              . '( '
                . 'SELECT MIN(von) '
                . 'FROM hlsemester '
                . 'WHERE sem IN (\'' . $semList . '\') '
              . ')';
    }
    $sql .= ' ORDER BY von, bis, id ASC';

    $this->dboperation->clear();
    $this->dboperation->query($sql);

    return $this->getAllRows();
  }

  
  public function getOne($pId)
  {
    $id = $this->dboperation->getInt4Sql($pId);
    $this->dboperation->clear();
    $this->dboperation->query('SELECT id, sem, von, bis FROM hlsemester WHERE id = "' . $id . '"');

    return $this->getFirstRow();
  }

  
  public function getZeitraum($pSemesterString)
  {
    $result = array();
    $semString = $this->dboperation->getString4Sql($pSemesterString);
    $sql = 'select min(von) as von, max(bis) as bis from hlsemester where sem in (' . StringOperations::convertCommaSeparatedList2InList($semString) . ')';
    $this->dboperation->clear();
    $this->dboperation->query($sql);
    $this->dboperation->selectrow(0);
    $result['von'] = $this->getDatamodulValue('von');
    $result['bis'] = $this->getDatamodulValue('bis');

    return $result;
  }

  
  public function getSemesterByDate($pDate)
  {
    $this->dboperation->clear();
    $date = $this->datetimeop->getFormatDate4Server($pDate);
    $sql = 'SELECT id, sem, von, bis FROM hlsemester where von <= ' . $date . ' and bis >= ' . $date
           . ' ORDER BY von, bis, id ASC ';
    $this->dboperation->query($sql);

    if ($this->dboperation->rowcount() > 0) {
      return $this->getFirstRow();
    }

    return false;
  }

  
  public function getAllSemesterSinceDate($pDate)
  {
    $sql = 'SELECT id, sem, von, bis '
           . 'FROM hlsemester '
           . 'WHERE bis >= ' . $this->datetimeop->getFormatDate4Server($pDate)
           . ' ORDER BY von, bis, id ASC';

    $this->dboperation->clear();
    $this->dboperation->query($sql);

    return $this->getAllRows();
  }

  public function getSemesterAfterDate($pDate)
  {
    $sql = 'SELECT id, sem, von, bis '
           . 'FROM hlsemester '
           . 'WHERE von > ' . $this->datetimeop->getFormatDate4Server($pDate)
           . ' ORDER BY von, bis, id ASC';

    $this->dboperation->clear();
    $this->dboperation->query($sql);

    return $this->getAllRows();
  }

  public function getSemesterBeforeDate($pDate)
  {
    $sql = 'SELECT id, sem, von, bis '
           . 'FROM hlsemester '
           . 'WHERE von < ' . $this->datetimeop->getFormatDate4Server($pDate)
           . ' ORDER BY von, bis, id ASC';

    $this->dboperation->clear();
    $this->dboperation->query($sql);

    return $this->getAllRows();
  }

  public function getSemesterNach($pSem)
  {
    $brSem = $this->getSemesterBySemAry(array($pSem));
    $semesterAfter = $this->getSemesterAfterDate($brSem[0]->von);

    return ArrayOperations::mapAttribut($semesterAfter, 'sem');
  }

  public function getSemesterVor($pSem)
  {
    $brSem = $this->getSemesterBySemAry(array($pSem));
    $semesterBefore = $this->getSemesterBeforeDate($brSem[0]->von);

    return ArrayOperations::mapAttribut($semesterBefore, 'sem');
  }
}