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


abstract class TabCollectionPage extends BrowseableObjectsPageBaseWT
{
  
  protected $_tabs;
  protected $_contentHeader;
  protected $_contentId;

  public function __construct($pPageTitle, $pPageId, $pTool, $pContentHeader, $pAnzahlAllerObjekte = 1, $pAnzahlObjekteProSeite = 1)
  {
    parent::__construct($pPageTitle, $pPageId, $pTool, $pAnzahlAllerObjekte, $pAnzahlObjekteProSeite);
    
    
    $pContentHeader = (StringOperations::substr($pContentHeader, -2) == ', ') ? StringOperations::substr($pContentHeader, 0, -2) : $pContentHeader;
    $this->_contentHeader = $pContentHeader;
  }

  
  public function addTabPage($pTabPage)
  {
    if (!is_array($this->_tabs)) {
      $this->_tabs = array();
    }
    $this->_tabs[] = $pTabPage;
  }

  public function setContentId($pContentId)
  {
    $this->_contentId = $pContentId;
  }

  
  public function getContentHeader()
  {
    return $this->_contentHeader;
  }

  
  public function getContent()
  {
    if (!is_array($this->_tabs) || count($this->_tabs) == 0) {
      return 'Ungültige Konfiguration!';
    }
    $result = '';
    $result .= $this->getStatusMessage();
    $tabid = ($this->_contentId != '' ? $this->_contentId : 'defaultTab');
    $result .= '<div id="' . $tabid . "\" class=\"reiter_leiste_div\">\n";
    $result .= '<ul class="reiterleiste">' . "\n";
    $counter = 0;
    foreach ($this->_tabs as $tab) {
      $result .= '<li class="reiter">' . "\n";
      $result .= '<a class="text_reiter" href="#tabs-' . ($counter + 1) . '">' . $tab->getTabName() . '</a>' . "\n";
      $result .= '</li>' . "\n";
      $counter++;
    }
    $result .= "</ul>\n";

    $counter = 1;
    foreach ($this->_tabs as $tab) {
      $result .= '<div class="content tab" id="tabs-' . $counter . '">' . "\n";
      $result .= $tab->getContent();
      $result .= '</div>';
      $counter++;
    }

    $result .= '</div>';
    $result .= '<script type="text/javascript">$(function() {$( "#' . CleanOperations::cleanStringForJs($tabid) . '" ).tabs();});</script>';
    $result .= $this->getButtonLeiste();

    return $result;
  }

  
  protected function getStatusMessage()
  {
    return '';
  }

  
  public function getSpecialJS()
  {
    $result = parent::getSpecialJS();
    foreach ($this->_tabs as $tab) {
      $tabJs = $tab->getSpecialJS();
      foreach ($tabJs as $js) {
        if (!in_array($js, $result)) {
          $result[] = $js;
        }
      }
    }
    $result[] = 'js/filter.js';
    $result[] = 'js/tabpage.js';

    return $result;
  }

  
  public function getSpecialCSS()
  {
    $result = parent::getSpecialCSS();
    foreach ($this->_tabs as $tab) {
      $tabCss = $tab->getSpecialCSS();
      
      if (!empty($tabCss)) {
        foreach ($tabCss as $css) {
          if (!in_array($css, $result)) {
            $result[] = $css;
          }
        }
      }
    }
    $result[] = 'css/tabpage.css';

    return $result;
  }

  
  protected function getButtonLeiste()
  {
    return '';
  }

  public function getJSDeclarations()
  {
    $result = parent::getJSDeclarations();
    foreach ($this->_tabs as $tab) {
      $result .= $tab->getJSDeclarations() . "\n";
    }

    return $result;
  }
}