mirror of
https://github.com/getgrav/grav-plugin-admin.git
synced 2026-05-07 23:18:00 +02:00
Merge branch 'feature/multilang' of github.com:getgrav/grav-plugin-admin into 1.10
# Conflicts: # CHANGELOG.md # classes/plugin/AdminController.php # classes/plugin/Twig/AdminTwigExtension.php
This commit is contained in:
@@ -10,6 +10,7 @@ use Grav\Common\GPM\Licenses;
|
||||
use Grav\Common\GPM\Response;
|
||||
use Grav\Common\Grav;
|
||||
use Grav\Common\Helpers\YamlLinter;
|
||||
use Grav\Common\Language\Language;
|
||||
use Grav\Common\Language\LanguageCodes;
|
||||
use Grav\Common\Page\Collection;
|
||||
use Grav\Common\Page\Interfaces\PageInterface;
|
||||
@@ -24,6 +25,8 @@ use Grav\Common\User\Interfaces\UserCollectionInterface;
|
||||
use Grav\Common\User\User;
|
||||
use Grav\Common\Utils;
|
||||
use Grav\Framework\Collection\ArrayCollection;
|
||||
use Grav\Framework\Route\Route;
|
||||
use Grav\Framework\Route\RouteFactory;
|
||||
use Grav\Plugin\Login\Login;
|
||||
use Grav\Plugin\Login\TwoFactorAuth\TwoFactorAuth;
|
||||
use PicoFeed\Parser\MalformedXmlException;
|
||||
@@ -74,8 +77,11 @@ class Admin
|
||||
/** @var bool */
|
||||
public $multilang;
|
||||
|
||||
/** @var string */
|
||||
public $language;
|
||||
|
||||
/** @var array */
|
||||
public $languages_enabled;
|
||||
public $languages_enabled = [];
|
||||
|
||||
/** @var Uri $uri */
|
||||
protected $uri;
|
||||
@@ -128,27 +134,25 @@ class Admin
|
||||
$this->session = $grav['session'];
|
||||
$this->user = $grav['user'];
|
||||
$this->permissions = [];
|
||||
$language = $grav['language'];
|
||||
|
||||
/** @var Language $language */
|
||||
$language = $grav['language'];
|
||||
|
||||
$this->multilang = $language->enabled();
|
||||
|
||||
// Load utility class
|
||||
if ($language->enabled()) {
|
||||
$this->multilang = true;
|
||||
if ($this->multilang) {
|
||||
$this->language = $language->getLanguage();
|
||||
$this->languages_enabled = (array)$this->grav['config']->get('system.languages.supported', []);
|
||||
|
||||
//Set the currently active language for the admin
|
||||
$language = $this->grav['uri']->param('lang');
|
||||
if (!$language) {
|
||||
if (!$this->session->admin_lang) {
|
||||
$this->session->admin_lang = $this->grav['language']->getLanguage();
|
||||
}
|
||||
$language = $this->session->admin_lang;
|
||||
$languageCode = $this->grav['uri']->param('lang');
|
||||
if (!$languageCode && !$this->session->admin_lang) {
|
||||
$this->session->admin_lang = $language->getLanguage();
|
||||
}
|
||||
$this->grav['language']->setActive($language ?: 'en');
|
||||
} else {
|
||||
$this->grav['language']->setActive('en');
|
||||
$this->multilang = false;
|
||||
$this->language = 'en';
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -180,6 +184,11 @@ class Admin
|
||||
return $languages;
|
||||
}
|
||||
|
||||
public function getLanguage(): string
|
||||
{
|
||||
return $this->language;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the found configuration blueprints
|
||||
*
|
||||
@@ -276,15 +285,56 @@ class Admin
|
||||
return Grav::instance()['session']->lastPageRoute ?: self::route();
|
||||
}
|
||||
|
||||
public function getAdminRoute(string $path = '', $languageCode = null): Route
|
||||
{
|
||||
/** @var Language $language */
|
||||
$language = $this->grav['language'];
|
||||
$languageCode = $languageCode ?? $language->getLanguage();
|
||||
$languagePrefix = $language->getLanguageURLPrefix($languageCode);
|
||||
|
||||
$parts = [
|
||||
'path' => $path,
|
||||
'query' => '',
|
||||
'query_params' => [],
|
||||
'grav' => [
|
||||
// TODO: Make URL to be /admin/en, not /en/admin.
|
||||
'root' => RouteFactory::getRoot() . $languagePrefix . $this->base,
|
||||
'language' => '', //$languageCode,
|
||||
'route' => ltrim($path, '/'),
|
||||
'params' => ''
|
||||
],
|
||||
];
|
||||
|
||||
return RouteFactory::createFromParts($parts);
|
||||
}
|
||||
|
||||
public function adminUrl(string $route = '', $languageCode = null)
|
||||
{
|
||||
/** @var string $base_url */
|
||||
$baseUrl = $this->grav['base_url'];
|
||||
|
||||
return $baseUrl . $this->getAdminRoute($route, $languageCode);
|
||||
}
|
||||
|
||||
/**
|
||||
* Static helper method to return current route.
|
||||
*
|
||||
* @return string
|
||||
* @deprecated 1.10 Use $admin->getCurrentRoute() instead
|
||||
*/
|
||||
public static function route()
|
||||
{
|
||||
user_error(__CLASS__ . '::' . __FUNCTION__ . '() is deprecated since Admin 1.9.7, use $admin->getCurrentRoute() instead', E_USER_DEPRECATED);
|
||||
|
||||
$admin = Grav::instance()['admin'];
|
||||
|
||||
return $admin->getCurrentRoute();
|
||||
}
|
||||
|
||||
public function getCurrentRoute()
|
||||
{
|
||||
$pages = Grav::instance()['pages'];
|
||||
$route = '/' . ltrim(Grav::instance()['admin']->route, '/');
|
||||
$route = '/' . ltrim($this->route, '/');
|
||||
|
||||
/** @var PageInterface $page */
|
||||
$page = $pages->dispatch($route);
|
||||
@@ -298,6 +348,64 @@ class Admin
|
||||
return $parent_route;
|
||||
}
|
||||
|
||||
/**
|
||||
* Redirect to the route stored in $this->redirect
|
||||
*
|
||||
* Route may or may not be prefixed by /en or /admin or /en/admin.
|
||||
*
|
||||
* @param string $redirect
|
||||
* @param int$redirectCode
|
||||
*/
|
||||
public function redirect($redirect, $redirectCode = 303)
|
||||
{
|
||||
// No redirect, do nothing.
|
||||
if (!$redirect) {
|
||||
return;
|
||||
}
|
||||
|
||||
$redirect = '/' . ltrim($redirect, '/');
|
||||
$base = $this->base;
|
||||
|
||||
// Check if we already have an admin path: /admin.
|
||||
if (Utils::startsWith($redirect, $base)) {
|
||||
$this->grav->redirect($redirect, $redirectCode);
|
||||
}
|
||||
|
||||
if ($this->isMultilang()) {
|
||||
// Check if URL does not have language prefix.
|
||||
if (!Utils::pathPrefixedByLangCode($redirect)) {
|
||||
/** @var Language $language */
|
||||
$language = $this->grav['language'];
|
||||
|
||||
// Prefix path with language prefix: /en
|
||||
// TODO: Use /admin/en instead of /en/admin in the future.
|
||||
$redirect = $language->getLanguageURLPrefix($this->grav['session']->admin_lang) . $base . $redirect;
|
||||
} else {
|
||||
// TODO: Use /admin/en instead of /en/admin in the future.
|
||||
//$redirect = preg_replace('`^(/[^/]+)/admin`', '\\1', $redirect);
|
||||
|
||||
// Check if we already have language prefixed admin path: /en/admin
|
||||
$this->grav->redirect($redirect, $redirectCode);
|
||||
}
|
||||
} else {
|
||||
// TODO: Use /admin/en instead of /en/admin in the future.
|
||||
// Prefix path with /admin
|
||||
$redirect = $base . $redirect;
|
||||
}
|
||||
|
||||
$this->grav->redirect($redirect, $redirectCode);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if multilang is active
|
||||
*
|
||||
* @return bool True if multilang is active
|
||||
*/
|
||||
protected function isMultilang()
|
||||
{
|
||||
return count($this->grav['config']->get('system.languages.supported', [])) > 1;
|
||||
}
|
||||
|
||||
public static function getTempDir()
|
||||
{
|
||||
try {
|
||||
|
||||
@@ -609,48 +609,12 @@ class AdminBaseController
|
||||
|
||||
/**
|
||||
* Redirect to the route stored in $this->redirect
|
||||
*
|
||||
* Route may or may not be prefixed by /en or /admin or /en/admin.
|
||||
*/
|
||||
public function redirect()
|
||||
{
|
||||
if (!$this->redirect) {
|
||||
return;
|
||||
}
|
||||
|
||||
$base = $this->admin->base;
|
||||
$this->redirect = '/' . ltrim($this->redirect, '/');
|
||||
$multilang = $this->isMultilang();
|
||||
|
||||
$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;
|
||||
}
|
||||
|
||||
// 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)
|
||||
&& !Utils::startsWith($this->redirect, $base)
|
||||
) {
|
||||
$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;
|
||||
}
|
||||
}
|
||||
|
||||
if (!$redirect) {
|
||||
$redirect = $this->redirect;
|
||||
}
|
||||
|
||||
$this->grav->redirect($redirect, $this->redirectCode);
|
||||
$this->admin->redirect($this->redirect, $this->redirectCode);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -11,6 +11,7 @@ use Grav\Common\GPM\GPM as GravGPM;
|
||||
use Grav\Common\GPM\Installer;
|
||||
use Grav\Common\Grav;
|
||||
use Grav\Common\Data;
|
||||
use Grav\Common\Language\Language;
|
||||
use Grav\Common\Page\Interfaces\PageInterface;
|
||||
use Grav\Common\Page\Media;
|
||||
use Grav\Common\Page\Medium\ImageMedium;
|
||||
@@ -1257,10 +1258,7 @@ class AdminController extends AdminBaseController
|
||||
|
||||
$this->admin->setMessage($this->admin::translate('PLUGIN_ADMIN.SUCCESSFULLY_SAVED'), 'info');
|
||||
|
||||
$multilang = $this->isMultilang();
|
||||
$admin_route = $this->admin->base;
|
||||
$redirect_url = '/' . ($multilang ? ($this->grav['session']->admin_lang) : '') . $admin_route . '/' . $this->view;
|
||||
$this->setRedirect($redirect_url);
|
||||
$this->setRedirect($this->admin->getAdminRoute("/{$this->view}")->toString());
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -1364,10 +1362,8 @@ class AdminController extends AdminBaseController
|
||||
|
||||
$multilang = $this->isMultilang();
|
||||
|
||||
if ($multilang) {
|
||||
if (!$obj->language()) {
|
||||
$obj->language($this->grav['session']->admin_lang);
|
||||
}
|
||||
if ($multilang && !$obj->language()) {
|
||||
$obj->language($this->admin->language);
|
||||
}
|
||||
$admin_route = $this->admin->base;
|
||||
|
||||
@@ -1548,16 +1544,12 @@ class AdminController extends AdminBaseController
|
||||
|
||||
$data = (array)$this->data;
|
||||
|
||||
if (isset($data['lang'])) {
|
||||
$language = $data['lang'];
|
||||
} else {
|
||||
$language = $this->grav['uri']->param('lang');
|
||||
}
|
||||
$language = $data['lang'] ?? $this->grav['uri']->param('lang');
|
||||
|
||||
if (isset($data['redirect'])) {
|
||||
$redirect = 'pages/' . $data['redirect'];
|
||||
$redirect = '/pages/' . $data['redirect'];
|
||||
} else {
|
||||
$redirect = 'pages';
|
||||
$redirect = '/pages';
|
||||
}
|
||||
|
||||
|
||||
@@ -1567,8 +1559,7 @@ class AdminController extends AdminBaseController
|
||||
|
||||
$this->admin->setMessage($this->admin::translate('PLUGIN_ADMIN.SUCCESSFULLY_SWITCHED_LANGUAGE'), 'info');
|
||||
|
||||
$admin_route = $this->admin->base;
|
||||
$this->setRedirect('/' . $language . $admin_route . '/' . $redirect);
|
||||
$this->setRedirect($this->admin->getAdminRoute($redirect)->toString());
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -1584,27 +1575,30 @@ class AdminController extends AdminBaseController
|
||||
return false;
|
||||
}
|
||||
|
||||
$data = (array)$this->data;
|
||||
$language = $data['lang'];
|
||||
/** @var Language $language */
|
||||
$language = $this->grav['language'];
|
||||
|
||||
if ($language) {
|
||||
$this->grav['session']->admin_lang = $language ?: 'en';
|
||||
$data = (array)$this->data;
|
||||
$lang = $data['lang'] ?? null;
|
||||
|
||||
if ($lang) {
|
||||
$this->grav['session']->admin_lang = $lang ?: 'en';
|
||||
}
|
||||
|
||||
$uri = $this->grav['uri'];
|
||||
$obj = $this->admin->page($uri->route());
|
||||
$this->preparePage($obj, false, $language);
|
||||
$this->preparePage($obj, false, $lang);
|
||||
|
||||
$file = $obj->file();
|
||||
if ($file) {
|
||||
$filename = $this->determineFilenameIncludingLanguage($obj->name(), $language);
|
||||
$filename = $this->determineFilenameIncludingLanguage($obj->name(), $lang);
|
||||
|
||||
$path = $obj->path() . DS . $filename;
|
||||
$aFile = File::instance($path);
|
||||
$aFile->save();
|
||||
|
||||
$aPage = new Page();
|
||||
$aPage->init(new \SplFileInfo($path), $language . '.md');
|
||||
$aPage->init(new \SplFileInfo($path), $lang . '.md');
|
||||
$aPage->header($obj->header());
|
||||
$aPage->rawMarkdown($obj->rawMarkdown());
|
||||
$aPage->template($obj->template());
|
||||
@@ -1621,7 +1615,9 @@ class AdminController extends AdminBaseController
|
||||
}
|
||||
|
||||
$this->admin->setMessage($this->admin::translate('PLUGIN_ADMIN.SUCCESSFULLY_SWITCHED_LANGUAGE'), 'info');
|
||||
$this->setRedirect('/' . $language . $uri->route());
|
||||
|
||||
// TODO: better multilanguage support needed.
|
||||
$this->setRedirect($language->getLanguageURLPrefix($lang) . $uri->route());
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -2449,9 +2445,9 @@ class AdminController extends AdminBaseController
|
||||
*
|
||||
* @param PageInterface $page
|
||||
* @param bool $clean_header
|
||||
* @param string $language
|
||||
* @param string $languageCode
|
||||
*/
|
||||
protected function preparePage(PageInterface $page, $clean_header = false, $language = '')
|
||||
protected function preparePage(PageInterface $page, $clean_header = false, $languageCode = '')
|
||||
{
|
||||
$input = (array)$this->data;
|
||||
|
||||
@@ -2463,18 +2459,29 @@ class AdminController extends AdminBaseController
|
||||
|
||||
if (isset($input['name']) && !empty($input['name'])) {
|
||||
$type = strtolower($input['name']);
|
||||
$page->template($type);
|
||||
$name = preg_replace('|.*/|', '', $type);
|
||||
if ($language) {
|
||||
$name .= '.' . $language;
|
||||
} else {
|
||||
$language = $this->grav['language'];
|
||||
if ($language->enabled()) {
|
||||
$name .= '.' . $language->getLanguage();
|
||||
|
||||
/** @var Language $language */
|
||||
$language = $this->grav['language'];
|
||||
if ($language->enabled()) {
|
||||
$languageCode = $languageCode ?: $language->getLanguage();
|
||||
if ($languageCode) {
|
||||
$isDefault = $languageCode === $language->getDefault();
|
||||
$includeLang = !$isDefault || (bool)$this->grav['config']->get('system.languages.include_default_lang_file_extension', true);
|
||||
if (!$includeLang) {
|
||||
// Check if the language specific file exists; use it if it does.
|
||||
$includeLang = file_exists("{$page->path()}/{$name}.{$languageCode}.md");
|
||||
}
|
||||
// Keep existing .md file if we're updating default language, otherwise always append the language.
|
||||
if ($includeLang) {
|
||||
$name .= '.' . $languageCode;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$name .= '.md';
|
||||
$page->name($name);
|
||||
$page->template($type);
|
||||
}
|
||||
|
||||
// Special case for Expert mode: build the raw, unset content
|
||||
|
||||
@@ -9,6 +9,7 @@ use Grav\Common\Language\Language;
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\TwigFilter;
|
||||
use Twig\TwigFunction;
|
||||
use Grav\Plugin\Admin\Admin;
|
||||
|
||||
class AdminTwigExtension extends AbstractExtension
|
||||
{
|
||||
@@ -38,7 +39,8 @@ class AdminTwigExtension extends AbstractExtension
|
||||
public function getFunctions(): array
|
||||
{
|
||||
return [
|
||||
new TwigFunction('getPageUrl', [$this, 'getPageUrl'], ['needs_context' => true]),
|
||||
new TwigFunction('admin_route', [$this, 'adminRouteFunc']),
|
||||
new TwigFunction('getPageUrl', [$this, 'getPageUrl']),
|
||||
new TwigFunction('clone', [$this, 'cloneFunc']),
|
||||
];
|
||||
}
|
||||
@@ -65,21 +67,20 @@ class AdminTwigExtension extends AbstractExtension
|
||||
return clone $obj;
|
||||
}
|
||||
|
||||
public function getPageUrl($context, PageInterface $page)
|
||||
public function adminRouteFunc(string $route = '', string $languageCode = null)
|
||||
{
|
||||
$page_route = trim($page->rawRoute(), '/');
|
||||
$page_lang = $page->language();
|
||||
$base_url = $context['base_url'];
|
||||
$base_url_simple = $context['base_url_simple'];
|
||||
$admin_lang = Grav::instance()['session']->admin_lang ?: 'en';
|
||||
/** @var Admin $admin */
|
||||
$admin = Grav::instance()['admin'];
|
||||
|
||||
if ($page_lang && $page_lang !== $admin_lang) {
|
||||
$page_url = $base_url_simple . '/' . $page_lang . '/' . $context['admin_route'] . '/pages/' . $page_route;
|
||||
} else {
|
||||
$page_url = $base_url . '/pages/' . $page_route;
|
||||
}
|
||||
return $admin->getAdminRoute($route, $languageCode)->toString(true);
|
||||
}
|
||||
|
||||
return $page_url;
|
||||
public function getPageUrl(PageInterface $page)
|
||||
{
|
||||
/** @var Admin $admin */
|
||||
$admin = Grav::instance()['admin'];
|
||||
|
||||
return $admin->getAdminRoute('/pages' . $page->rawRoute(), $page->language())->toString(true);
|
||||
}
|
||||
|
||||
public static function tuFilter()
|
||||
|
||||
Reference in New Issue
Block a user