<?php

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


class ExportCalculationNl extends ExportCalculation
{
  public function getVarValue4NewsletterEdit($varname, $datamodul)
  {
    $vorbelegung = '';
    $istVorbelegt = StringOperations::strpos($varname, TRENNER_VORBELEGUNG) > 0;
    if ($istVorbelegt) {
      $parts = explode(TRENNER_VORBELEGUNG, $varname);
      $varname = array_shift($parts);
      $vorbelegung = implode(TRENNER_VORBELEGUNG, $parts);
    }

    $praefix = 'nledit';
    $pflicht = ''; 
    $person = '';
    $pflichtfeldPos = StringOperations::strpos(StringOperations::strtolower($varname), '_pflichtfeld:');
    if ($pflichtfeldPos > 0) {
      $varname = StringOperations::substr($varname, 0, $pflichtfeldPos);
    }
    
    
    else { 
      if (StringOperations::strpos($varname, ':') > 0) {
        $varname = StringOperations::substr($varname, 0, StringOperations::strpos($varname, ':'));
      }
    }

    $value = NlSessionOperations::getVarValueNl($varname);

    if (StringOperations::substr($varname, 0, 10) == 'kennzcheck') {
      
      $posMinus = StringOperations::strpos($varname, '-');
      if ($posMinus > 10) {
        $lfdnr = StringOperations::substr($varname, 10, $posMinus - 10);
        $kennz = StringOperations::substr($varname, $posMinus + 1);

        return $this->html->CreateInputTag(
          $praefix . $person . '_kennzcheck' . StringOperations::substr($varname, 10),
          'checkbox',
          $praefix . '_kennzcheck' . $lfdnr . 'id',
          '',
          '1',
          '',
          $datamodul->hasKennzeichen($kennz)
        );
      } else {
        Logger::logg(WB_LOG_PREFIX_NL . 'falsches Format bei der kennzcheck Variablen ' . $varname, 50);

        return '';
      }
    } elseif (StringOperations::substr($varname, 0, 11) == 'kennzselect') {
      return '';
    }
    
    
    elseif (StringOperations::substr($varname, 5, 5) == '_prio') {
      
      $value = NlSessionOperations::getVarValueNl(StringOperations::substr($varname, 0, 5));
    }

    switch (StringOperations::strtolower($varname)) {
      default:
        $maxlen = '';
        if (isset($this->maxlengths[$varname])) {
          $maxlen = ' maxlength="' . CleanOperations::cleanStringForHtml($this->maxlengths[$varname]) . '"';
        }

        return $this->html->CreateInputTag(
          $praefix . '_' . $varname,
          'text',
          $praefix . '_' . $varname . 'id',
          'tedit nledit ' . $praefix . '_' . $varname . 'class',
          $value,
          $maxlen,
          '',
          '',
          '',
          $vorbelegung
        ) . $pflicht;
      case 'formaction':
        $up = new URLParameterNewsletter(false);
        $up->kathaupt = 228;
        $up->pid = 'Newsletter';

        return CleanOperations::cleanStringForHtml(CmsAnb::getAnbindung()->rewriteUrl($up));
      case 'formaction_abmeldung':
        $tmp = CleanOperations::cleanStringForHtml($this->getVarValue4NewsletterEdit('formaction', $datamodul));

        return $tmp . '&amp;abmeldeformular=1';
      case 'submitname_anmeldung':
        return 'nl_anmeldung';
      case 'submitname_abmeldung':
        return 'nl_abmeldung';
      case 'bemerkung_textarea':
        $value = NlSessionOperations::getVarValueNl('bemerkung');

          return HTMLOperations::CreateTextareaTag(50, 5, $praefix . '_' . 'bemerkung', $value, $praefix . '_bemerkungid', 'tedit nledit ' . $praefix . '_bemerkungclass', '', '', '', $vorbelegung);

      case 'geschlecht_mw_def':
        $dontshowF = true;
        
      case 'geschlecht_def':
        $value = NlSessionOperations::getVarValueNl('geschlecht');
        $default = '<option value="">Bitte w&auml;hlen</option>';
        
      case 'geschlecht_mw':
        if (!isset($default)) {
          $dontshowF = true;
        }
        
      case 'geschlecht':
        $options = array();
        if (isset($default)) {
          $options[] = $default;
        }
        $options[] = '<option value="W" ' . ($value == 'W' ? $this->html->getEmptyAttribute('selected') : '') . '>W - Weiblich</option>';
        $options[] = '<option value="M" ' . ($value == 'M' ? $this->html->getEmptyAttribute('selected') : '') . '>M - Männlich</option>';
        if (!isset($dontshowF)) {
          $options[] = '<option value="F" ' . ($value == 'F' ? $this->html->getEmptyAttribute('selected') : '') . '>F - Firma</option>';
        }

        return $this->html->CreateSelectTag($options, $praefix . '_geschlecht', 'tedit ' . $praefix . ' ' . $praefix . $varname . 'class', $praefix . 'edit_geschlechtid', 1);
      case 'geschlecht_mwd_radio':
      case 'geschlecht_mw_radio':
      case 'geschlecht_mwdf_radio':
        $modifier = StringOperations::substr($varname, 11, -6);
        $mitDivers = StringOperations::strpos($modifier, 'd') !== false;
        $mitFirma = StringOperations::strpos($modifier, 'f') !== false;
        $value = NlSessionOperations::getVarValueNl('geschlecht');

        $input_m = $this->getGeschlechtRB($praefix, 'm', $value);
        $input_w = $this->getGeschlechtRB($praefix, 'w', $value);
        $input_d = $mitDivers ? $this->getGeschlechtRB($praefix, 'd', $value, 'divers') : '';
        $input_f = $mitFirma ? $this->getGeschlechtRB($praefix, 'f', $value, 'Firma') : '';

        return
          $input_m
          . $input_w
          . $input_d
          . $input_f;
      case 'geschlecht_anrede_def':
      case 'geschlecht_anrede_mw':
      case 'geschlecht_anrede_mw_def':
      case 'geschlecht_anrede_mwf':
      case 'geschlecht_anrede_mwf_def':
      case 'geschlecht_anrede_mwd':
      case 'geschlecht_anrede_mwd_def':
      case 'geschlecht_anrede_mwdf':
      case 'geschlecht_anrede_mwdf_def':
      case 'geschlecht_anrede':
        $value = NlSessionOperations::getVarValueNl('geschlecht');

        $parts = explode('_', $varname);

        $selectMitDefault = in_array('def', $parts);
        $selectMitFirma = (StringOperations::strpos($parts[2], 'f') !== false) && $parts[2] != 'def';
        $selectMitDivers = StringOperations::strpos($parts[2], 'd') !== false;

        $default = '<option value="">Bitte w&auml;hlen</option>';
        $firmenteil = '<option value="F" ' . ($value == 'F' ? $this->html->getEmptyAttribute('selected') : '') . '>Firma</option>';
        $diversteil = '<option value="D" ' . ($value == 'D' ? $this->html->getEmptyAttribute('selected') : '') . '>Divers</option>';

        $options = array();
        if ($selectMitDefault) {
          $options[] = $default;
        }
        $options[] = '<option value="W" ' . ($value == 'W' ? $this->html->getEmptyAttribute('selected') : '') . '>Frau</option>';
        $options[] = '<option value="M" ' . ($value == 'M' ? $this->html->getEmptyAttribute('selected') : '') . '>Herr</option>';
        if ($selectMitFirma) {
          $options[] = $firmenteil;
        }
        if ($selectMitDivers) {
          $options[] = $diversteil;
        }

        return $this->html->CreateSelectTag($options, $praefix . '_geschlecht', 'tedit ' . $praefix . ' ' . $praefix . $varname . 'class', $praefix . 'edit_geschlechtid', 1);
      case 'interessen_erstcheckbox':
        $erstbox = true;
        
      case 'interessen_checkbox':
        $markierteInteressensgebiete = NlSessionOperations::getNlInteressengebieteKennzFromSession();
        if (!isset($erstbox)) {
          $erstbox = false;
        }

        return $this->getInteressensgebieteList($erstbox, $markierteInteressensgebiete, $praefix, false);

      case 'captcha_ohnebr':
        $nobr = true;
        
      case 'captcha':
        if (!isset($nobr)) {
          $nobr = false;
        }

        $capi = \Kufer\Web\Classes\Captchas\CaptchaFactory::getCaptcha4KuferWEB();

        return $capi->getCaptcha($nobr, '', 'nledit');
      case 'dse':
        $varname = 'dse_unterschrieben_datum';
        $value = NlSessionOperations::getVarValueNl($varname);
        $str = $this->html->CreateInputTag(
          $praefix . '_' . $varname,
          'checkbox',
          "tedit_{$varname}id",
          '',
          DateTimeOperations::getDateTimeNowExact(),
          '',
          $value != ''
        );

        return $str . $pflicht;
    }
  }

  public function getGeschlechtRB($praefix, $pGeschlecht, $pCurrentValue, $pBezeichnung = '')
  {
    $geschlechtUpper = StringOperations::strtoupper($pGeschlecht);
    $bezeichnung = $pBezeichnung != '' ? $pBezeichnung : $pGeschlecht;
    $bezeichnung = LanguageOperations::translateGenderShort($bezeichnung);

    return $this->html->CreateInputTag(
      $praefix . '_geschlecht',
      'radio',
      $praefix . '_geschlechtid' . $geschlechtUpper,
      'edit ' . $praefix . ' ' . $praefix . '_geschlechtclass' . $geschlechtUpper,
      $geschlechtUpper,
      '',
      ($pCurrentValue == $geschlechtUpper)
    ) . ' ' . $bezeichnung . ' ';
  }

  public function getInteressensgebieteList($pErstCheckbox, $pMarkierteInteressen, $pPraefix, $pDisabled)
  {
    $dbw = new DBWorkHLNewsletterInteressensgebiete();
    $interessensgebiete = $dbw->getNewsletterInteressensgebiete();
    if (count($interessensgebiete) === 0) {
      return '';
    }
    $retour = '<ul id="nl_interessensgebiete"' . (KW_BOOTSTRAP ? ' class="list-group"' : '') . '>';
    $i = 0;
    foreach ($interessensgebiete as $gebiet) {
      $checked = in_array($gebiet->kennz, $pMarkierteInteressen);
      $id = $pPraefix . '_interessensgebiet' . $i . 'id';
      $retour .= '<li class="nl_interessensgebiet' . (KW_BOOTSTRAP ? ' list-group-item' : '') . '">';
      $box = $this->html->CreateInputTag(
        $pPraefix . '_interessensgebiet' . $i,
        'checkbox',
        $id,
        '',
        $gebiet->kennz,
        '',
        $checked,
        $pDisabled
      );
      $labelstart = '<label for="' . CleanOperations::cleanStringForHtml($id) . '">';
      $labelend = '</label>';
      $bezeichnung = CleanOperations::cleanStringForHtml($gebiet->bezeichnung);
      if ($pErstCheckbox) {
        $retour .= $labelstart . $box . $bezeichnung . $labelend;
      } else {
        $retour .= $labelstart . $bezeichnung . $box . $labelend;
      }
      $retour .= '</li>';
      $i++;
    }

    return $retour . '</ul>';
  }

  public function getVarValue4Newsletter($varname, $datamodul)
  {
    if (!is_a($datamodul, 'Datamodul')) {
      return '';
    }
    
    if (StringOperations::startsWith($varname, 'kennz') && StringOperations::strlen($varname) > 5) {
      if ($datamodul->hasKennzeichen(StringOperations::substr($varname, 5))) {
        return 1;
      } else {
        return 0;
      }
    }

    switch (StringOperations::strtolower($varname)) {
      default: return $this->getDefaultVarValue($varname, $datamodul);
      case 'interessen_erstcheckbox':
        $erstbox = true;
        
      case 'interessen_checkbox':
        $markierteInteressensgebiete = NlSessionOperations::getNlInteressengebieteKennzFromSession();
        if (!isset($erstbox)) {
          $erstbox = false;
        }

        return $this->getInteressensgebieteList($erstbox, $markierteInteressensgebiete, 'nl', true);
    }
  }

  public function getVarValue4NewsletterInteressengebiet($pVarname, $pDatamodul)
  {
    if (!is_a($pDatamodul, 'Datamodul')) {
      return '';
    }

    switch (StringOperations::strtolower($pVarname)) {
      default: return $this->getDefaultVarValue($pVarname, $pDatamodul);
    }
  }

  public function getVarValue4NewsletterBest($varname, $datamodul)
  {
    $praefix = 'nlbest';

    $value = NlSessionOperations::getVarValueNl($varname);

    if (StringOperations::substr($varname, 0, 10) == 'kennzcheck') {
      
      $lfdnr = StringOperations::substr($varname, 10, StringOperations::strpos($varname, '-'));
      $kennz = StringOperations::substr($varname, StringOperations::strpos($varname, '-') + 1);

      return $this->html->CreateInputTag(
        $praefix . '_kennzcheck' . $lfdnr,
        'checkbox',
        $praefix . '_kennzcheck' . $lfdnr . 'id',
        '',
        '1',
        '',
        $datamodul->hasKennzeichen($kennz),
        true 
      );
    } elseif (StringOperations::substr($varname, 0, 11) == 'kennzselect') {
      return '';
    }
    
    
    elseif (StringOperations::substr($varname, 5, 5) == '_prio') {
      
      $value = NlSessionOperations::getVarValueNl(StringOperations::substr($varname, 0, 5));
    }

    switch (StringOperations::strtolower($varname)) {
      default:
        if ($value == '') {
          return '<span class="' . $praefix . '">&nbsp;</span>';
        } else {
          return '<span class="' . $praefix . '">' . CleanOperations::cleanStringForHtml($value) . '</span>';
        }
        
      case 'bemerkung_br':
      case 'bemerkung_textareabemerkung_textarea':
        $value = NlSessionOperations::getVarValueNl('bemerkung');
        if ($value == '') {
          return '<span class="' . $praefix . '">&nbsp;</span>';
        } else {
          return '<span class="' . $praefix . '">' . nl2br(CleanOperations::cleanStringForHtml($value)) . '</span>';
        }
        
      case 'geschlecht_def':
        $value = NlSessionOperations::getVarValueNl('geschlecht');
        $default = '<option value="">Bitte w&auml;hlen</option>';
        
      case 'geschlecht_mw':
        if (!isset($default)) {
          $dontshowF = true;
        }
        
      case 'geschlecht':
        if (!isset($default)) {
          $default = '';
        }
        $str = '<select id="' . $praefix . 'edit_geschlechtid" ' . HTMLOperations::getEmptyAttribute('disabled') . ' name="' . $praefix . '_geschlecht" class="tedit ' . $praefix . ' ' . $praefix . CleanOperations::cleanStringForHtml($varname)
                . 'class" size="1">' . $default
                . '<option value="W" ' . ($value == 'W' ? $this->html->getEmptyAttribute('selected') : '') . '>W - Weiblich</option>'
                . '<option value="M" ' . ($value == 'M' ? $this->html->getEmptyAttribute('selected') : '') . '>M - Männlich</option>';
        if (!isset($dontshowF)) {
          $str .= '<option value="F" ' . ($value == 'F' ? $this->html->getEmptyAttribute('selected') : '') . '>F - Firma</option>';
        }

        $str .= '</select>';

        return $str;
      case 'geschlecht_mw_radio':
        $value = NlSessionOperations::getVarValueNl('geschlecht');
        $input_m = $this->html->CreateInputTag(
          $praefix . '_geschlecht',
          'radio',
          $praefix . '_geschlechtidM',
          'edit ' . $praefix . ' ' . $praefix . '_geschlechtclassM',
          'M',
          '',
          ($value == 'M'),
          true 
        ) . ' m';
        $input_w = $this->html->CreateInputTag(
          $praefix . '_geschlecht',
          'radio',
          $praefix . '_geschlechtidW',
          'edit ' . $praefix . ' ' . $praefix . '_geschlechtclassW',
          'W',
          '',
          ($value == 'W'),
          true 
        ) . ' w';

        return
          $input_m
          . $input_w;
      case 'geschlecht_anrede_def':
        $value = NlSessionOperations::getVarValueNl('geschlecht');
        $default = '<option value="">Bitte w&auml;hlen</option>';
        
      case 'geschlecht_anrede_mw':
        if (!isset($default)) {
          $dontshowF = true;
        }
        
      case 'geschlecht_anrede':
        if (!isset($default)) {
          $default = '';
        }
        $str = '<select id="' . $praefix . 'edit_geschlechtid" ' . HTMLOperations::getEmptyAttribute('disabled') . ' name="' . $praefix . '_geschlecht" class="tedit ' . $praefix . ' ' . $praefix . CleanOperations::cleanStringForHtml($varname)
                . 'class" size="1">' . $default
                . '<option value="W" ' . ($value == 'W' ? $this->html->getEmptyAttribute('selected') : '') . '>Frau</option>'
                . '<option value="M" ' . ($value == 'M' ? $this->html->getEmptyAttribute('selected') : '') . '>Herr</option>';
        if (!isset($dontshowF)) {
          $str .= '<option value="F" ' . ($value == 'F' ? $this->html->getEmptyAttribute('selected') : '') . '>Firma</option>';
        }

        $str .= '</select>';

        return $str;
      case 'interessen_erstcheckbox':
        $erstbox = true;
        
      case 'interessen_checkbox':
        $markierteInteressensgebiete = NlSessionOperations::getNlInteressengebieteKennzFromSession();
        if (!isset($erstbox)) {
          $erstbox = false;
        }

        return $this->getInteressensgebieteList($erstbox, $markierteInteressensgebiete, $praefix, true);
    }
  }
}