From c395609192468de8e1f60e1bfd81f1a0b174b4be Mon Sep 17 00:00:00 2001
From: Frank Naegler <frank.naegler@typo3.org>
Date: Fri, 27 May 2016 12:53:10 +0200
Subject: [PATCH] [TASK] Deprecate MenuViewHelperTrait

The MenuViewHelperTrait has been marked as deprecated.
All methods of the Trait have been implemented in a new
AbstractMenuViewHelper class.

Resolves: #75209
Releases: master
Change-Id: Ie8cad645c80c3cb7814dd2b69f22feb729334779
Reviewed-on: https://review.typo3.org/48111
Reviewed-by: Markus Klein <markus.klein@typo3.org>
Tested-by: Markus Klein <markus.klein@typo3.org>
Reviewed-by: Morton Jonuschat <m.jonuschat@mojocode.de>
Tested-by: Morton Jonuschat <m.jonuschat@mojocode.de>
---
 ...5209-CodecleanupForMenuViewHelperTrait.rst |  27 ++++
 .../Menu/AbstractMenuViewHelper.php           | 115 ++++++++++++++++++
 .../ViewHelpers/Menu/CategoriesViewHelper.php |   5 +-
 .../ViewHelpers/Menu/DirectoryViewHelper.php  |  12 +-
 .../ViewHelpers/Menu/KeywordsViewHelper.php   |   4 +-
 .../ViewHelpers/Menu/ListViewHelper.php       |   7 +-
 .../ViewHelpers/Menu/MenuViewHelperTrait.php  |  10 ++
 .../ViewHelpers/Menu/SectionViewHelper.php    |   4 +-
 .../ViewHelpers/Menu/UpdatedViewHelper.php    |   8 +-
 9 files changed, 164 insertions(+), 28 deletions(-)
 create mode 100644 typo3/sysext/core/Documentation/Changelog/master/Deprecation-75209-CodecleanupForMenuViewHelperTrait.rst
 create mode 100644 typo3/sysext/fluid_styled_content/Classes/ViewHelpers/Menu/AbstractMenuViewHelper.php

diff --git a/typo3/sysext/core/Documentation/Changelog/master/Deprecation-75209-CodecleanupForMenuViewHelperTrait.rst b/typo3/sysext/core/Documentation/Changelog/master/Deprecation-75209-CodecleanupForMenuViewHelperTrait.rst
new file mode 100644
index 000000000000..0e5e84637eaa
--- /dev/null
+++ b/typo3/sysext/core/Documentation/Changelog/master/Deprecation-75209-CodecleanupForMenuViewHelperTrait.rst
@@ -0,0 +1,27 @@
+==========================================================
+Deprecation: #75209 - Code cleanup for MenuViewHelperTrait
+==========================================================
+
+Description
+===========
+
+The ``MenuViewHelperTrait`` has been marked as deprecated.
+All methods of the Trait have been implemented in a new ``AbstractMenuViewHelper`` class.
+
+
+Impact
+======
+
+Using the methods of the ``MenuViewHelperTrait`` will trigger a deprecation log entry.
+
+
+Affected Installations
+======================
+
+Instances with custom extensions that use the ``MenuViewHelperTrait`.
+
+
+Migration
+=========
+
+Extend the new ``AbstractMenuViewHelper`` which contains all methods instead of using the trait.
diff --git a/typo3/sysext/fluid_styled_content/Classes/ViewHelpers/Menu/AbstractMenuViewHelper.php b/typo3/sysext/fluid_styled_content/Classes/ViewHelpers/Menu/AbstractMenuViewHelper.php
new file mode 100644
index 000000000000..f53b13aada6a
--- /dev/null
+++ b/typo3/sysext/fluid_styled_content/Classes/ViewHelpers/Menu/AbstractMenuViewHelper.php
@@ -0,0 +1,115 @@
+<?php
+namespace TYPO3\CMS\FluidStyledContent\ViewHelpers\Menu;
+
+/*
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+
+use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
+use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
+use TYPO3\CMS\Frontend\Page\PageRepository;
+
+/**
+ * Class AbstractMenuViewHelper
+ */
+abstract class AbstractMenuViewHelper extends AbstractViewHelper
+{
+    /**
+     * Get the constraints for the page based on doktype and field "nav_hide"
+     *
+     * By default the following doktypes are always ignored:
+     * - 6: Backend User Section
+     * - > 200: Folder (254)
+     *          Recycler (255)
+     *
+     * Optional are:
+     * - 199: Menu separator
+     * - nav_hide: Not in menu
+     *
+     * @param bool $includeNotInMenu Should pages which are hidden for menu's be included
+     * @param bool $includeMenuSeparator Should pages of type "Menu separator" be included
+     * @return string
+     */
+    protected function getPageConstraints($includeNotInMenu = false, $includeMenuSeparator = false)
+    {
+        $constraints = array();
+
+        $constraints[] = 'doktype NOT IN (' . PageRepository::DOKTYPE_BE_USER_SECTION . ',' . PageRepository::DOKTYPE_RECYCLER . ',' . PageRepository::DOKTYPE_SYSFOLDER . ')';
+
+        if (!$includeNotInMenu) {
+            $constraints[] = 'nav_hide = 0';
+        }
+
+        if (!$includeMenuSeparator) {
+            $constraints[] = 'doktype != ' . PageRepository::DOKTYPE_SPACER;
+        }
+
+        return 'AND ' . implode(' AND ', $constraints);
+    }
+
+    /**
+     * Get a filtered list of page UIDs according to initial list
+     * of UIDs and entryLevel parameter.
+     *
+     * @param array $pageUids
+     * @param int|null $entryLevel
+     * @return array
+     */
+    protected function getPageUids(array $pageUids, $entryLevel = 0)
+    {
+        $typoScriptFrontendController = $this->getTypoScriptFrontendController();
+
+        // Remove empty entries from array
+        $pageUids = array_filter($pageUids);
+
+        // If no pages have been defined, use the current page
+        if (empty($pageUids)) {
+            if ($entryLevel !== null) {
+                if ($entryLevel < 0) {
+                    $entryLevel = count($typoScriptFrontendController->tmpl->rootLine) - 1 + $entryLevel;
+                }
+                $pageUids = array($typoScriptFrontendController->tmpl->rootLine[$entryLevel]['uid']);
+            } else {
+                $pageUids = array($typoScriptFrontendController->id);
+            }
+        }
+
+        return $pageUids;
+    }
+
+    /**
+     * @param array $variables
+     * @return mixed
+     */
+    protected function renderChildrenWithVariables(array $variables)
+    {
+        foreach ($variables as $name => $value) {
+            $this->templateVariableContainer->add($name, $value);
+        }
+
+        $output = $this->renderChildren();
+
+        foreach ($variables as $name => $_) {
+            $this->templateVariableContainer->remove($name);
+        }
+
+        return $output;
+    }
+
+    /**
+     * @return TypoScriptFrontendController
+     */
+    protected function getTypoScriptFrontendController()
+    {
+        return $GLOBALS['TSFE'];
+    }
+}
diff --git a/typo3/sysext/fluid_styled_content/Classes/ViewHelpers/Menu/CategoriesViewHelper.php b/typo3/sysext/fluid_styled_content/Classes/ViewHelpers/Menu/CategoriesViewHelper.php
index 5e4311ec4fce..402f063e491d 100644
--- a/typo3/sysext/fluid_styled_content/Classes/ViewHelpers/Menu/CategoriesViewHelper.php
+++ b/typo3/sysext/fluid_styled_content/Classes/ViewHelpers/Menu/CategoriesViewHelper.php
@@ -14,7 +14,6 @@ namespace TYPO3\CMS\FluidStyledContent\ViewHelpers\Menu;
  * The TYPO3 project - inspiring people to share!
  */
 
-use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
 use TYPO3\CMS\Fluid\Core\ViewHelper\Exception;
 use TYPO3\CMS\Frontend\Category\Collection\CategoryCollection;
 
@@ -36,10 +35,8 @@ use TYPO3\CMS\Frontend\Category\Collection\CategoryCollection;
  * Page with category 1 and 2 assigned
  * </output>
  */
-class CategoriesViewHelper extends AbstractViewHelper
+class CategoriesViewHelper extends AbstractMenuViewHelper
 {
-    use MenuViewHelperTrait;
-
     /**
      * Output escaping is disabled as child content contains HTML by default
      *
diff --git a/typo3/sysext/fluid_styled_content/Classes/ViewHelpers/Menu/DirectoryViewHelper.php b/typo3/sysext/fluid_styled_content/Classes/ViewHelpers/Menu/DirectoryViewHelper.php
index e8c02ae9004f..ee926a194a80 100644
--- a/typo3/sysext/fluid_styled_content/Classes/ViewHelpers/Menu/DirectoryViewHelper.php
+++ b/typo3/sysext/fluid_styled_content/Classes/ViewHelpers/Menu/DirectoryViewHelper.php
@@ -14,8 +14,6 @@ namespace TYPO3\CMS\FluidStyledContent\ViewHelpers\Menu;
  * The TYPO3 project - inspiring people to share!
  */
 
-use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
-
 /**
  * A view helper which returns the subpages of the given pages
  *
@@ -35,10 +33,8 @@ use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
  * Subpage 1 of page with uid = 2
  * </output>
  */
-class DirectoryViewHelper extends AbstractViewHelper
+class DirectoryViewHelper extends AbstractMenuViewHelper
 {
-    use MenuViewHelperTrait;
-
     /**
      * Output escaping is disabled as child content contains HTML by default
      *
@@ -111,8 +107,10 @@ class DirectoryViewHelper extends AbstractViewHelper
             $typoScriptFrontendController->register['ceMenuLevel_directory']--;
 
             if ($typoScriptFrontendController->register['ceMenuLevel_directory'] === 0) {
-                unset($typoScriptFrontendController->register['ceMenuLevel_directory']);
-                unset($typoScriptFrontendController->register['ceMenuMaximumLevel_directory']);
+                unset(
+                    $typoScriptFrontendController->register['ceMenuLevel_directory'],
+                    $typoScriptFrontendController->register['ceMenuMaximumLevel_directory']
+                );
             }
         }
 
diff --git a/typo3/sysext/fluid_styled_content/Classes/ViewHelpers/Menu/KeywordsViewHelper.php b/typo3/sysext/fluid_styled_content/Classes/ViewHelpers/Menu/KeywordsViewHelper.php
index 855131cbd7de..fd523ee5f3b6 100644
--- a/typo3/sysext/fluid_styled_content/Classes/ViewHelpers/Menu/KeywordsViewHelper.php
+++ b/typo3/sysext/fluid_styled_content/Classes/ViewHelpers/Menu/KeywordsViewHelper.php
@@ -14,7 +14,6 @@ namespace TYPO3\CMS\FluidStyledContent\ViewHelpers\Menu;
  * The TYPO3 project - inspiring people to share!
  */
 use TYPO3\CMS\Core\Database\DatabaseConnection;
-use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
 
 /**
  * A view helper which returns pages with one of the same keywords as the given pages
@@ -38,9 +37,8 @@ use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
  * Page with the keyword "typo3"
  * </output>
  */
-class KeywordsViewHelper extends AbstractViewHelper
+class KeywordsViewHelper extends AbstractMenuViewHelper
 {
-    use MenuViewHelperTrait;
 
     /**
      * Output escaping is disabled as child content contains HTML by default
diff --git a/typo3/sysext/fluid_styled_content/Classes/ViewHelpers/Menu/ListViewHelper.php b/typo3/sysext/fluid_styled_content/Classes/ViewHelpers/Menu/ListViewHelper.php
index b7c5b2977588..85cfaf56dd4c 100644
--- a/typo3/sysext/fluid_styled_content/Classes/ViewHelpers/Menu/ListViewHelper.php
+++ b/typo3/sysext/fluid_styled_content/Classes/ViewHelpers/Menu/ListViewHelper.php
@@ -13,7 +13,6 @@ namespace TYPO3\CMS\FluidStyledContent\ViewHelpers\Menu;
  *
  * The TYPO3 project - inspiring people to share!
  */
-use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
 
 /**
  * A view helper which returns a list of pages
@@ -33,13 +32,11 @@ use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
  * Page with uid = 2
  * </output>
  */
-class ListViewHelper extends AbstractViewHelper
+class ListViewHelper extends AbstractMenuViewHelper
 {
-    use MenuViewHelperTrait;
-
     /**
      * Output escaping is disabled as child content contains HTML by default
-     * 
+     *
      * @var bool
      */
     protected $escapeOutput = false;
diff --git a/typo3/sysext/fluid_styled_content/Classes/ViewHelpers/Menu/MenuViewHelperTrait.php b/typo3/sysext/fluid_styled_content/Classes/ViewHelpers/Menu/MenuViewHelperTrait.php
index c3ee21eee737..30d35f739a90 100644
--- a/typo3/sysext/fluid_styled_content/Classes/ViewHelpers/Menu/MenuViewHelperTrait.php
+++ b/typo3/sysext/fluid_styled_content/Classes/ViewHelpers/Menu/MenuViewHelperTrait.php
@@ -14,12 +14,14 @@ namespace TYPO3\CMS\FluidStyledContent\ViewHelpers\Menu;
  * The TYPO3 project - inspiring people to share!
  */
 
+use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
 use TYPO3\CMS\Frontend\Page\PageRepository;
 
 /**
  * Trait for Menu-ViewHelpers that require support functions for
  * working with menus that require page selection constraints.
+ * @deprecated since TYPO3 v8, will be removed in TYPO3 v9
  */
 trait MenuViewHelperTrait
 {
@@ -37,10 +39,12 @@ trait MenuViewHelperTrait
      *
      * @param bool $includeNotInMenu Should pages which are hidden for menu's be included
      * @param bool $includeMenuSeparator Should pages of type "Menu separator" be included
+     * @deprecated since TYPO3 v8, will be removed in TYPO3 v9, use AbstractMenuViewHelper instead of MenuViewHelperTrait
      * @return string
      */
     protected function getPageConstraints($includeNotInMenu = false, $includeMenuSeparator = false)
     {
+        GeneralUtility::logDeprecatedFunction();
         $constraints = array();
 
         $constraints[] = 'doktype NOT IN (' . PageRepository::DOKTYPE_BE_USER_SECTION . ',' . PageRepository::DOKTYPE_RECYCLER . ',' . PageRepository::DOKTYPE_SYSFOLDER . ')';
@@ -62,10 +66,12 @@ trait MenuViewHelperTrait
      *
      * @param array $pageUids
      * @param int|NULL $entryLevel
+     * @deprecated since TYPO3 v8, will be removed in TYPO3 v9, use AbstractMenuViewHelper instead of MenuViewHelperTrait
      * @return array
      */
     protected function getPageUids(array $pageUids, $entryLevel = 0)
     {
+        GeneralUtility::logDeprecatedFunction();
         $typoScriptFrontendController = $this->getTypoScriptFrontendController();
 
         // Remove empty entries from array
@@ -88,10 +94,12 @@ trait MenuViewHelperTrait
 
     /**
      * @param array $variables
+     * @deprecated since TYPO3 v8, will be removed in TYPO3 v9, use AbstractMenuViewHelper instead of MenuViewHelperTrait
      * @return mixed
      */
     protected function renderChildrenWithVariables(array $variables)
     {
+        GeneralUtility::logDeprecatedFunction();
         foreach ($variables as $name => $value) {
             $this->templateVariableContainer->add($name, $value);
         }
@@ -106,10 +114,12 @@ trait MenuViewHelperTrait
     }
 
     /**
+     * @deprecated since TYPO3 v8, will be removed in TYPO3 v9, use AbstractMenuViewHelper instead of MenuViewHelperTrait
      * @return TypoScriptFrontendController
      */
     protected function getTypoScriptFrontendController()
     {
+        GeneralUtility::logDeprecatedFunction();
         return $GLOBALS['TSFE'];
     }
 }
diff --git a/typo3/sysext/fluid_styled_content/Classes/ViewHelpers/Menu/SectionViewHelper.php b/typo3/sysext/fluid_styled_content/Classes/ViewHelpers/Menu/SectionViewHelper.php
index 8a3aedfa56a7..a024abcbebb3 100644
--- a/typo3/sysext/fluid_styled_content/Classes/ViewHelpers/Menu/SectionViewHelper.php
+++ b/typo3/sysext/fluid_styled_content/Classes/ViewHelpers/Menu/SectionViewHelper.php
@@ -15,7 +15,6 @@ namespace TYPO3\CMS\FluidStyledContent\ViewHelpers\Menu;
  */
 
 use TYPO3\CMS\Core\Utility\GeneralUtility;
-use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
 
 /**
  * A view helper which returns content elements with 'Show in Section Menus' enabled
@@ -45,9 +44,8 @@ use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
  * Content element 3 in page with uid = 1 and "Show in section menu's" enabled
  * </output>
  */
-class SectionViewHelper extends AbstractViewHelper
+class SectionViewHelper extends AbstractMenuViewHelper
 {
-    use MenuViewHelperTrait;
 
     /**
      * Output escaping is disabled as child content contains HTML by default
diff --git a/typo3/sysext/fluid_styled_content/Classes/ViewHelpers/Menu/UpdatedViewHelper.php b/typo3/sysext/fluid_styled_content/Classes/ViewHelpers/Menu/UpdatedViewHelper.php
index 708272596e16..d5c2eedc1fe8 100644
--- a/typo3/sysext/fluid_styled_content/Classes/ViewHelpers/Menu/UpdatedViewHelper.php
+++ b/typo3/sysext/fluid_styled_content/Classes/ViewHelpers/Menu/UpdatedViewHelper.php
@@ -14,8 +14,6 @@ namespace TYPO3\CMS\FluidStyledContent\ViewHelpers\Menu;
  * The TYPO3 project - inspiring people to share!
  */
 
-use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
-
 /**
  * A view helper which returns recently updated subpages (multiple levels) of the given pages
  *
@@ -35,13 +33,11 @@ use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
  * Recently updated subpage 3
  * </output>
  */
-class UpdatedViewHelper extends AbstractViewHelper
+class UpdatedViewHelper extends AbstractMenuViewHelper
 {
-    use MenuViewHelperTrait;
-
     /**
      * Output escaping is disabled as child content contains HTML by default
-     * 
+     *
      * @var bool
      */
     protected $escapeOutput = false;
-- 
GitLab