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


class DBWorkStichwort extends DBWork
{
  public function __construct($pDBTyp = 1, $pNewInstance = false)
  {
    parent::__construct($pDBTyp, $pNewInstance);
    $this->select = 'st.id, st.knr, st.stichwort, st.info';
  }

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

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

  
  public function getCurrentRow($pRefreshCache = false)
  {
    $tmp = $this->getNewObject();
    $tmp->id = $this->getDatamodulValue('id');
    $tmp->knr = $this->getDatamodulValue('knr');
    $tmp->stichwort = $this->getDatamodulValue('stichwort');
    $tmp->info = $this->getDatamodulValue('info');

    return $tmp;
  }

  
  public function getStichworte4Kurs($pKnr)
  {
    $knr = $this->dboperation->getString4Sql($pKnr);
    $sql = 'SELECT ' . $this->select . ' FROM stichwor st'
           . ' WHERE  st.knr = \'' . $knr . '\' ORDER BY st.knr, st.id';
    $this->dboperation->clear();
    $this->dboperation->query($sql);

    return $this->getAllRows();
  }

  
  public function getStichwort($pSwId)
  {
    $swId = $this->dboperation->getInt4Sql($pSwId);
    $sql = 'SELECT ' . $this->select . ' FROM stichwor st'
    
           . ' WHERE  st.id = \'' . $swId . '\' ORDER BY st.knr, st.id';
    $this->dboperation->clear();
    $this->dboperation->query($sql);

    return $this->getFirstRow();
  }

  
  public function getStichwAsString($pSeparator, $pKnr)
  {
    $knr = $this->dboperation->getString4Sql($pKnr);
    $sql = 'SELECT ' . $this->select . ' FROM stichwor st'
    
           . ' WHERE  st.knr = \'' . $knr . '\' ORDER BY st.knr, st.id';
    $this->dboperation->clear();
    $this->dboperation->query($sql);
    $stw_ary = $this->getAllRows();
    $retstr = '';
    $stw_len = count($stw_ary);
    for ($i = 0;$i < $stw_len;$i++) {
      $retstr .= $stw_ary[$i]->stichwort;
      if ($i < $stw_len - 1) {
        $retstr .= $pSeparator . ' ';
      }
    }

    return $retstr;
  }

  
  public function getStichwListbyKnr($pKnr)
  {
    $knr = $this->dboperation->getString4Sql($pKnr);
    $sql = 'SELECT ' . $this->select . ' FROM stichwor st '
           . ' WHERE  st.knr = \'' . $knr . '\' ORDER BY st.knr, st.id';
    $this->dboperation->clear();
    $this->dboperation->query($sql);
    $ary = array();
    $rows = $this->dboperation->rowcount();
    if ($rows > 0) {
      for ($i = 0; $i < $rows; $i++) {
        $this->dboperation->selectrow($i);
        $tmp = $this->getCurrentRow();
        if (isset($tmp)) {
          if (!array_key_exists($tmp->stichwort, $ary)) {
            $ary[$tmp->stichwort] = $tmp->info;
          }
        }
      }
    }

    return $ary;
  }

  
  public function getStichworteForKurse($pSemlist, $pLetter = '')
  {
    $sql = 'SELECT distinct(stichwort) ' . $this->getSearchSQLForStichworteForKurse($pSemlist, $pLetter);
    $sql .= ' ORDER BY st.stichwort asc';
    $this->dboperation->clear();
    $this->dboperation->query($sql);
    $stichwortAry = array();
    $rowcount = $this->dboperation->rowcount();
    for ($i = 0; $i < $rowcount; $i++) {
      $this->dboperation->selectrow($i);
      $dm = new Datamodul();
      $dm->id = $this->getDatamodulValue('stichwort');
      $stichwortAry[] = $dm;
    }

    return $stichwortAry;
  }

  
  public function getStichworteCountForKurse($pSemlist, $pLetter = '')
  {
    $sql = 'SELECT count(distinct(stichwort)) as anzahl ' . $this->getSearchSQLForStichworteForKurse($pSemlist, $pLetter);
    $this->dboperation->clear();
    $this->dboperation->query($sql);
    $this->dboperation->selectrow(0);

    return $this->getDatamodulValue('anzahl');
  }

  
  public function getSearchSQLForStichworteForKurse($pSemlist, $pLetter = '')
  {
    $sql = ' FROM stichwor st join kurse k on st.knr = k.nr WHERE ';
    $sql .= $this->datetimeop->getGueltigPart('k.', 'internetvon', 'internetbis');
    $sql .= ' AND ' . DBWorkKurseBase::getSQLInternetIntranetAktiv('k.');

    if ($pSemlist != '') {
      $sql .= ' AND k.semester in ('
              . $this->stringop->convertCommaSeparatedList2InList($pSemlist) . ') ';
    }
    if ($pLetter != '') {
      $sql .= " AND st.stichwort like '$pLetter%' ";
    }

    return $sql;
  }

  
  public function getGetLetterList($pLetterAry = array(), $pSemlist = '')
  {
    $sql = 'SELECT ' . $this->dboperation->dbparser->substring('wort', 1, 1) . ' AS buchstabe, count(*) AS anzahl '
      . 'FROM (SELECT stichwort as wort ' . $this->getSearchSQLForStichworteForKurse($pSemlist) . ' GROUP BY stichwort) y '
      . 'GROUP BY ' . $this->dboperation->dbparser->substring('wort', 1, 1);
    $this->dboperation->clear();
    $this->dboperation->query($sql);

    
    $letterAry = $pLetterAry;

    for ($i = 0; $i < $this->dboperation->rowcount(); $i++) {
      $this->dboperation->selectrow($i);
      $letter = StringOperations::strtoupper($this->getDatamodulValue('buchstabe'));
      $count = $this->getDatamodulValue('anzahl');
      $letterAry[$letter] = $count;
    }
    
    $dmArray = array();
    ksort($letterAry, SORT_STRING);
    foreach ($letterAry as $letter => $count) {
      $dm = new Buchstabe();
      $dm->buchstabe = $letter;
      $dm->anzahl_datensaetze = $count;
      array_push($dmArray, $dm);
    }

    return $dmArray;
  }

  
  public function getStichworteGruppiert($pLetter, $pSemlist)
  {
    $sql = 'SELECT distinct(st.stichwort) ' . $this->getSearchSQLForStichworteForKurse($pSemlist, $pLetter) . ' ORDER BY st.stichwort';
    $this->dboperation->clear();
    $this->dboperation->query($sql);
    $stichwortAry = array();
    $rowcount = $this->dboperation->rowcount();
    for ($i = 0; $i < $rowcount; $i++) {
      $this->dboperation->selectrow($i);
      $dm = new Datamodul();
      $dm->id = $this->getDatamodulValue('stichwort');
      $stichwortAry[] = $dm;
    }

    return $stichwortAry;
  }
}


class DBWorkHLStichwort extends DBWork
{
  public function __construct($pDBTyp = 1, $pNewInstance = false)
  {
    parent::__construct($pDBTyp, $pNewInstance);
    $this->select = 'id, hl_stichw, cloud_anzeige, cloud_prio, cloud_groesse';
  }

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

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

  
  public function getCurrentRow($pRefreshCache = false)
  {
    $tmp = $this->getNewObject();
    $tmp->id = $this->getDatamodulValue('id');
    $tmp->stichwort = $this->getDatamodulValue('hl_stichw');
    $tmp->cloud_anzeige = $this->getDatamodulValue('cloud_anzeige');
    $tmp->cloud_prio = $this->getDatamodulValue('cloud_prio');
    $tmp->cloud_groesse = $this->getDatamodulValue('cloud_groesse');

    return $tmp;
  }

  
  public function getStichworteHL()
  {
    $this->dboperation->clear();
    $this->dboperation->query('SELECT ' . $this->select . ' FROM hlstichw ORDER BY id');

    return $this->getAllRows();
  }

  
  public function getStichworteHLCloud()
  {
    $this->dboperation->clear();
    $this->dboperation->query('SELECT ' . $this->select . " FROM hlstichw WHERE cloud_anzeige LIKE 'W' ORDER BY id");

    return $this->getAllRows();
  }
}