From dbe855733eb9142596d22886c86c38e05544649a Mon Sep 17 00:00:00 2001
From: Torsten Oppermann <torsten@sgalinski.de>
Date: Thu, 27 Jul 2017 12:38:53 +0200
Subject: [PATCH] [TASK] Fixing tremplates not found issue when having
 controller in backend folder

---
 ...wsController.php => BackendController.php} |  4 +-
 Classes/ViewHelpers/AbstractViewHelper.php    | 51 ++++++++++++++
 .../AddJavaScriptFileViewHelper.php           | 49 +++++++++++++
 .../InlineLanguageLabelsViewHelper.php        | 70 +++++++++++++++++++
 .../TypoScript/Backend/constants.txt          | 22 ------
 Configuration/TypoScript/Backend/setup.txt    | 22 ------
 .../Default.html => Layouts/Backend.html}     |  0
 .../{Backend => }/Partials/ButtonBar.html     |  0
 .../News => Templates/Backend}/Index.html     |  5 +-
 ext_localconf.php                             | 11 ---
 ext_tables.php                                |  2 +-
 11 files changed, 176 insertions(+), 60 deletions(-)
 rename Classes/Controller/{Backend/NewsController.php => BackendController.php} (96%)
 create mode 100644 Classes/ViewHelpers/AbstractViewHelper.php
 create mode 100644 Classes/ViewHelpers/AddJavaScriptFileViewHelper.php
 create mode 100644 Classes/ViewHelpers/InlineLanguageLabelsViewHelper.php
 delete mode 100644 Configuration/TypoScript/Backend/constants.txt
 delete mode 100644 Configuration/TypoScript/Backend/setup.txt
 rename Resources/Private/{Backend/Layouts/Default.html => Layouts/Backend.html} (100%)
 rename Resources/Private/{Backend => }/Partials/ButtonBar.html (100%)
 rename Resources/Private/{Backend/Templates/News => Templates/Backend}/Index.html (74%)

diff --git a/Classes/Controller/Backend/NewsController.php b/Classes/Controller/BackendController.php
similarity index 96%
rename from Classes/Controller/Backend/NewsController.php
rename to Classes/Controller/BackendController.php
index 8fc166f..fa7812c 100644
--- a/Classes/Controller/Backend/NewsController.php
+++ b/Classes/Controller/BackendController.php
@@ -1,6 +1,6 @@
 <?php
 
-namespace SGalinski\SgNews\Controller\Backend;
+namespace SGalinski\SgNews\Controller;
 
 /***************************************************************
  *  Copyright notice
@@ -36,7 +36,7 @@ use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
 /**
  * News Controller
  */
-class NewsController extends ActionController {
+class BackendController extends ActionController {
 
 	/**
 	 * DocHeaderComponent
diff --git a/Classes/ViewHelpers/AbstractViewHelper.php b/Classes/ViewHelpers/AbstractViewHelper.php
new file mode 100644
index 0000000..5690834
--- /dev/null
+++ b/Classes/ViewHelpers/AbstractViewHelper.php
@@ -0,0 +1,51 @@
+<?php
+
+namespace SGalinski\SgNews\ViewHelpers;
+
+/***************************************************************
+ *  Copyright notice
+ *
+ *  (c) sgalinski Internet Services (https://www.sgalinski.de)
+ *
+ *  All rights reserved
+ *
+ *  This script is part of the TYPO3 project. The TYPO3 project is
+ *  free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  The GNU General Public License can be found at
+ *  http://www.gnu.org/copyleft/gpl.html.
+ *
+ *  This script is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  This copyright notice MUST APPEAR in all copies of the script!
+ ***************************************************************/
+
+use TYPO3\CMS\Fluid\ViewHelpers\Be\AbstractBackendViewHelper;
+
+/**
+ * Abstract view helper
+ */
+class AbstractViewHelper extends AbstractBackendViewHelper {
+	/**
+	 * Returns the base url of the site
+	 *
+	 * Note: Works only in frontend mode
+	 *
+	 * @return string
+	 */
+	public function getBaseUrl() {
+		if ($GLOBALS['TSFE']->absRefPrefix !== '') {
+			$baseUrl = $GLOBALS['TSFE']->absRefPrefix;
+		} else {
+			$baseUrl = $GLOBALS['TSFE']->baseUrl;
+		}
+
+		return $baseUrl;
+	}
+}
diff --git a/Classes/ViewHelpers/AddJavaScriptFileViewHelper.php b/Classes/ViewHelpers/AddJavaScriptFileViewHelper.php
new file mode 100644
index 0000000..c8cd53b
--- /dev/null
+++ b/Classes/ViewHelpers/AddJavaScriptFileViewHelper.php
@@ -0,0 +1,49 @@
+<?php
+
+namespace SGalinski\SgNews\ViewHelpers;
+
+	/***************************************************************
+	 *  Copyright notice
+	 *
+	 *  (c) sgalinski Internet Services (http://www.sgalinski.de)
+	 *
+	 *  All rights reserved
+	 *
+	 *  This script is part of the TYPO3 project. The TYPO3 project is
+	 *  free software; you can redistribute it and/or modify
+	 *  it under the terms of the GNU General Public License as published by
+	 *  the Free Software Foundation; either version 3 of the License, or
+	 *  (at your option) any later version.
+	 *
+	 *  The GNU General Public License can be found at
+	 *  http://www.gnu.org/copyleft/gpl.html.
+	 *
+	 *  This script is distributed in the hope that it will be useful,
+	 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+	 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+	 *  GNU General Public License for more details.
+	 *
+	 *  This copyright notice MUST APPEAR in all copies of the script!
+	 ***************************************************************/
+
+/**
+ * View helper to add custom javascript files
+ *
+ * Example:
+ * {namespace lfe=SGalinski\Lfeditor\ViewHelpers}
+ * <lfe:addJavaScriptFile javaScriptFile="{f:uri.resource(path: 'Scripts/Frontend.js')}" />
+ */
+class AddJavaScriptFileViewHelper extends AbstractViewHelper {
+	/**
+	 * Adds a custom javascript file
+	 *
+	 * @param string $javaScriptFile
+	 * @return void
+	 */
+	public function render($javaScriptFile) {
+		$javaScriptFile = (TYPO3_MODE === 'FE' ? $this->getBaseUrl() : '') . $javaScriptFile;
+		$this->getPageRenderer()->addJsFile($javaScriptFile);
+	}
+}
+
+?>
diff --git a/Classes/ViewHelpers/InlineLanguageLabelsViewHelper.php b/Classes/ViewHelpers/InlineLanguageLabelsViewHelper.php
new file mode 100644
index 0000000..5bc0076
--- /dev/null
+++ b/Classes/ViewHelpers/InlineLanguageLabelsViewHelper.php
@@ -0,0 +1,70 @@
+<?php
+
+namespace SGalinski\SgNews\ViewHelpers;
+
+/***************************************************************
+ *  Copyright notice
+ *
+ *  (c) sgalinski Internet Services (https://www.sgalinski.de)
+ *
+ *  All rights reserved
+ *
+ *  This script is part of the SG project. The SG project is
+ *  free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  The GNU General Public License can be found at
+ *  http://www.gnu.org/copyleft/gpl.html.
+ *
+ *  This script is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  This copyright notice MUST APPEAR in all copies of the script!
+ ***************************************************************/
+
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
+
+/**
+ * View helper to render language labels to
+ * json array to be used in js applications.
+ *
+ * Renders to SG.lang.'extension name' object
+ *
+ * Example:
+ * {namespace rs=SGalinski\RsEvents\ViewHelpers}
+ * <rs:inlineLanguageLabels labels="label01,label02" />
+ */
+class InlineLanguageLabelsViewHelper extends AbstractViewHelper {
+	/**
+	 * Renders the required javascript to make the language labels available
+	 *
+	 * @param string $labels Comma separated list of label keys to include
+	 * @param boolean $htmlEscape
+	 * @return string
+	 */
+	public function render($labels = '', $htmlEscape = FALSE) {
+		$extensionName = $this->controllerContext->getRequest()->getControllerExtensionName();
+		$labels = GeneralUtility::trimExplode(',', $labels, TRUE);
+		$languageArray = [];
+		foreach ($labels as $key) {
+			$value = LocalizationUtility::translate($key, $extensionName);
+			$languageArray[$key] = ($htmlEscape ? htmlentities($value) : $value);
+		}
+		return '
+			<script type="text/javascript">
+			var SG = SG || {};
+			SG.lang = SG.lang || {};
+			SG.lang.' . $extensionName . ' = SG.lang.' . $extensionName . ' || {};
+			var languageLabels = ' . json_encode($languageArray) . ';
+			for (label in languageLabels) {
+				SG.lang.' . $extensionName . '[label] = languageLabels[label];
+			}
+			</script>
+		';
+	}
+}
diff --git a/Configuration/TypoScript/Backend/constants.txt b/Configuration/TypoScript/Backend/constants.txt
deleted file mode 100644
index 023c08a..0000000
--- a/Configuration/TypoScript/Backend/constants.txt
+++ /dev/null
@@ -1,22 +0,0 @@
-module.tx_sgnews {
-	settings < plugin.tx_sgnews.settings
-	persistence < plugin.tx_sgnews.persistence
-	view < plugin.tx_sgnews.view
-	view {
-		# cat=plugin.tx_sgnews/file; type=string; label=Path to template root (FE)
-		templateRootPaths {
-			10 = EXT:sg_news/Resources/Private/Backend/Templates/
-		}
-
-		# cat=plugin.tx_sgnews/file; type=string; label=Path to template partials (FE)
-		partialRootPaths {
-			10 = EXT:sg_news/Resources/Private/Backend/Partials/
-		}
-
-		# cat=plugin.tx_sgnews/file; type=string; label=Path to template layouts (FE)
-		layoutRootPaths {
-			10 = EXT:sg_news/Resources/Private/Backend/Layouts/
-		}
-	}
-}
-
diff --git a/Configuration/TypoScript/Backend/setup.txt b/Configuration/TypoScript/Backend/setup.txt
deleted file mode 100644
index b23bfcb..0000000
--- a/Configuration/TypoScript/Backend/setup.txt
+++ /dev/null
@@ -1,22 +0,0 @@
-module.tx_sgnews {
-	settings < plugin.tx_sgnews.settings
-	persistence < plugin.tx_sgnews.persistence
-	view < plugin.tx_sgnews.view
-	view {
-		# cat=plugin.tx_sgnews/file; type=string; label=Path to template root (FE)
-		templateRootPaths {
-			10 = {$module.tx_sgnews.view.templateRootPaths.10}
-		}
-
-		# cat=plugin.tx_sgnews/file; type=string; label=Path to template partials (FE)
-		partialRootPaths {
-			10 = {$module.tx_sgnews.view.partialRootPaths.10}
-		}
-
-		# cat=plugin.tx_sgnews/file; type=string; label=Path to template layouts (FE)
-		layoutRootPaths {
-			10 = {$module.tx_sgnews.view.layoutRootPaths.10}
-		}
-	}
-}
-
diff --git a/Resources/Private/Backend/Layouts/Default.html b/Resources/Private/Layouts/Backend.html
similarity index 100%
rename from Resources/Private/Backend/Layouts/Default.html
rename to Resources/Private/Layouts/Backend.html
diff --git a/Resources/Private/Backend/Partials/ButtonBar.html b/Resources/Private/Partials/ButtonBar.html
similarity index 100%
rename from Resources/Private/Backend/Partials/ButtonBar.html
rename to Resources/Private/Partials/ButtonBar.html
diff --git a/Resources/Private/Backend/Templates/News/Index.html b/Resources/Private/Templates/Backend/Index.html
similarity index 74%
rename from Resources/Private/Backend/Templates/News/Index.html
rename to Resources/Private/Templates/Backend/Index.html
index 651ad16..208d379 100644
--- a/Resources/Private/Backend/Templates/News/Index.html
+++ b/Resources/Private/Templates/Backend/Index.html
@@ -1,10 +1,11 @@
 {namespace sg=SGalinski\SgRoutes\ViewHelpers}
 
-<f:layout name="Default" />
+<f:layout name="Backend" />
 
 <f:section name="iconButtons">
 </f:section>
-
+<f:section name="headline">
+</f:section>
 <f:section name="content">
 	<f:flashMessages />
 	<p>
diff --git a/ext_localconf.php b/ext_localconf.php
index e0561cd..0ed3fdf 100644
--- a/ext_localconf.php
+++ b/ext_localconf.php
@@ -13,17 +13,6 @@ $extPath = \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('sg_news'
 $tsPath = $extPath . 'Configuration/TypoScript/Common/';
 \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addTypoScriptSetup(file_get_contents($tsPath . 'setup.txt'));
 
-// backend typoscript configuration
-if (TYPO3_MODE === 'BE') {
-	$tsPath = $extPath . 'Configuration/TypoScript/Backend/';
-
-	\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addTypoScriptConstants(
-		file_get_contents($tsPath . 'constants.txt')
-	);
-
-	\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addTypoScriptSetup(file_get_contents($tsPath . 'setup.txt'));
-}
-
 // plugin configurations
 \TYPO3\CMS\Extbase\Utility\ExtensionUtility::configurePlugin(
 	'SGalinski.sg_news',
diff --git a/ext_tables.php b/ext_tables.php
index dcbe427..983a3a4 100644
--- a/ext_tables.php
+++ b/ext_tables.php
@@ -35,7 +35,7 @@ if (TYPO3_MODE === 'BE') {
 		'News',
 		'',
 		[
-			'Backend\News' => 'index',
+			'Backend' => 'index',
 		],
 		[
 			'access' => 'user,group',
-- 
GitLab