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


class SaveToolDruckListenSettings extends SaveToolSettings
{
  private $druckListSettingsDb;
  private $listName;
  private $listId;
  private $toolId;
  private $defaultListSettings;

  public function __construct($pTNListSettingsDb, $pListName, $pListId, $pToolId, $pDefaultListSettings)
  {
    $this->druckListSettingsDb = $pTNListSettingsDb;
    $this->listName = $pListName;
    $this->listId = $pListId;
    $this->toolId = $pToolId;
    $this->defaultListSettings = $pDefaultListSettings;
  }

  protected function fillToolSettingIfNecessary($pSetting)
  {
    
    
  }

  protected function saveSettingsToFile()
  {
    parent::saveSettingsToFile();
    
    $this->druckListSettingsDb->saveListSettings($this->listId, $this->toolId);
  }

  protected function setSetting($pSetting)
  {
    $listSettings = $this->druckListSettingsDb->getListSettings($this->listId, $pSetting->toolId);
    $varName = $pSetting->name;

    $toolSettings = ToolSettingsAPI::getToolSettings($pSetting->toolId);
    
    $this->toolSettingsAry[$pSetting->toolId] = $toolSettings;

    
    
    if (is_array($varName)) {
      foreach ($varName as $name) {
        if (isset($pSetting->value[$name])) {
          $toolSettings->{$name} = $pSetting->value[$name];
        }
      }
      ToolSettingsAPI::setToolSettings($toolSettings);
    }
    
    
    elseif (in_array($varName, array('stdplanuhrzeit', 'ktStatusPdfListeGrafik', 'ktStatusPdfListeGrafikUseParent'))) {
      parent::setSetting($pSetting);
    } else {
      
      
      if (!$listSettings) {
        if (is_null($this->defaultListSettings)) {
          $this->defaultListSettings = DruckListeFactory::create($this->listName, $pSetting->toolId);
        }
        $defaultSettings = $this->defaultListSettings->settings;
        $defaultSettings->loadFromPOST($this->defaultListSettings);
        $listSettings = $defaultSettings->toDruckListSettings($this->listId);
        $listSettings->description = $this->listName;
      }

      
      if ($varName == 'listsettings') {
        
        $value = $this->getListSettingsValueFromAdminSettings($pSetting);
      }
      
      elseif ($varName == 'zusatzspalten' && !empty($pSetting->config->items)) {
        $value = (string)$this->ValuestoBitmask($pSetting->value);
      }
      
      else {
        $value = $pSetting->value;
      }
      $listSettings->{$varName} = $value;
      
      $this->druckListSettingsDb->setListSettings($listSettings);
    }
  }

  
  private function getListSettingsValueFromAdminSettings($pSetting)
  {
    
    if ($pSetting->toolId == 21 || $pSetting->toolId == 20) { 

      
      
      
      

      
      
      

      
      $defaultWotagAry = array('Mo' => '', 'Di' => '', 'Mi' => '', 'Do' => '', 'Fr' => '', 'Sa' => '', 'So' => '');
      $listSettingAry = get_object_vars(new \webtools\drucklisten\Wochentage($defaultWotagAry));
    
      
    } else {
      
      $listSettingAry = get_object_vars(new EinstellDarstellung());
    }

    
    foreach ($listSettingAry as $key => $val) {
      
      $listSettingAry[$key] = @$pSetting->value[$key];
    }
    $i = 0;
    $setValues = array();
    foreach ($listSettingAry as $wert) {
      if ($wert) {
        $setValues[$i] = 'W';
      } else {
        $setValues[$i] = 'F';
      }
      $i++;
    }
    
    return implode('', $setValues);
  }

  
}