Skip to content

Commit

Permalink
Merge pull request #127 from Web-Ex-Machina/master
Browse files Browse the repository at this point in the history
Fix 2 bugs in tl_page extends
  • Loading branch information
LupusVII committed Mar 26, 2019
2 parents 6adf939 + d5329cd commit 16583ff
Show file tree
Hide file tree
Showing 25 changed files with 551 additions and 126 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ Thumbs.db
.build/
.idea/
nbproject/
*.sublime-*

# Vendors
/vendor

# Distribution
/dist
/build
/build
4 changes: 3 additions & 1 deletion src/Classes/I18nl10n.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@

namespace Verstaerker\I18nl10nBundle\Classes;

use Contao\Controller;

/**
* Class I18nl10n
*
* Global Functions for i18nl10n module
*
* @package Verstaerker\I18nl10n\Classes
*/
class I18nl10n extends \Controller
class I18nl10n extends Controller
{
/**
* Known and unsupported Contao modules
Expand Down
13 changes: 13 additions & 0 deletions src/Hook/GenerateFrontendUrlHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ public function generateFrontendUrl($arrRow, $strParams, $strUrl)
? $GLOBALS['TL_LANGUAGE']
: $arrRow['language'];

// Do not look for a translation page if there is an alias given already existing in the language we want
if ($arrRow['alias']) {
$objAlias = \Database::getInstance()
->prepare('SELECT alias FROM tl_page_i18nl10n WHERE alias = ? AND language = ?')
->limit(1)
->execute($arrRow['alias'], $language);
}

// Try to get l10n alias by language and pid
if ($language !== $arrLanguages['default']) {
$database = \Database::getInstance();
Expand Down Expand Up @@ -119,6 +127,11 @@ public function generateFrontendUrl($arrRow, $strParams, $strUrl)
}
}

// Catch "/" page aliases and do not add suffix to them (as they are considered as base request)
if ($strL10nUrl == $language."//".\Config::get('urlSuffix')) {
$strL10nUrl = $language."/";
}

return $strL10nUrl;
}
}
36 changes: 36 additions & 0 deletions src/Hook/GetArticleHook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Verstaerker\I18nl10nBundle\Hook;

/**
* Class GetArticleHook
* @package Verstaerker\I18nl10nBundle\Hook
*
* Determine when to show content elements in relation to the current page language.
*/
class GetArticleHook
{
/**
* Check if the current article has visible elements and return an empty template if not
* @param [ArticleModel] $objRow Article row
* @return [nothing]
*/
public function checkIfEmpty($objRow)
{
$objElements = \ContentModel::findPublishedByPidAndTable($objRow->id, "tl_article");

$blnDisplay = false;
if ($objElements && $objElements->count() > 0) {
while ($objElements->next()) {
if ($objElements->language == "" || $objElements->language == $GLOBALS['TL_LANGUAGE']) {
$blnDisplay = true;
break;
}
}
}

if (!$blnDisplay) {
$objRow->customTpl = "mod_i18nl10n_article_empty";
}
}
}
14 changes: 12 additions & 2 deletions src/Hook/GetPageIdFromUrlHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,17 @@ public function getPageIdFromUrl(array $arrFragments)
array_insert($arrMappedFragments, 1, array('auto_item'));
}

// Restore the urlSuffix config if it has been removed in initializeSystem hook
if (\Config::has('tmpUrlSuffix')) {
\Config::set('urlSuffix', \Config::get('tmpUrlSuffix'));
\Config::set('tmpUrlSuffix', '');
}

// Consider that - as Contao native does - if we don't have a fragment, make it / to find homepage.
if ($arrMappedFragments[0] === "") {
$arrMappedFragments[0] = "/";
}

return $arrMappedFragments;
}

Expand Down Expand Up @@ -147,8 +158,7 @@ private function mapUrlFragments($arrFragments)
*/
private function findAliasByLocalizedAliases($arrFragments, $strLanguage)
{
$arrAlias = array
(
$arrAlias = array(
'alias' => $arrFragments[0],
'l10nAlias' => ''
);
Expand Down
42 changes: 42 additions & 0 deletions src/Hook/InitializeSystemHook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Verstaerker\I18nl10nBundle\Hook;

use Contao\Controller;
use Verstaerker\I18nl10nBundle\Classes\I18nl10n;

/**
* Class InitializeSystemHook
* @package Verstaerker\I18nl10nBundle\Hook
*
* Implementation of i18nl10n search logic.
*/
class InitializeSystemHook
{
public function initializeSystem()
{
// If there is no request, add the browser language
if ("" === \Environment::get('request')) {
// check if the browser language is available
$arrLanguages = I18nl10n::getInstance()->getAvailableLanguages();
$userLanguage = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);

$languages = $arrLanguages[$_SERVER['HTTP_HOST']] ?: $arrLanguages['*'];

if (in_array($userLanguage, $languages['languages'])) {
$strRedirect = $userLanguage."/";
} else {
$strRedirect = $languages['default']."/";
}

Controller::redirect($strRedirect);
}

// If we are on the homepage, remove the urlSuffix
$arrFragments = explode("/", \Environment::get('request'));
if ("" === $arrFragments[1] && "" != \Config::get('urlSuffix')) {
\Config::set('tmpUrlSuffix', \Config::get('urlSuffix'));
\Config::set('urlSuffix', "");
}
}
}
95 changes: 58 additions & 37 deletions src/Hook/ReplaceInsertTagsHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,51 +25,72 @@ public function replaceInsertTags($strTag)

$arrArguments = explode('::', $strTag);

if ($arrArguments[0] === 'i18nl10n' && $arrArguments[1] === 'link') {
$objNextPage = I18nl10n::getInstance()->findL10nWithDetails($arrArguments[2], $GLOBALS['TL_LANGUAGE']);
if ($arrArguments[0] !== 'i18nl10n') {
return false;
}

if ($objNextPage === null) {
return false;
}
$objNextPage = I18nl10n::getInstance()->findL10nWithDetails($arrArguments[2], $GLOBALS['TL_LANGUAGE']);

switch ($objNextPage->type) {
case 'redirect':
$strUrl = \Controller::replaceInsertTags($objNextPage->url);
if ($objNextPage === null) {
return false;
}

if (strncasecmp($strUrl, 'mailto:', 7) === 0) {
$strUrl = \StringUtil::encodeEmail($strUrl);
}
break;
switch ($objNextPage->type) {
case 'redirect':
$strUrl = \Controller::replaceInsertTags($objNextPage->url);

case 'forward':
$intForwardId = $objNextPage->jumpTo ?: \PageModel::findFirstPublishedByPid($objNextPage->id)
->current()->id;
if (strncasecmp($strUrl, 'mailto:', 7) === 0) {
$strUrl = \StringUtil::encodeEmail($strUrl);
}
break;

$objNext = \PageModel::findWithDetails($intForwardId);
case 'forward':
$intForwardId = $objNextPage->jumpTo ?: \PageModel::findFirstPublishedByPid($objNextPage->id)
->current()->id;

if ($objNext !== null) {
$strUrl = I18nl10n::generateFrontendUrl($objNext->row(), null, '');
break;
}
$objNext = \PageModel::findWithDetails($intForwardId);

// no break
default:
$strUrl = I18nl10n::generateFrontendUrl($objNextPage->row(), null, '');
if ($objNext !== null) {
$strUrl = I18nl10n::generateFrontendUrl($objNext->row(), null, '');
break;
}

$strName = $objNextPage->title;
$strTarget = $objNextPage->target ?
(($objPage->outputFormat == 'xhtml') ? LINK_NEW_WINDOW : ' target="_blank"') : '';
$strTitle = $objNextPage->pageTitle ?: $objNextPage->title;

return sprintf(
'<a href="%s" title="%s"%s>%s</a>',
$strUrl,
specialchars($strTitle),
$strTarget,
specialchars($strName)
);
}

// no break
default:
$strUrl = I18nl10n::generateFrontendUrl($objNextPage->row(), null, '');
break;
}

$strName = $objNextPage->title;
$strTarget = $objNextPage->target ?
(($objPage->outputFormat == 'xhtml') ? LINK_NEW_WINDOW : ' target="_blank"') : '';
$strTitle = $objNextPage->pageTitle ?: $objNextPage->title;

switch ($arrArguments[1]) {
case 'link':
return sprintf(
'<a href="%s" title="%s"%s>%s</a>',
$strUrl,
specialchars($strTitle),
$strTarget,
specialchars($strName)
);
break;

case 'link_url':
return $strUrl;
break;

case 'link_title':
return $objNextPage->pageTitle ?: $objNextPage->title;
break;

case 'link_name':
return $objNextPage->title;
break;

default:
return false;
}

return false;
Expand Down
8 changes: 4 additions & 4 deletions src/Resources/contao/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,8 @@
array_insert(
$GLOBALS['BE_MOD']['design'],
array_search('page', array_keys($GLOBALS['BE_MOD']['design'])) + 1,
array
(
'i18nl10n' => array
(
array(
'i18nl10n' => array(
'tables' => array('tl_page_i18nl10n'),
'icon' => 'bundles/verstaerkeri18nl10n/img/i18nl10n.png'
)
Expand All @@ -53,13 +51,15 @@
/**
* HOOKS
*/
$GLOBALS['TL_HOOKS']['initializeSystem'][] = array('Verstaerker\I18nl10nBundle\Hook\InitializeSystemHook', 'initializeSystem');
$GLOBALS['TL_HOOKS']['generateFrontendUrl'][] = array('Verstaerker\I18nl10nBundle\Hook\GenerateFrontendUrlHook', 'generateFrontendUrl');
$GLOBALS['TL_HOOKS']['getPageIdFromUrl'][] = array('Verstaerker\I18nl10nBundle\Hook\GetPageIdFromUrlHook', 'getPageIdFromUrl');
$GLOBALS['TL_HOOKS']['generateBreadcrumb'][] = array('Verstaerker\I18nl10nBundle\Hook\GenerateBreadcrumbHook', 'generateBreadcrumb');
$GLOBALS['TL_HOOKS']['executePostActions'][] = array('Verstaerker\I18nl10nBundle\Hook\ExecutePostActionsHook', 'executePostActions');
$GLOBALS['TL_HOOKS']['isVisibleElement'][] = array('Verstaerker\I18nl10nBundle\Hook\IsVisibleElementHook', 'isVisibleElement');
$GLOBALS['TL_HOOKS']['replaceInsertTags'][] = array('Verstaerker\I18nl10nBundle\Hook\ReplaceInsertTagsHook', 'replaceInsertTags');
$GLOBALS['TL_HOOKS']['loadDataContainer'][] = array('Verstaerker\I18nl10nBundle\Hook\LoadDataContainerHook', 'setLanguages');
$GLOBALS['TL_HOOKS']['getArticle'][] = array('Verstaerker\I18nl10nBundle\Hook\GetArticleHook', 'checkIfEmpty');

// Append language selection for tl_content
$GLOBALS['TL_HOOKS']['loadDataContainer'][] = array('Verstaerker\I18nl10nBundle\Hook\LoadDataContainerHook', 'appendLanguageSelectCallback');
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/contao/dca/tl_article.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function addIcon($row, $label)
{
$count = $l10nItem['items'];
$title = $GLOBALS['TL_LANG']['LNG'][$l10nItem['language']] . ": $count " . $GLOBALS['TL_LANG']['tl_article']['elements'];
$l10nItemIcon = 'system/modules/i18nl10n/assets/img/i18nl10n.png';
$l10nItemIcon = 'bundles/verstaerkeri18nl10n/img/i18nl10n.png';

if ($l10nItem['language'])
{
Expand Down
8 changes: 4 additions & 4 deletions src/Resources/contao/dca/tl_page.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
'options_callback' => array('tl_page_l10n', 'languageOptions'),
'eval' => array
(
'style' => 'width:250px',
'style' => 'width:100%',
'chosen' => true,
'includeBlankOption' => true
)
Expand Down Expand Up @@ -176,7 +176,7 @@ public function editL10n($row, $href, $label, $title, $icon)
$strImgName = $row['i18nl10n_published'] ? 'i18nl10n.png' : 'i18nl10n_invisible.png';

return sprintf(
'<a href="%1$s" title="%2$s"><img src="system/modules/i18nl10n/assets/img/%3$s"></a>',
'<a href="%1$s" title="%2$s"><img src="bundles/verstaerkeri18nl10n/img/%3$s"></a>',
$strButtonUrl,
specialchars($strTitle),
$strImgName
Expand Down Expand Up @@ -509,8 +509,8 @@ public function setDnsMandatory()
public function extendRootPalettes()
{
$GLOBALS['TL_DCA']['tl_page']['palettes']['root'] = str_replace(
'language,fallback,staticFiles,staticPlugins;',
'language,fallback,staticFiles,staticPlugins;{module_i18nl10n},i18nl10n_localizations;',
'language,fallback;',
'language,fallback;{module_i18nl10n},i18nl10n_localizations;',
$GLOBALS['TL_DCA']['tl_page']['palettes']['root']
);
}
Expand Down
Loading

0 comments on commit 16583ff

Please sign in to comment.