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

injectContentConfigurationService()   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 1
1
<?php
2
namespace DCNGmbH\MooxCore\Provider;
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\Service\ConfigurationService;
28
use FluidTYPO3\Flux\Form;
29
use FluidTYPO3\Flux\Provider\ContentProvider;
30
use FluidTYPO3\Flux\Provider\ProviderInterface;
31
use FluidTYPO3\Flux\Utility\ExtensionNamingUtility;
32
use FluidTYPO3\Flux\Utility\PathUtility;
33
use TYPO3\CMS\Core\DataHandling\DataHandler;
34
use TYPO3\CMS\Core\Utility\GeneralUtility;
35
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
36
37
/**
38
 * ConfigurationProvider for records in tt_content
39
 *
40
 * This Configuration Provider has the lowest possible priority
41
 * and is only used to execute a set of hook-style methods for
42
 * processing records. This processing ensures that relationships
43
 * between content elements get stored correctly.
44
 */
45
class CoreContentProvider extends ContentProvider implements ProviderInterface {
46
47
	const MODE_RECORD = 'record';
48
	const MODE_PRESELECT = 'preselect';
49
	const CTYPE_MENU = 'menu';
50
	const CTYPE_TABLE = 'table';
51
	const CTYPE_FIELDNAME = 'CType';
52
	const MENUTYPE_FIELDNAME = 'menu_type';
53
	const MENU_SELECTEDPAGES = 0;
54
	const MENU_SUBPAGESOFSELECTEDPAGES = 1;
55
	const MENU_SUBPAGESOFSELECTEDPAGESWITHABSTRACT = 4;
56
	const MENU_SUBPAGESOFSELECTEDPAGESWITHSECTIONS = 7;
57
	const MENU_SITEMAP = 2;
58
	const MENU_SITEMAPSOFSELECTEDPAGES = 8;
59
	const MENU_SECTIONINDEX = 3;
60
	const MENU_RECENTLYUPDATED = 5;
61
	const MENU_RELATEDPAGES = 6;
62
	const MENU_CATEGORIZEDPAGES = 'categorized_pages';
63
	const MENU_CATEGORIZEDCONTENT = 'categorized_content';
64
	const THEAD_NONE = 'none';
65
	const THEAD_TOP = 'top';
66
	const THEAD_LEFT = 'left';
67
68
	/**
69
	 * @var string
70
	 */
71
	protected $extensionKey = 'DCNGmbH.MooxCore';
72
73
	/**
74
	 * @var integer
75
	 */
76
	protected $priority = 0;
77
78
	/**
79
	 * @var string
80
	 */
81
	protected $tableName = 'tt_content';
82
83
	/**
84
	 * @var string
85
	 */
86
	protected $fieldName = 'content_options';
87
88
	/**
89
	 * Filled with an integer-or-string -> Fluid section name
90
	 * map which maps machine names of menu types to human
91
	 * readable values that are sensible as Fluid section names.
92
	 * When type is selected in menu element, corresponding
93
	 * section gets rendered.
94
	 *
95
	 * @var array
96
	 */
97
	protected $menuTypeToSectionNameMap = array(
98
		self::MENU_SELECTEDPAGES => 'SelectedPages',
99
		self::MENU_SUBPAGESOFSELECTEDPAGES => 'SubPagesOfSelectedPages',
100
		self::MENU_SUBPAGESOFSELECTEDPAGESWITHABSTRACT => 'SubPagesOfSelectedPagesWithAbstract',
101
		self::MENU_SUBPAGESOFSELECTEDPAGESWITHSECTIONS => 'SubPagesOfSelectedPagesWithSections',
102
		self::MENU_SITEMAP => 'SiteMap',
103
		self::MENU_SITEMAPSOFSELECTEDPAGES => 'SiteMapsOfSelectedPages',
104
		self::MENU_SECTIONINDEX => 'SectionIndex',
105
		self::MENU_RECENTLYUPDATED => 'RecentlyUpdated',
106
		self::MENU_RELATEDPAGES => 'RelatedPages',
107
		self::MENU_CATEGORIZEDPAGES => 'CategorizedPages',
108
		self::MENU_CATEGORIZEDCONTENT => 'CategorizedContent'
109
	);
110
111
	/**
112
	* @var ConfigurationService
113
	*/
114
	protected $contentConfigurationService;
115
116
	/**
117
	 * @param ConfigurationService $contentConfigurationService
118
	 * @return void
119
	 */
120
	public function injectContentConfigurationService(ConfigurationService $contentConfigurationService) {
121
		$this->contentConfigurationService = $contentConfigurationService;
122
	}
123
124
	/**
125
	 * @return void
126
	 */
127
	public function initializeObject() {
128
		$typoScript = $this->contentConfigurationService->getAllTypoScript();
129
		$settings = $typoScript['plugin']['tx_mooxcore']['settings'];
130
		$this->templateVariables['settings'] = $settings;
131
		$this->templatePathAndFilename = PathUtility::translatePath($settings['defaults']['template']);
132
	}
133
134
	/**
135
	 * Note: This Provider will -always- trigger on any tt_content record
136
	 * but has the lowest possible (0) priority, ensuring that any
137
	 * Provider which wants to take over, can do so.
138
	 *
139
	 * @param array $row
140
	 * @param string $table
141
	 * @param string $field
142
	 * @param string $extensionKey
143
	 * @return boolean
144
	 */
145
	public function trigger(array $row, $table, $field, $extensionKey = NULL) {
146
		return ($table === $this->tableName && ($field === $this->fieldName || NULL === $field));
147
	}
148
149
	/**
150
	 * @param array $row
151
	 * @return Form
152
	 */
153
	public function getForm(array $row) {
154
		if (self::CTYPE_MENU === $row[self::CTYPE_FIELDNAME]) {
155
			// addtional menu variables
156
			$menuType = $row[self::MENUTYPE_FIELDNAME];
157
			$partialTemplateName = $this->menuTypeToSectionNameMap[$menuType];
158
			$this->templateVariables['menuPartialTemplateName'] = $partialTemplateName;
159
			$this->templateVariables['pageUids'] = GeneralUtility::trimExplode(',', $row['pages']);
160
		}
161
		if (self::CTYPE_TABLE == $row[self::CTYPE_FIELDNAME]) {
162
			$this->templateVariables['tableHeadPositions'] = array(
163
				self::THEAD_NONE => $this->translateLabel('tableHead.none', 'moox_core'),
0 ignored issues
show
Unused Code introduced by
The call to CoreContentProvider::translateLabel() has too many arguments starting with 'moox_core'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
164
				self::THEAD_TOP => $this->translateLabel('tableHead.top', 'moox_core'),
0 ignored issues
show
Unused Code introduced by
The call to CoreContentProvider::translateLabel() has too many arguments starting with 'moox_core'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
165
				self::THEAD_LEFT => $this->translateLabel('tableHead.left', 'moox_core'),
0 ignored issues
show
Unused Code introduced by
The call to CoreContentProvider::translateLabel() has too many arguments starting with 'moox_core'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
166
			);
167
		}
168
		return parent::getForm($row);
169
	}
170
171
	/**
172
	 * @param array $row
173
	 * @return string|NULL
174
	 */
175
	public function getExtensionKey(array $row) {
176
		$extensionKey = $this->extensionKey;
177
		if (FALSE === empty($row['content_variant'])) {
178
			$extensionKey = $row['content_variant'];
179
		}
180
		return ExtensionNamingUtility::getExtensionKey($extensionKey);
181
	}
182
183
	/**
184
	 * @param array $row
185
	 * @return string
186
	 */
187
	public function getControllerExtensionKeyFromRecord(array $row) {
188
		return $this->getExtensionKey($row);
189
	}
190
191
	/**
192
	 * @param array $row
193
	 * @return string
194
	 */
195
	public function getTemplatePathAndFilename(array $row) {
0 ignored issues
show
Coding Style introduced by
getTemplatePathAndFilename uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
196
		$extensionKey = $this->getExtensionKey($row);
197
		$variant = $this->getVariant($row);
198
		$version = $this->getVersion($row);
199
		$registeredTypes = (array) $GLOBALS['TYPO3_CONF_VARS']['DCNGmbH.MooxCore']['types'];
200
		$templateName = TRUE === in_array($row['CType'], $registeredTypes) ? $row['CType'] : 'default';
201
		$template = $this->contentConfigurationService->resolveTemplateFileForVariant($extensionKey, $templateName, $variant, $version);
202
 		return $template;
203
	}
204
205
	/**
206
	 * @param array $row
207
	 * @return string
208
	 */
209 View Code Duplication
	protected function getVariant(array $row) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
210
		$defaults = $this->contentConfigurationService->getDefaults();
211
		if (self::MODE_RECORD !== $defaults['mode'] && TRUE === empty($row['content_variant'])) {
212
			return $defaults['variant'];
213
		}
214
		return $row['content_variant'];
215
	}
216
217
	/**
218
	 * @param array $row
219
	 * @return string
220
	 */
221 View Code Duplication
	protected function getVersion(array $row) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
222
		$defaults = $this->contentConfigurationService->getDefaults();
223
		if (self::MODE_RECORD !== $defaults['mode'] && TRUE === empty($row['content_version'])) {
224
			return $defaults['version'];
225
		}
226
		return $row['content_version'];
227
	}
228
229
	/**
230
	 * @param array $row
231
	 * @return string
232
	 */
233
	public function getControllerActionFromRecord(array $row) {
234
		return strtolower($row['CType']);
235
	}
236
237
	/**
238
	 * @param string $operation
239
	 * @param integer $id
240
	 * @param array $row
241
	 * @param DataHandler $reference
242
	 * @param array $removals Allows overridden methods to pass an additional array of field names to remove from the stored Flux value
243
	 * @return void
244
	 */
245
	public function postProcessRecord($operation, $id, array &$row, DataHandler $reference, array $removals = array()) {
246
		$defaults = $this->contentConfigurationService->getDefaults();
247
		if (self::MODE_RECORD === $defaults['mode']) {
248
			if (TRUE === empty($row['content_variant'])) {
249
				$row['content_variant'] = $defaults['variant'];
250
			}
251
			if (TRUE === empty($row['content_version'])) {
252
				$row['content_version'] = $defaults['version'];
253
			}
254
		}
255
		return parent::postProcessRecord($operation, $id, $row, $reference);
256
	}
257
258
	/**
259
	 * @param array $row
260
	 * @return array
261
	 */
262
	public function getTemplatePaths(array $row) {
263
		$paths = parent::getTemplatePaths($row);
264
265
		$variant = $this->getVariant($row);
266
		if (FALSE === empty($variant)) {
267
			$extensionKey = ExtensionNamingUtility::getExtensionKey($variant);
268
			if (FALSE === empty($extensionKey)) {
269
				$overlayPaths = $this->contentConfigurationService->getViewConfigurationForExtensionName($extensionKey);
270
				$paths = array_merge_recursive($paths, $overlayPaths);
271
			}
272
		}
273
274
		return $paths;
275
	}
276
277
	/**
278
	 * @param string $key
279
	 * @return string|NULL
280
	 */
281
	protected function translateLabel($key) {
282
		return LocalizationUtility::translate($key, 'moox_core');
283
	}
284
285
}