diff --git a/README.md b/README.md index b2c30b5..2201651 100755 --- a/README.md +++ b/README.md @@ -185,6 +185,10 @@ We now have created a basic Twack-component and you now the general concepts how ## Changelog +### Changes in 2.2.4 (2022-04-29) + +- Api: Added support for Multi-Language URLS + ### Changes in 2.2.3 (2022-03-08) * getAjaxOf: Use AppApi-function if installed diff --git a/Twack.module.php b/Twack.module.php index ba36714..8731156 100755 --- a/Twack.module.php +++ b/Twack.module.php @@ -46,7 +46,7 @@ public static function getModuleInfo() { return [ 'title' => 'Twack', 'author' => 'Sebastian Schendel', - 'version' => '2.2.3', + 'version' => '2.2.4', 'summary' => 'Reusable components for your ProcessWire-templates.', 'singular' => true, 'autoload' => true, diff --git a/TwackApiAccess.class.php b/TwackApiAccess.class.php index 0b55cd3..f36675a 100644 --- a/TwackApiAccess.class.php +++ b/TwackApiAccess.class.php @@ -6,35 +6,62 @@ class TwackApiAccess { public static function pageIDRequest($data) { $data = AppApiHelper::checkAndSanitizeRequiredParameters($data, ['id|int']); $page = wire('pages')->get('id=' . $data->id); - return self::pageRequest($page); + return self::pageRequest($page, ''); } public static function dashboardRequest() { $page = wire('pages')->get('/'); - return self::pageRequest($page); + return self::pageRequest($page, ''); } public static function pagePathRequest($data) { $data = AppApiHelper::checkAndSanitizeRequiredParameters($data, ['path|pagePathName']); - $page = wire('pages')->get('/' . $data->path); - return self::pageRequest($page); + $path = '/' . trim($data->path, '/') . '/'; + $page = wire('pages')->get('path="' . $path . '"'); + + if (!$page->id && wire('modules')->isInstalled('LanguageSupport')) { + // Check if its a root path + $rootPage = wire('pages')->get('/'); + foreach ($rootPage->urls as $key => $value) { + if ($value !== $path) { + continue; + } + return self::pageRequest($rootPage, $key); + } + } + + $info = wire('pages')->pathFinder()->get($path); + if (!empty($info['language']['name'])) { + return self::pageRequest($page, $info['language']['name']); + } + + return self::pageRequest($page, ''); } - protected static function pageRequest(Page $page) { + protected static function pageRequest(Page $page, $languageFromPath) { if (!wire('modules')->isInstalled('Twack')) { throw new InternalServererrorException('Twack module not found.'); } wire('twack')->enableAjaxResponse(); if (wire('modules')->isInstalled('LanguageSupport')) { - $lang = '' . strtolower(wire('input')->get->pageName('lang')); - $langAlt = SELF::getLanguageCode($lang); - if (!empty($lang) && wire('languages')->get($lang) instanceof Page && wire('languages')->get($lang)->id) { - wire('user')->language = wire('languages')->get($lang); - } elseif (!empty($langAlt) && wire('languages')->get($langAlt) instanceof Page && wire('languages')->get($langAlt)->id) { - wire('user')->language = wire('languages')->get($langAlt); + if (!empty($languageFromPath) && wire('languages')->get($languageFromPath) instanceof Page && wire('languages')->get($languageFromPath)->id) { + wire('user')->language = wire('languages')->get($languageFromPath); } else { - wire('user')->language = wire('languages')->getDefault(); + $lang = '' . strtolower(wire('input')->get->pageName('lang')); + $langAlt = SELF::getLanguageCode($lang); + + if (!empty($lang) && wire('languages')->get($lang) instanceof Page && wire('languages')->get($lang)->id) { + wire('user')->language = wire('languages')->get($lang); + } elseif (!empty($langAlt) && wire('languages')->get($langAlt) instanceof Page && wire('languages')->get($langAlt)->id) { + wire('user')->language = wire('languages')->get($langAlt); + } else { + wire('user')->language = wire('languages')->getDefault(); + } + } + + if (!$page->viewable(wire('user')->language)) { + throw new ForbiddenException(); } }