1 | <?php |
||
31 | class FileFormatter implements IFormatter { |
||
32 | /** @var ViewInfoCache */ |
||
33 | protected $infoCache; |
||
34 | /** @var IURLGenerator */ |
||
35 | protected $urlGenerator; |
||
36 | /** @var IL10N */ |
||
37 | protected $l; |
||
38 | /** @var string */ |
||
39 | protected $user; |
||
40 | |||
41 | /** |
||
42 | * @param ViewInfoCache $infoCache |
||
43 | * @param IURLGenerator $urlGenerator |
||
44 | * @param IL10N $l |
||
45 | * @param string $user |
||
46 | */ |
||
47 | 25 | public function __construct(ViewInfoCache $infoCache, IURLGenerator $urlGenerator, IL10N $l, $user) { |
|
53 | |||
54 | /** |
||
55 | * @param IEvent $event |
||
56 | * @param string $parameter The parameter to be formatted |
||
57 | * @return string The formatted parameter |
||
58 | */ |
||
59 | 12 | public function format(IEvent $event, $parameter) { |
|
60 | 12 | $param = $this->fixLegacyFilename($parameter); |
|
61 | |||
62 | // If the activity is about the very same file, we use the current path |
||
63 | // for the link generation instead of the one that was saved. |
||
64 | 12 | $fileId = ''; |
|
65 | 12 | if (is_array($param)) { |
|
66 | $fileId = key($param); |
||
67 | $param = $param[$fileId]; |
||
68 | $info = $this->infoCache->getInfoById($this->user, $fileId, $param); |
||
69 | 12 | } elseif ($event->getObjectType() === 'files' && $event->getObjectName() === $param) { |
|
70 | 2 | $fileId = $event->getObjectId(); |
|
71 | 2 | $info = $this->infoCache->getInfoById($this->user, $fileId, $param); |
|
72 | 2 | } else { |
|
73 | 10 | $info = $this->infoCache->getInfoByPath($this->user, $param); |
|
74 | } |
||
75 | |||
76 | 12 | if ($info['is_dir']) { |
|
77 | 4 | $linkData = ['dir' => $info['path']]; |
|
78 | 4 | } else { |
|
79 | 8 | $parentDir = (substr_count($info['path'], '/') === 1) ? '/' : dirname($info['path']); |
|
80 | 8 | $fileName = basename($info['path']); |
|
81 | $linkData = [ |
||
82 | 8 | 'dir' => $parentDir, |
|
83 | 8 | 'scrollto' => $fileName, |
|
84 | 8 | ]; |
|
85 | } |
||
86 | |||
87 | 12 | if ($info['view'] !== '') { |
|
88 | 2 | $linkData['view'] = $info['view']; |
|
89 | 2 | } |
|
90 | |||
91 | 12 | $param = trim($param, '/'); |
|
92 | 12 | if ($param === '') { |
|
93 | 1 | $param = '/'; |
|
94 | 1 | } |
|
95 | |||
96 | 12 | $fileLink = $this->urlGenerator->linkToRouteAbsolute('files.view.index', $linkData); |
|
97 | |||
98 | 12 | return '<file link="' . $fileLink . '" id="' . Util::sanitizeHTML($fileId) . '">' . Util::sanitizeHTML($param) . '</file>'; |
|
99 | } |
||
100 | |||
101 | /** |
||
102 | * Prepend leading slash to filenames of legacy activities |
||
103 | * @param string|array $filename |
||
104 | * @return string|array |
||
105 | */ |
||
106 | 10 | protected function fixLegacyFilename($filename) { |
|
116 | |||
117 | /** |
||
118 | * Split the path from the filename string |
||
119 | * |
||
120 | * @param string $filename |
||
121 | * @return array Array with path and filename |
||
122 | */ |
||
123 | 6 | protected function splitPathFromFilename($filename) { |
|
132 | } |
||
133 |