Completed
Push — master ( 56025a...b385a5 )
by Fabien
52:20
created
Classes/Module/ModulePidService.php 1 patch
Indentation   +207 added lines, -207 removed lines patch added patch discarded remove patch
@@ -19,212 +19,212 @@
 block discarded – undo
19 19
  */
20 20
 class ModulePidService
21 21
 {
22
-    /**
23
-     * The data type (table)
24
-     *
25
-     * @var string
26
-     */
27
-    protected $dataType = '';
28
-
29
-    /**
30
-     * A collection of speaking error messages why the pid is invalid.
31
-     *
32
-     * @var array
33
-     */
34
-    protected $errors = [];
35
-
36
-    /**
37
-     * ModulePidService constructor.
38
-     */
39
-    public function __construct()
40
-    {
41
-        $this->dataType = $this->getModuleLoader()->getDataType();
42
-    }
43
-
44
-    /**
45
-     * Returns a class instance
46
-     *
47
-     * @return \Fab\Vidi\Module\ModulePidService|object
48
-     */
49
-    static public function getInstance()
50
-    {
51
-        return GeneralUtility::makeInstance(self::class);
52
-    }
53
-
54
-    /**
55
-     * @return bool
56
-     */
57
-    public function isConfiguredPidValid(): bool
58
-    {
59
-        $errors = $this->validateConfiguredPid();
60
-        return empty($errors);
61
-    }
62
-
63
-    /**
64
-     * @return array
65
-     */
66
-    public function validateConfiguredPid(): array
67
-    {
68
-        $configuredPid = $this->getConfiguredNewRecordPid();
69
-        $this->validateRootLevel($configuredPid);
70
-        $this->validatePageExist($configuredPid);
71
-        $this->validateDoktype($configuredPid);
72
-        return $this->errors;
73
-    }
74
-
75
-    /**
76
-     * Return the default configured pid.
77
-     *
78
-     * @return int
79
-     */
80
-    public function getConfiguredNewRecordPid(): int
81
-    {
82
-        if (GeneralUtility::_GP(Parameter::PID)) {
83
-            $configuredPid = (int)GeneralUtility::_GP(Parameter::PID);
84
-        } else {
85
-
86
-            // Get pid from User TSConfig if any.
87
-            $tsConfigPath = sprintf('tx_vidi.dataType.%s.storagePid', $this->dataType);
88
-            $result = $this->getBackendUser()->getTSConfig($tsConfigPath);
89
-            $configuredPid = isset($result['value'])
90
-                ? $configuredPid = (int)$result['value']
91
-                : $this->getModuleLoader()->getDefaultPid();
92
-        }
93
-
94
-        return $configuredPid;
95
-    }
96
-
97
-    /**
98
-     * Check if pid is 0 and given table is allowed on root level.
99
-     *
100
-     * @param int $configuredPid
101
-     * @return void
102
-     */
103
-    protected function validateRootLevel(int $configuredPid): void
104
-    {
105
-        if ($configuredPid > 0) {
106
-            return;
107
-        }
108
-
109
-        $isRootLevel = (bool)Tca::table()->get('rootLevel');
110
-        if (!$isRootLevel) {
111
-            $this->errors[] = sprintf(
112
-                'You are not allowed to use page id "0" unless you set $GLOBALS[\'TCA\'][\'%1$s\'][\'ctrl\'][\'rootLevel\'] = 1;',
113
-                $this->dataType
114
-            );
115
-        }
116
-    }
117
-
118
-    /**
119
-     * Check if a page exists for the configured pid
120
-     *
121
-     * @param int $configuredPid
122
-     * @return void
123
-     */
124
-    protected function validatePageExist(int $configuredPid): void
125
-    {
126
-        if ($configuredPid === 0) {
127
-            return;
128
-        }
129
-
130
-        $page = $this->getPage($configuredPid);
131
-        if (empty($page)) {
132
-            $this->errors[] = sprintf(
133
-                'No page found for the configured page id "%s".',
134
-                $configuredPid
135
-            );
136
-        }
137
-    }
138
-
139
-    /**
140
-     * Check if configured page is a sysfolder and if it is allowed.
141
-     *
142
-     * @param int $configuredPid
143
-     * @return void
144
-     */
145
-    protected function validateDoktype(int $configuredPid): void
146
-    {
147
-        if ($configuredPid === 0) {
148
-            return;
149
-        }
150
-
151
-        $page = $this->getPage($configuredPid);
152
-        if (!empty($page)
153
-            && (int)$page['doktype'] !== PageRepository::DOKTYPE_SYSFOLDER
154
-            && !$this->isTableAllowedOnStandardPages()
155
-            && $this->getModuleLoader()->hasComponentInDocHeader(\Fab\Vidi\View\Button\NewButton::class)) {
156
-            $this->errors[] = sprintf(
157
-                'The page with the id "%s" either has to be of the type "folder" (doktype=254) or the table "%s" has to be allowed on standard pages.',
158
-                $configuredPid,
159
-                $this->dataType
160
-            );
161
-        }
162
-    }
163
-
164
-    /**
165
-     * Check if given table is allowed on standard pages
166
-     *
167
-     * @return bool
168
-     * @see \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::allowTableOnStandardPages()
169
-     */
170
-    protected function isTableAllowedOnStandardPages(): bool
171
-    {
172
-        $allowedTables = explode(',', $GLOBALS['PAGES_TYPES']['default']['allowedTables']);
173
-        return in_array($this->dataType, $allowedTables, true);
174
-    }
175
-
176
-    /**
177
-     * Returns the page record of the configured pid
178
-     *
179
-     * @param int $configuredPid
180
-     * @return array
181
-     */
182
-    protected function getPage(int $configuredPid): ?array
183
-    {
184
-        $query = $this->getQueryBuilder('pages');
185
-        $query->getRestrictions()->removeAll(); // we are in BE context.
186
-
187
-        $page = $query->select('doktype')
188
-            ->from('pages')
189
-            ->where('deleted = 0',
190
-                'uid = ' . $configuredPid)
191
-            ->execute()
192
-            ->fetch();
193
-
194
-        return is_array($page)
195
-            ? $page
196
-            : [];
197
-    }
198
-
199
-    /**
200
-     * @param string $tableName
201
-     * @return object|QueryBuilder
202
-     */
203
-    protected function getQueryBuilder($tableName): QueryBuilder
204
-    {
205
-        /** @var ConnectionPool $connectionPool */
206
-        $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
207
-        return $connectionPool->getQueryBuilderForTable($tableName);
208
-    }
209
-
210
-    /**
211
-     * Returns an instance of the current Backend User.
212
-     *
213
-     * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
214
-     */
215
-    protected function getBackendUser()
216
-    {
217
-        return $GLOBALS['BE_USER'];
218
-    }
219
-
220
-    /**
221
-     * Get the Vidi Module Loader.
222
-     *
223
-     * @return \Fab\Vidi\Module\ModuleLoader|object
224
-     */
225
-    protected function getModuleLoader()
226
-    {
227
-        return GeneralUtility::makeInstance(\Fab\Vidi\Module\ModuleLoader::class);
228
-    }
22
+	/**
23
+	 * The data type (table)
24
+	 *
25
+	 * @var string
26
+	 */
27
+	protected $dataType = '';
28
+
29
+	/**
30
+	 * A collection of speaking error messages why the pid is invalid.
31
+	 *
32
+	 * @var array
33
+	 */
34
+	protected $errors = [];
35
+
36
+	/**
37
+	 * ModulePidService constructor.
38
+	 */
39
+	public function __construct()
40
+	{
41
+		$this->dataType = $this->getModuleLoader()->getDataType();
42
+	}
43
+
44
+	/**
45
+	 * Returns a class instance
46
+	 *
47
+	 * @return \Fab\Vidi\Module\ModulePidService|object
48
+	 */
49
+	static public function getInstance()
50
+	{
51
+		return GeneralUtility::makeInstance(self::class);
52
+	}
53
+
54
+	/**
55
+	 * @return bool
56
+	 */
57
+	public function isConfiguredPidValid(): bool
58
+	{
59
+		$errors = $this->validateConfiguredPid();
60
+		return empty($errors);
61
+	}
62
+
63
+	/**
64
+	 * @return array
65
+	 */
66
+	public function validateConfiguredPid(): array
67
+	{
68
+		$configuredPid = $this->getConfiguredNewRecordPid();
69
+		$this->validateRootLevel($configuredPid);
70
+		$this->validatePageExist($configuredPid);
71
+		$this->validateDoktype($configuredPid);
72
+		return $this->errors;
73
+	}
74
+
75
+	/**
76
+	 * Return the default configured pid.
77
+	 *
78
+	 * @return int
79
+	 */
80
+	public function getConfiguredNewRecordPid(): int
81
+	{
82
+		if (GeneralUtility::_GP(Parameter::PID)) {
83
+			$configuredPid = (int)GeneralUtility::_GP(Parameter::PID);
84
+		} else {
85
+
86
+			// Get pid from User TSConfig if any.
87
+			$tsConfigPath = sprintf('tx_vidi.dataType.%s.storagePid', $this->dataType);
88
+			$result = $this->getBackendUser()->getTSConfig($tsConfigPath);
89
+			$configuredPid = isset($result['value'])
90
+				? $configuredPid = (int)$result['value']
91
+				: $this->getModuleLoader()->getDefaultPid();
92
+		}
93
+
94
+		return $configuredPid;
95
+	}
96
+
97
+	/**
98
+	 * Check if pid is 0 and given table is allowed on root level.
99
+	 *
100
+	 * @param int $configuredPid
101
+	 * @return void
102
+	 */
103
+	protected function validateRootLevel(int $configuredPid): void
104
+	{
105
+		if ($configuredPid > 0) {
106
+			return;
107
+		}
108
+
109
+		$isRootLevel = (bool)Tca::table()->get('rootLevel');
110
+		if (!$isRootLevel) {
111
+			$this->errors[] = sprintf(
112
+				'You are not allowed to use page id "0" unless you set $GLOBALS[\'TCA\'][\'%1$s\'][\'ctrl\'][\'rootLevel\'] = 1;',
113
+				$this->dataType
114
+			);
115
+		}
116
+	}
117
+
118
+	/**
119
+	 * Check if a page exists for the configured pid
120
+	 *
121
+	 * @param int $configuredPid
122
+	 * @return void
123
+	 */
124
+	protected function validatePageExist(int $configuredPid): void
125
+	{
126
+		if ($configuredPid === 0) {
127
+			return;
128
+		}
129
+
130
+		$page = $this->getPage($configuredPid);
131
+		if (empty($page)) {
132
+			$this->errors[] = sprintf(
133
+				'No page found for the configured page id "%s".',
134
+				$configuredPid
135
+			);
136
+		}
137
+	}
138
+
139
+	/**
140
+	 * Check if configured page is a sysfolder and if it is allowed.
141
+	 *
142
+	 * @param int $configuredPid
143
+	 * @return void
144
+	 */
145
+	protected function validateDoktype(int $configuredPid): void
146
+	{
147
+		if ($configuredPid === 0) {
148
+			return;
149
+		}
150
+
151
+		$page = $this->getPage($configuredPid);
152
+		if (!empty($page)
153
+			&& (int)$page['doktype'] !== PageRepository::DOKTYPE_SYSFOLDER
154
+			&& !$this->isTableAllowedOnStandardPages()
155
+			&& $this->getModuleLoader()->hasComponentInDocHeader(\Fab\Vidi\View\Button\NewButton::class)) {
156
+			$this->errors[] = sprintf(
157
+				'The page with the id "%s" either has to be of the type "folder" (doktype=254) or the table "%s" has to be allowed on standard pages.',
158
+				$configuredPid,
159
+				$this->dataType
160
+			);
161
+		}
162
+	}
163
+
164
+	/**
165
+	 * Check if given table is allowed on standard pages
166
+	 *
167
+	 * @return bool
168
+	 * @see \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::allowTableOnStandardPages()
169
+	 */
170
+	protected function isTableAllowedOnStandardPages(): bool
171
+	{
172
+		$allowedTables = explode(',', $GLOBALS['PAGES_TYPES']['default']['allowedTables']);
173
+		return in_array($this->dataType, $allowedTables, true);
174
+	}
175
+
176
+	/**
177
+	 * Returns the page record of the configured pid
178
+	 *
179
+	 * @param int $configuredPid
180
+	 * @return array
181
+	 */
182
+	protected function getPage(int $configuredPid): ?array
183
+	{
184
+		$query = $this->getQueryBuilder('pages');
185
+		$query->getRestrictions()->removeAll(); // we are in BE context.
186
+
187
+		$page = $query->select('doktype')
188
+			->from('pages')
189
+			->where('deleted = 0',
190
+				'uid = ' . $configuredPid)
191
+			->execute()
192
+			->fetch();
193
+
194
+		return is_array($page)
195
+			? $page
196
+			: [];
197
+	}
198
+
199
+	/**
200
+	 * @param string $tableName
201
+	 * @return object|QueryBuilder
202
+	 */
203
+	protected function getQueryBuilder($tableName): QueryBuilder
204
+	{
205
+		/** @var ConnectionPool $connectionPool */
206
+		$connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
207
+		return $connectionPool->getQueryBuilderForTable($tableName);
208
+	}
209
+
210
+	/**
211
+	 * Returns an instance of the current Backend User.
212
+	 *
213
+	 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
214
+	 */
215
+	protected function getBackendUser()
216
+	{
217
+		return $GLOBALS['BE_USER'];
218
+	}
219
+
220
+	/**
221
+	 * Get the Vidi Module Loader.
222
+	 *
223
+	 * @return \Fab\Vidi\Module\ModuleLoader|object
224
+	 */
225
+	protected function getModuleLoader()
226
+	{
227
+		return GeneralUtility::makeInstance(\Fab\Vidi\Module\ModuleLoader::class);
228
+	}
229 229
 
230 230
 }
Please login to merge, or discard this patch.