Completed
Push — master ( 2b6e28...3d0129 )
by
unknown
10:26
created

ProviderField::translateLabel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
namespace DCNGmbH\MooxCore\UserFunction;
3
/*****************************************************************
4
 *  Copyright notice
5
 *
6
 *  (c) 2014 Claus Due <[email protected]>
7
 *
8
 *  All rights reserved
9
 *
10
 *  This script is part of the TYPO3 project. The TYPO3 project is
11
 *  free software; you can redistribute it and/or modify
12
 *  it under the terms of the GNU General Public License as published by
13
 *  the Free Software Foundation; either version 3 of the License, or
14
 *  (at your option) any later version.
15
 *
16
 *  The GNU General Public License can be found at
17
 *  http://www.gnu.org/copyleft/gpl.html.
18
 *
19
 *  This script is distributed in the hope that it will be useful,
20
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 *  GNU General Public License for more details.
23
 *
24
 *  This copyright notice MUST APPEAR in all copies of the script!
25
 *****************************************************************/
26
27
use DCNGmbH\MooxCore\Provider\CoreContentProvider;
28
use DCNGmbH\MooxCore\Service\ConfigurationService;
29
use TYPO3\CMS\Core\Resource\Utility\BackendUtility;
30
use TYPO3\CMS\Core\Utility\GeneralUtility;
31
use TYPO3\CMS\Extbase\Object\ObjectManager;
32
use TYPO3\CMS\Extbase\Object\ObjectManagerInterface;
33
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
34
35
/**
36
 * Variants Field TCA user function
37
 */
38
class ProviderField {
39
40
	/**
41
	 * @var ObjectManagerInterface
42
	 */
43
	protected $objectManager;
44
45
	/**
46
	 * @var ConfigurationService
47
	 */
48
	protected $configurationService;
49
50
	/**
51
	 * @param ObjectManagerInterface $objectManager
52
	 * @reutrn void
53
	 */
54
	public function injectObjectManager(ObjectManagerInterface $objectManager) {
55
		$this->objectManager = $objectManager;
56
	}
57
58
	/**
59
	 * @param ConfigurationService $configurationService
60
	 * @return void
61
	 */
62
	public function injectConfigurationService(ConfigurationService $configurationService) {
63
		$this->configurationService = $configurationService;
64
	}
65
66
	/**
67
	 * Constructor
68
	 */
69
	public function __construct() {
70
		/** @var ObjectManager $objectManager */
71
		$objectManager = GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
72
		$this->injectObjectManager($objectManager);
73
		/** @var ConfigurationService $configurationService */
74
		$configurationService = $this->objectManager->get('DCNGmbH\MooxCore\Service\ConfigurationService');
75
		$this->injectConfigurationService($configurationService);
76
	}
77
78
	/**
79
	 * @param array $parameters
80
	 * @return string
81
	 */
82
	public function createVariantsField(array $parameters) {
83
		$parameters['row'] = $this->loadRecord('tt_content', $parameters['row']['uid']);
84
		$extensionKeys = $this->configurationService->getVariantExtensionKeysForContentType($parameters['row']['CType']);
85
		$defaults = $this->configurationService->getDefaults();
86
		$preSelected = $parameters['row']['content_variant'];
87
		if (CoreContentProvider::MODE_PRESELECT === $defaults['mode'] && TRUE === empty($preSelected)) {
88
			$preSelected = $defaults['variant'];
89
		}
90
		if (TRUE === is_array($extensionKeys) && 0 < count($extensionKeys)) {
91
			$options = $this->renderOptions($extensionKeys);
92
		} else {
93
			$options = array();
94
		}
95
		return $this->renderSelectField($parameters, $options, $preSelected);
96
	}
97
98
	/**
99
	 * @param array $variants
100
	 * @return array
101
	 */
102
	protected function renderOptions(array $variants) {
103
		$options = array();
104
		foreach ($variants as $variantSetup) {
105
			list ($extensionKey, $labelReference, $icon) = $variantSetup;
106
			$translatedLabel = $this->translateLabel($labelReference, $extensionKey);
107
			if (NULL === $translatedLabel) {
108
				$translatedLabel = $extensionKey;
109
			}
110
			$optionsIcon = '<img src="' . $icon . '" alt="' . $extensionKey . '" /> ';
111
			$options[$extensionKey] = array($optionsIcon, $translatedLabel);
112
		}
113
		return $options;
114
	}
115
116
	/**
117
	 * @param array $parameters
118
	 * @param array $options
119
	 * @param mixed $selectedValue
120
	 * @return string
121
	 */
122
	protected function renderSelectField($parameters, $options, $selectedValue) {
123
		$optionsIcons = array();
124
		$optionsLabels = array();
125
		$selectedIcon = '<img alt="" src="" />';
126
		foreach ($options as $extensionKey => $optionsSetup) {
127
			list($optionsIcons[$extensionKey], $optionsLabels[$extensionKey]) = $optionsSetup;
128
		}
129
		$hasSelectedValue = (TRUE === empty($selectedValue) || TRUE === array_key_exists($selectedValue, $optionsLabels));
130
		$selected = (TRUE === empty($selectedValue) ? ' selected="selected"' : NULL);
131
		foreach ($optionsIcons as $value => $img) {
132
			if ($value === $selectedValue) {
133
				$selectedIcon = $img;
134
				break;
135
			}
136
		}
137
		$html = array(
138
			'<div class="form-control-wrap"><div class="input-group"><div class="input-group-addon input-group-icon">' . $selectedIcon . '</div><select class="select form-control" name="' . $parameters['itemFormElName'] . '" onchange="' . $parameters['fieldChangeFunc']['TBE_EDITOR_fieldChanged'] . ';' . $parameters['fieldChangeFunc']['alert'] . '">',
139
			'<option' . $selected . ' value="">' . $this->translateLabel('tt_content.nativeLabel', 'MooxCore') . '</option>'
140
		);
141
		foreach ($optionsLabels as $value => $label) {
142
			$selected = $value === $selectedValue ? ' selected="selected"' : NULL;
143
			$html[] = '<option' . $selected . ' value="' . $value . '">' . $label . '</option>';
144
		}
145
		if (FALSE === $hasSelectedValue) {
146
			$html[] = '<option selected="selected">INVALID: ' . $selectedValue . '</option>';
147
		}
148
		$html[] = '</select></div></div>';
149
		return implode(LF, $html);
150
	}
151
152
	/**
153
	 * @param array $parameters
154
	 * @return string
155
	 */
156
	public function createVersionsField(array $parameters) {
157
		$parameters['row'] = $this->loadRecord('tt_content', $parameters['row']['uid']);
158
		$options = array();
159
		$defaults = $this->configurationService->getDefaults();
160
		$preSelectedVariant = $parameters['row']['content_variant'];
161
		$preSelectedVersion = $parameters['row']['content_version'];
162
		if (CoreContentProvider::MODE_PRESELECT === $defaults['mode']) {
163
			if (TRUE === empty($preSelectedVariant)) {
164
				$preSelectedVariant = $defaults['variant'];
165
			}
166
			if (TRUE === empty($preSelectedVersion)) {
167
				$preSelectedVersion = $defaults['version'];
168
			}
169
		}
170
171
		$versions = $this->configurationService->getVariantVersions($parameters['row']['CType'], $preSelectedVariant);
172
		if (TRUE === is_array($versions) && 0 < count($versions)) {
173
			foreach ($versions as $version) {
174
				$icon = $this->configurationService->getIconFromVersion($preSelectedVariant, $parameters['row']['CType'], $version);
175
				$versionIcon = '<img src="' . $icon . '" alt="" /> ';
176
				$options[$version] = array($versionIcon, $version);
177
			}
178
		}
179
		return $this->renderSelectField($parameters, $options, $preSelectedVersion);
180
	}
181
182
	/**
183
	 * @param string $key
184
	 * @param string $extensionKey
185
	 * @return string|NULL
186
	 */
187
	protected function translateLabel($key, $extensionKey) {
188
		return LocalizationUtility::translate($key, $extensionKey);
189
	}
190
191
	/**
192
	 * @param string $table
193
	 * @param integer $uid
194
	 * @return array
195
	 * @codeCoverageIgnore
196
	 */
197
	protected function loadRecord($table, $uid) {
198
		return \TYPO3\CMS\Backend\Utility\BackendUtility::getRecord($table, (integer) $uid);
199
	}
200
201
}