File: D:/HostingSpaces/SBogers10/topswtwmobile.komma.pro/app/KommaApp/Shop/Menus/LanguageMenu.php
<?php
namespace KommaApp\Shop\menus;
class LanguageMenu extends Menu
{
/*
* @var Array
* languageIds
*/
protected $languageIds = [];
/**
* @return string $listItems
*/
public function getUnsortedList()
{
// Define shop languages
$shopLanguages = \Shop::shopRepository()->getShopsWithLanguageById(\Shop::getId());
foreach ($shopLanguages as $language) {
$this->languageIds[] = $language->language_id;
}
// Check routable Type and get menu items
switch (\Shop::getRouteData()->routable_type) {
case "Komma\\Kms\\Pages\\Models\\PageTranslation":
$currentId = \Shop::getRouteData()->routable->page_id;
$this->menuItems = $this->menuRepository->getMenuItemsFromPageById($currentId);
break;
case "Komma\\Kms\\Categories\\Models\\CategoryTranslation":
$currentId = \Shop::getRouteData()->routable->category_id;
$this->menuItems = $this->menuRepository->getMenuItemsFromCategoryById($currentId);
break;
case "Komma\\Kms\\Products\\Models\\ShopProductTranslation":
$currentId = \Shop::getRouteData()->routable->products_shop_id;
$this->menuItems = $this->menuRepository->getMenuItemsFromProductById($currentId);
break;
}
// Generate output
$listItems = array('first_item' => '', 'other_items' => '');
if (!empty($this->menuItems)) {
$this->menuItems = $this->stripDoubleRoutes($this->menuItems, 'language_id');
foreach ($this->languageIds as $languageId) {
if (isset($this->menuItems[$languageId])) {
$item = $this->menuItems[$languageId];
$this->router->currentRouteName() == '/' . $item->route ? $key = 'first_item' : $key = 'other_items';
$listItems[$key] .= '<li>';
$listItems[$key] .= '<a href="/' . $item->route . '?' . $this->createQueryString() . '">';
$listItems[$key] .= '<span class="flag" style="background-image: url(\'/images/structure/flags/' . $item->iso_2 . '.svg\')"></span>';
$listItems[$key] .= '</a>';
$listItems[$key] .= '</li>';
}
}
}
//Add the german flag for the non germany shops, with redirect to the home page
if (\Shop::getShop()->id == 1) {
$route = (\Config::get('komma/tops.https') == true ? 'https://' : 'http://');
$route .= (\Config::get('komma/tops.www') == true ? 'www.' : '');
$route .= 'topskwlfilter.de';
$route .= '?ref='.\Request::getHttpHost();
$listItems['other_items'] .= '<li>';
$listItems['other_items'] .= '<a href="' . $route .' ">';
$listItems['other_items'] .= '<span class="flag" style="background-image: url(\'/images/structure/flags/de.svg\')"></span>';
$listItems['other_items'] .= '</a>';
$listItems['other_items'] .= '</li>';
}
return $listItems['first_item'] . $listItems['other_items'];
}
public function createQueryString()
{
//Get the Get items
$input = \Input::all();
$input['switch'] = 'language';
return http_build_query($input);
}
/**
* @param array $items
* @param string $keyType
* @return array
*/
protected function stripDoubleRoutes(array $items, $keyType = 'id')
{
$menuItems = [];
$checkForRoutes = [];
foreach ($items as $item) {
$addItem = true;
// If this item already exists with a different route
if (isset($checkForRoutes[$item->{$keyType}])) {
$addItem = false;
$existingRoute = $checkForRoutes[$item->{$keyType}];
// Always make sure the shortest route is added to the array
if (strlen($item->route) > strlen($existingRoute)) {
$addItem = true;
}
}
// Do we need to add / update this item to the array?
if ($addItem) {
$menuItems[$item->{$keyType}] = $item;
// Also update the array $checkForRoutes
$checkForRoutes[$item->{$keyType}] = $item->route;
}
}
return $menuItems;
}
}