From f53227bc0cbc8296cdf7d0dc0ad6206bbae7d08c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pascal=20D=C3=BCrsteler?= <pascal.duersteler@gmail.com>
Date: Mon, 24 Mar 2014 12:06:03 +0100
Subject: [PATCH] [TASK] Refactor copy-pasted getJavaScriptLabelsFromLocallang

This refactors the copy-pasted method from ext:recycler into
LanguageService.php.

Resolves: #57225
Releases: master
Change-Id: I0312068b70583feeff9d4cbf5f96fbac75baa793
Reviewed-on: http://review.typo3.org/28694
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Tested-by: Wouter Wolters <typo3@wouterwolters.nl>
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
---
 typo3/sysext/lang/Classes/LanguageService.php | 23 +++++++++++
 .../Controller/RecyclerModuleController.php   | 25 +-----------
 typo3/sysext/t3editor/Classes/T3editor.php    | 38 +------------------
 3 files changed, 25 insertions(+), 61 deletions(-)

diff --git a/typo3/sysext/lang/Classes/LanguageService.php b/typo3/sysext/lang/Classes/LanguageService.php
index 3b4fd66d5c77..7a22abd99f7e 100644
--- a/typo3/sysext/lang/Classes/LanguageService.php
+++ b/typo3/sysext/lang/Classes/LanguageService.php
@@ -461,4 +461,27 @@ class LanguageService {
 		}
 	}
 
+	/**
+	 * Gets labels with a specific fetched from the current locallang file.
+	 * This is useful for e.g gathering javascript labels.
+	 *
+	 * @param string $prefix Prefix to select the correct labels
+	 * @param string $strip Sub-prefix to be removed from label names in the result
+	 * @return array Processed labels
+	 */
+	public function getLabelsWithPrefix($prefix, $strip = '') {
+		$extraction = array();
+		$labels = array_merge((array)$GLOBALS['LOCAL_LANG']['default'], (array)$GLOBALS['LOCAL_LANG'][$GLOBALS['LANG']->lang]);
+		// Regular expression to strip the selection prefix and possibly something from the label name:
+		$labelPattern = '#^' . preg_quote($prefix, '#') . '(' . preg_quote($strip, '#') . ')?#';
+		// Iterate through all locallang labels:
+		foreach ($labels as $label => $value) {
+			if (strpos($label, $prefix) === 0) {
+				$key = preg_replace($labelPattern, '', $label);
+				$extraction[$key] = $value;
+			}
+		}
+		return $extraction;
+	}
+
 }
diff --git a/typo3/sysext/recycler/Classes/Controller/RecyclerModuleController.php b/typo3/sysext/recycler/Classes/Controller/RecyclerModuleController.php
index 5a478202d984..6c2cf346fb04 100644
--- a/typo3/sysext/recycler/Classes/Controller/RecyclerModuleController.php
+++ b/typo3/sysext/recycler/Classes/Controller/RecyclerModuleController.php
@@ -226,34 +226,11 @@ class RecyclerModuleController extends \TYPO3\CMS\Backend\Module\BaseScriptClass
 			'depth_4' => $this->languageService->sL('LLL:EXT:lang/locallang_core.xlf:labels.depth_4'),
 			'depth_infi' => $this->languageService->sL('LLL:EXT:lang/locallang_core.xlf:labels.depth_infi')
 		);
-		$extensionLabels = $this->getJavaScriptLabelsFromLocallang('js.', 'label_');
+		$extensionLabels = $this->languageService->getLabelsWithPrefix('js.', 'label_');
 		$javaScriptLabels = array_merge($coreLabels, $extensionLabels);
 		return $javaScriptLabels;
 	}
 
-	/**
-	 * Gets labels to be used in JavaScript fetched from the current locallang file.
-	 *
-	 * @param string $selectionPrefix Prefix to select the correct labels (default: 'js.')
-	 * @param string $stripFromSelectionName  Sub-prefix to be removed from label names in the result (default: '')
-	 * @return array Labels to be used in JavaScript of the current locallang file
-	 * @todo Check, whether this method can be moved in a generic way to $GLOBALS['LANG']
-	 */
-	protected function getJavaScriptLabelsFromLocallang($selectionPrefix = 'js.', $stripFromSelectionName = '') {
-		$extraction = array();
-		$labels = array_merge((array)$GLOBALS['LOCAL_LANG']['default'], (array)$GLOBALS['LOCAL_LANG'][$this->languageService->lang]);
-		// Regular expression to strip the selection prefix and possibly something from the label name:
-		$labelPattern = '#^' . preg_quote($selectionPrefix, '#') . '(' . preg_quote($stripFromSelectionName, '#') . ')?#';
-		// Iterate through all locallang labels:
-		foreach ($labels as $label => $value) {
-			if (strpos($label, $selectionPrefix) === 0) {
-				$key = preg_replace($labelPattern, '', $label);
-				$extraction[$key] = $value;
-			}
-		}
-		return $extraction;
-	}
-
 	/**
 	 * Gets the buttons that shall be rendered in the docHeader.
 	 *
diff --git a/typo3/sysext/t3editor/Classes/T3editor.php b/typo3/sysext/t3editor/Classes/T3editor.php
index ed6de26f0059..690e9a7723e4 100644
--- a/typo3/sysext/t3editor/Classes/T3editor.php
+++ b/typo3/sysext/t3editor/Classes/T3editor.php
@@ -180,7 +180,7 @@ class T3editor implements \TYPO3\CMS\Core\SingletonInterface {
 
 			$content .= \TYPO3\CMS\Core\Utility\GeneralUtility::wrapJS(
 				'T3editor = T3editor || {};' .
-				'T3editor.lang = ' . json_encode($this->getJavaScriptLabels()) . ';' . LF .
+				'T3editor.lang = ' . json_encode($GLOBALS['LANG']->getLabelsWithPrefix('js.', 'label_')) . ';' . LF .
 				'T3editor.PATH_t3e = "' . $GLOBALS['BACK_PATH'] . $path_t3e . '"; ' . LF .
 				'T3editor.PATH_codemirror = "' . $GLOBALS['BACK_PATH'] . $path_codemirror . '"; ' . LF .
 				'T3editor.URL_typo3 = "' . htmlspecialchars(\TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_SITE_URL') . TYPO3_mainDir) . '"; ' . LF .
@@ -300,42 +300,6 @@ class T3editor implements \TYPO3\CMS\Core\SingletonInterface {
 		return '[' . $stylesheet . 'T3editor.PATH_t3e + "res/css/t3editor_inner.css"]';
 	}
 
-	/**
-	 * Gets the labels to be used in JavaScript in the Ext JS interface.
-	 * @todo this method is copied from EXT:Recycler, maybe this should be refactored into a helper class
-	 *
-	 * @return array The labels to be used in JavaScript
-	 */
-	protected function getJavaScriptLabels() {
-		$coreLabels = array();
-		$extensionLabels = $this->getJavaScriptLabelsFromLocallang('js.', 'label_');
-		return array_merge($coreLabels, $extensionLabels);
-	}
-
-	/**
-	 * Gets labels to be used in JavaScript fetched from the current locallang file.
-	 * @todo this method is copied from EXT:Recycler, maybe this should be refactored into a helper class
-	 *
-	 * @param string $selectionPrefix Prefix to select the correct labels (default: 'js.')
-	 * @param string $stripFromSelectionName Sub-prefix to be removed from label names in the result (default: '')
-	 * @return array Lables to be used in JavaScript of the current locallang file
-	 * @todo Check, whether this method can be moved in a generic way to $GLOBALS['LANG']
-	 */
-	protected function getJavaScriptLabelsFromLocallang($selectionPrefix = 'js.', $stripFromSelectionName = '') {
-		$extraction = array();
-		$labels = array_merge((array)$GLOBALS['LOCAL_LANG']['default'], (array)$GLOBALS['LOCAL_LANG'][$GLOBALS['LANG']->lang]);
-		// Regular expression to strip the selection prefix and possibly something from the label name:
-		$labelPattern = '#^' . preg_quote($selectionPrefix, '#') . '(' . preg_quote($stripFromSelectionName, '#') . ')?#';
-		// Iterate throuh all locallang lables:
-		foreach ($labels as $label => $value) {
-			if (strpos($label, $selectionPrefix) === 0) {
-				$key = preg_replace($labelPattern, '', $label);
-				$extraction[$key] = $value;
-			}
-		}
-		return $extraction;
-	}
-
 	/**
 	 * Generates HTML with code editor
 	 *
-- 
GitLab