<?php

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


class DBWorkFeedBackBogenFragen extends DBWorkGenerated
{
  
  private $dbwFeedBackFragen;

  public function __construct($pDBTyp = 1, $pNewInstance = false)
  {
    parent::__construct($pDBTyp, $pNewInstance);
    $this->select = 'id, bogenid, frageid, bereich, lfdnr, anzeigetext';

    $this->dbwFeedBackFragen = new DBWorkFeedBackFragen($pDBTyp, true);
  }

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

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

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

    $tmp->id = $this->getDatamodulValue('id');
    $tmp->bereich = $this->getDatamodulValue('bereich');
    $tmp->anzeigetext = $this->getDatamodulValue('anzeigetext');
    $tmp->bogenid = $this->getDatamodulValue('bogenid');
    $tmp->frageid = $this->getDatamodulValue('frageid');
    $tmp->lfdnr = $this->getDatamodulValue('lfdnr');

    
    if ($tmp->frageid > 0) {
      $tmp->feedbackfrage = $this->dbwFeedBackFragen->getFeedBackFrageById($tmp->frageid);
    } else {
      $tmp->feedbackfrage = new FeedBackFragen();
    }

    return $tmp;
  }

  public function canupdate($varname)
  {
    switch ($varname) {
      default: return parent::canupdate($varname);
      case 'feedbackfrage': return false;
    }
  }

  
  public function getFeedBackBogenFragenByBogenIDs($pBogenIDs)
  {
    if (empty($pBogenIDs)) {
      return array();
    }

    $bogenIDs = $this->dboperation->getInts4Sql($pBogenIDs);

    $sqlstr = 'SELECT ' . $this->select . ' FROM ' . $this->getTableName() . ' WHERE ' . $this->createInStatement('bogenid', $bogenIDs)
              . ' ORDER BY lfdnr ASC';

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

    return $this->getAllRows();
  }

  public function getFeedBackBogenFrageById($pID)
  {
    $id = $this->dboperation->getInt4Sql($pID);

    $sqlstr = 'SELECT ' . $this->select . ' FROM ' . $this->getTableName() . ' WHERE id = ' . $id;

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

    return $this->getFirstRow();
  }

  
  public function getFeedBackBogenFragenByBogenID($pBogenID)
  {
    $bogenID = $this->dboperation->getInt4Sql($pBogenID);

    $sqlstr = 'SELECT ' . $this->select . ' FROM ' . $this->getTableName() . ' WHERE bogenid = ' . $bogenID
              . ' ORDER BY lfdnr ASC';

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

    return $this->getAllRows();
  }

  
  public function getFeedBackFragenByBogenID($pBogenID)
  {
    $bogenID = $this->dboperation->getInt4Sql($pBogenID);

    $sqlstr = 'SELECT ' . $this->select . ' FROM ' . $this->getTableName() . ' WHERE bogenid = ' . $bogenID
              . ' AND frageid > 0 ORDER BY lfdnr ASC';

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

    return $this->getAllRows();
  }

  
  public function getFeedBackBereichByBogenID($pBogenID)
  {
    $bogenID = $this->dboperation->getInt4Sql($pBogenID);

    $sqlstr = 'SELECT ' . $this->select . ' FROM ' . $this->getTableName() . ' WHERE bogenid = ' . $bogenID
              . ' AND frageid <= 0 ORDER BY lfdnr ASC';

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

    return $this->getAllRows();
  }

  
  public function getFeedBackBereicheForBogenFrage($pBogenFrage)
  {
    $bereichTeile = explode('.', $pBogenFrage->bereich);

    $bogenID = $this->dboperation->getInt4Sql($pBogenFrage->bogenid);

    $sqlstr = 'SELECT ' . $this->select . ' FROM ' . $this->getTableName() . ' WHERE bogenid = ' . $bogenID . ' AND ( 1=0 OR ';

    $bereich = '';
    foreach ($bereichTeile as $key => $bereichTeil) {
      if ($key < count($bereichTeile) - 2) {
        $bereich .= $bereichTeil . '.';
        $ber = $this->dboperation->getString4Sql($bereich);
        $sqlstr .= ' bereich like \'' . $ber . '\' OR ';
      }
    }
    $sqlstr = rtrim($sqlstr, ' OR');

    $sqlstr .= ') ORDER BY lfdnr ASC';

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

    return $this->getAllRows();
  }
}