<?php

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


class PageMaAdressUebersicht extends BrowseableObjectsPageBaseWT
{
  private $_data;

  private $_searchadresse;
  private $_statusMessage;
  private $_adrSuchForm;

  public function __construct($pTool, $pData, $pAnzahlAllerAdressen, $pAnzahlAdressenProSeite, $pPageId, $pStatusMessage = '', $pAdrSuchForm = '')
  {
    
    
    parent::__construct('Übersicht der Adressen', $pPageId, $pTool, $pAnzahlAllerAdressen, $pAnzahlAdressenProSeite);
    $this->_data = $pData;
    
    
    $this->_searchadresse = (GetOperations::getVarValue('searchadresse', false) ? '&amp;searchadresse=1' : '');
    $this->_statusMessage = $pStatusMessage;
    $this->_adrSuchForm = $pAdrSuchForm;
  }

  
  public function getContentHeader()
  {
    
    return 'Adressen';
  }

  

  
  public function getContent()
  {
    
    $sectionId4Reiter = 410;

    $menu = new Menu();
    

    $tool_id = $this->tool->getToolId();
    $toolsection_id = $this->id; 

    $allowDetails = (isset($this->links['adressDetails']) && $this->links['adressDetails'] ? 2 : 0);
    $result = '';

    if ($this->_statusMessage !== '') {
      $result .= $this->_statusMessage;
    }

    $result .= '<div id="main_content">' . "\n";

    if (!(count($this->_data) == 0 && $this->allgSettings->buttonleite_position == BUTTONBAR_BOTH)
       && ($this->allgSettings->buttonleite_position == BUTTONBAR_TOP || $this->allgSettings->buttonleite_position == BUTTONBAR_BOTH)) {
      $result .= $this->_getMainButtons();
    }

    $counter = 0;
    if (count($this->_data) > 0) {
      foreach ($this->_data as $adresse) {
        $builder = new SightBuilderUebersicht($this->tool->getToolId(), $toolsection_id, $adresse, true);
        
        $evenClass = ($counter % 2 == 0) ? ' farbe_sicht_erstezeile' : '';
        $result .= '<div class="content_zeile' . $evenClass . '">';
        $result .= '<div class="steuerelemente">';

        if (isset($this->links['checkboxen']) && $this->links['checkboxen']) {
          $result .= '<input type="checkbox" id="adr_' . CleanOperations::cleanInt($adresse->id) . '"  name="' . CleanOperations::cleanInt($adresse->id) . '" class="adrcheckbox markable" value="' . CleanOperations::cleanInt($adresse->id) . '">' . BR_WT;
        }

        
        $result .= $menu->getUserLink(
          '<img src="images/kursdetails.gif" alt="Zu den Details" title="Adressdetails">',
          'usertools.php?tool_id=' . CleanOperations::cleanInt($tool_id) . '&toolsection_id=' . $sectionId4Reiter . '&anr=' . CleanOperations::cleanInt($adresse->id) 
                                      . '&currentIndex=' . CleanOperations::cleanInt($this->currentIndex * $this->objectsPerPage + $counter + 1),
          $allowDetails,
          false
        );

        $result .= '</div>';
        $result .= $builder->buildSight();
        $result .= '</div>' . "\n";
        $counter++;
      }
    } else { 
      if ($this->_searchadresse == '') {
        
        $result .= $this->getNoDataMessageDefault(1);
      } else {
        
        $result .= $this->getNoDataMessageDefault(2);
      }
    }
    $result .= '</div>' . "\n";
    if ($this->allgSettings->buttonleite_position != BUTTONBAR_TOP) {
      $result .= $this->_getMainButtons();
    }

    $result .= $this->_adrSuchForm;

    return $result;
  }

  

  
  private function _getMainButtons()
  {
    

    $result = '';
    
    if (count($this->_data) > 0) {
      $result .= '<div class="main_buttons adr">';

      if (isset($this->links['markieren']) && $this->links['markieren']) {
        $result .= "<input type='image' name='markieren' value='1' src='layout/button_small_markieren.gif' border='0' "
              . " title='Markieren von Adressen' alt='Markieren von Adressen' class=\"alleMarkieren\">\n";
        static $isDialogSet = false;
        if (!$isDialogSet) {
          $result .= $this->_getDialogMarkieren();
          $isDialogSet = true;
        }
      }
    }
    
    if (isset($this->links['newAdresse']) && $this->links['newAdresse']) {
      $result .= '<a id="newAdresse" class="button_img" href="#" title="neue Adresse">'
                   . HTMLOperations::createButtonSpan('neue Adresse', 'neue Adresse', '', '', 'images/adressadd.gif')
                   . '</a>' . "\n";
    }


    
    if (count($this->_data) > 0) {
      if (isset($this->links['print']) && $this->links['print']) {
        $result .= '<a href="javascript:window.print();" id="DruckAdressen" title="Adressliste drucken">'
                   . '<img src="layout/button_druck_small.gif" alt="Adressliste drucken"></a>' . "\n";
      }
    }

    
    if (count($this->_data) > 0 || $this->_searchadresse != '') {
      if (isset($this->links['filter'])) {
        
        
        $imgText = '<img src="layout/button_search_small24.gif" title="Suchfilter" alt="Suchfilter">';
        $result .= $this->getLinkButton4JS($imgText, '', 'Suchfilter', $this->links['filter'], '#ui-dialog-title-adresssuche', false, 'smallmargin_right adrSuchbutton');
      }
    }
    if (count($this->_data) > 0) {
      
      $result .= $this->getNavigaton();

      $result .= "\n" . '</div>' . "\n";
    }  

    return $result;
  }

  

  
  protected function getNoDataMessageDefault($messageType)
  {

    
    switch ($messageType) {
      default:
      case 1: 
        $message = 'Es sind keine Adressen vorhanden.';
        break;
      case 2: 
        $message = 'Für Ihre Filteroptionen wurden keine Adressen gefunden';
        break;
    }

    return $this->getNoDataMessage($message);
  }

  
  private function _getDialogMarkieren()
  {
    
    $textAlle = 'Alle ' . BR . 'markieren';
    $titleAltAlle = 'Alle Adressen markieren';
    $textKeiner = 'Alle Markierungen' . BR . 'entfernen';
    $titleAltKeiner = 'Adressen nicht markieren';
    $textAlleDerSeite = 'Alle dieser' . BR . 'Seite markieren';
    $titleAltAlleDerSeite = 'Adressen dieser Seite markieren';
    $textKeinerDerSeite = ' Markierungen dieser ' . BR . 'Seite entfernen';
    $titleAltKeinerDerSeite = 'Adressen dieser Seite nicht markieren';

    return '<div id="dialog_markierenneu">'
              . '<div class="nowrap">'
              . HTMLOperations::createButtonSpan($textAlle, $titleAltAlle, 'mark', 'markall', 'images/okay.gif')
              . HTMLOperations::createButtonSpan($textKeiner, $titleAltKeiner, 'mark', 'unmarkall', 'images/abort.gif')
            . '</div>'
            . '<div class="nowrap">'
              . HTMLOperations::createButtonSpan($textAlleDerSeite, $titleAltAlleDerSeite, 'mark', 'markside', 'images/okay.gif')
              . HTMLOperations::createButtonSpan($textKeinerDerSeite, $titleAltKeinerDerSeite, 'mark', 'unmarkside', 'images/abort.gif')
            . '</div>'
          . '</div>';
  }

  

  public function getJSDeclarations()
  {
    $result = parent::getJSDeclarations();
    $result .= 'registerNamespace("KuferTools");' . "\n";
    $result .= JsOperations::getDeclarationInt('KuferTools.ToolSectionId', $this->id);
    $result .= $this->getJSForIds($this->_data);

    $sprachSettings = SettingsFactory::getSettings('SprachSettingsObject');
    $result .= JsOperations::getDeclarationJson('KuferTools.Spracheinstellungen', $sprachSettings);
    $result .= 'KuferTools.AdressInfo;' . "\n";

    return $result;
  }

  public function getSpecialCSS()
  {
    $cssAry = parent::getSpecialCss();
    $cssAry[] = './css/personenuebersicht.css';

    return $cssAry;
  }

  public function getSpecialJS()
  {
    $fileAry = parent::getSpecialJS();


    $fileAry[] = 'js/MATool/adressinfo.js';
    $fileAry[] = 'js/MATool/personendetails.js';

    return $fileAry;
  }
}