<?php

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

interface IPageWT
{
  
  public function getPageTitle();

  
  public function getContentHeader();

  
  public function getContent();

  
  public function getSpecialCss();

  
  public function getSpecialJS();

  
  public function requiresFullScreen();

  
  public function getPageId();

  
  public function getJSDeclarations();

  
  public function getAjaxObject();
}

abstract class PageBaseWT implements IPageWT
{
  
  protected $pageTitle = '';
  
  protected $links;

  
  protected $id = 0;

  
  protected $tool;

  
  protected $toolSettings;

  
  protected $allgSettings;

  
  public function __construct($pPageTitle, $pPageId, $pTool)
  {
    $this->pageTitle = $pPageTitle;
    $this->id = $pPageId;
    $this->tool = $pTool;
    $this->links = array();
    $this->toolSettings = ToolSettingsAPI::getToolSettings($this->tool->getToolId());
    $this->allgSettings = AllgSettingsAPI::getAllgSettings();
  }

  
  public function getPageTitle()
  {
    $algsettings = AllgSettingsAPI::getAllgSettings();
    $toolid = $this->tool->getToolId();

    if ($algsettings->headlineToolueberschriftenWieImMenu && $toolid != 0) {
      $tool0settings = ToolSettingsAPI::getToolSettings(0);
      $jsObjectToArray = new JsObjectToArray($tool0settings->menuBar);

      $beschriftung = \Kufer\Menu\MenuPunkt\MenuPunkt4Tool::buttonGetDefaultBeschriftung($toolid);

      
      foreach ($jsObjectToArray->ToArray() as $array) {
        if (isset($array['toolid']) && $array['toolid'] == $toolid) {
          $beschriftung = $array['beschriftung'] == '' ? $beschriftung : $array['beschriftung'];
        }
      }

      $this->pageTitle = $beschriftung;
    } else {
      
    }

    return $this->pageTitle;
  }

  public function getSpecialCss()
  {
    return array();
  }

  public static function getStandardJS()
  {
    return array('../jquery/jquery-1.12.4.min.js', '../jquery/jquery-migrate-1.4.1.min.js', '../jquery/jquery-ui-1.11.4.custom.min.js',
                  'js/project.js', 'js/json2.js', 'js/base64.js', 'functions.js', 'js/localization.js', '../jquery/plugins/jquery.inlineedit.js',
                  '../jquery/plugins/jquery-ui-timepicker-addon.min.js', '../jquery/plugins/jquery.contextmenu.js',
                  '../jquery/plugins/colorpicker/colorpicker.js', 'js/utils.js', 'js/Formulare/sightbuilder.js');
  }

  public function getSpecialJS()
  {
    return array();
  }

  public function setLinks($pLinks)
  {
    if (is_array($pLinks)) {
      $this->links = $pLinks;
    } else {
      throw new Exception('IllegalArgumentException');
    }
  }

  public static function getStandardCss()
  {
    return array('../jquery/css/smoothness/jquery-ui-1.11.4.custom.min.css',
                 
                 './css/webtools.css', './css/skin.css');
  }

  
  public function getJSDeclarations()
  {
    $ret
    = 'registerNamespace("KuferTools");
     registerNamespace("KuferTools.Spracheinstellungen");
     registerNamespace("KuferTools.KonstantenOderSettings");
     registerNamespace("KuferTools.ReloadSchutz");' . "\n";

    $ret .= JsOperations::getDeclarationInt('KuferTools.ToolSectionId', $this->id);

    $sprachSettings = SettingsFactory::getSettings('SprachSettingsObject');
    $ret .= JsOperations::getDeclarationJson('KuferTools.Spracheinstellungen', $sprachSettings);
    $ret .= JsOperations::getDeclarationInt('KuferTools.ReloadSchutz.PostID', (1 + SessionOperations::getVarValueWTIntern('postId', 0)));

    $objsettings = ToolSettingsAPI::getToolSettings(0);
    $ret .= JsOperations::getDeclarationJson('KuferTools.KonstantenOderSettings', array('MAX_ANZAHL_ATTACHMENTS' => $objsettings->uploadmaxcount));

    return $ret;
  }

  
  protected function getSelektorKursFelderToDetails()
  {
    $felderKursClassArray = array('id', 'nrab', 'kurzbez', 'kurzbez_titel', 'titel', 'titel_zeile1', 'titel_untertitel');

    $selector = '.kurs_';
    $selector .= implode('.formelement, .kurs_', $felderKursClassArray);
    $selector .= '.formelement';

    return JsOperations::getDeclarationString('KuferTools.SetLinksFromFieldsToDetails', $selector);
  }

  
  public function getPageId()
  {
    return $this->id;
  }

  
  public function requiresFullScreen()
  {
    return DARSTELLUNG_MIT_LEFT_MAIN_MENU;
  }

  
  protected function getLinkButton4JS($pText, $pIdAttr = '', $pTitle = '', $pPrivileg = 2, $pHref = '#', $pAddNBSP = false, $pAClass = '')
  {
    if ($pPrivileg == 0) {
      return('');
    }
    $idAttr = ($pIdAttr != '') ? ' id="' . CleanOperations::cleanStringForHtml($pIdAttr) . '" ' : '';
    $aClass = '';
    if ($pAClass != '') {
      $aClass = ' class="' . CleanOperations::cleanStringForHtml($pAClass) . '"';
    }
    if ($pPrivileg == 1) {
      return('<a' . $aClass . '>' . CleanOperations::cleanStringContainingHtml($pText) . '</a>' . ($pAddNBSP ? '&nbsp;' : '') . "\n");
    }
    if ($pPrivileg == 2) {
      return('<a ' . $idAttr . '  href="' . CleanOperations::cleanStringForHtml($pHref) . '" title="' . CleanOperations::cleanStringForHtml($pTitle) . '"' . $aClass . '>' . CleanOperations::cleanStringContainingHtml($pText) . '</a>' . ($pAddNBSP ? '&nbsp;' : '') . "\n");
    }
  }

  protected function getJSForIds($pData)
  {
    $result = '';

    $idsOnPage = ArrayOperations::mapAttribut($pData, 'id');
    $result .= JsOperations::getDeclarationJson('KuferTools.IdsOnPage', $idsOnPage);

    $selectedIds = SessionOperations::getVarValueWTIntern('selnrstr_' . $this->tool->getToolId());
    $selIds = (StringOperations::strlen($selectedIds) > 0) ? explode(':', $selectedIds) : array();
    $result .= JsOperations::getDeclarationJson('KuferTools.SelectedIds', $selIds);

    return $result;
  }

  
  protected function getNoDataMessage($pMessage)
  {
    $result = '<div class="nodatamessage">';
    $result .= CleanOperations::cleanStringForHtml($pMessage);
    $result .= '</div>';

    return $result;
  }

  
  protected function getNoDataMessageDefault($messageType)
  {
    $sprachSettings = SettingsFactory::getSettings('SprachSettingsObject');
    switch ($messageType) {
      case 1: 
        $message = 'In dem ausgewählten Zeitraum finden keine ' . $sprachSettings->Kurs['Plural']['Nominativ'] . ' statt.';
        break;
      case 2: 
        $message = 'Für Ihre Filteroptionen wurden keine ' . $sprachSettings->Kurs['Plural']['Nominativ'] . ' gefunden';
        break;
      case 3: 
        $message = 'Es wurden keine ' . $sprachSettings->Kurs['Plural']['Nominativ'] . ' gefunden.';
        break;
      case 4: 
        $message = 'Es wurden keine Mitarbeiter zu Ihren ' . $sprachSettings->Kurs['Plural']['Dativ'] . ' gefunden.';
        break;
    }

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

  
  protected function getShowMessage4ExternerKursBLAK($pKurs)
  {
    $dbwdoz = new DBWorkDozenten(1, true);
    $doz = $dbwdoz->getAnsprechpByKnr($pKurs->id);

    
    $meldung = '';

    $email = (($doz->firmaemail != '') ? '<a href=\\\'mailto:' . CleanOperations::cleanStringForHtml($doz->firmaemail) . '\\\'>' . CleanOperations::cleanStringForHtml($doz->firmaemail) . '</a>' : '');
    $web = $doz->firmawww;
    if ($web != '') {
      $hatProtocol = StringOperations::hasURLprotocol($web);

      if (!$hatProtocol) {
        $href = 'http://' . $web;
      } else {
        $href = $web;
      }
      $web = '<a href=\\\'' . CleanOperations::cleanStringForHtml($href) . '\\\' target=\\\'_blank\\\'>' . CleanOperations::cleanStringForHtml($web) . '</a>';
    }

    $zeilen = array(
                    array('wert' => '<b>' . $doz->firmaname . '</b>', 'zeilentitel' => ''),
                    array('wert' => $doz->firmazusatz1, 'zeilentitel' => ''),
                    array('wert' => $doz->firmazusatz2, 'zeilentitel' => ''),
                    array('wert' => $doz->firmazusatz3, 'zeilentitel' => ''),
                    array('wert' => $doz->firmastr, 'zeilentitel' => ''),
                    array('wert' => $doz->firmaort, 'zeilentitel' => ''),
                    array('wert' => $doz->firmatel, 'zeilentitel' => 'Tel: '),
                    array('wert' => $doz->firmafax, 'zeilentitel' => 'Fax: '),
                    array('wert' => $email, 'zeilentitel' => 'E-Mail: '),
                    array('wert' => $web, 'zeilentitel' => 'Website: ')
    );

    foreach ($zeilen as $zeile) {
      if ($zeile['wert'] != '') {
        $meldung .= $zeile['zeilentitel'] . $zeile['wert'] . BR_WT;
      }
    }

    $titel = 'Anmeldeinformationen unter:';

    return 'onClick="showMessage(\'' . CleanOperations::cleanStringForJs($meldung) . '\', \'' . CleanOperations::cleanStringForJs($titel) . '\', undefined , 500)"';
  }

  
  public function getAjaxObject()
  {
    $result = new stdClass();
    $result->page = $this->getContent(); 
    $result->header = $this->getContentHeader();
    $result->css = $this->getSpecialCSS();
    $result->js = $this->getSpecialJS();
    $result->jsdeclarations = $this->getJSDeclarations();

    return $result;
  }
}


abstract class BrowseableObjectsPageBaseWT extends PageBaseWT
{
  
  protected $currentIndex;

  
  protected $maxIndex;

  protected $countAllObjects;

  protected $objectsPerPage;

  
  public function __construct($pPageTitle, $pPageId, $pTool, $countAllObjects = 1, $objectsPerPage = 1)
  {
    parent::__construct($pPageTitle, $pPageId, $pTool);
    $this->countAllObjects = $countAllObjects;
    $this->objectsPerPage = $objectsPerPage;

    
    $this->maxIndex = 0;
    $this->currentIndex = 0;
    if ($this->objectsPerPage > 0 && $this->countAllObjects > 0) {
      $this->maxIndex = ceil($this->countAllObjects / $this->objectsPerPage) - 1;
      $this->currentIndex = RequestOperations::getVarValue('currentIndex', 1) - 1; 
      $this->currentIndex = $this->currentIndex < 0 ? 0 : $this->currentIndex;
      $this->currentIndex = $this->currentIndex > $this->maxIndex ? $this->maxIndex : $this->currentIndex;
    }






  }

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

    return $cssAry;
  }

  
  public function getSpecialJS()
  {
    $jsAry = parent::getSpecialJS();
    $jsAry[] = './js/BrowseableObjectsPageBaseWT.js';

    return $jsAry;
  }

  
  protected function getNavigaton()
  {
    $currentIndex = $this->currentIndex;
    $maxIndex = $this->maxIndex;
    $url = URL::loadCurrent();
    $nextLinkContent = '<img src="layout/button_vorwaerts_small.gif" alt="vorw&auml;rts bl&auml;ttern" title="vorw&auml;rts bl&auml;ttern">';
    $previousLinkContent = '<img src="layout/button_rueckwaerts_small.gif" alt="r&uuml;ckw&auml;rts bl&auml;ttern" title="r&uuml;ckw&auml;rts bl&auml;ttern">';
    $cssIdPrefix = 'Uebersicht';

    $catalogBar = new CatalogBarWithJsDialog($currentIndex, $maxIndex, $url->getUrlRelative(), $nextLinkContent, $previousLinkContent, $cssIdPrefix);

    return $catalogBar->Render();
  }

  
  protected function getHinweisPopup($pText, $pImgAltTitle, $pJqTitle)
  {
    return '<img class="hinweis" src="images/help.gif" alt="' . CleanOperations::cleanStringForHtml($pImgAltTitle) . '" title="' . CleanOperations::cleanStringForHtml($pImgAltTitle) . '"
                OnClick="showMessage(\'' . CleanOperations::cleanStringForJs($pText) . '\'' . ($pJqTitle != '' ? ",'" . CleanOperations::cleanStringForJs($pJqTitle) . "'" : '') . ')" >';
  }
}