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