[TASK] Doctrine: migrate ext:recycler/Utility
[Packages/TYPO3.CMS.git] / typo3 / sysext / recycler / Classes / Utility / RecyclerUtility.php
1 <?php
2 namespace TYPO3\CMS\Recycler\Utility;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Backend\Utility\BackendUtility;
18 use TYPO3\CMS\Core\Database\ConnectionPool;
19 use TYPO3\CMS\Core\Database\Query\QueryContextType;
20 use TYPO3\CMS\Core\Type\Bitmask\Permission;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22
23 /**
24 * Helper class for the 'recycler' extension.
25 */
26 class RecyclerUtility
27 {
28 /************************************************************
29 * USER ACCESS
30 *
31 *
32 ************************************************************/
33 /**
34 * Checks the page access rights (Code for access check mostly taken from FormEngine)
35 * as well as the table access rights of the user.
36 *
37 * @param string $table The table to check access for
38 * @param string $row Record array
39 * @return bool Returns TRUE is the user has access, or FALSE if not
40 */
41 public static function checkAccess($table, $row)
42 {
43 $backendUser = static::getBackendUser();
44
45 // Checking if the user has permissions? (Only working as a precaution, because the final permission check is always down in TCE. But it's good to notify the user on beforehand...)
46 // First, resetting flags.
47 $hasAccess = false;
48 $calcPRec = $row;
49 BackendUtility::fixVersioningPid($table, $calcPRec);
50 if (is_array($calcPRec)) {
51 if ($table === 'pages') {
52 $calculatedPermissions = $backendUser->calcPerms($calcPRec);
53 $hasAccess = (bool)($calculatedPermissions & Permission::PAGE_EDIT);
54 } else {
55 $calculatedPermissions = $backendUser->calcPerms(BackendUtility::getRecord('pages', $calcPRec['pid']));
56 // Fetching pid-record first.
57 $hasAccess = (bool)($calculatedPermissions & Permission::CONTENT_EDIT);
58 }
59 // Check internals regarding access:
60 if ($hasAccess) {
61 $hasAccess = $backendUser->recordEditAccessInternals($table, $calcPRec);
62 }
63 }
64 if (!$backendUser->check('tables_modify', $table)) {
65 $hasAccess = false;
66 }
67 return $hasAccess;
68 }
69
70 /**
71 * Returns the path (visually) of a page $uid, fx. "/First page/Second page/Another subpage"
72 * Each part of the path will be limited to $titleLimit characters
73 * Deleted pages are filtered out.
74 *
75 * @param int $uid Page uid for which to create record path
76 * @param string $clause is additional where clauses, eg.
77 * @param int $titleLimit Title limit
78 * @param int $fullTitleLimit Title limit of Full title (typ. set to 1000 or so)
79 * @return mixed Path of record (string) OR array with short/long title if $fullTitleLimit is set.
80 */
81 public static function getRecordPath($uid, $clause = '', $titleLimit = 1000, $fullTitleLimit = 0)
82 {
83 $uid = (int)$uid;
84 $output = ($fullOutput = '/');
85 if ($uid === 0) {
86 return $output;
87 }
88 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
89 $queryBuilder->getQueryContext()->setContext(QueryContextType::UNRESTRICTED);
90
91 $clause = trim($clause);
92 $loopCheck = 100;
93 while ($loopCheck > 0) {
94 $loopCheck--;
95
96 $queryBuilder
97 ->select('uid', 'pid', 'title', 'deleted', 't3ver_oid', 't3ver_wsid')
98 ->from('pages')
99 ->where($queryBuilder->expr()->eq('uid', (int)$uid));
100 if (!empty($clause)) {
101 $queryBuilder->andWhere($clause);
102 }
103 $row = $queryBuilder->execute()->fetch();
104 if ($row !== false) {
105 BackendUtility::workspaceOL('pages', $row);
106 if (is_array($row)) {
107 BackendUtility::fixVersioningPid('pages', $row);
108 $uid = (int)$row['pid'];
109 $output = '/' . htmlspecialchars(GeneralUtility::fixed_lgd_cs($row['title'], $titleLimit)) . $output;
110 if ($row['deleted']) {
111 $output = '<span class="text-danger">' . $output . '</span>';
112 }
113 if ($fullTitleLimit) {
114 $fullOutput = '/' . htmlspecialchars(GeneralUtility::fixed_lgd_cs($row['title'], $fullTitleLimit)) . $fullOutput;
115 }
116 } else {
117 break;
118 }
119 } else {
120 break;
121 }
122 }
123 if ($fullTitleLimit) {
124 return array($output, $fullOutput);
125 } else {
126 return $output;
127 }
128 }
129
130 /**
131 * Gets the name of the field with the information whether a record is deleted.
132 *
133 * @param string $tableName Name of the table to get the deleted field for
134 * @return string Name of the field with the information whether a record is deleted
135 */
136 public static function getDeletedField($tableName)
137 {
138 $TCA = self::getTableTCA($tableName);
139 if ($TCA && isset($TCA['ctrl']['delete']) && $TCA['ctrl']['delete']) {
140 return $TCA['ctrl']['delete'];
141 }
142 return '';
143 }
144
145 /**
146 * Check if parent record is deleted
147 *
148 * @param int $pid
149 * @return bool
150 */
151 public static function isParentPageDeleted($pid)
152 {
153 if ((int)$pid === 0) {
154 return false;
155 }
156 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
157 $queryBuilder->getQueryContext()->setContext(QueryContextType::UNRESTRICTED);
158
159 $deleted = $queryBuilder
160 ->select('deleted')
161 ->from('pages')
162 ->where($queryBuilder->expr()->eq('uid', (int)$pid))
163 ->execute()
164 ->fetchColumn();
165
166 return (bool)$deleted;
167 }
168
169 /**
170 * Get pid of uid
171 *
172 * @param int $uid
173 * @param string $table
174 * @return int
175 */
176 public static function getPidOfUid($uid, $table)
177 {
178 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
179 $queryBuilder->getQueryContext()->setContext(QueryContextType::UNRESTRICTED);
180
181 $pid = $queryBuilder
182 ->select('pid')
183 ->from($table)
184 ->where($queryBuilder->expr()->eq('uid', (int)$uid))
185 ->execute()
186 ->fetchColumn();
187
188 return (int)$pid;
189 }
190
191 /**
192 * Gets the TCA of the table used in the current context.
193 *
194 * @param string $tableName Name of the table to get TCA for
195 * @return array|FALSE TCA of the table used in the current context
196 */
197 public static function getTableTCA($tableName)
198 {
199 $TCA = false;
200 if (isset($GLOBALS['TCA'][$tableName])) {
201 $TCA = $GLOBALS['TCA'][$tableName];
202 }
203 return $TCA;
204 }
205
206 /**
207 * Returns the BackendUser
208 *
209 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
210 */
211 protected static function getBackendUser()
212 {
213 return $GLOBALS['BE_USER'];
214 }
215
216 /**
217 * Returns an instance of LanguageService
218 *
219 * @return \TYPO3\CMS\Lang\LanguageService
220 */
221 protected static function getLanguageService()
222 {
223 return $GLOBALS['LANG'];
224 }
225
226 /**
227 * Returns the modifyable tables of the current user
228 */
229 public static function getModifyableTables()
230 {
231 if ((bool)$GLOBALS['BE_USER']->user['admin']) {
232 $tables = array_keys($GLOBALS['TCA']);
233 } else {
234 $tables = explode(',', $GLOBALS['BE_USER']->groupData['tables_modify']);
235 }
236 return $tables;
237 }
238 }