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


class DBWorkDruckListSettings extends DBWork
{
  public function __construct($pDBTyp = 1, $pNewInstance = false)
  {
    parent::__construct($pDBTyp, $pNewInstance);

    $this->select = 'ID, ListID, ToolID, Typ, Description, ' . $this->clob('listsettings') . ', Title1, Title2, Endbem, Feed, Portrait, Zusatzspalten, ausfall';
  }

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

  
  public function _idOk($id)
  {
    return !empty($id) && is_numeric($id);
  }

  
  public function _toolIdOk($toolId)
  {
    return !empty($toolId) && is_numeric($toolId);
  }

  
  public function _listIdOk($listId)
  {
    return !empty($listId) && is_numeric($listId);
  }

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

    return parent::canupdate($varname);
  }

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

  
  public function getCurrentRow($pRefreshCache = false)
  {
    $obj = $this->getNewObject();
    $obj->id = $this->getDatamodulValue('ID');
    $obj->typ = $this->getDatamodulValue('Typ');
    $obj->listID = $this->getDatamodulValue('ListID');
    $obj->toolID = $this->getDatamodulValue('ToolID');
    $obj->listsettings = $this->getDatamodulValue('Listsettings');
    $obj->description = $this->getDatamodulValue('Description');
    $obj->title1 = $this->getDatamodulValue('Title1');
    $obj->title2 = $this->getDatamodulValue('Title2');
    $obj->endbem = $this->getDatamodulValue('Endbem');
    $obj->feed = $this->getDatamodulValue('Feed');
    $obj->portrait = $this->getDatamodulValue('Portrait');
    
    $obj->zusatzspalten = $this->getDatamodulValue('Zusatzspalten');
    
    $obj->ausfall = $this->getDatamodulValue('ausfall');

    return $obj;
  }

  
  public function getSettings($listId, $toolId)
  {
    if (!$this->_listIdOk($listId) || !$this->_toolIdOk($toolId)) {
      return false;
    }

    $this->dboperation->clear();
    $this->dboperation->query("SELECT $this->select FROM web_drucklisten WHERE ListID = $listId AND ToolID = $toolId");
    if ($this->dboperation->rowcount() == 0) {
      return false;
    }

    return $this->getFirstRow();
  }

  
  public function getSettingsById($id)
  {
    if (!$this->_idOk($id)) {
      return false;
    }

    $this->dboperation->clear();
    $this->dboperation->query("SELECT $this->select FROM web_drucklisten WHERE ID = $id");

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

    return $this->getFirstRow();
  }

  
  public function getSettingsID($listId, $toolId)
  {
    if (!$this->_listIdOk($listId) || !$this->_toolIdOk($toolId)) {
      return false;
    }

    $this->dboperation->clear();
    $this->dboperation->query("SELECT ID FROM web_drucklisten WHERE ListID = $listId AND ToolID = $toolId");

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

    $this->dboperation->selectrow(0);

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

  
  public function addSettings($listId, $toolId, $settings)
  {
    if (!$this->_listIdOk($listId) || !$this->_toolIdOk($toolId)) {
      return false;
    }

    $id = $this->getSettingsID($listId, $toolId);

    if ($id == -1) { 
      $id = $this->insertRecord('web_drucklisten', '', 'Description');
    }

    $settings->toolID = $toolId;
    $settings->listID = $listId;
    $settings->id = $id;
    $org = $this->getSettingsById($id);
    $this->updateObject($settings, $org, 'web_drucklisten', 'id');
  }
}