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


class Monatsplangrafik extends DruckGrafikKW
{
  
  public $vonkwdat;

  
  public $aktmonat;

  
  public $aktjahr;

  
  public $zellheight;

  public function getListID()
  {
    return 19;
  }

  

  public function getListTyp()
  {
    return '17';
  }

  

  public function loadFactoryDefaults()
  {
    parent::loadFactoryDefaults();
    $this->setTitle1();
  }

  

  public function setTitle1()
  {
    $this->settings->title1 = 'Monatsplan';
  }

  

  public function getDruckbereichEnabled($optionName)
  {
    
    switch ($optionName) {
        case 'querformat':
          return true;
      }

    return false;
  }

  
  public function _prepare($pKnr = '')
  {
    parent::_prepare($pKnr);

    switch ($this->settings->kalenderwoche->auswahl) {
            case 'dat':
                  break;
            case 'kw':

                  $this->kurstage->von = $this->kwary['Mo'];

                  $kwnr = $this->settings->kalenderwoche->kwnr;
                $kwjahr = $this->settings->kalenderwoche->kwjahr;
                $kwary = KalenderwochenAPI::getKalenderWochebyNr($kwjahr, 4 + $kwnr);

                $this->kurstage->bis = $kwary['So'];

                  break;
            case 'mon':
                $aktmonat = $this->settings->kalenderwoche->kwmonat;
                $aktjahr = $this->settings->kalenderwoche->kwjahr;


                if (StringOperations::strlen($aktmonat) == 1) {
                  $aktmonat = '0' . $aktmonat;
                }


                $this->kurstage->von = '01.' . $aktmonat . '.' . $aktjahr;

                $monatstage = $this->dtop->getDaysofMonth($aktmonat, $aktjahr);

                $this->kurstage->bis = $monatstage . '.' . $aktmonat . '.' . $aktjahr;
                break;
        }
    $this->zellheight = 22;
  }

  

  
  protected function _printDruckListe($pKurs = '')
  {
    
    $this->pdf->ln(-0.5 * $this->height);
    $this->pdf->CellAtX('3%', '', 'B', '', '', '7.5%', 1, $this->height, 0);
    $this->pdf->CellAtX('10.5%', 'Montag', 'B', '', '', '12.4%', 1, $this->height, 0);
    $this->pdf->CellAtX('22.9%', 'Dienstag', 'B', '', '', '12.4%', 1, $this->height, 0);
    $this->pdf->CellAtX('35.3%', 'Mittwoch', 'B', '', '', '12.4%', 1, $this->height, 0);
    $this->pdf->CellAtX('47.7%', 'Donnerstag', 'B', '', '', '12.4%', 1, $this->height, 0);
    $this->pdf->CellAtX('60.1%', 'Freitag', 'B', '', '', '12.4%', 1, $this->height, 0);
    $this->pdf->CellAtX('72.5%', 'Samstag', 'B', '', '', '12.4%', 1, $this->height, 0);
    $this->pdf->CellAtX('84.9%', 'Sonntag', 'B', '', '', '12.4%', 1, $this->height, 0);
    $this->pdf->ln($this->height);

    
    
    switch ($this->settings->kalenderwoche->auswahl) {
            case 'dat':
                $this->aktmonat = StringOperations::substr($this->kurstage->von, 3, 2);
                $this->aktjahr = (int)StringOperations::substr($this->kurstage->von, 6, 4);
                $day = StringOperations::substr($this->kurstage->von, 0, 2);
                $kwobj = KalenderwochenAPI::getKalenderWochenNrfromDate($day, $this->aktmonat, $this->aktjahr);
                 $kawoary = KalenderwochenAPI::getKalenderWochebyNr($kwobj->kwjahr, $kwobj->kwnr);
                  $this->vonkwdat = $kawoary['Mo'];
                  $this->biskwdat = $this->kurstage->bis;
                  $anzwo = $this->getAnzWochen($this->vonkwdat, $this->biskwdat);
                  break;
            case 'kw':
                  $this->vonkwdat = $this->kwary['Mo'];
                  $this->aktmonat = (int)StringOperations::substr($this->vonkwdat, 3, 2);
                  $this->aktjahr = (int)(StringOperations::substr($this->vonkwdat, 6, 4));
                  $anzwo = 5;
                  break;
            case 'mon':
                $this->aktmonat = $this->settings->kalenderwoche->kwmonat;
                $this->aktjahr = $this->settings->kalenderwoche->kwjahr;
                $kwobj = KalenderwochenAPI::getKalenderWochenNrfromDate(01, $this->aktmonat, $this->aktjahr);
                
                $this->vonkwdat = KalenderwochenAPI::getKalenderWochebyNr($kwobj->kwjahr, $kwobj->kwnr);
                $this->vonkwdat = $this->vonkwdat['Mo'];
                
                $kwbis = KalenderwochenAPI::getHighestKWofMonth($this->aktmonat, $this->aktjahr);
                if ($kwobj->kwnr >= 52) {
                  $kwobj->kwnr = 0;
                }
                    $anzwo = $kwbis - $kwobj->kwnr + 1;
                break;
        }

    
    
    
    $monat = StringOperations::substr($this->vonkwdat, 3, 2);
    $jahr = (int)(StringOperations::substr($this->vonkwdat, 6, 4));
    $day = StringOperations::substr($this->vonkwdat, 0, 2);
    $this->aktmonat = (string)$this->aktmonat;
    if (StringOperations::strlen($this->aktmonat) == 1) {
      $this->aktmonat = '0' . $this->aktmonat;
    }

    
    $kwobj = KalenderwochenAPI::getKalenderWochenNrfromDate($day, $monat, $jahr);
    
    $kw = $kwobj->kwnr;
    $jahr = $kwobj->kwjahr;

    
    
    
    $this->kwary = KalenderwochenAPI::getKalenderWochebyNr($jahr, $kw);
    $dtag0 = $this->kwary['Mo'];

    
    $datum = isset($this->kursetage[0]->datum) ? $this->kursetage[0]->datum : '';
    while (($this->dtop->isDateBefore($datum, $dtag0) || strcmp($this->aktmonat, StringOperations::substr($datum, 3, 2)) > 0) && $this->kursetage) {
      array_shift($this->kursetage);
      $datum = isset($this->kursetage[0]->datum) ? $this->kursetage[0]->datum : '';
    }
    
    
    for ($i = 0;$i < $anzwo;$i++) {
      
      $this->kwary = KalenderwochenAPI::getKalenderWochebyNr($jahr, $kw);
      $this->_printKursCell($kw);
      
      $kwobj = KalenderwochenAPI::incKalenderwoche($kw, $jahr, 1);
      $jahr = $kwobj->kwjahr;
      $kw = $kwobj->kwnr;
    }
  }

  

  
  protected function _printKursCell($pKalwoche)
  {
    
    
    $this->pdf->CellAtX('3%', 'KW ' . $pKalwoche, 'B', '', '', '7.5%', 1, $this->zellheight, 0);
    $this->pdf->ln($this->zellheight * 8 / 22);
    $this->pdf->CellAtX('3%', $this->kwary['Mo'] . '-', 'B', '', '', '7.5%', 0, $this->zellheight * 8 / 22, 0);
    $this->pdf->ln($this->zellheight * 5 / 22);
    $this->pdf->CellAtX('3%', $this->kwary['So'], 'B', '', '', '7.5%', 0, $this->zellheight * 8 / 22, 0);
    $this->pdf->ln(-$this->zellheight * 13 / 22);
    $i = 0;
    
    foreach ($this->kwary as $dtag) {
      $this->pdf->CellAtX(12.4 * $i + 10.5 . '%', '', '', '', '', '12.4%', 1, $this->zellheight, 0);

      
      if ($this->settings->kalenderwoche->auswahl == 'mon') {
        if (strcmp($this->aktmonat, StringOperations::substr($dtag, 3, 2)) == 0) {
          $this->_printSingleCell($i, $dtag);
        } 
      } 
            else {
              $this->_printSingleCell($i, $dtag);
            }
      $i++;
    }
    $this->pdf->ln($this->zellheight);
  }

  

  
  public function _printSingleCell($i, $dtag)
  {
    
    $this->pdf->CellAtX(12.4 * $i + 10.5 . '%', StringOperations::substr($dtag, 0, 3), '', '', 8, '12.4%', 0, $this->height, 0);
    
    $datum = isset($this->kursetage[0]->datum) ? $this->kursetage[0]->datum : '';
    for ($cou = 0;strcmp($dtag, $datum) == 0;$cou++) {
      
      if ($cou < 2) {
        $this->pdf->ln($this->zellheight * 4 / 22 + $cou * $this->zellheight * 7 / 22);
        $this->pdf->CellAtX(12.4 * $i + 10.5 . '%', $this->kursetage[0]->zeit_beg . '-' . $this->kursetage[0]->zeit_ende . ' ' . $this->kursetage[0]->knr, '', '', 8, '12.4%', 0, $this->height, 0);
        $this->pdf->ln($this->zellheight * 3 / 22);
        $this->pdf->CellAtX(12.4 * $i + 10.5 . '%', $this->kursetage[0]->kursort->bezeichnung, '', '', 8, '12.4%', 0, $this->height, 0);
        $this->pdf->ln(-$this->zellheight * 7 / 22 + $cou * $this->zellheight * -7 / 22);
        array_shift($this->kursetage);
        $datum = isset($this->kursetage[0]->datum) ? $this->kursetage[0]->datum : '';
      }
      else {
        $this->pdf->ln($this->zellheight * 17 / 22);
        $this->pdf->CellAtX(12.4 * $i + 10.5 . '%', '[...]', '', '', 8, '12.4%', 0, $this->height, 0);
        array_shift($this->kursetage);
        $datum = isset($this->kursetage[0]->datum) ? $this->kursetage[0]->datum : '';
        $this->pdf->ln(-$this->zellheight * 17 / 22);
      }
    } 
  }

  

  
  public function _getKalenderMonatsplan($changeHL = '')
  {
    
    if ($changeHL) {
      return '';
    }
    $kwjahr = $this->settings->kalenderwoche->kwjahr;
    $kwnr = $this->settings->kalenderwoche->kwnr;
    $kwmonat = $this->settings->kalenderwoche->kwmonat;
    $seljahropt = $this->_getSelOptionKWJahr($kwjahr);
    $selnropt = $this->_getSelOptionKWnr($kwnr);
    $selmonopt = $this->_getSelOptionKWMonat($kwmonat);
    
    $datvon = $this->kurstage->von;
    $datbis = $this->kurstage->bis;

    $result = '<fieldset id="monatsplan"><legend>Wochenplan für Monatsgrafiken</legend>';
    
    $disabled = false;
    $result .= "<div id='radioauswahl'>";

    
    $checked = $this->settings->kalenderwoche->auswahl == 'mon';
    $indiv = " onclick = 'setFieldsToDisabled(false,true,false,true)'";
    $result .= "<div id='radio1'>";
    $result .= $this->htmlop->CreateInputTag('auswahl', 'radio', 'aus_mon', '', 'mon', $indiv, $checked, $disabled);
    $result .= '<label for="aus_mon">Auswahl via Monat</label>'
                            . '</div>';

    
    $checked = $this->settings->kalenderwoche->auswahl == 'kw';
    $indiv = " onclick = 'setFieldsToDisabled(true,false,false,true)'";
    $result .= "<div id='radio2'>";
    $result .= $this->htmlop->CreateInputTag('auswahl', 'radio', 'aus_kw', '', 'kw', $indiv, $checked, $disabled);
    $result .= '<label for="aus_kw">Auswahl via KW</label>'
                            . '</div>';

    
    $checked = $this->settings->kalenderwoche->auswahl == 'dat';
    $indiv = "onclick = 'setFieldsToDisabled(true,true,true,false)'";
    $result .= "<div id='radio3'>";
    $result .= $this->htmlop->CreateInputTag('auswahl', 'radio', 'aus_dat', '', 'dat', $indiv, $checked, $disabled);
    $result .= '<label for="aus_dat">Auswahl via Datum</label>' . '</div>';
    $result .= '</div>';
    
    $result .= "<div id='selectfelder'>";

    $disabled = $this->settings->kalenderwoche->auswahl == 'mon' ? '' : 'disabled';

    $result .= "<div class = 'kwmonat'>" . '<label id="l_kwmonat" for="kwmonat">Monat:</label>'
                . $this->htmlop->CreateSelectTag($selmonopt, 'kwmonat', '', 'kwmonat', 1, '', false, $disabled) . '</div>';
    
    $disabled = $this->settings->kalenderwoche->auswahl == 'kw' ? '' : 'disabled';
    $result .= "<div class = 'kwnr'>" . '<label id="l_kwnrmon" for="kwnr">Kalenderwoche:</label>'
             . $this->htmlop->CreateSelectTag($selnropt, 'kwnr', '', 'kwnr', 1, '', false, $disabled) . '</div>';
    
    $result .= "<div class = 'kwjahr'>" . '<label id="l_kwjahrmon" for="kwjahr">Kalenderjahr:</label>'
                . $this->htmlop->CreateSelectTag($seljahropt, 'kwjahr', '', 'kwjahr', 1, '', false, $disabled) . '</div>';


    
    
    

    $disabled = $this->settings->kalenderwoche->auswahl == 'dat' ? '' : 'disabled';
    

    $result .= "<div class = 'datum_von'>"
        . '<label id="l_ktvon" for="kt_von">von:</label>'
        . $this->htmlop->CreateInputTag('kt_von', 'text', 'ktdat_von', 'date', $datvon, '', false, $disabled);
    $result .= '</div>';
    $result .= "<div class = 'datum_bis'>"
                    . '<label id="l_ktbis" for="kt_bis">bis:</label>'
                            . $this->htmlop->CreateInputTag('kt_bis', 'text', 'ktdat_bis', 'date', $datbis, '', false, $disabled);
    $result .= '</div>';
    $result .= '</div>'; 
    $result .= '</fieldset>';

    return $result;
  }

  

  
  
  public function isMonatsplanUsed()
  {
    return true;
  }

  
  public function isWochenplanUsed()
  {
    return false;
  }

  
  public function _getKalenderWochenplan($changeHL = '')
  {
    return '';
  }

  
}