Completed
Push — master ( e0ed7b...0e5eef )
by Jakub
03:22
created

Classes/Controller/CoreContentController.php (7 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace DCNGmbH\MooxCore\Controller;
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
use DCNGmbH\MooxCore\Provider\CoreContentProvider;
27
use FluidTYPO3\Flux\Controller\AbstractFluxController;
28
use FluidTYPO3\Flux\Utility\RecursiveArrayUtility;
29
use TYPO3\CMS\Core\Utility\GeneralUtility;
30
use TYPO3\CMS\Core\Utility\MathUtility;
31
32
/**
33
 * Class CoreContentController
34
 */
35
class CoreContentController extends AbstractFluxController {
36
37
	/**
38
	 * @var string
39
	 */
40
	protected $fluxRecordField = 'content_options';
41
42
	/**
43
	 * @var string
44
	 */
45
	protected $fluxTableName = 'tt_content';
46
47
	/**
48
	 * @return void
49
	 */
50
	protected function initializeProvider() {
51
		$this->provider = $this->objectManager->get('DCNGmbH\MooxCore\Provider\CoreContentProvider');
52
	}
53
54
	/**
55
	 * @return void
56
	 */
57
	protected function initializeViewVariables() {
58
		$row = $this->getRecord();
59
		$flexFormData = $this->configurationService->convertFlexFormContentToArray($row['pi_flexform']);
60
		$this->settings = RecursiveArrayUtility::merge($this->settings, $flexFormData, FALSE, FALSE);
0 ignored issues
show
The call to RecursiveArrayUtility::merge() has too many arguments starting with FALSE.

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...
61
		$form = $this->provider->getForm($row);
62
		$generalSettings = $this->configurationService->convertFlexFormContentToArray($row['pi_flexform'], $form);
63
		$contentSettings = $this->configurationService->convertFlexFormContentToArray($row['content_options'], $form);
64
		$this->settings = RecursiveArrayUtility::merge($this->settings, $generalSettings, FALSE, FALSE);
0 ignored issues
show
The call to RecursiveArrayUtility::merge() has too many arguments starting with FALSE.

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...
65
		if (FALSE === isset($this->settings['content'])) {
66
			$this->settings['content'] = $contentSettings;
67
		} else {
68
			$this->settings['content'] = RecursiveArrayUtility::merge($this->settings['content'], $contentSettings);
69
		}
70
		parent::initializeViewVariables();
71
	}
72
73
	/**
74
	 * @return void
75
	 */
76
	public function defaultAction() {
77
78
	}
79
80
	/**
81
	 * @return void
82
	 */
83
	public function headerAction() {
84
85
	}
86
87
	/**
88
	 * @return void
89
	 */
90
	public function textAction() {
91
92
	}
93
94
	/**
95
	 * @return void
96
	 */
97
	public function textpicAction() {
98
99
	}
100
101
	/**
102
	 * @return void
103
	 */
104
	public function imageAction() {
105
106
	}
107
108
	/**
109
	 * @return void
110
	 */
111
	public function bulletsAction() {
112
113
	}
114
115
	/**
116
	 * @return void
117
	 */
118
	public function uploadsAction() {
119
120
	}
121
122
	/**
123
	 * @return void
124
	 */
125
	public function tableAction() {
126
127
	}
128
129
	/**
130
	 * @return void
131
	 */
132
	public function mediaAction() {
133
134
	}
135
136
	/**
137
	 * @return void
138
	 */
139
	public function menuAction() {
0 ignored issues
show
menuAction 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...
140
		$record = $this->getRecord();
141
		$type = $record[CoreContentProvider::MENUTYPE_FIELDNAME];
142
		switch ($type) {
143 View Code Duplication
			case CoreContentProvider::MENU_CATEGORIZEDPAGES:
0 ignored issues
show
This code seems to be duplicated across 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...
144
				$selected = $record['selected_categories'];
145
				$bindings = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
146
					'uid_foreign',
147
					'sys_category_record_mm',
148
					"fieldname = 'categories' AND tablenames = 'pages' AND uid_local IN (" . $selected . ')',
149
					'uid_foreign',
150
					'sorting ASC'
151
				);
152
				$pageUids = array_map('array_pop', $bindings);
153
				$this->view->assign('pageUids', $pageUids);
154
				break;
155 View Code Duplication
			case CoreContentProvider::MENU_CATEGORIZEDCONTENT:
0 ignored issues
show
This code seems to be duplicated across 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...
156
				$selected = $record['selected_categories'];
157
				$bindings = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
158
					'uid_foreign',
159
					'sys_category_record_mm',
160
					"fieldname = 'categories' AND tablenames = 'tt_content' AND uid_local IN (" . $selected . ')',
161
					'uid_foreign',
162
					'sorting ASC'
163
				);
164
				$contentUids = array_map('array_pop', $bindings);
165
				$this->view->assign('contentUids', $contentUids);
166
				break;
167
			case CoreContentProvider::MENU_RELATEDPAGES:
168
				$whereKeywords = $this->getWhereQueryForKeywords($record);
169
				$selectedUids = TRUE === empty($record['pages']) ? $record['uid'] : $record['pages'];
170
				$where = $whereKeywords . ' AND uid NOT IN (' . $selectedUids . ')';
171
				$bindings = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
172
					'uid',
173
					'pages',
174
					$where,
175
					'',
176
					'sorting ASC'
177
				);
178
				$pageUids = array_map('array_pop', $bindings);
179
				$this->view->assign('pageUids', $pageUids);
180
				break;
181
			default:
182
		}
183
	}
184
185
	/**
186
	 * @return void
187
	 */
188
	public function shortcutAction() {
189
		$record = $this->getRecord();
190
		$contentUids = array_map(function($index) {
191
			if (0 !== strpos($index, 'tt_content_') && FALSE === MathUtility::canBeInterpretedAsInteger($index)) {
192
				return FALSE;
193
			}
194
			return str_replace('tt_content_', '', $index);
195
		}, GeneralUtility::trimExplode(',', $record['records']));
196
197
		$this->view->assign('contentUids', implode(',', array_filter($contentUids)));
198
	}
199
200
	/**
201
	 * @return void
202
	 */
203
	public function divAction() {
204
205
	}
206
207
	/**
208
	 * @return void
209
	 */
210
	public function htmlAction() {
211
212
	}
213
214
	/**
215
	 * @param $record
216
	 * @return string
217
	 */
218
	protected function getWhereQueryForKeywords($record) {
0 ignored issues
show
getWhereQueryForKeywords 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...
219
		$selectedUids = $record['pages'];
220
		if (TRUE == empty($selectedUids)) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
221
			$selectedUids = $record['pid'];
222
		}
223
		$keywordsRows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
224
			'keywords',
225
			'pages',
226
			'uid IN (' . $selectedUids . ')',
227
			'',
228
			'sorting ASC'
229
		);
230
231
		$keywords = implode(',', array_map('array_pop', $keywordsRows));
232
		$keywordsArray = array_unique(explode(',', $keywords));
233
		$keyWordsWhereArr = array();
234
		foreach ($keywordsArray as $word) {
235
			$word = trim($word);
236
			if ($word) {
237
				$keyWordsWhereArr[] = 'keywords LIKE \'%' . $GLOBALS['TYPO3_DB']->quoteStr($word, 'pages') . '%\'';
238
			}
239
		}
240
		$where = empty($keyWordsWhereArr) ? '' : '(' . implode(' OR ', $keyWordsWhereArr) . ')';
241
		return $where;
242
	}
243
244
}