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


class SightConfigurator
{
  
  private $_sightId;

  protected $_classForSaveDiv = '';

  
  protected $_headerheight = 30;

  
  protected $widthConfig;
  
  protected $heightConfig;

  
  public function __construct($sightId, $pWidthConfig = 950, $pHeightConfig = 500)
  {
    $this->_sightId = $sightId;
    $this->widthConfig = $pWidthConfig;
    $this->heightConfig = $pHeightConfig;
  }

  
  public function save()
  {
    $elements = PostOperations::getVarValue('elements');


    $dbwork = new DBWorkSightsSettings();
    
    $dbwork->deleteSettings4Sight($this->_sightId);

    
    if (is_array($elements)) {
      foreach ($elements as $element) {

        
        if (CodingOperations::areInternalAndExternalEncodingEqual()) {
          
          
          
          $encode = new EncodeCharsetFromTo(UTF_8, CodingOperations::getInternalEncoding());
          $element = $encode->encode($element);
        }

        $array = explode('|', $element);
        $isLabel = $array[0];
        $id = $array[1];
        $left = $array[2];
        $top = $array[3];
        $width = $array[4];
        $height = $array[5];
        $title = $array[6];
        $mandatory = $array[7];
        $fontsize = $array[8];
        $depositation = $array[9];
        $bold = $array[10];
        $ausrichtung = $array[11];
        $textfarbe = $array[12];
        $wenn_leer = $array[13];
        

        
        
        $ausrichtung = StringOperations::strtoupper(StringOperations::substr($ausrichtung, 0, 1));
        
        
        
        
        if (StringOperations::substr($textfarbe, 0, 1) != '#') {
          $textfarbe = StringOperations::convertColorRgbToHex($textfarbe);
        }

        $column = new Column();
        $column->sight_field_id = $id;
        $column->title = $isLabel ? $title : '';
        $column->width = $width;
        $column->fontsize = $fontsize;
        $column->bold = $bold;
        $column->x = $left;
        $column->y = $top;
        $column->mandatory_field = $isLabel ? false : $mandatory;
        $column->depositation_only = $isLabel ? false : $depositation;
        $column->height = $height;
        $column->is_label = $isLabel;
        $column->ausrichtung = $ausrichtung;
        $column->textfarbe = $textfarbe;
        $column->wenn_leer = StringOperations::html2Txt($wenn_leer);

        $dbwork->addColumn($column);
      }
    }
  }

  
  public function build()
  {
    $result = '<div id="configurator">';
    $result .= $this->_createConfigDiv();
    $result .= $this->_createElementsDivHeading();
    $result .= $this->_createElementsDiv();
    $result .= $this->_createExplanationDiv();
    $result .= $this->_createSaveButton();
    $result .= $this->_getJQueryMagic();
    $result .= '</div>';

    return $result;
  }

  
  private function _getDialogDiv4LeererInhalt()
  {
    
    $result = '<div id="leerer_inhalt">';
    $result .= '<input id="leer_nichts" type="radio" name="leer" value="0" checked>';
    $result .= '<label for="leer_nichts">das Label des Feldes angezeigt werden.</label>' . BR_WT;
    $result .= '<input id="leer_hide" type="radio" name="leer" value="1">';
    $result .= '<label for="leer_hide">das Label des Feldes nicht angezeigt werden.</label>' . BR_WT;
    $result .= '<input id="leer_alt" type="radio" name="leer" value="2">';
    $result .= '<label for="leer_alt">das Label und folgender Alternativtext angezeigt werden:</label>' . BR_WT;
    $result .= '<input id="leer_alternativtext" size="52">' . BR_WT;
    $result .= '</div>';

    return $result;
  }

  
  protected function _getJQueryMagic()
  {
    return $this->_getDialogDiv4LeererInhalt()
      . '<script type="text/javascript">
        $(document).ready(function() {
        ' . $this->_loadUsedElements() . '
            });
      </script>';
  }

  
  private function _loadUsedElements()
  {
    $dbwork = new DBWorkSightsSettings();
    $elements = $dbwork->getColumns($this->_sightId);

    $result = "\n";

    foreach ($elements as $element) {
      $id = $element->is_label ? ('label_' . $element->sight_field_id) : $element->sight_field_id;

      $x = (int)$element->x;
      $y = (int)$element->y;

      $ausrichtung = $element->getAusrichtung4CSS();
      $textfarbe = (!empty($element->textfarbe)) ? $element->textfarbe : '#000000';

      
      if ($element->height < 10) {
        $element->height = 10;
      }

      if ($element->width < 10) {
        $element->width = 10;
      }

      if ($element->fontsize < 7 || $element->fontsize > $element->height - 3) {
        $element->fontsize = $element->height - 3;
      }

      $result
        .= "$('#" . CleanOperations::cleanStringForJs($id) . "')"
        . ".detach().appendTo($('#config'))"
        . ".css('left', " . CleanOperations::cleanInt($x) . ')'
        . ".css('top', " . CleanOperations::cleanInt($y) . ')'
        . ".css('position', 'absolute')"
        . '.height(' . CleanOperations::cleanFloatMitPunkt($element->height) . ')'
        . '.width(' . CleanOperations::cleanFloatMitPunkt($element->width) . ')'
        . ".css('font-size', " . CleanOperations::cleanFloatMitPunkt($element->fontsize) . ')'
        . ".css('text-align', '" . CleanOperations::cleanStringForJs($ausrichtung) . "')"
        . ".css('color', '" . CleanOperations::cleanStringForJs($textfarbe) . "')";

      if ($element->mandatory_field) { 
        $result .= ".addClass('mandatory')";
      }
      if ($element->bold) { 
        $result .= ".addClass('bold')";
      }
      if ($element->is_label && !empty($element->title)) { 
        
        
        
        
        
        
        

        $result .= '.find( "span.text" ).text("' . CleanOperations::cleanStringForJs($element->title) . '")';
        $result .= ";\n $('#" . CleanOperations::cleanInt($element->sight_field_id) . "').find('span.text').text('" . CleanOperations::cleanStringForJs($element->title) . "')";
      }

      $result .= ";\n";

      
      
      
      
      $result
      .= " $('#" . CleanOperations::cleanStringForJs($id) . "').data('wenn_leer','" . CleanOperations::cleanStringForJs($element->wenn_leer) . "');\n";
    }

    return $result;
  }

  
  

  
  protected function _createElementsDiv()
  {
    $result = '<div id="elements" class="droppable elementsdiv"' . $this->_getStyleForElementsDiv() . '>';

    $dbfg = new DBWorkFieldGroups();
    $dbsf = new DBWorkSightsFields();
    $groups = $dbfg->getFieldGroupsbySightID($this->_sightId);


    foreach ($groups as $group) {
      $result .= '<h3>' . CleanOperations::cleanStringForHtml($group->text) . '</h3>';
      $fields = $dbsf->getFieldsbyGroupID($group->id, $this->_sightId);
      
      foreach ($fields as $field) {
        $result .= $this->_createLabel($field);
        $result .= $this->_createInput($field);
        $result .= BR_WT;
      }
    }

    $result .= '</div>';

    return $result;
  }

  
  protected function _getStyleForElementsDiv()
  {
    
    return 'style="top: ' . CleanOperations::cleanInt($this->heightConfig + 10 + 30  + $this->_headerheight) . 'px;"';
  }

  
  private function _createLabel($field)
  {
    return sprintf(
      '<label class="configurable dnr" id="label_%s" name="%s"><span class="text">%s:</span></label>',
      CleanOperations::cleanStringForHtml($field->id),
      CleanOperations::cleanStringForHtml($field->name),
      CleanOperations::cleanStringForHtml($field->name)
    );
  }

  
  private function _createInput($field)
  {
    return sprintf(
      '<span class="configurable dnr inputspan %s" id="%s"><span class="text">%s</span></span>',
      ($field->canedit == false) ? 'readonly' : '',
      CleanOperations::cleanStringForHtml($field->id),
      CleanOperations::cleanStringForHtml($field->name)
    );
  }

  
  protected function _createConfigDiv()
  {
    if ($this->heightConfig > 0 || $this->widthConfig > 0) {
      $width = 'width: ' . CleanOperations::cleanInt($this->widthConfig) . 'px;';
      $height = 'height: ' . CleanOperations::cleanStringForHtml($this->heightConfig) . 'px;';
      $style = 'style="' . $width . ' ' . $height . '"';
    }

    return
      '<div id="config" class="droppable configdiv" ' . $style . '>'
      . '</div>';
  }

  protected function _createExplanationDiv()
  {
    $style = ' top: ' . CleanOperations::cleanInt($this->heightConfig + 10 + 30 + $this->_headerheight) . 'px; left: 510px;';
    $styleHeading = ' top: ' . CleanOperations::cleanInt($this->heightConfig + 10 + 30) . 'px; left: 510px;';
    $result = '<h3 style="position: absolute; ' . $styleHeading . '">Erklärung:</h3>';
    $result .= '<div class="explanation" style="' . $style
      . '">Zum Einfügen der Felder einfach mit der Maus in den festgelegten Bereich ziehen und dann loslassen.' . BR_WT
      . 'Links finden Sie die "Überschriften" mit denen Sie die Felder bezeichnen können (Beispiel: Dozent(Name, Vorname) - geändert in DozentIn:).' . BR_WT
      . 'Rechts die eigentlichen Werte (Beispiel: Lex, Andrea).' . BR_WT
      . 'Ergibt in der Anzeige: "DozentIn: Lex, Andrea"' . BR_WT
      . 'Mit Rechtsklick auf die Felder haben Sie weitere Optionen wie Schriftgröße, "fette" Schriftformatierung oder Ausrichtung zur Auswahl.</div>';

    return $result;
  }

  protected function _createElementsDivHeading()
  {
    $style = ' style="position: absolute; top: ' . CleanOperations::cleanInt($this->heightConfig + 10 + $this->_headerheight) . 'px;"';

    return '<h3' . $style . '>Variablen und Felder, die zur Verfügung stehen:</h3>';
  }

  
  protected function _createSaveButton()
  {
    $style = ' style="position: absolute; top: ' . CleanOperations::cleanInt($this->heightConfig + 10) . 'px;"';
    $class = '';
    if ($this->_classForSaveDiv != '') {
      $class = 'class="' . CleanOperations::cleanStringForHtml($this->_classForSaveDiv) . '" ';
    }

    return
      '<div id="savediv"' . $class . $style . '>'
      . '<input id="save" type="image" src="layout/button_save_sight.gif" title="Speichern" alt="Speichern" />'
      . '</div>';
  }
}