Skip to content

Commit

Permalink
DatabasePageRouter updated to resolve multilanguage pages
Browse files Browse the repository at this point in the history
  • Loading branch information
HansSchouten committed Apr 17, 2020
1 parent ed302d6 commit 1756b1b
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 29 deletions.
5 changes: 4 additions & 1 deletion config/config.example.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,10 @@
|
*/
'page' => [
'class' => PHPageBuilder\Page::class
'class' => PHPageBuilder\Page::class,
'translation' => [
'class' => PHPageBuilder\PageTranslation::class,
]
],

/*
Expand Down
3 changes: 2 additions & 1 deletion src/Contracts/PageBuilderContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ public function renderPageBuilder(PageContract $page);
* Render the given page.
*
* @param PageContract $page
* @param null $language
*/
public function renderPage(PageContract $page);
public function renderPage(PageContract $page, $language = null);

/**
* Update the given page with the given data (an array of html blocks)
Expand Down
13 changes: 13 additions & 0 deletions src/Contracts/PageTranslationContract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace PHPageBuilder\Contracts;

interface PageTranslationContract
{
/**
* Return the page this translation belongs to.
*
* @return PageContract
*/
public function getPage();
}
12 changes: 6 additions & 6 deletions src/Contracts/RouterContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ interface RouterContract
* Return the page corresponding to the given URL.
*
* @param $url
* @return PageContract|null
* @return PageTranslationContract|null
*/
public function resolve($url);

/**
* Return the full page instance based on the given matched route.
* Return the full page translation instance based on the given matched route or page translation id.
* (this method is helpful when extending a router to perform additional checks after a route has been matched)
*
* @param string $matchedRoute the matched route
* @param string $matchedPageId the page id corresponding to the matched route
* @return PageContract|null
* @param string $matchedRoute the matched route
* @param string $matchedPageTranslationId the page translation id corresponding to the matched route
* @return PageTranslationContract|null
*/
public function getMatchedPage(string $matchedRoute, string $matchedPageId);
public function getMatchedPage(string $matchedRoute, string $matchedPageTranslationId);

/**
* Order the given routes into the order in which they need to be evaluated.
Expand Down
6 changes: 5 additions & 1 deletion src/Modules/GrapesJS/PageBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,11 +230,15 @@ public function renderPageBuilder(PageContract $page)
* Render the given page.
*
* @param PageContract $page
* @param null $language
* @throws Exception
*/
public function renderPage(PageContract $page)
public function renderPage(PageContract $page, $language = null)
{
$renderer = new PageRenderer($this->theme, $page);
if (! is_null($language)) {
$renderer->setLanguage($language);
}
echo $renderer->render();
}

Expand Down
33 changes: 17 additions & 16 deletions src/Modules/Router/DatabasePageRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PHPageBuilder\Modules\Router;

use PHPageBuilder\Contracts\PageContract;
use PHPageBuilder\Contracts\PageTranslationContract;
use PHPageBuilder\Contracts\RouterContract;
use PHPageBuilder\Repositories\PageRepository;
use PHPageBuilder\Repositories\PageTranslationRepository;
Expand All @@ -25,9 +26,9 @@ class DatabasePageRouter implements RouterContract
protected $routeParameters;

/**
* @var array $routeToPageIdMapping
* @var array $routeToPageTranslationIdMapping
*/
protected $routeToPageIdMapping;
protected $routeToPageTranslationIdMapping;

/**
* DatabasePageRouter constructor.
Expand All @@ -37,14 +38,14 @@ public function __construct()
$this->pageRepository = new PageRepository;
$this->pageTranslationRepository = new PageTranslationRepository;
$this->routeParameters = [];
$this->routeToPageIdMapping = [];
$this->routeToPageTranslationIdMapping = [];
}

/**
* Return the page from database corresponding to the given URL.
*
* @param $url
* @return PageContract|null
* @return PageTranslationContract|null
*/
public function resolve($url)
{
Expand All @@ -54,11 +55,11 @@ public function resolve($url)
$urlSegments = explode('/', $url);

// request all routes and convert each to its segments using / as separator
$pageTranslations = $this->pageTranslationRepository->getAll(['page_id', 'route']);
$pageTranslations = $this->pageTranslationRepository->getAll(['id', 'route']);
$routes = [];
foreach ($pageTranslations as $pageTranslation) {
$route = $pageTranslation['route'];
$this->routeToPageIdMapping[$route] = $pageTranslation['page_id'];
$route = $pageTranslation->route;
$this->routeToPageTranslationIdMapping[$route] = $pageTranslation->id;
$routeSegments = explode('/', $route);
$routes[] = $routeSegments;
}
Expand All @@ -70,7 +71,7 @@ public function resolve($url)
foreach ($orderedRoutes as $routeSegments) {
if ($this->onRoute($urlSegments, $routeSegments)) {
$fullRoute = implode('/', $routeSegments);
$matchedPage = $this->getMatchedPage($fullRoute, $this->routeToPageIdMapping[$fullRoute]);
$matchedPage = $this->getMatchedPage($fullRoute, $this->routeToPageTranslationIdMapping[$fullRoute]);

if ($matchedPage) {
global $phpb_route_parameters;
Expand Down Expand Up @@ -126,18 +127,18 @@ public function routeOrderComparison($route1, $route2)
}

/**
* Return the full page instance based on the given matched route or page id.
* Return the full page translation instance based on the given matched route or page translation id.
* (this method is helpful when extending a router to perform additional checks after a route has been matched)
*
* @param string $matchedRoute the matched route
* @param string $matchedPageId the page id corresponding to the matched route
* @return PageContract|null
* @param string $matchedRoute the matched route
* @param string $matchedPageTranslationId the page translation id corresponding to the matched route
* @return PageTranslationContract|null
*/
public function getMatchedPage(string $matchedRoute, string $matchedPageId)
public function getMatchedPage(string $matchedRoute, string $matchedPageTranslationId)
{
$page = $this->pageRepository->findWithId($matchedPageId);
if ($page instanceof PageContract) {
return $page;
$pageTranslation = $this->pageTranslationRepository->findWithId($matchedPageTranslationId);
if ($pageTranslation instanceof PageTranslationContract) {
return $pageTranslation;
}
return null;
}
Expand Down
8 changes: 5 additions & 3 deletions src/PHPageBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PHPageBuilder\Contracts\AuthContract;
use PHPageBuilder\Contracts\PageContract;
use PHPageBuilder\Contracts\PageTranslationContract;
use PHPageBuilder\Contracts\WebsiteManagerContract;
use PHPageBuilder\Contracts\PageBuilderContract;
use PHPageBuilder\Contracts\RouterContract;
Expand Down Expand Up @@ -276,9 +277,10 @@ public function handlePublicRequest()
}

// let the page router resolve the current URL
$page = $this->router->resolve(urldecode($_SERVER['REQUEST_URI']));
if ($page instanceof PageContract) {
$this->pageBuilder->renderPage($page);
$pageTranslation = $this->router->resolve(urldecode($_SERVER['REQUEST_URI']));
if ($pageTranslation instanceof PageTranslationContract) {
$page = $pageTranslation->getPage();
$this->pageBuilder->renderPage($page, $pageTranslation->locale);
exit();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function getTranslations()
$records = (new PageTranslationRepository)->findWhere('page_id', $this->getId());
$translations = [];
foreach ($records as $record) {
$translations[$record['locale']] = $record;
$translations[$record->locale] = (array) $record;
}
$this->translations = $translations;
}
Expand Down
20 changes: 20 additions & 0 deletions src/PageTranslation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace PHPageBuilder;

use PHPageBuilder\Contracts\PageContract;
use PHPageBuilder\Contracts\PageTranslationContract;
use PHPageBuilder\Repositories\PageRepository;

class PageTranslation implements PageTranslationContract
{
/**
* Return the page this translation belongs to.
*
* @return PageContract
*/
public function getPage()
{
return (new PageRepository)->findWithId($this->page_id);
}
}
16 changes: 16 additions & 0 deletions src/Repositories/PageTranslationRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,20 @@ class PageTranslationRepository extends BaseRepository implements PageTranslatio
* @var string
*/
protected $table = 'page_translations';

/**
* The class that represents each page translation.
*
* @var string
*/
protected $class;

/**
* PageTranslationRepository constructor.
*/
public function __construct()
{
parent::__construct();
$this->class = phpb_instance('page.translation');
}
}

0 comments on commit 1756b1b

Please sign in to comment.