[TASK] Remove ternary operator where not needed
[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\Type\Bitmask\Permission;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21 /**
22 * Helper class for the 'recycler' extension.
23 *
24 * @author Julian Kleinhans <typo3@kj187.de>
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 static public function checkAccess($table, $row) {
42 $backendUser = static::getBackendUser();
43
44 // 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...)
45 // First, resetting flags.
46 $hasAccess = FALSE;
47 $calcPRec = $row;
48 BackendUtility::fixVersioningPid($table, $calcPRec);
49 if (is_array($calcPRec)) {
50 if ($table === 'pages') {
51 // If 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 static public function getRecordPath($uid, $clause = '', $titleLimit = 1000, $fullTitleLimit = 0) {
82 $uid = (int)$uid;
83 $output = ($fullOutput = '/');
84 if ($uid === 0) {
85 return $output;
86 }
87 $databaseConnection = static::getDatabaseConnection();
88 $clause = trim($clause) !== '' ? ' AND ' . $clause : '';
89 $loopCheck = 100;
90 while ($loopCheck > 0) {
91 $loopCheck--;
92 $res = $databaseConnection->exec_SELECTquery('uid,pid,title,deleted,t3ver_oid,t3ver_wsid', 'pages', 'uid=' . $uid . $clause);
93 if ($res !== FALSE) {
94 $row = $databaseConnection->sql_fetch_assoc($res);
95 $databaseConnection->sql_free_result($res);
96 BackendUtility::workspaceOL('pages', $row);
97 if (is_array($row)) {
98 BackendUtility::fixVersioningPid('pages', $row);
99 $uid = (int)$row['pid'];
100 $output = '/' . htmlspecialchars(GeneralUtility::fixed_lgd_cs($row['title'], $titleLimit)) . $output;
101 if ($row['deleted']) {
102 $output = '<span class="text-danger">' . $output . '</span>';
103 }
104 if ($fullTitleLimit) {
105 $fullOutput = '/' . htmlspecialchars(GeneralUtility::fixed_lgd_cs($row['title'], $fullTitleLimit)) . $fullOutput;
106 }
107 } else {
108 break;
109 }
110 } else {
111 break;
112 }
113 }
114 if ($fullTitleLimit) {
115 return array($output, $fullOutput);
116 } else {
117 return $output;
118 }
119 }
120
121 /**
122 * Gets the name of the field with the information whether a record is deleted.
123 *
124 * @param string $tableName Name of the table to get the deleted field for
125 * @return string Name of the field with the information whether a record is deleted
126 */
127 static public function getDeletedField($tableName) {
128 $TCA = self::getTableTCA($tableName);
129 if ($TCA && isset($TCA['ctrl']['delete']) && $TCA['ctrl']['delete']) {
130 return $TCA['ctrl']['delete'];
131 }
132 return '';
133 }
134
135 /**
136 * Gets the TCA of the table used in the current context.
137 *
138 * @param string $tableName Name of the table to get TCA for
139 * @return array|FALSE TCA of the table used in the current context
140 */
141 static public function getTableTCA($tableName) {
142 $TCA = FALSE;
143 if (isset($GLOBALS['TCA'][$tableName])) {
144 $TCA = $GLOBALS['TCA'][$tableName];
145 }
146 return $TCA;
147 }
148
149 /**
150 * Gets the current backend charset.
151 *
152 * @return string The current backend charset
153 */
154 static public function getCurrentCharset() {
155 $lang = static::getLanguageService();
156 return $lang->csConvObj->parse_charset($lang->charSet);
157 }
158
159 /**
160 * Determines whether the current charset is not UTF-8
161 *
162 * @return bool Whether the current charset is not UTF-8
163 */
164 static public function isNotUtf8Charset() {
165 return self::getCurrentCharset() !== 'utf-8';
166 }
167
168 /**
169 * Gets an UTF-8 encoded string (only if the current charset is not UTF-8!).
170 *
171 * @param string $string String to be converted to UTF-8 if required
172 * @return string UTF-8 encoded string
173 */
174 static public function getUtf8String($string) {
175 if (self::isNotUtf8Charset()) {
176 $string = static::getLanguageService()->csConvObj->utf8_encode($string, self::getCurrentCharset());
177 }
178 return $string;
179 }
180
181 /**
182 * Returns an instance of DatabaseConnection
183 *
184 * @return \TYPO3\CMS\Core\Database\DatabaseConnection
185 */
186 static protected function getDatabaseConnection() {
187 return $GLOBALS['TYPO3_DB'];
188 }
189
190 /**
191 * Returns the BackendUser
192 *
193 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
194 */
195 static protected function getBackendUser() {
196 return $GLOBALS['BE_USER'];
197 }
198
199 /**
200 * Returns an instance of LanguageService
201 *
202 * @return \TYPO3\CMS\Lang\LanguageService
203 */
204 static protected function getLanguageService() {
205 return $GLOBALS['LANG'];
206 }
207
208 /**
209 * Returns the modifyable tables of the current user
210 */
211 static public function getModifyableTables() {
212 if ((bool)$GLOBALS['BE_USER']->user['admin']) {
213 $tables = array_keys($GLOBALS['TCA']);
214 } else {
215 $tables = explode(',', $GLOBALS['BE_USER']->groupData['tables_modify']);
216 }
217 return $tables;
218 }
219
220 }