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 | */ |
||
46 | 24 | public function __construct(ViewInfoCache $infoCache, IURLGenerator $urlGenerator, IL10N $l) { |
|
51 | |||
52 | /** |
||
53 | * @param string $user |
||
54 | */ |
||
55 | 17 | public function setUser($user) { |
|
58 | |||
59 | /** |
||
60 | * @param IEvent $event |
||
61 | * @param string $parameter The parameter to be formatted |
||
62 | * @return string The formatted parameter |
||
63 | */ |
||
64 | 12 | public function format(IEvent $event, $parameter) { |
|
65 | 12 | $param = $this->fixLegacyFilename($parameter); |
|
66 | |||
67 | // If the activity is about the very same file, we use the current path |
||
68 | // for the link generation instead of the one that was saved. |
||
69 | 12 | $fileId = ''; |
|
70 | 12 | if (is_array($param)) { |
|
71 | $fileId = key($param); |
||
72 | $param = $param[$fileId]; |
||
73 | $info = $this->infoCache->getInfoById($this->user, $fileId, $param); |
||
74 | 12 | } elseif ($event->getObjectType() === 'files' && $event->getObjectName() === $param) { |
|
75 | 2 | $fileId = $event->getObjectId(); |
|
76 | 2 | $info = $this->infoCache->getInfoById($this->user, $fileId, $param); |
|
77 | } else { |
||
78 | 10 | $info = $this->infoCache->getInfoByPath($this->user, $param); |
|
79 | } |
||
80 | |||
81 | 12 | if ($info['is_dir']) { |
|
82 | 4 | $linkData = ['dir' => $info['path']]; |
|
83 | } else { |
||
84 | 8 | $parentDir = (substr_count($info['path'], '/') === 1) ? '/' : dirname($info['path']); |
|
85 | 8 | $fileName = basename($info['path']); |
|
86 | $linkData = [ |
||
87 | 8 | 'dir' => $parentDir, |
|
88 | 8 | 'scrollto' => $fileName, |
|
89 | ]; |
||
90 | } |
||
91 | |||
92 | 12 | if ($info['view'] !== '') { |
|
93 | 2 | $linkData['view'] = $info['view']; |
|
94 | } |
||
95 | |||
96 | 12 | $param = trim($param, '/'); |
|
97 | 12 | if ($param === '') { |
|
98 | 1 | $param = '/'; |
|
99 | } |
||
100 | |||
101 | 12 | $fileLink = $this->urlGenerator->linkToRouteAbsolute('files.view.index', $linkData); |
|
102 | |||
103 | 12 | return '<file link="' . $fileLink . '" id="' . Util::sanitizeHTML($fileId) . '">' . Util::sanitizeHTML($param) . '</file>'; |
|
104 | } |
||
105 | |||
106 | /** |
||
107 | * Prepend leading slash to filenames of legacy activities |
||
108 | * @param string|array $filename |
||
109 | * @return string|array |
||
110 | */ |
||
111 | 10 | protected function fixLegacyFilename($filename) { |
|
121 | |||
122 | /** |
||
123 | * Split the path from the filename string |
||
124 | * |
||
125 | * @param string $filename |
||
126 | * @return array Array with path and filename |
||
127 | */ |
||
128 | 6 | protected function splitPathFromFilename($filename) { |
|
137 | } |
||
138 |