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


class NormalSight extends BasisSight
{
  
  public $firstcolor;
  
  public $secondcolor;
  
  public $tabletitlecolor;
  
  public $textcolor;
  
  public $printmodus = false;
  
  public $editid;
  
  public $addformstr;
  
  public $editformstr;
  
  public $editformselfstr;
  
  public $editbuttonhiddenstr;
  
  public $deletebuttonhiddenstr;
  
  public $editsavebuttonhiddenstr;
  
  public $editcancelbuttonhiddenstr;
  
  public $colorevent;
  
  public $titletext = '';

  
  public $showEmailAsLink = true;

  
  public $alignButtonsLeft = false;

  public $tableId = '';
  public $tableClass = '';

  
  public function __construct()
  {
    parent::__construct();
  }

  
  public function initiate()
  {
    parent::initiate();
    $this->textcolor = '#000000';
    if (!$this->printmodus) {
      $this->tabletitlecolor = "class='farbe_sicht_header'";
      $this->firstcolor = "class='farbe_sicht_erstezeile'";
      $this->secondcolor = "class='farbe_sicht_zweitezeile'";
    } else {
      $this->tabletitlecolor = "bgcolor='#FFFFFF'";
      $this->firstcolor = "bgcolor='#FFFFFF'";
      $this->secondcolor = "bgcolor='#FFFFFF'";
    }
  }

  public function getCount()
  {
    if (isset($this->dataary)) {
      return count($this->dataary);
    }

    return 0;
  }

  
  public function getErrorNoData()
  {
    return "<br><br><center><font face='arial' color='red' size='3'>Für die aktuelle Auswahl sind keine Daten verfügbar.</font></center><br><br><br>";
  }

  
  public function getErrorNoColumns()
  {
    return "<br><br><center><font face='arial' color='red' size='2'>Die zentralen Einstellungen für diese Ansicht müssen ggf. geändert werden.</font><br>
            <font face='arial' color='red' size='2'>Bitte wenden Sie sich an Ihren Systemadministrator!</font></center><br><br>";
  }

  
  public function checkErrors()
  {
    if (count($this->columnsary) < 1) {
      return $this->getErrorNoColumns();
    }
    if (count($this->dataary) < 1) {
      return $this->getErrorNoData();
    }

    return '';
  }

  
  public function getTitle()
  {
    return '';
  }

  
  public function getHeader()
  { 
    $str = "<table border='0' width='100%'>";
    $allgsettings = AllgSettingsAPI::getAllgSettings();
    if ($this->printmodus) {
      $str
        .= "  <tr>\n"
        . "   <td width='50%' align='left'><font id='text_vhs_titel'><b>" . CleanOperations::cleanStringForHtml($allgsettings->vhsname) . '</b></font></td>'
        . "   <td width='50%' align='right'><font id='text_vhs_titel'><b>Stand: " . date('d.m.Y') . '</b></font></td>'
        . "  </tr>\n"
        . "  <tr><td><font id='text_vhs_titel'>" . CleanOperations::cleanStringForHtml($allgsettings->vhsstrasse) . "</font></td><td>&nbsp;</td></tr>\n"
        . "  <tr><td><font id='text_vhs_titel'>" . CleanOperations::cleanStringForHtml($allgsettings->vhstelefon) . "</font></td><td>&nbsp;</td></tr>\n"
        . "  <tr><td colspan='2'></td></tr>";
    }
    $str
      .= "  <tr><td nowrap width='50%' align='left'>" . CleanOperations::cleanStringContainingHtml($this->getTitle()) . '</td>'
      . "    <td nowrap width='50%' align='right'></td>"
        . " </tr>\n";
    $tmp = $this->checkErrors();
    if (trim($tmp) != '') {
      $str
        .= "  <tr><td nowrap width='50%' align='left'>" . $tmp . '</td>'
        . "    <td width='50%' align='right'></td>"
            . "  </tr>\n";
    }
    $str .= "</table>\n";

    return $str;
  }

  
  public function getTable()
  {
    if (count($this->dataary) < 1) {
      return '';
    }
    $str = '';

    $id = ($this->tableId !== '') ? ' id="' . CleanOperations::cleanStringForHtml($this->tableId) . '"' : '';
    $class = ($this->tableClass !== '') ? ' class="' . CleanOperations::cleanStringForHtml($this->tableClass) . '"' : '';

    if ($this->printmodus) {
      $str .= "<table{$id}{$class} border='2' cellpadding='3' cellspacing='2'>";
    } else {
      $str .= "<table{$id}{$class} border='0' cellpadding='3' cellspacing='2'>";
    }

    $str .= $this->getTableHeader();
    $str .= $this->getTBodyOpen();
    for ($i = 0;$i < count($this->dataary);$i++) {
      if ($i % 2 == 0) {
        $color = $this->firstcolor;
      } else {
        $color = $this->secondcolor;
      }
      $str .= $this->getTrOpen($color);
      $str .= $this->getTableRow($i);
      $str .= "</tr>\n";
    }
    $str .= $this->getTBodyClose();
    $str .= $this->getTFooter();
    $str .= "</table>\n";

    return $str;
  }

  protected function getTBodyOpen()
  {
    return '';
  }

  protected function getTBodyClose()
  {
    return '';
  }

  protected function getTHeadOpen()
  {
    return '';
  }

  protected function getTHeadClose()
  {
    return '';
  }

  protected function getTFooter()
  {
    return '';
  }

  protected function getTrOpen($pColor)
  {
    return '<tr ' . $pColor . " onmouseover=\"setRowID(this, 'farbe_td_highlight');\"onmouseout=\"setRowID(this,'');\">";
  }

  
  public function getTableHeaderCol($colindex)
  {
    $str = '';
    $title = $this->columnsary[$colindex]->title;
    $str .= '  <th ' . $this->tabletitlecolor . " width='" . CleanOperations::cleanInt($this->columnsary[$colindex]->width) . "px'><font face='arial' size='" . CleanOperations::cleanStringForHtml($this->columnsary[$colindex]->fontsize) . "'>";
    if ($this->columnsary[$colindex]->bold == 1) {
      $str .= '<b>';
    }
    
    if ($this->printmodus && ($title === '' || $title === null)) {
      $str .= '&nbsp;';
    } else {
      $str .= '    ' . CleanOperations::cleanStringContainingHtml($title);
    }
    if ($this->columnsary[$colindex]->bold == 1) {
      $str .= '</b>';
    }
    $str .= "  </font></th>\n";

    return $str;
  }

  
  public function getTableHeader()
  {
    $str = $this->getTableHeaderCheckCells();
    if ($this->alignButtonsLeft) {
      $str .= '<th></th>';
    }

    for ($i = 0;$i < count($this->columnsary);$i++) {
      $str .= $this->getTableHeaderCol($i);
    }

    return $this->getTHeadOpen() . '<tr>' . $str . '</tr>' . $this->getTHeadClose() . "\n";
  }

  
  public function getTableHeaderCheckCells()
  {
    return '';
  }

  
  public function isLikeNumber($value)
  {
    $value = str_replace(':', '', $value);
    $value = str_replace('.', '', $value);
    $value = str_replace(',', '', $value);

    return is_numeric($value);
  }

  
  public function getTableRow($rowindex = -1)
  {
    $str = '';
    
    
    if (isset($this->colorevent)) {
      $this->textcolor = $this->colorevent->getTextColor($this->dataary[$rowindex]);
    }
    
    $str .= $this->getTableRowSelection($rowindex);
    for ($i = 0;$i < count($this->columnsary);$i++) {
      $value = $this->getTableRowContent($rowindex, $i);
      $str .= $this->getTableCell($value, $this->columnsary[$i], $this->textcolor);
    }

    return str_replace('{setid}', $this->dataary[$rowindex]->id, $str);
  }

  public function getTableCell($pValue, $pColumn, $pTextcolor = true)
  {
    $str = "  <td width='" . CleanOperations::cleanInt($pColumn->width) . "px' align='" . ($this->isLikeNumber($pValue) ? 'right' : 'left') . "'>";
    $str .= "    <font face='arial' size='" . CleanOperations::cleanStringForHtml($pColumn->fontsize) . "' color='" . CleanOperations::cleanStringForHtml($pTextcolor) . "'>";
    if ($pColumn->bold == 1) {
      $str .= '<b>';
    }
    $str .= $pValue;
    if ($pColumn->bold == 1) {
      $str .= '</b>';
    }
    $str .= "  </font></td>\n";

    return $str;
  }

  
  public function getTableRowSelection($pRowindex)
  {
    return '';
  }

  
  public function _getAmpel($anmeldungeng)
  {
    $modus = '';
    $text = '';
    $grafik = '';
    switch ($anmeldungeng) {
      case 3:
        $modus = $this->objsettings->optionstatus4;
        $text = $this->objsettings->optionstatustext4;
        $grafik = 'images/rot.gif';
        break;
      case 2:
        $modus = $this->objsettings->optionstatus3;
        $text = $this->objsettings->optionstatustext3;
        $grafik = 'images/orange.gif';
        break;
      case 1:
        $modus = $this->objsettings->optionstatus2;
        $text = $this->objsettings->optionstatustext2;
        $grafik = 'images/gruen.gif';
        break;
      case 0:
        $modus = $this->objsettings->optionstatus1;
        $text = $this->objsettings->optionstatustext1;
        $grafik = 'images/gelb.gif';
        break;
    }

    if ($modus == 'text') {
      return '<font id="text_hinweis" size="1">' . CleanOperations::cleanStringForHtml($text) . '</font>';
    } else {
      return '<img src="' . CleanOperations::cleanStringForHtml($grafik) . '" border="0" title="' . CleanOperations::cleanStringForHtml($text) . '">';
    }
  }

  
  public function getTableRowContent($rowindex, $colindex)
  { 
    $value = $this->dataary[$rowindex]->getVarValueEx($this->columnsary[$colindex]->fieldname, $this->columnsary[$colindex]->datamodul);
    
    
    

    
    
    if ($this->checkEmail($value, $colindex)) {
      $validation = ValidierungBasis::getInstance();
      if ($validation->isValidVar('email', $value) === true) {
        $value = '<a href="mailto:' . CleanOperations::cleanStringForHtml($value) . '">' . CleanOperations::cleanStringForHtml($value) . '</a>';
      }
    } 

    $varname = $this->columnsary[$colindex]->fieldname;
    switch ($varname) {
      case 'anmeldungeng':
        return $this->_getAmpel($this->dataary[$rowindex]->getVarValue('anmeldungeng'));
      case 'ausfall':
        if ($this->columnsary[$colindex]->datamodul === 'kurstag') {
          $text = ConstantOperations::getKurstagStatus($value);

          return '<span title="' . CleanOperations::cleanStringForHtml($text) . '">' . CleanOperations::cleanStringForHtml($value) . '</span>';
        }
        
      case 'abschluss': 
        if ($this->columnsary[$colindex]->datamodul === 'kurs') {
          $value = $this->dataary[$rowindex]->getVarValueEx($this->columnsary[$colindex]->fieldname, $this->columnsary[$colindex]->datamodul);

          return ($value !== 'J') ? 'N' : 'J';
        }

        return ($value === '' || $value === null) ? '&nbsp;' : $value;
      case 'klassenbuchueok':

        return ComboboxAPI::getKlassenbuchUEGehalten($this->dataary[$rowindex]->klassenbuchueok, true);
      case 'darstellungWeb':

          $dbwDokumenteWeb = new DBWorkDokumenteWeb(1, true);
          $dokumenteWebAry = $dbwDokumenteWeb->getDokumente4Kurs($this->dataary[0]->id);

          if (!empty($dokumenteWebAry)) {
            $str = 'Dokumente:<ul>';
            foreach ($dokumenteWebAry as $dokumenteWeb) {
              $str .= '<li>'
                      . '<a href="' . CleanOperations::cleanStringForHtml($dokumenteWeb->internet) . '">' . CleanOperations::cleanStringForHtml($dokumenteWeb->bezeichnung) . '</a>'
                      . (($dokumenteWeb->stand != '') ? ' (Stand ' . CleanOperations::cleanStringForHtml($dokumenteWeb->stand) . ')' : '') . BR . CleanOperations::cleanStringForHtml($dokumenteWeb->bemerkung)
                      . '</li>';
            }
            $str .= '</ul>';
          } else {
            $str = 'Für diesen Kurs sind keine Dokumente vorhanden.';
          }

          return $str;
      
      case 'berufsgruppe_komma':
      case 'berufsgruppe_html':
          $separator = 'komma';
          if ($varname == 'berufsgruppe_html') {
            $separator = 'html';
          }
          $value = '';

        $dbwkennz = new DBWorkKennz(1, true);
        $datenklasse = StringOperations::strtolower($this->columnsary[$colindex]->datamodul);
        if ($datenklasse === 'teilnehmer') {
          $tnr = $this->dataary[$rowindex]->getVarValueEx('id', $datenklasse);
        } elseif ($datenklasse === 'belegung') {
          $tnr = $this->dataary[$rowindex]->getVarValueEx('tnr', $datenklasse);
        }
        if (!empty($tnr)) {
          $value = $dbwkennz->_getBerufsgruppen4TN($separator, $tnr);
        }

        return $value;
      
      
      case 'bemerkung_edit':
        $value = $this->dataary[$rowindex]->getVarValueEx('bemerkung', $this->columnsary[$colindex]->datamodul);
        $belid = $this->dataary[$rowindex]->getVarValueEx('id', $this->columnsary[$colindex]->datamodul);
        $hidden = '<input type="hidden" class="hidden_belid" value="' . CleanOperations::cleanInt($belid) . '">';

        return '<img src="images/edit.gif" class="edit_bemerkung"><span class="bel_text">' . CleanOperations::cleanStringForHtml($value) . '</span>' . $hidden;
      case 'bemerk_ext_edit':
        $value = $this->dataary[$rowindex]->getVarValueEx('bemerk_ext', $this->columnsary[$colindex]->datamodul);
        $belid = $this->dataary[$rowindex]->getVarValueEx('id', $this->columnsary[$colindex]->datamodul);
        $hidden = '<input type="hidden" class="hidden_belid" value="' . CleanOperations::cleanInt($belid) . '">';

        return '<img src="images/edit.gif" class="edit_bemerk_ext"><span class="bel_text">' . CleanOperations::cleanStringForHtml($value) . '</span>' . $hidden;
      case 'info_edit':
        $value = $this->dataary[$rowindex]->getVarValueEx('info', $this->columnsary[$colindex]->datamodul);
        $belid = $this->dataary[$rowindex]->getVarValueEx('id', $this->columnsary[$colindex]->datamodul);
        $hidden = '<input type="hidden" class="hidden_belid" value="' . CleanOperations::cleanInt($belid) . '">';

        return '<img src="images/edit.gif" class="edit_info"><span class="bel_text">' . CleanOperations::cleanStringForHtml($value) . '</span>' . $hidden;
      case 'bemerkungen':
        if ($this->columnsary[$colindex]->datamodul === 'kurstag') {
          $ausfall = $this->dataary[$rowindex]->getVarValueEx('ausfall', $this->columnsary[$colindex]->datamodul);
          if ($ausfall == 'A') {
            return 'Achtung: Ausfall ' . $value;
          }
        }
      
      
      
      case 'barrierefrei':
        if ($this->columnsary[$colindex]->datamodul === 'kursort' || $this->columnsary[$colindex]->datamodul === 'gebaeude') {
          if ($this->dataary[$rowindex]->getVarValueEx('barrierefrei', $this->columnsary[$colindex]->datamodul)) {
            return 'Ja';
          } else {
            return '&nbsp;';
          }
        }
      
      
      default:
        return (trim($value) === '' || $value === null) ? '&nbsp;' : $value;
    }
  }

  

  
  public function checkEmail($pValue, $pCol)
  {
    if ($this->showEmailAsLink && ($this->columnsary[$pCol]->fieldname == 'email')
        && !$this->printmodus && trim($pValue) != '') {
      return true;
    }

    return false;
  }

  
  public function getFooter()
  {
    return '';
  }

  
  public function getMaxLength($varname)
  {
    return 0;
  }

  
  public function getEditSaveButton($edit_id)
  {
    return "<input type='image' title='Speichern' src='layout/button_save.gif'>"
      . $this->editsavebuttonhiddenstr . "
      <input type='hidden' name='editid' value='" . CleanOperations::cleanStringForHtml($edit_id) . "'>\n";
  }

  
  public function getEditCancelButton()
  {
    
    
    
    
    return "<a href='" . CleanOperations::cleanStringForHtml($this->editformselfstr . $this->editcancelbuttonhiddenstr) . "'><img border='0' src='layout/button_abort.gif' /></a>";
  }

  
  public function createSight()
  {
    
    $this->clear();
    
    $this->getColumns();
    $this->getData();
    
    $this->sight .= $this->getHeader();
    $this->sight .= $this->getTable();
    $this->sight .= $this->getFooter();
  }

  
  public function createTable()
  { 
    $this->clear();
    
    $this->getColumns();
    $this->getData();
    
    $this->sight .= $this->getTable();
  }

  
  public function getEmailShortWithLink($pValue, $pColindex)
  {
    $value = $pValue;
    if (($this->showEmailAsLink)
      && ($this->columnsary[$pColindex]->fieldname == 'email')
      && (!$this->printmodus)
      && ($value != '')) {
      $orgValue = $value;
      $colWidth = $this->columnsary[$pColindex]->width;
      $charCount = floor($colWidth / 7);

      if (StringOperations::strlen($value) > $charCount) {
        $value = StringOperations::substr($value, 0, $charCount) . '...';
      }
    } else {
      $orgValue = $pValue;
    }

    if ($value === null || $value === '') { 
      $ret = '&nbsp;';
    } else {
      if (($this->columnsary[$pColindex]->fieldname == 'email') && (!$this->printmodus) && ($this->showEmailAsLink)) {
        $ret = "<a href='mailto:" . CleanOperations::cleanStringForHtml($orgValue) . "'>" . CleanOperations::cleanStringForHtml($value) . '</a>';
      } else {
        $ret = $value;
      }
    }

    return $ret;
  }

  
}