e81bfe099b9f7d21148c073611c6146a23153e55
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Template / Components / MetaInformation.php
1 <?php
2 namespace TYPO3\CMS\Backend\Template\Components;
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\Imaging\Icon;
19 use TYPO3\CMS\Core\Imaging\IconFactory;
20 use TYPO3\CMS\Core\Resource\Exception\InsufficientFolderAccessPermissionsException;
21 use TYPO3\CMS\Core\Resource\Exception\ResourceDoesNotExistException;
22 use TYPO3\CMS\Core\Resource\FolderInterface;
23 use TYPO3\CMS\Core\Resource\ResourceFactory;
24 use TYPO3\CMS\Core\Utility\GeneralUtility;
25 use TYPO3\CMS\Lang\LanguageService;
26
27 /**
28 * MetaInformation
29 */
30 class MetaInformation
31 {
32 /**
33 * The recordArray.
34 * Typically this is a page record
35 *
36 * @var array
37 */
38 protected $recordArray = [];
39
40 /**
41 * Set the RecordArray
42 *
43 * @param array $recordArray RecordArray
44 */
45 public function setRecordArray(array $recordArray)
46 {
47 $this->recordArray = $recordArray;
48 }
49
50 /**
51 * Generate the page path for docHeader
52 *
53 * @return string The page path
54 */
55 public function getPath()
56 {
57 $pageRecord = $this->recordArray;
58 $title = '';
59 // Is this a real page
60 if (is_array($pageRecord) && $pageRecord['uid']) {
61 $title = substr($pageRecord['_thePathFull'], 0, -1);
62 // Remove current page title
63 $pos = strrpos($title, $pageRecord['title']);
64 if ($pos !== false) {
65 $title = substr($title, 0, $pos);
66 }
67 } elseif (!empty($pageRecord['combined_identifier'])) {
68 try {
69 $resourceObject = ResourceFactory::getInstance()->getInstance()->getObjectFromCombinedIdentifier($pageRecord['combined_identifier']);
70 $title = $resourceObject->getStorage()->getName() . ':';
71 $title .= $resourceObject->getParentFolder()->getReadablePath();
72 } catch (ResourceDoesNotExistException $e) {
73 } catch (InsufficientFolderAccessPermissionsException $e) {
74 }
75 }
76 // Setting the path of the page
77 // crop the title to title limit (or 50, if not defined)
78 $beUser = $this->getBackendUser();
79 $cropLength = empty($beUser->uc['titleLen']) ? 50 : $beUser->uc['titleLen'];
80 $croppedTitle = GeneralUtility::fixed_lgd_cs($title, -$cropLength);
81 if ($croppedTitle !== $title) {
82 $pagePath = '<abbr title="' . htmlspecialchars($title) . '">' . htmlspecialchars($croppedTitle) . '</abbr>';
83 } else {
84 $pagePath = htmlspecialchars($title);
85 }
86 return $pagePath;
87 }
88
89 /**
90 * Setting page icon with context menu + uid for docheader
91 *
92 * @return string Record info
93 */
94 public function getRecordInformation()
95 {
96 $recordInformations = $this->getRecordInformations();
97 if (!empty($recordInformations)) {
98 $recordInformation = $recordInformations['icon'] .
99 ' <strong>' . htmlspecialchars($recordInformations['title']) . ($recordInformations['uid'] !== '' ? '&nbsp;[' . $recordInformations['uid'] . ']' : '') . '</strong>' .
100 (!empty($recordInformations['additionalInfo']) ? ' ' . htmlspecialchars($recordInformations['additionalInfo']) : '');
101 } else {
102 $recordInformation = '';
103 }
104 return $recordInformation;
105 }
106
107 /**
108 * Setting page icon
109 *
110 * @return string Record icon
111 */
112 public function getRecordInformationIcon()
113 {
114 $recordInformations = $this->getRecordInformations();
115 if (!empty($recordInformations)) {
116 $recordInformationIcon = $recordInformations['icon'];
117 } else {
118 $recordInformationIcon = null;
119 }
120 return $recordInformationIcon;
121 }
122
123 /**
124 * Setting page title
125 *
126 * @return string Record title, already htmlspecialchar()'ed
127 */
128 public function getRecordInformationTitle()
129 {
130 $recordInformations = $this->getRecordInformations();
131 if (!empty($recordInformations)) {
132 $title = $recordInformations['title'];
133 } else {
134 $title = '';
135 }
136
137 // crop the title to title limit (or 50, if not defined)
138 $beUser = $this->getBackendUser();
139 $cropLength = empty($beUser->uc['titleLen']) ? 50 : $beUser->uc['titleLen'];
140 return htmlspecialchars(GeneralUtility::fixed_lgd_cs($title, $cropLength));
141 }
142
143 /**
144 * Setting page uid
145 *
146 * @return null|int Record uid
147 */
148 public function getRecordInformationUid()
149 {
150 $recordInformations = $this->getRecordInformations();
151 if (!empty($recordInformations)) {
152 $recordInformationUid = $recordInformations['uid'];
153 } else {
154 $recordInformationUid = null;
155 }
156 return $recordInformationUid;
157 }
158
159 /**
160 * Setting page array
161 *
162 * @return array Record info
163 */
164 protected function getRecordInformations()
165 {
166 $pageRecord = $this->recordArray;
167 if (empty($pageRecord)) {
168 return [];
169 }
170
171 $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
172 $uid = '';
173 $title = '';
174 $additionalInfo = (!empty($pageRecord['_additional_info']) ? $pageRecord['_additional_info'] : '');
175 // Add icon with context menu, etc:
176 // If there IS a real page
177 if (is_array($pageRecord) && $pageRecord['uid']) {
178 $toolTip = BackendUtility::getRecordToolTip($pageRecord, 'pages');
179 $iconImg = '<span ' . $toolTip . '>' . $iconFactory->getIconForRecord('pages', $pageRecord, Icon::SIZE_SMALL)->render() . '</span>';
180 // Make Icon:
181 $theIcon = BackendUtility::wrapClickMenuOnIcon($iconImg, 'pages', $pageRecord['uid']);
182 $uid = $pageRecord['uid'];
183 $title = BackendUtility::getRecordTitle('pages', $pageRecord);
184 } elseif (is_array($pageRecord) && !empty($pageRecord['combined_identifier'])) {
185 // If the module is about a FAL resource
186 try {
187 $resourceObject = ResourceFactory::getInstance()->getInstance()->getObjectFromCombinedIdentifier($pageRecord['combined_identifier']);
188 $fileMountTitle = $resourceObject->getStorage()->getFileMounts()[$resourceObject->getIdentifier()]['title'];
189 $title = $fileMountTitle ?: $resourceObject->getName();
190 // If this is a folder but not in within file mount boundaries this is the root folder
191 if ($resourceObject instanceof FolderInterface && !$resourceObject->getStorage()->isWithinFileMountBoundaries($resourceObject)) {
192 $iconImg = '<span title="' . htmlspecialchars($title) . '">' . $iconFactory->getIconForResource(
193 $resourceObject,
194 Icon::SIZE_SMALL,
195 null,
196 ['mount-root' => true]
197 )->render() . '</span>';
198 } else {
199 $iconImg = '<span title="' . htmlspecialchars($title) . '">' . $iconFactory->getIconForResource(
200 $resourceObject,
201 Icon::SIZE_SMALL
202 )->render() . '</span>';
203 }
204 $theIcon = BackendUtility::wrapClickMenuOnIcon($iconImg, 'sys_file', $pageRecord['combined_identifier']);
205 } catch (ResourceDoesNotExistException $e) {
206 $theIcon = '';
207 }
208 } else {
209 // On root-level of page tree
210 // Make Icon
211 $iconImg = '<span title="' .
212 htmlspecialchars($GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename']) .
213 '">' .
214 $iconFactory->getIcon('apps-pagetree-root', Icon::SIZE_SMALL)->render() . '</span>';
215 if ($this->getBackendUser()->isAdmin()) {
216 $theIcon = BackendUtility::wrapClickMenuOnIcon($iconImg, 'pages', 0);
217 } else {
218 $theIcon = $iconImg;
219 }
220 $uid = '0';
221 $title = $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'];
222 }
223 // returns array for icon, title, uid and additional info
224 return [
225 'uid' => $uid,
226 'icon' => $theIcon,
227 'title' => $title,
228 'additionalInfo' => $additionalInfo
229 ];
230 }
231
232 /**
233 * Get LanguageService Object
234 *
235 * @return LanguageService
236 */
237 protected function getLanguageService()
238 {
239 return $GLOBALS['LANG'];
240 }
241
242 /**
243 * Get the Backend User Object
244 *
245 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
246 */
247 protected function getBackendUser()
248 {
249 return $GLOBALS['BE_USER'];
250 }
251 }