Commit 4558a25e authored by Fabio Stegmeyer's avatar Fabio Stegmeyer

[TASK] Code cleanup & fixing all issues marked by the IDE

parent cf2a78ba
......@@ -27,7 +27,7 @@ namespace TYPO3\Languagevisibility\Element;
/**
*
* @author Daniel Poetzinger <poetzinger@aoemedia.de>
* @author Daniel Poetzinger <poetzinger@aoemedia.de>
* @coauthor Tolleiv Nietsch <nietsch@aoemedia.de>
* @coauthor Timo Schmidt <schmidt@aoemedia.de>
*/
......@@ -39,7 +39,7 @@ class ContentElement extends RecordElement {
* (non-PHPdoc)
* @see classes/tx_languagevisibility_element#initialisations()
*/
protected function initialisations() {
protected function initialisations(): void {
}
/**
......
......@@ -26,10 +26,14 @@ namespace TYPO3\Languagevisibility\Element;
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
use Doctrine\DBAL\DBALException;
use ReflectionClass;
use ReflectionException;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException;
use TYPO3\CMS\Core\Cache\Frontend\VariableFrontend;
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Context\Exception\AspectNotFoundException;
use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
use TYPO3\CMS\Core\Site\SiteFinder;
use TYPO3\CMS\Core\Utility\GeneralUtility;
......@@ -63,17 +67,16 @@ abstract class Element {
protected $globalVisibilitySettings = [];
/**
* @var \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend
* @var VariableFrontend
*/
protected $cache;
/**
* @param array $row
* @param string $tablename
* @return Element
* @throws \ReflectionException
* @throws \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException
* @throws InvalidRowException
* @throws ReflectionException
* @throws NoSuchCacheException
* @throws InvalidRowException|DBALException
*/
public function __construct(array $row, string $tablename = '') {
$this->table = $tablename;
......@@ -109,7 +112,7 @@ abstract class Element {
* @param string $table
* @return void
*/
public function setTable(string $table) {
public function setTable(string $table): void {
$this->table = $table;
}
......@@ -123,35 +126,23 @@ abstract class Element {
}
/**
* @return array
*/
public function getRow(): array {
return $this->row;
}
/**
* @param array $row
*/
public function setRow(array $row): void {
$this->row = $row;
}
/**
* Method to deternmine that an Element will not be instanciated with
* Method to determine that an Element will not be instantiated with
* data of an overlay.
*
* @param array $row
* @return bool
*/
protected function isRowOriginal(array $row) {
protected function isRowOriginal(array $row): bool {
return !isset($row[$GLOBALS['TCA'][$this->table]['ctrl']['transOrigPointerField']]) ||
$row[$GLOBALS['TCA'][$this->table]['ctrl']['transOrigPointerField']] === 0;
}
/**
* possibility to add inits in subclasses
*
* @return void
**/
protected function initialisations() {
protected function initialisations(): void {
}
/**
......@@ -202,18 +193,22 @@ abstract class Element {
public function getInformativeDescription(): string {
if ($this->isMonolithicTranslated()) {
return LocalizationUtility::translate('backend.contentElement.notDefault', 'languagevisibility');
} elseif ($this->isLanguageSetToAll()) {
}
if ($this->isLanguageSetToAll()) {
return LocalizationUtility::translate('backend.contentElement.languageSetToAll', 'languagevisibility');
} elseif ($this->isLanguageSetToDefault()) {
}
if ($this->isLanguageSetToDefault()) {
return LocalizationUtility::translate('backend.contentElement.normal', 'languagevisibility');
} else {
return LocalizationUtility::translate('backend.contentElement.translated', 'languagevisibility');
}
return LocalizationUtility::translate('backend.contentElement.translated', 'languagevisibility');
}
/**
* This method is used to determine the visibility of the element. Technically it merges the visibility of
* the default language record and the overlay record and returns the visibility. The visibility in the overlayrecord
* the default language record and the overlay record and returns the visibility. The visibility in the overlayRecord
* can overwrite the visibility of its own language.
* This method is only need to display the visibility setting in the backend.
*
......@@ -244,7 +239,6 @@ abstract class Element {
* This method is only need to display the visibility setting in the backend.
*
* @param int $languageid
* @param int $languageid
* @return string
* @return string|null
* @deprecated
......@@ -287,6 +281,7 @@ abstract class Element {
* fetches and sets the global / default language visibility settings for the element
*
* @return array
* @throws DBALException
*/
public function fetchGlobalVisibilitySettings(): array {
......@@ -359,7 +354,7 @@ abstract class Element {
}
/**
* Checks if the current record is set to language all (that is typically used to indicate that per default this element is visible in all langauges)
* Checks if the current record is set to language all (that is typically used to indicate that per default this element is visible in all languages)
*
* @return bool
*/
......@@ -397,14 +392,10 @@ abstract class Element {
$result = FALSE;
if (!is_numeric($languageid)) {
$result = FALSE;
} else {
if ($languageid === 0) {
$result = TRUE;
} else {
if ($this->_hasOverlayRecordForLanguage($languageid)) {
$result = TRUE;
}
}
} else if ($languageid === 0) {
$result = TRUE;
} else if ($this->_hasOverlayRecordForLanguage($languageid)) {
$result = TRUE;
}
return $result;
......@@ -431,7 +422,7 @@ abstract class Element {
}
/**
* Method to get a short description of the elementtype.
* Method to get a short description of the elementType.
* An extending class should overwrite this method.
*
* @return string
......@@ -444,7 +435,7 @@ abstract class Element {
* Gets the cache
*
* @return VariableFrontend
* @throws \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException
* @throws NoSuchCacheException
*/
protected function getCache(): VariableFrontend {
if (!$this->cache) {
......@@ -486,7 +477,7 @@ abstract class Element {
*
* @param array $row
* @return bool
* @throws \TYPO3\CMS\Core\Context\Exception\AspectNotFoundException
* @throws AspectNotFoundException
*/
protected function getEnableFieldResult(array $row): bool {
$ctrl = $GLOBALS['TCA'][$this->table]['ctrl'];
......
......@@ -26,16 +26,14 @@ namespace TYPO3\Languagevisibility\Element;
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\Languagevisibility\Manager\CacheManager;
use TYPO3\Languagevisibility\Service\FrontendServices;
use TYPO3\Languagevisibility\Exception\TableNotSupportedException;
/**
* Class tx_languagevisibility_elementFactory
* Class ElementFactory
*/
class ElementFactory {
......@@ -44,13 +42,13 @@ class ElementFactory {
*
* @param string $table table
* @param array $row identifier
* @param bool $overlay_ids boolean parameter to overlay uids if the user is in workspace context
* @throws \UnexpectedValueException
* @param bool $overlay_ids boolean parameter to overlay UIDs if the user is in workspace context
* @return Element
* @throws TableNotSupportedException
*/
public function getElementForTable(string $table, array $row, bool $overlay_ids = TRUE): Element {
if (!FrontendServices::isSupportedTable($table)) {
throw new \UnexpectedValueException($table . ' not supported ', 1195039394);
throw new TableNotSupportedException($table . ' not supported ', 1195039394);
}
if (
......@@ -95,7 +93,7 @@ class ElementFactory {
} elseif (isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['languagevisibility']['recordElementSupportedTables'][$table])) {
$element = $this->getElementInstance(RecordElement::class, $row);
} else {
throw new UnexpectedValueException($table . ' not supported ', 1195039394);
throw new TableNotSupportedException($table . ' not supported ', 1195039394);
}
break;
}
......@@ -105,69 +103,11 @@ class ElementFactory {
return $element;
}
/**
* This method is needed because the getRootline method from t3lib_pageSelect causes an error when
* getRootline is called be cause getRootline internally uses languagevisibility to determine the
* visibility during the rootline calculation. This results in an unlimited recursion.
*
* @todo The rooline can be build in a smarter way, once the rootline for a page has been created
* same parts of the rootline not have to be calculated twice.
* @param $uid
* @param $languageid
* @return array
* @internal param \The $integer page uid for which to seek back to the page tree root.
*/
protected function getOverlayedRootLine($uid, $languageid) {
$cacheManager = CacheManager::getInstance();
$cacheData = $cacheManager->get('overlayedRootline');
$isCacheEnabled = CacheManager::isCacheEnabled();
if (!$isCacheEnabled || !isset($cacheData[$uid][$languageid])) {
$uid = (int) $uid;
// Initialize:
$selFields = GeneralUtility::uniqueList('pid,uid,t3ver_oid,t3ver_wsid,t3ver_state,title,alias,nav_title,media,layout,hidden,starttime,endtime,fe_group,extendToSubpages,doktype,TSconfig,is_siteroot,mount_pid,mount_pid_ol,fe_login_mode,' . $GLOBALS['TYPO3_CONF_VARS']['FE']['addRootLineFields']);
$loopCheck = 0;
$theRowArray = Array();
while ($uid !== 0 && $loopCheck < 20) { // Max 20 levels in the page tree.
$row = BackendUtility::getRecordWSOL('pages', $uid, $selFields, ' AND pages.doktype!=255');
if (!$row) {
// broken rootline
return [];
}
BackendUtility::fixVersioningPid('pages', $row);
if (is_array($row)) {
// Mount Point page types are allowed ONLY a) if they are the outermost record in rootline and b) if the overlay flag is not set:
$uid = $row['pid']; // Next uid
}
// Add row to rootline with language overlaid:
$langvisHook = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_page.php']['getPageOverlay']['languagevisility'];
unset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_page.php']['getPageOverlay']['languagevisility']);
$theRowArray[] = BackendUtility::getRecordLocalization('pages', $uid, $languageid);
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_page.php']['getPageOverlay']['languagevisility'] = $langvisHook;
$loopCheck++;
}
// Create output array (with reversed order of numeric keys):
$output = Array();
$c = count($theRowArray);
foreach ($theRowArray as $key => $val) {
$c--;
$output[$c] = $val;
}
$cacheData[$uid][$languageid] = $output;
$cacheManager->set('overlayedRootline', $cacheData);
}
return $cacheData[$uid][$languageid];
}
/**
* Gets instance depending on TYPO3 version
*
* @param string $name name of the class
* @param array $row row that is used to initialaze element instance
* @param array $row row that is used to initialize element instance
* @return Object
*/
private function getElementInstance($name, $row) {
......
......@@ -12,7 +12,7 @@ class PageElement extends RecordElement {
/**
* Returns which field in the language should be used to read the default visibility
*
*@return string (blank=default / page=page)
* @return string (blank=default / page=page)
**/
public function getFieldToUseForDefaultVisibility(): string {
return 'page';
......
......@@ -25,24 +25,21 @@ namespace TYPO3\Languagevisibility\Element;
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
use TYPO3\CMS\Core\Context\Exception\AspectNotFoundException;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* Class RecordElement
*
* @author Daniel Poetzinger <poetzinger@aoemedia.de>
* @author Daniel Poetzinger <poetzinger@aoemedia.de>
* @coauthor Tolleiv Nietsch <nietsch@aoemedia.de>
* @coauthor Timo Schmidt <schmidt@aoemedia.de>
*/
class RecordElement extends Element {
/**
* Returns a formal description of the record element.
*
* (non-PHPdoc)
* @see classes/tx_languagevisibility_element#getElementDescription()
* @return string
*/
public function getElementDescription(): string {
......@@ -57,11 +54,11 @@ class RecordElement extends Element {
* @see classes/tx_languagevisibility_element#getOverLayRecordForCertainLanguageImplementation($languageId)
* @param $languageId
* @return array|mixed
* @throws \TYPO3\CMS\Core\Context\Exception\AspectNotFoundException
* @throws AspectNotFoundException
*/
protected function getOverLayRecordForCertainLanguageImplementation($languageId): array {
if (empty($this->table)) {
return array();
return [];
}
$ctrl = $GLOBALS['TCA'][$this->table]['ctrl'];
......@@ -73,13 +70,20 @@ class RecordElement extends Element {
->from($this->table)
->where(
$queryBuilder->expr()->andX(
$queryBuilder->expr()->eq('pid', $queryBuilder->createNamedParameter($this->getPid(), \PDO::PARAM_INT)),
$queryBuilder->expr()->eq($ctrl['languageField'], $queryBuilder->createNamedParameter($languageId, \PDO::PARAM_INT))
$queryBuilder->expr()->eq(
'pid', $queryBuilder->createNamedParameter($this->getPid(), \PDO::PARAM_INT)
),
$queryBuilder->expr()->eq(
$ctrl['languageField'], $queryBuilder->createNamedParameter($languageId, \PDO::PARAM_INT)
)
)
);
if ($languageId > 0) {
$queryBuilder->andWhere(
$queryBuilder->expr()->eq($ctrl['transOrigPointerField'], $queryBuilder->createNamedParameter($this->getUid(), \PDO::PARAM_INT))
$queryBuilder->expr()->eq(
$ctrl['transOrigPointerField'],
$queryBuilder->createNamedParameter($this->getUid(), \PDO::PARAM_INT)
)
);
} else {
$queryBuilder->andWhere(
......@@ -96,16 +100,4 @@ class RecordElement extends Element {
return $olrow;
}
/**
* Returns the fallback order of an record element.
*
* (non-PHPdoc)
* @see classes/tx_languagevisibility_element#getFallbackOrder($language)
* @param SiteLanguage $language
* @return array
*/
public function getFallbackOrder(SiteLanguage $language): array {
return $language->getFallbackLanguageIds();
}
}
......@@ -24,6 +24,7 @@ namespace TYPO3\Languagevisibility\Element;
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/**
* A visibility object represents the visibility of an element.
* It contains a visibilityString(-,yes,no,f,t) and a visibility
......@@ -42,7 +43,7 @@ class Visibility {
protected $visibilityString = '';
/**
* Holds a description for the visiblitiy.
* Holds a description for the visibility.
*
* @var string
*/
......
......@@ -27,7 +27,7 @@ namespace TYPO3\Languagevisibility\Exception;
/**
*
* @author Daniel Poetzinger <poetzinger@aoemedia.de>
* @author Daniel Poetzinger <poetzinger@aoemedia.de>
* @coauthor Tolleiv Nietsch <nietsch@aoemedia.de>
* @coauthor Timo Schmidt <schmidt@aoemedia.de>
*/
......
......@@ -5,7 +5,7 @@ namespace TYPO3\Languagevisibility\Exception;
/***************************************************************
* Copyright notice
*
* (c) 2007 AOE media (dev@aoemedia.de)
* (c) 2020 sgalinski Internet Services (https://www.sgalinski.de)
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
......@@ -27,10 +27,8 @@ namespace TYPO3\Languagevisibility\Exception;
/**
*
* @author Daniel Poetzinger <poetzinger@aoemedia.de>
* @coauthor Tolleiv Nietsch <nietsch@aoemedia.de>
* @coauthor Timo Schmidt <schmidt@aoemedia.de>
* @author Fabio Stegmeyer <fabio.stegmeyer@sgalinski.de>
*/
class InvalidRowException extends \Exception {
class TableNotSupportedException extends \Exception {
}
......@@ -25,35 +25,39 @@ namespace TYPO3\Languagevisibility\Hook;
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/#
use Exception;
use TYPO3\CMS\Backend\Controller\EditDocumentController;
use TYPO3\CMS\Backend\Form\FormDataProvider\DatabaseUserPermissionCheck;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\Languagevisibility\Service\BackendServices;
/**
* Class/Function which manipulates the item-array for the listing (see piFlexform).
*
* @author Fabrizio Brance
* @author Timo Schmidt
* @author Fabrizio Brance
* @author Timo Schmidt
*/
class AlternativeDocumentHook {
/**
* @param array $params
* @param DatabaseUserPermissionCheck $permissionCheck
* @param EditDocumentController $ref
* @param EditDocumentController|null $ref
* @return bool
* @throws Exception
*/
public function makeEditForm_accessCheck(
array $params, DatabaseUserPermissionCheck $permissionCheck, EditDocumentController $ref = NULL
) {
): bool {
if ($params['hasAccess']) {
return TRUE;
}
// user wants to edit/create page record but has no access to default language
// user wants to edit/create page record but has no access to default language
$hasAccess = FALSE;
if ($params['table'] === 'pages' && !$GLOBALS['BE_USER']->checkLanguageAccess(0)) {
if (BackendServices::hasUserAccessToPageRecord((int) $params['uid'], $params['cmd'])) {
$row = BackendUtility::getRecord($params['table'], $params['uid']);
if ($row !== NULL && BackendServices::hasUserAccessToPageRecord($row, $params['cmd'])) {
$hasAccess = TRUE;
}
}
......
......@@ -25,15 +25,16 @@ namespace TYPO3\Languagevisibility\Hook;
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
use Exception;
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Context\Exception\AspectNotFoundException;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\ContentObject\Menu\AbstractMenuContentObject;
use TYPO3\CMS\Frontend\ContentObject\Menu\AbstractMenuFilterPagesHookInterface;
use TYPO3\Languagevisibility\Service\FrontendServices;
use TYPO3\Languagevisibility\Service\VisibilityService;
/**
* Class tx_languagevisibility_hooks_tslib_menu
* Class MenuHook
*
* @package AOE\Languagevisibility\Hooks
*/
......@@ -47,12 +48,17 @@ class MenuHook implements AbstractMenuFilterPagesHookInterface {
* @param $spacer
* @param AbstractMenuContentObject $obj
* @return bool
* @throws AspectNotFoundException
* @throws Exception
*/
public function tslib_menu_filterMenuPagesHook(array &$data, array $banUidArray, $spacer, AbstractMenuContentObject $obj) {
public function tslib_menu_filterMenuPagesHook(
array &$data, array $banUidArray, $spacer, AbstractMenuContentObject $obj
): bool {
if (!FrontendServices::checkVisiblityForElement(
$data,
'pages',
GeneralUtility::makeInstance(Context::class)->getPropertyFromAspect('language', 'id'))
GeneralUtility::makeInstance(Context::class)->getPropertyFromAspect('language', 'id')
)
) {
return FALSE;
}
......@@ -68,8 +74,9 @@ class MenuHook implements AbstractMenuFilterPagesHookInterface {
* @param bool $spacer
* @param AbstractMenuContentObject $obj
* @return bool
* @throws AspectNotFoundException
*/
public function processFilter(array &$data, array $banUidArray, $spacer, AbstractMenuContentObject $obj) {
public function processFilter(array &$data, array $banUidArray, $spacer, AbstractMenuContentObject $obj): bool {
return $this->tslib_menu_filterMenuPagesHook($data, $banUidArray, $spacer, $obj);
}
}
......@@ -25,7 +25,11 @@ namespace TYPO3\Languagevisibility\Hook;
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
use Exception;
use ReflectionException;
use TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException;
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Context\Exception\AspectNotFoundException;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
use TYPO3\CMS\Core\Utility\GeneralUtility;
......@@ -33,10 +37,11 @@ use TYPO3\CMS\Frontend\Page\PageRepository;
use TYPO3\CMS\Frontend\Page\PageRepositoryGetPageOverlayHookInterface;
use TYPO3\CMS\Frontend\Page\PageRepositoryGetRecordOverlayHookInterface;
use TYPO3\Languagevisibility\Exception\InvalidRowException;
use TYPO3\Languagevisibility\Exception\TableNotSupportedException;
use TYPO3\Languagevisibility\Service\FrontendServices;
/**
* Class tx_languagevisibility_hooks_t3lib_page
* Class PageHook
*
* @package Aoe\Languagevisibility\Hooks
*/
......@@ -49,18 +54,18 @@ class PageHook implements PageRepositoryGetPageOverlayHookInterface, PageReposit
* 2) $lUid == null
* is relevant if we did the overlay ourselfs and the processing within getPageOverlay function is not relevant anymore
* 3) $lUid changed
* is relevant if we just changed the target-languge but require getPageOverlay to proceed with the overlay-chrunching
* is relevant if we just changed the target-languge but require getPageOverlay to proceed with the overlay-crunching
* 4) $lUid changed to 0 (which may be the case for forced fallbacks to default). Please check Core Setting hideIfNotTranslated in this case to be sure the page can be shown in this case
*
* @param mixed $pageInput
* @param integer $lUid Passed ad reference!
* @param PageRepository $parent
* @return void
* @throws \ReflectionException
* @throws \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException
* @throws \TYPO3\CMS\Core\Context\Exception\AspectNotFoundException
* @throws NoSuchCacheException
* @throws AspectNotFoundException|TableNotSupportedException
* @throws Exception
*/
public function getPageOverlay_preProcess(&$pageInput, &$lUid, PageRepository $parent) {
public function getPageOverlay_preProcess(&$pageInput, &$lUid, PageRepository $parent): void {
if (is_int($pageInput)) {
$pageId = $pageInput;
} elseif (is_array($pageInput) && isset($pageInput['uid'])) {
......@@ -106,25 +111,25 @@ class PageHook implements PageRepositoryGetPageOverlayHookInterface, PageReposit
/**
* The flow in t3lib_page is:
* - call preProcess
* - if uid and pid > then overlay if langauge != 0
* - if uid and pid > then overlay if language != 0
* - after this postProcess is called - which only corrects the overlay row for certain elements
*
* @param string $table
* @param array $row
* @param integer $sys_language_content
* @param integer|bool $sys_language_content
* @param string $OLmode
* @param PageRepository $parent
* @return void
*/
public function getRecordOverlay_preProcess($table, &$row, &$sys_language_content, $OLmode, PageRepository $parent
) {
if (!FrontendServices::isSupportedTable($table)
|| (!is_array($row))
|| (!isset($row['uid']))) {
): void {
if ((!is_array($row))