[BUGFIX] Use late static binding for compilable viewhelpers
[Packages/TYPO3.CMS.git] / typo3 / sysext / belog / Classes / ViewHelpers / FormatDetailsViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Belog\ViewHelpers;
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\Fluid\Core\ViewHelper\AbstractViewHelper;
18 use TYPO3\CMS\Belog\Domain\Model\LogEntry;
19 use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface;
20 use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface;
21
22 /**
23 * Create detail string from log entry
24 *
25 * @author Christian Kuhn <lolli@schwarzbu.ch>
26 * @internal
27 */
28 class FormatDetailsViewHelper extends AbstractViewHelper implements CompilableInterface {
29
30 /**
31 * Create formatted detail string from log row.
32 *
33 * The method handles two properties of the model: details and logData
34 * Details is a string with possible %s placeholders, and logData an array
35 * with the substitutions.
36 * Furthermore, possible files in logData are stripped to their basename if
37 * the action logged was a file action
38 *
39 * @param LogEntry $logEntry
40 * @return string Formatted details
41 */
42 public function render(LogEntry $logEntry) {
43 return static::renderStatic(
44 array(
45 'logEntry' => $logEntry
46 ),
47 $this->buildRenderChildrenClosure(),
48 $this->renderingContext
49 );
50 }
51
52 /**
53 * @param array $arguments
54 * @param callable $renderChildrenClosure
55 * @param RenderingContextInterface $renderingContext
56 *
57 * @return string
58 * @throws Exception
59 */
60 static public function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext) {
61 $logEntry = $arguments['logEntry'];
62 $detailString = $logEntry->getDetails();
63 $substitutes = $logEntry->getLogData();
64 // Strip paths from file names if the log was a file action
65 if ($logEntry->getType() === 2) {
66 $substitutes = self::stripPathFromFilenames($substitutes);
67 }
68 // Substitute
69 $detailString = vsprintf($detailString, $substitutes);
70 // Remove possible pending other %s
71 $detailString = str_replace('%s', '', $detailString);
72 return htmlspecialchars($detailString);
73 }
74
75 /**
76 * Strips path from array of file names
77 *
78 * @param array $files
79 * @return array
80 */
81 static protected function stripPathFromFilenames(array $files = array()) {
82 foreach ($files as $key => $file) {
83 $files[$key] = basename($file);
84 }
85 return $files;
86 }
87
88 }