oncode of WebsiteBaker
4/22/2014 - 3:10 PM

Display language tabs. Root sites can be named with country or without (de, de-CH, ...).

Display language tabs. Root sites can be named with country or without (de, de-CH, ...).

<?php

global $database;
$language = defined('LANGUAGE') ? LANGUAGE : DEFAULT_LANGUAGE;

$site = 0;

if (defined('PAGE_ID') && PAGE_ID != 0) {
  $site = PAGE_ID;
} else if (isset($_POST['referrer'])) {
  $site = (int)$_POST['referrer'];
} else if (isset($_GET['referrer'])) {
  $site = (int)$_GET['referrer'];
}

switch ($language) {
  case 'NL':
    $textLanguage = 'Taal';
    break;
  case 'EN':
    $textLanguage = 'Language';  
    break;
  case 'FR':
    $textLanguage = 'Langue';
    break;
  case 'IT':
    $textLanguage = 'Lingua';
    break;
  case 'DE':
  default:
    $textLanguage = 'Sprache';
}

$pages = $database->query('
  SELECT
    `' . TABLE_PREFIX . 'pages`.`root_parent`, `parent`.`link`
  FROM
    `' . TABLE_PREFIX . 'pages`
  INNER JOIN
    `' . TABLE_PREFIX . 'pages` as `parent`
    ON `' . TABLE_PREFIX . 'pages`.`root_parent` = `parent`.`page_id`
  WHERE
    `' . TABLE_PREFIX . 'pages`.`page_id` = ' . $site . '
');

$page = $pages->fetchRow();
$rootPageId = $page['root_parent'];

$parts = explode('-', substr($page['link'], 1), 2);

$rootPageCountry = $parts[1];
$rootPageLanguage = $parts[0];

$rootPages = $database->query('
  SELECT
    `page_id`, `link`
  FROM
    `' . TABLE_PREFIX . 'pages`
  WHERE
    `' . TABLE_PREFIX . 'pages`.`level` = 0
  ORDER BY
    `' . TABLE_PREFIX . 'pages`.`position`
');

ob_start();

$countries = array();

while ($rootPage = $rootPages->fetchRow()) {
  $parts = explode('-', substr($rootPage['link'], 1), 2);

  if (count($parts) == 2) {
    $countries[strtoupper($parts[1])][] = array(
      'language' => $parts[0],
      'id'       => $rootPage['page_id']
    );
  }
}

echo '<span class="text">' . $textLanguage . ': </span>';

$i = 0;
$size = count($countries[strtoupper($rootPageCountry)]);

foreach ($countries[strtoupper($rootPageCountry)] as $language) {
  echo '<a class="'
     . ($rootPageLanguage == $language['language'] ? 'active' : '')
     . '" href="'
     . WB_URL . PAGES_DIRECTORY
     . '/'
     . $language['language']
     . '-'
     . strtolower($rootPageCountry) . PAGE_EXTENSION
     . '">'
     . $language['language']
     .'</a>';

  if ($i != $size - 1) {
    echo '<span class="language-separator">|</span>';
  }

  $i++;
}

return ob_get_clean();