Commit 994099be authored by Fabrice Gangler's avatar Fabrice Gangler 🎨
Browse files

REFACTOR: add getBaseUrl() to AppController

parent 8e9f5370
Pipeline #9972 passed with stage
in 4 minutes and 7 seconds
......@@ -43,7 +43,7 @@ class CommonTaxonomyController extends MetaTaxonomyController
{
$firstLink = [
'name' => __d('Breadcrumbs', 'Taxonomy.index'),
'url' => $this->getBaseUrl(false)
'url' => $this->getBaseUrl('mapping', false)
];
array_unshift($links, $firstLink);
parent::setBreadcrumbs($links);
......
......@@ -159,23 +159,6 @@ class MetaTaxonomyController extends AppController
return false;
}
/**
* Before render callback.
* Called after the controller action is run, but before the view is rendered.
*
* Used to set view variables that are required on every request:
* - viewBaseUrl
*
* @param Event $event The beforeRender event.
* @return void
*/
public function beforeRender(Event $event)
{
parent::beforeRender($event);
$this->set('viewBaseUrl', $this->getBaseUrl());
}
/**
* Business rules that define if current user is editor
*
......@@ -197,25 +180,4 @@ class MetaTaxonomyController extends AppController
}
return false;
}
/**
* Returns the base URL of the mapping based on the user's language
*
* @param bool $withLanguagePrefix (optional) add the language prefix in the URL, by default it's true
* @return string base URL of the mapping based on the user's language.
*/
final protected function getBaseUrl($withLanguagePrefix = true)
{
$url = '';
if ($withLanguagePrefix === true) {
$url .= '/'.$this->selectedLanguage .'/';
}
if ($this->selectedLanguage === 'fr') {
$url .= 'cartographie/';
} else {
$url .= 'mapping/';
}
return $url ;
}
}
......@@ -72,7 +72,7 @@ class TaxonomysController extends CommonTaxonomyController
// Check that the current URL is correct
$currentUrl = $this->request->here(false);
$allowedUrl = $this->getBaseUrl();
$allowedUrl = $this->getBaseUrl('mapping');
if ($allowedUrl !== $currentUrl) {
return $this->redirect("$allowedUrl", 301);
}
......@@ -133,11 +133,11 @@ class TaxonomysController extends CommonTaxonomyController
// Some checks before display content
if (is_null($primaryId)) {
// if the requested primary level does not exist, redirect to the root mapping.
return $this->redirect($this->getBaseUrl(), 301);
return $this->redirect($this->getBaseUrl('mapping'), 301);
} else {
// Check that the current URL is correct
$currentUrl = $this->request->here(false);
$allowedUrl = $this->getBaseUrl() ."$primarySlug/";
$allowedUrl = $this->getBaseUrl('mapping') ."$primarySlug/";
if ($allowedUrl !== $currentUrl) {
return $this->redirect("$allowedUrl", 301);
}
......@@ -156,7 +156,7 @@ class TaxonomysController extends CommonTaxonomyController
$links = array();
$links[] = [
'name' => $primaryName,
'url' => $this->getBaseUrl(false) ."$primarySlug/",
'url' => $this->getBaseUrl('mapping', false) ."$primarySlug/",
];
$this->setBreadcrumbs($links);
}
......@@ -207,14 +207,14 @@ class TaxonomysController extends CommonTaxonomyController
// Some checks before display content
if (is_null($taxonId)) {
// if the requested taxon does not exist, redirect to the root mapping.
return $this->redirect($this->getBaseUrl(), 301);
return $this->redirect($this->getBaseUrl('mapping'), 301);
} elseif (is_null($parent_id)) {
// if the requested taxon is a primary level, redirect it to the official URL.
return $this->redirect($this->getBaseUrl() ."$taxonSlug/", 301);
return $this->redirect($this->getBaseUrl('mapping') ."$taxonSlug/", 301);
} else {
// Check that the current URL is correct
$currentUrl = $this->request->here(false);
$allowedUrl = $this->getBaseUrl() ."$primarySlug/$taxonSlug/$taxonId";
$allowedUrl = $this->getBaseUrl('mapping') ."$primarySlug/$taxonSlug/$taxonId";
if ($allowedUrl !== $currentUrl) {
return $this->redirect("$allowedUrl", 301);
}
......@@ -228,7 +228,7 @@ class TaxonomysController extends CommonTaxonomyController
$baseTaxonUrl = "$primarySlug/$taxonSlug";
$taxonName = $mappingTaxons[$taxonId]['title'];
$this->set('taxonId', "$taxonId");
$this->set('baseUrl', $this->getBaseUrl() ."$baseTaxonUrl");
$this->set('baseUrl', $this->getBaseUrl('mapping') ."$baseTaxonUrl");
$this->set('title', "$taxonName");
$this->set('data', $data);
$this->set('_serialize', ['data']);
......@@ -237,11 +237,11 @@ class TaxonomysController extends CommonTaxonomyController
$links = array();
$links[] = [
'name' => $primaryName,
'url' => $this->getBaseUrl(false) ."$primarySlug/",
'url' => $this->getBaseUrl('mapping', false) ."$primarySlug/",
];
$links[] = [
'name' => "$taxonName",
'url' => $this->getBaseUrl(false) ."$baseTaxonUrl/$taxonId",
'url' => $this->getBaseUrl('mapping', false) ."$baseTaxonUrl/$taxonId",
];
$this->setBreadcrumbs($links);
}
......@@ -292,10 +292,10 @@ class TaxonomysController extends CommonTaxonomyController
// Some checks before load data
if (is_null($taxonId)) {
// the requested taxon does not exist, redirect to the root mapping.
return $this->redirect($this->getBaseUrl(), 301);
return $this->redirect($this->getBaseUrl('mapping'), 301);
} elseif (is_null($parent_id)) {
// the requested taxon is a primary level, redirect it to the official URL.
return $this->redirect($this->getBaseUrl() ."$taxonSlug/", 301);
return $this->redirect($this->getBaseUrl('mapping') ."$taxonSlug/", 301);
} else {
$parentTaxonUrl = "$primarySlug/";
$taxonUrl = $parentTaxonUrl ."$taxonSlug/$taxonId";
......@@ -307,7 +307,7 @@ class TaxonomysController extends CommonTaxonomyController
$data = $this->TaxonomysSoftwares->getSoftwareTaxonomyWithUsers($taxonId, $softwareId);
if (!isset($data) || !is_array($data) || count($data) === 0) {
// the requested IDs does not exist in database, redirect to the taxon page.
return $this->redirect($this->getBaseUrl() .$taxonUrl, 301);
return $this->redirect($this->getBaseUrl('mapping') .$taxonUrl, 301);
} else {
$software = $data[$softwareId]['software'];
$softwareSlug = $software->slug_name;
......@@ -316,7 +316,7 @@ class TaxonomysController extends CommonTaxonomyController
// Check that the current URL is correct
$realCurrentUrl = $this->request->here(false);
$allowedUrl = $this->getBaseUrl() . $currentUrl;
$allowedUrl = $this->getBaseUrl('mapping') . $currentUrl;
if ($allowedUrl !== $realCurrentUrl) {
return $this->redirect("$allowedUrl", 301);
}
......@@ -336,16 +336,16 @@ class TaxonomysController extends CommonTaxonomyController
$links = array();
$links[] = [
'name' => $primaryName,
'url' => $this->getBaseUrl(false) . $parentTaxonUrl,
'url' => $this->getBaseUrl('mapping', false) . $parentTaxonUrl,
];
$links[] = [
'name' => "$taxonName",
'url' => $this->getBaseUrl(false) . $taxonUrl,
'url' => $this->getBaseUrl('mapping', false) . $taxonUrl,
];
$links[] = [
'name' => "Utilisateurs du logiciel ". $software->softwarename,
'name' => "Logiciel ". $software->softwarename,
'url' => $this->getBaseUrl(false) . $currentUrl,
'url' => $this->getBaseUrl('mapping', false) . $currentUrl,
];
$this->setBreadcrumbs($links);
}
......
......@@ -76,7 +76,7 @@ class TaxonomysSoftwaresController extends CommonTaxonomySoftwareController
if (isset($this->request->params['softwareId'])) {
$softwareId = $this->request->params['softwareId'];
} else {
return $this->redirect($this->getBaseUrl(), 301);
return $this->redirect($this->getBaseUrl('mapping'), 301);
}
}
......@@ -86,7 +86,7 @@ class TaxonomysSoftwaresController extends CommonTaxonomySoftwareController
try {
$software = $softwaresTable->get($softwareId);
} catch (RecordNotFoundException $e) { // When there is no $softwareId record in database
return $this->redirect($this->getBaseUrl(), 301);
return $this->redirect($this->getBaseUrl('mapping'), 301);
}
// Check that the current URL is correct
......
......@@ -146,6 +146,7 @@ class AppController extends Controller
}
$this->set('appVersion', $this->appVersion);
$this->set('appFullBaseUrl', Configure::read('App.fullBaseUrl'));
$this->set('mappingBaseUrl', $this->getBaseUrl('mapping'));
$this->set('breadcrumbs', $this->getBreadcrumbs());
}
......@@ -230,6 +231,33 @@ class AppController extends Controller
$this->set('explicitLanguages', $this->explicitLanguages);
}
/**
* Returns the base URL based on the user's language
* Usage:
* - $this->getBaseUrl() = /fr/ or /en/
* - $this->getBaseUrl('mapping') = /fr/cartographie/ or /en/mapping/
*
* @param string $type (optional) allowed values : '' or 'mapping', by default it's ''
* @param bool $withLanguagePrefix (optional) add the language prefix in the URL, by default it's true
* @return string base URL based on the user's language.
*/
final protected function getBaseUrl(string $type = '', $withLanguagePrefix = true)
{
$url = '';
if ($withLanguagePrefix === true) {
$url .= '/'.$this->selectedLanguage .'/';
}
if ($type === 'mapping') {
if ($this->selectedLanguage === 'fr') {
$url .= 'cartographie/';
} else {
$url .= 'mapping/';
}
}
return $url ;
}
private function setOpenGraph()
{
//For Social MEDIAS => OPENGRAPH
......
......@@ -21,14 +21,14 @@
foreach ($mappingHead as $primaryId => $primaryLevel) {
$primaryName = $mappingTaxons[$primaryId]['title'];
$primarySlug = $mappingTaxons[$primaryId]['slug'];
$url = "$viewBaseUrl/$primarySlug/";
$url = "$mappingBaseUrl/$primarySlug/";
$linkPrimaryLevel = $this->Html->link($primaryName, $url, ['class' => 'button']);
$subLlist = [];
$htmlSubLlist = "";
if(isset($mappingTaxons[$primaryId]['children'])){
foreach ($mappingTaxons[$primaryId]['children'] as $subId => $subName) {
$subSlug = $mappingTaxons[$subId]['slug'];
$url = "$viewBaseUrl/$primarySlug/$subSlug/$subId";
$url = "$mappingBaseUrl/$primarySlug/$subSlug/$subId";
$subLlist[] = $this->Html->link($subName, $url);
}
$htmlSubLlist = $this->Html->nestedList($subLlist);
......
......@@ -23,7 +23,7 @@ $attId = "mappingPrimaryLevel$primaryId";
if(isset($mappingTaxons[$primaryId]['children'])){
foreach ($mappingTaxons[$primaryId]['children'] as $subId => $subName) {
$subSlug = $mappingTaxons[$subId]['slug'];
$url = "$viewBaseUrl/$primarySlug/$subSlug/$subId";
$url = "$mappingBaseUrl/$primarySlug/$subSlug/$subId";
$subLlist[] = $this->Html->link($subName, $url);
}
echo $this->Html->nestedList($subLlist);
......
......@@ -12,6 +12,16 @@
if (!isset($appVersion)) {
$appVersion = Cake\Core\Configure::read("VERSION.footer");
}
// This template is used all the time (even on error pages).
// But, on error pages, the $mappingBaseUrl variable is not available.
// We force its creation that is always available.
if (!isset($mappingBaseUrl)) {
$mappingBaseUrl= '/en/mapping/';
if($selectedLanguage === 'fr') {
$mappingBaseUrl = '/fr/cartographie/';
}
}
?>
<div class="navbar">
......@@ -54,17 +64,13 @@
<ul class="nav navbar-nav navbar-left nav-pull-down" id="navbar-mainMenu">
<?php
$txtMapping = __d("ElementNavigation", "navbar.mapping") ;
$urlMapping = '/en/mapping/';
if($selectedLanguage === 'fr') {
$urlMapping = '/fr/cartographie/';
}
?>
<?php if ($this->isHere("Taxonomys", 'mapping')): ?>
<li class="current">
<span class=" text-center"><?= __d("ElementNavigation", "navbar.mapping") ?></span>
</li>
<?php else: ?>
<li> <?= $this->Html->link($txtMapping, $urlMapping, ["id" => "mappingPage"]) ?></li>
<li> <?= $this->Html->link($txtMapping, $mappingBaseUrl, ["id" => "mappingPage"]) ?></li>
<?php endif; ?>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment