예제 #1
0
 public function run()
 {
     $currentLang = Yii::$app->composition['langShortCode'];
     // Get current link
     $current = Yii::$app->menu->current;
     $rule = Yii::$app->menu->currentUrlRule;
     $items = [];
     // Loop through all languages
     foreach (Yii::$app->adminLanguage->getLanguages() as $lang) {
         // Find item of current link with the lang
         $item = Yii::$app->menu->find()->where(['nav_id' => $current->navId])->lang($lang['short_code'])->with('hidden')->one();
         if ($item) {
             $link = $item->link;
             if ($item->type == 2 && !empty($rule)) {
                 $routeParams = [$rule['route']];
                 foreach ($rule['params'] as $key => $value) {
                     $routeParams[$key] = $value;
                 }
                 $compositionObject = Yii::createObject(Composition::className());
                 $compositionObject->off(Composition::EVENT_AFTER_SET);
                 $compositionObject['langShortCode'] = $lang['short_code'];
                 $link = Yii::$app->urlManager->createMenuItemUrl($routeParams, $item->id, $compositionObject);
             }
             $items[] = ['href' => $link, 'isCurrent' => $currentLang == $lang['short_code'], 'language' => $lang];
         } else {
             $items[] = ['href' => Yii::$app->urlManager->prependBaseUrl($lang['short_code']), 'isCurrent' => $currentLang == $lang['short_code'], 'language' => $lang];
         }
     }
     return $this->render('index', ['items' => $items, 'currentLanguage' => Yii::$app->adminLanguage->getLanguageByShortCode($currentLang)]);
 }
예제 #2
0
 public function testMultiDomainMapping()
 {
     $request = new Request();
     $request->pathInfo = 'foo/bar';
     $request->hostInfo = 'example.fr';
     $composition = new \luya\web\Composition($request, ['hostInfoMapping' => ['example.fr' => ['langShortCode' => 'fr', 'x' => 'y']]]);
     $resolv = $composition->getResolvedPathInfo($request);
     $this->assertEquals('fr', $resolv['compositionKeys']['langShortCode']);
     $this->assertTrue(isset($resolv['compositionKeys']['x']));
 }
예제 #3
0
 public function testLocalisation()
 {
     $request = new Request();
     $comp = new Composition($request);
     // default
     $this->assertEquals('en_EN', $comp->getLocale());
     $comp->locales = ['de' => 'de_CH.utf'];
     $comp->setKey('langShortCode', 'de');
     $this->assertEquals('de_CH.utf', $comp->getLocale());
 }
예제 #4
0
 private function urlReplaceModule($url, $navItemId, \luya\web\Composition $composition)
 {
     $route = $this->removeBaseUrl($url);
     $route = $composition->removeFrom($route);
     $module = $this->findModuleInRoute($route);
     if (!$module) {
         return $url;
     }
     $item = $this->menu->find()->where(['id' => $navItemId])->with('hidden')->lang($composition['langShortCode'])->one();
     if (!$item) {
         throw new BadRequestHttpException("Unable to find nav_item_id '{$navItemId}' to generate the module link for url '{$url}'.");
     }
     // if the item type is (2) module and the current context module is not equals we don't have to remove to replace the module name
     // as this is an url rule not related to the current module.
     if ($item->type == 2) {
         if ($module !== $item->moduleName) {
             return $url;
         }
     }
     $replaceRoute = preg_replace("/{$module}/", rtrim($item->link, '/'), ltrim($route, '/'), 1);
     return $replaceRoute;
 }