[TASK] Merge submodule extbase into core
[Packages/TYPO3.CMS.git] / typo3 / sysext / recycler / Classes / Utility / RecyclerUtility.php
1 <?php
2 namespace TYPO3\CMS\Recycler\Utility;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2009-2013 Julian Kleinhans <typo3@kj187.de>
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 2 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 * Helper class for the 'recycler' extension.
28 *
29 * @author Julian Kleinhans <typo3@kj187.de>
30 */
31 class RecyclerUtility {
32
33 /************************************************************
34 * USER ACCESS
35 *
36 *
37 ************************************************************/
38 /**
39 * Checks the page access rights (Code for access check mostly taken from alt_doc.php)
40 * as well as the table access rights of the user.
41 *
42 * @param string $table The table to check access for
43 * @param string $row Record array
44 * @return boolean Returns TRUE is the user has access, or FALSE if not
45 */
46 static public function checkAccess($table, $row) {
47 // 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...)
48 // First, resetting flags.
49 $hasAccess = 0;
50 $calcPRec = $row;
51 \TYPO3\CMS\Backend\Utility\BackendUtility::fixVersioningPid($table, $calcPRec);
52 if (is_array($calcPRec)) {
53 if ($table == 'pages') {
54 // If pages:
55 $CALC_PERMS = $GLOBALS['BE_USER']->calcPerms($calcPRec);
56 $hasAccess = $CALC_PERMS & 2 ? 1 : 0;
57 } else {
58 $CALC_PERMS = $GLOBALS['BE_USER']->calcPerms(\TYPO3\CMS\Backend\Utility\BackendUtility::getRecord('pages', $calcPRec['pid']));
59 // Fetching pid-record first.
60 $hasAccess = $CALC_PERMS & 16 ? 1 : 0;
61 }
62 // Check internals regarding access:
63 if ($hasAccess) {
64 $hasAccess = $GLOBALS['BE_USER']->recordEditAccessInternals($table, $calcPRec);
65 }
66 }
67 if (!$GLOBALS['BE_USER']->check('tables_modify', $table)) {
68 $hasAccess = 0;
69 }
70 return $hasAccess ? TRUE : FALSE;
71 }
72
73 /**
74 * Returns the path (visually) of a page $uid, fx. "/First page/Second page/Another subpage"
75 * Each part of the path will be limited to $titleLimit characters
76 * Deleted pages are filtered out.
77 *
78 * @param integer Page uid for which to create record path
79 * @param string $clause is additional where clauses, eg.
80 * @param integer Title limit
81 * @param integer Title limit of Full title (typ. set to 1000 or so)
82 * @return mixed Path of record (string) OR array with short/long title if $fullTitleLimit is set.
83 */
84 static public function getRecordPath($uid, $clause = '', $titleLimit = 1000, $fullTitleLimit = 0) {
85 $loopCheck = 100;
86 $output = ($fullOutput = '/');
87 while ($uid != 0 && $loopCheck > 0) {
88 $loopCheck--;
89 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid,pid,title,deleted,t3ver_oid,t3ver_wsid', 'pages', 'uid=' . intval($uid) . (strlen(trim($clause)) ? ' AND ' . $clause : ''));
90 if (is_resource($res)) {
91 $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
92 $GLOBALS['TYPO3_DB']->sql_free_result($res);
93 \TYPO3\CMS\Backend\Utility\BackendUtility::workspaceOL('pages', $row);
94 if (is_array($row)) {
95 \TYPO3\CMS\Backend\Utility\BackendUtility::fixVersioningPid('pages', $row);
96 $uid = $row['pid'];
97 $output = '/' . htmlspecialchars(\TYPO3\CMS\Core\Utility\GeneralUtility::fixed_lgd_cs($row['title'], $titleLimit)) . $output;
98 if ($row['deleted']) {
99 $output = '<span class="deletedPath">' . $output . '</span>';
100 }
101 if ($fullTitleLimit) {
102 $fullOutput = '/' . htmlspecialchars(\TYPO3\CMS\Core\Utility\GeneralUtility::fixed_lgd_cs($row['title'], $fullTitleLimit)) . $fullOutput;
103 }
104 } else {
105 break;
106 }
107 } else {
108 break;
109 }
110 }
111 if ($fullTitleLimit) {
112 return array($output, $fullOutput);
113 } else {
114 return $output;
115 }
116 }
117
118 /**
119 * Gets the name of the field with the information whether a record is deleted.
120 *
121 * @param string $tableName: Name of the table to get the deleted field for
122 * @return string Name of the field with the information whether a record is deleted
123 */
124 static public function getDeletedField($tableName) {
125 $TCA = self::getTableTCA($tableName);
126 if ($TCA && isset($TCA['ctrl']['delete']) && $TCA['ctrl']['delete']) {
127 return $TCA['ctrl']['delete'];
128 }
129 }
130
131 /**
132 * Gets the TCA of the table used in the current context.
133 *
134 * @param string $tableName: Name of the table to get TCA for
135 * @return mixed TCA of the table used in the current context (array)
136 */
137 static public function getTableTCA($tableName) {
138 $TCA = FALSE;
139 if (isset($GLOBALS['TCA'][$tableName])) {
140 $TCA = $GLOBALS['TCA'][$tableName];
141 }
142 return $TCA;
143 }
144
145 /**
146 * Gets the current backend charset.
147 *
148 * @return string The current backend charset
149 */
150 static public function getCurrentCharset() {
151 return $GLOBALS['LANG']->csConvObj->parse_charset($GLOBALS['LANG']->charSet);
152 }
153
154 /**
155 * Determines whether the current charset is not UTF-8
156 *
157 * @return boolean Whether the current charset is not UTF-8
158 */
159 static public function isNotUtf8Charset() {
160 return self::getCurrentCharset() !== 'utf-8';
161 }
162
163 /**
164 * Gets an UTF-8 encoded string (only if the current charset is not UTF-8!).
165 *
166 * @param string $string: String to be converted to UTF-8 if required
167 * @return string UTF-8 encoded string
168 */
169 static public function getUtf8String($string) {
170 if (self::isNotUtf8Charset()) {
171 $string = $GLOBALS['LANG']->csConvObj->utf8_encode($string, self::getCurrentCharset());
172 }
173 return $string;
174 }
175
176 }
177
178
179 ?>