File: D:/HostingSpaces/SBogers10/topswtw.komma.pro/app/KommaApp/Shop/LanguageService.php
<?php
namespace KommaApp\Shop;
use Komma\Kms\Languages\ShopLanguage;
use KommaApp\Shop\Menus\MenuRepository;
class LanguageService
{
/*
* Array available languages
* [languageId => languageSlug]
*/
protected $availableLanguages;
/*
* String default language slug
*/
protected $defaultLanguage;
/*
* String current language slug
*/
protected $currentLanguage;
/*
* Int current languageId
*/
protected $currentLanguageId;
/*
* int Dialect Choice
* for custom dialect we use the laravel choice method
* default is set to 1
*/
protected $dialectCount = 1;
protected $menuRepository;
public function __construct(MenuRepository $menuRepository)
{
$this->menuRepository = $menuRepository;
}
public function bootFromUri($shopId)
{
$shopLanguages = \Shop::shopRepository()->getShopsWithLanguageById($shopId);
$this->setAvailableLanguages($shopLanguages);
if (!$currentId = $this->getLanguageIdFromUri()) return false;
$this->setCurrentLanguage($currentId);
}
public function bootFromLangId($langId)
{
$shopLanguages = \Shop::shopRepository()->getShopsWithLanguageById();
$this->setAvailableLanguages($shopLanguages);
$this->setCurrentLanguage($langId);
}
/**
* @param array $shopLanguages
* @internal param array $shops
*/
public function setAvailableLanguages(array $shopLanguages)
{
$availableLanguages = [];
$defaultLanguageId = null;
foreach ($shopLanguages as $key => $shopLanguage) {
$availableLanguages[$shopLanguage->language_id] = $shopLanguage->language_slug;
}
$this->availableLanguages = $availableLanguages;
}
public function getAvailableLanguages()
{
return $this->availableLanguages;
}
/**
* @param int $defaultLanguageId
*/
public function setDefaultLanguage($defaultLanguageId)
{
// Pass the default language to the Language object
if (isset($defaultLanguageId)) {
$defaultLanguage = $this->availableLanguages[$defaultLanguageId];
} else {
$defaultLanguage = \Config::get('app.locale');
}
$this->defaultLanguage = $defaultLanguage;
}
public function getLanguagesByShop($shop)
{
if (!$shopLanguages = ShopLanguage::select('language_id', 'languages.iso_2')
->leftJoin('languages', 'languages.id', '=', 'language_id')
->where('shop_id', '=', $shop->id)->get()
) return [$shop->default_language];
$return = [];
foreach ($shopLanguages as $lang) {
$return[$lang->iso_2] = $lang->language_id;
}
return $return;
}
/**
* @return mixed
*/
public function getDefaultLanguage()
{
return $this->defaultLanguage;
}
/*
* Set Current Language by URI
*/
public function setCurrentLanguage($currentLanguageId)
{
$this->currentLanguageId = $currentLanguageId;
$this->currentLanguage = $this->availableLanguages[$currentLanguageId];
\App::setLocale($this->currentLanguage);
$this->setDialect();
}
/**
* @return int
*/
public function getCurrentLanguage()
{
return $this->currentLanguage;
}
/**
* @return int
*/
public function getCurrentLanguageId()
{
return $this->currentLanguageId;
}
/**
* @return int
*/
public function getLanguageIdFromUri()
{
$temp = explode('/', $_SERVER['REQUEST_URI']);
if (!isset($temp[1])) return false;
return array_search($temp[1], $this->availableLanguages);
}
public function getLanguageByUrl(){
if(ends_with(\Request::root(), ['de', 'ch', 'at'])) return 'de';
if(ends_with(\Request::root(), ['nl', 'be'])) return 'nl';
return 'en';
}
/**
* This method will get the the alternate routes
* From the current loaded page
* Including the current page
*
* @return mixed
*/
public function getAlternateRoutes()
{
$menuItems = [];
// Check routable Type and get menu items
switch (\Shop::getRouteData()->routable_type) {
case "Komma\\Kms\\Pages\\Models\\PageTranslation":
$currentId = \Shop::getRouteData()->routable->page_id;
$menuItems = $this->menuRepository->getMenuItemsFromPageById($currentId);
break;
case "Komma\\Kms\\Categories\\Models\\CategoryTranslation":
$currentId = \Shop::getRouteData()->routable->category_id;
$menuItems = $this->menuRepository->getMenuItemsFromCategoryById($currentId);
break;
case "Komma\\Kms\\Products\\Models\\ShopProductTranslation":
$currentId = \Shop::getRouteData()->routable->products_shop_id;
$menuItems = $this->menuRepository->getMenuItemsFromProductById($currentId);
break;
}
return $menuItems;
}
public function getLangByIso($langIso)
{
return \DB::table('languages')
->where('iso_2', '=', $langIso)
->first();
}
/**
* Set dialect depending on domain and language
*
* @return bool
*/
public function setDialect()
{
// Resolve domain
if( ! $domain = \Shop::getDomain()->get()) return false;
// Set Belgian dialect for Dutch language
if($domain == 'topswtwfilters.be' && $this->currentLanguage == 'nl')
{
$this->dialectCount = 2;
}
// Set Austrian dialect for German language
if($domain == 'topskwlfilter.at' && $this->currentLanguage == 'de')
{
$this->dialectCount = 2;
}
}
/*
* For custom dialect we use the laravel choice method
*/
public function dialectCount()
{
return $this->dialectCount;
}
}