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