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

  
  define('ISKUFERTOOLS', true);
  

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

CodingOperations::encodeEingaben();
CodingOperations::setObStartIfInternalAndExternalEncodingAreDiffrent();
CodingOperations::setEncodingResponseHeader();

if (!session_id()) {
  session_start();
}

if (!SessionOperations::isAdmin()) {
  header('Location: index.php?sessionfail=1');
  die();
}

SecurityOperations::PurgeInput(true);

$obj = new SaveTNListe();
$obj->save();


class SaveTNListe
{
  public function save()
  {
    
    if (GetOperations::isDefined('copy')) {
      $sightId = GetOperations::getVarValue('sightid');
      $toolId = GetOperations::getVarValue('toolid', null) ?? GetOperations::getVarValue('tool');
      $selected = GetOperations::getVarValue('copy');

      $liste = TNListeFactory::create($selected, false, $toolId);

      if (!$liste) {
        die('Schwerer Fehler: Listenobjekt konnte nicht erzeugt werden!');
      }

      $dbwsql = new DBWorkHLTNList();
      $sqlSettings = $dbwsql->getHLByTyp($liste->getListTyp());

      $settings = new \webtools\lists\TNSettings();
      $settings->fromHLTNList($sqlSettings);
      $dbsettings = $settings->toTNListSettings();

      $dbw = new DBWorkTNListSettings();
      $dbw->addSettings($liste->getListID(), $toolId, $dbsettings);
    } else {
      $toolId = PostOperations::getVarValue('toolid');
      $liste = TNListeFactory::create(PostOperations::getVarValue('art'), false, $toolId);

      if (!$liste) {
        die('Schwerer Fehler: Listenobjekt konnte nicht erzeugt werden!');
      }

      
      $liste->loadSettings(PostOperations::getVarValue('toolid'));
      $tnsettings = $liste->settings;
      $tnsettings->loadFromPOST($liste);

      
      $dbsettings = $tnsettings->toTNListSettings();
      $dbw = new DBWorkTNListSettings();
      $dbw->addSettings($liste->getListID(), $toolId, $dbsettings);

      $sightId = PostOperations::getVarValue('sightid');
      $selected = PostOperations::getVarValue('art');

      $listeId = $liste->getListID();
      $toolSettings = ToolSettingsAPI::getToolSettings($toolId);
      $druckTnListeSettingsAry = SerializeOperations::unserialize($toolSettings->druckTnListeSettings);
      if (!is_array($druckTnListeSettingsAry)) {
        $druckTnListeSettingsAry = array();
      }
      $druckTnListeSettingsAry['vorKursende' . $listeId] = PostOperations::getVarValue('druckTnListeVorKursende' . $listeId);
      $druckTnListeSettingsAry['nachKursende' . $listeId] = PostOperations::getVarValue('druckTnListeNachKursende' . $listeId);

      $druckTnListeSettingsAry['tnListeBezeichnung' . $listeId] = PostOperations::getVarValue('tnListeBezeichnung' . $listeId);

      $toolSettings->druckTnListeSettings = SerializeOperations::serialize4Settings($druckTnListeSettingsAry);
      ToolSettingsAPI::setToolSettings($toolSettings);
    }
    
    header("location: hleinstell.php?sightid=$sightId&tool=$toolId&liste=$selected");
  }
}