From c0b9a6e8c5fe79fc2c1bde8ea587adfa653e8f15 Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Thu, 20 Aug 2015 10:54:27 -0600 Subject: [PATCH 1/5] missing lang string --- languages.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/languages.yaml b/languages.yaml index dd9adb7e..2d6ac697 100644 --- a/languages.yaml +++ b/languages.yaml @@ -267,6 +267,7 @@ en: DISABLED: Disabled ITEMS: Items ORDER_BY: Order By + ORDER: Order FOLDER: Folder TITLE: Title DATE: Date From 8e09213dc97fdcdd2a5e25befaf6d886703bfc89 Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Thu, 20 Aug 2015 14:55:24 -0600 Subject: [PATCH 2/5] Fix for #84 - unique page counting --- classes/admin.php | 9 +++++++-- themes/grav/templates/partials/nav.html.twig | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/classes/admin.php b/classes/admin.php index 99bbef95..2e56bd8b 100644 --- a/classes/admin.php +++ b/classes/admin.php @@ -364,12 +364,17 @@ class Admin * * @return array */ - public function routes() + public function routes($unique = false) { /** @var Pages $pages */ $pages = $this->grav['pages']; - return $pages->routes(); + if ($unique) { + $routes = array_unique($pages->routes()); + } else { + $routes = $pages->routes(); + } + return $routes; } /** diff --git a/themes/grav/templates/partials/nav.html.twig b/themes/grav/templates/partials/nav.html.twig index 3a188ad4..50dc4aff 100644 --- a/themes/grav/templates/partials/nav.html.twig +++ b/themes/grav/templates/partials/nav.html.twig @@ -27,7 +27,7 @@ {{ "PLUGIN_ADMIN.PAGES"|tu }} - {{ admin.routes|length }} + {{ admin.routes(true)|length }} From 3692dca100c22765386005014cbdd5a329968231 Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Thu, 20 Aug 2015 11:29:37 -0600 Subject: [PATCH 3/5] Default to english if language is not set --- classes/admin.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/classes/admin.php b/classes/admin.php index 2e56bd8b..99760aee 100644 --- a/classes/admin.php +++ b/classes/admin.php @@ -165,6 +165,12 @@ class Admin { if (!$this->user->authenticated && isset($form['username']) && isset($form['password'])) { $user = User::load($form['username']); + + //default to english if language not set + if (empty($user->language)) { + $user->set('language', 'en'); + } + if ($user->exists()) { $user->authenticated = true; From 0af4d223fef80e17b72e63ce6482c42aabdc3d77 Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Thu, 20 Aug 2015 15:39:13 -0600 Subject: [PATCH 4/5] redirect to rawroute --- classes/controller.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classes/controller.php b/classes/controller.php index ee3f7347..22eb4802 100644 --- a/classes/controller.php +++ b/classes/controller.php @@ -893,7 +893,7 @@ class AdminController if (method_exists($obj, 'unsetRouteSlug')) { $obj->unsetRouteSlug(); } - $this->setRedirect($this->view . $obj->route()); + $this->setRedirect($this->view . $obj->rawRoute()); } return true; From 8aa5c0ac2f589a29f15acff00e87fe87d5fb5c23 Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Thu, 20 Aug 2015 18:11:54 -0600 Subject: [PATCH 5/5] various fixes for redirects and parents --- classes/admin.php | 11 +++- classes/controller.php | 63 +++++++++++++------ .../forms/fields/select/select.html.twig | 2 +- 3 files changed, 55 insertions(+), 21 deletions(-) diff --git a/classes/admin.php b/classes/admin.php index 99760aee..01782849 100644 --- a/classes/admin.php +++ b/classes/admin.php @@ -590,7 +590,7 @@ class Admin $name = $page->modular() ? str_replace('modular/', '', $data['type']) : $data['type']; $page->name($name . '.md'); $page->header($header); - $page->frontmatter(Yaml::dump((array)$page->header())); + $page->frontmatter(Yaml::dump((array)$page->header(), 10, 2, false)); } else { // Find out the type by looking at the parent. $type = $parent->childType() ? $parent->childType() : $parent->blueprints()->get('child_type', @@ -642,7 +642,14 @@ class Admin */ public static function route() { - return dirname('/' . Grav::instance()['admin']->route); + $pages = Grav::instance()['pages']; + $route = '/' . ltrim(Grav::instance()['admin']->route, '/'); + + $page = $pages->dispatch($route); + $parent = $page->parent(); + $parent_route = $parent->rawRoute(); + + return $parent_route; } /** diff --git a/classes/controller.php b/classes/controller.php index 22eb4802..31136501 100644 --- a/classes/controller.php +++ b/classes/controller.php @@ -118,22 +118,39 @@ class AdminController } $base = $this->admin->base; - $this->redirect = '/' . ltrim($this->redirect, '/'); + $multilang = (count($this->grav['config']->get('system.languages.supported', [])) > 1); + $redirect = ''; + if ($multilang) { + // if base path does not already contain the lang code, add it + $langPrefix = '/' . $this->grav['session']->admin_lang; + if (!Utils::startsWith($base, $langPrefix . '/')) { + $base = $langPrefix . $base; + } - if (!Utils::startsWith($this->redirect, $base)) { - $this->redirect = $base . $this->redirect; + // now the first 4 chars of base contain the lang code. + // if redirect path already contains the lang code, and is != than the base lang code, then use redirect path as-is + if (Utils::pathPrefixedByLangCode($base) && + Utils::pathPrefixedByLangCode($this->redirect) && + substr($base, 0, 4) != substr($this->redirect, 0, 4)) { + $redirect = $this->redirect; + } else { + if (!Utils::startsWith($this->redirect, $base)) { + $this->redirect = $base . $this->redirect; + } + } + + } else { + if (!Utils::startsWith($this->redirect, $base)) { + $this->redirect = $base . $this->redirect; + } } - $this->grav->redirect($this->redirect, $this->redirectCode); + if (!$redirect) { + $redirect = $this->redirect; + } -// if ($base[3] !== '/') { -// $base = '/' . $this->grav['session']->admin_lang . $base; -// } - -// $path = trim(substr($this->redirect, 0, strlen($base)) == $base ? substr($this->redirect, strlen($base)) : $this->redirect, '/'); - -// $this->grav->redirect($base . '/' . preg_replace('|/+|', '/', $path), $this->redirectCode); + $this->grav->redirect($redirect, $this->redirectCode); } /** @@ -834,14 +851,13 @@ class AdminController $parent = $route && $route != '/' ? $pages->dispatch($route, true) : $pages->root(); $obj = $this->admin->page(true); + $original_slug = $obj->slug(); $original_order = intval(trim($obj->order(), '.')); - - // Change parent if needed and initialize move (might be needed also on ordering/folder change). $obj = $obj->move($parent); - $this->preparePage($obj); + $this->preparePage($obj, false, $obj->language()); // Reset slug and route. For now we do not support slug twig variable on save. $obj->slug($original_slug); @@ -893,7 +909,10 @@ class AdminController if (method_exists($obj, 'unsetRouteSlug')) { $obj->unsetRouteSlug(); } - $this->setRedirect($this->view . $obj->rawRoute()); + if (!$obj->language()) { + $obj->language($this->grav['session']->admin_lang); + } + $this->setRedirect('/' . $obj->language(). '/admin/' . $this->view . $obj->rawRoute()); } return true; @@ -1065,7 +1084,8 @@ class AdminController } $this->admin->setMessage($this->admin->translate('PLUGIN_ADMIN.SUCCESSFULLY_SWITCHED_LANGUAGE'), 'info'); - $this->setRedirect($redirect); + + $this->setRedirect('/' . $language .'/admin/' . $redirect); return true; } @@ -1112,7 +1132,7 @@ class AdminController } $this->admin->setMessage($this->admin->translate('PLUGIN_ADMIN.SUCCESSFULLY_SWITCHED_LANGUAGE'), 'info'); - $this->setRedirect($this->view . $aPage->route()); + $this->setRedirect('/' . $language . $uri->route()); return true; } @@ -1246,6 +1266,13 @@ class AdminController // Special case for Expert mode: build the raw, unset content if (isset($input['frontmatter']) && isset($input['content'])) { + // validate frontmatter manually + try { + $test = Yaml::parse($input['frontmatter'], true); + } catch (\RuntimeException $e) { + throw new \RuntimeException(sprintf('Validation failed: %s', $e->getMessage())); + } + $page->raw("---\n" . (string) $input['frontmatter'] . "\n---\n" . (string) $input['content']); unset($input['content']); } @@ -1278,7 +1305,7 @@ class AdminController }); } $page->header((object) $header); - $page->frontmatter(Yaml::dump((array) $page->header())); + $page->frontmatter(Yaml::dump((array) $page->header(), 10, 2, false)); } // Fill content last because it also renders the output. if (isset($input['content'])) { diff --git a/themes/grav/templates/forms/fields/select/select.html.twig b/themes/grav/templates/forms/fields/select/select.html.twig index 68377921..ef34f0ed 100644 --- a/themes/grav/templates/forms/fields/select/select.html.twig +++ b/themes/grav/templates/forms/fields/select/select.html.twig @@ -15,7 +15,7 @@ {% if field.form %}form="{{ field.form }}"{% endif %} > {% for key, text in field.options %} - + {% endfor %}