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 | 40 | 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 | * @param bool $allowHtml Should HTML be used to format the parameter? |
||
57 | * @param bool $verbose Should paths, names, etc be shortened or full length |
||
58 | * @return string The formatted parameter |
||
59 | */ |
||
60 | 29 | public function format(IEvent $event, $parameter, $allowHtml, $verbose = false) { |
|
61 | 29 | $param = $this->fixLegacyFilename($parameter); |
|
62 | |||
63 | // If the activity is about the very same file, we use the current path |
||
64 | // for the link generation instead of the one that was saved. |
||
65 | 29 | $fileId = ''; |
|
66 | 29 | if ($event->getObjectType() === 'files' && $event->getObjectName() === $param) { |
|
67 | 8 | $fileId = $event->getObjectId(); |
|
68 | 8 | $info = $this->infoCache->getInfoById($this->user, $fileId, $param); |
|
69 | 8 | } else { |
|
70 | 21 | $info = $this->infoCache->getInfoByPath($this->user, $param); |
|
71 | } |
||
72 | |||
73 | 29 | if ($info['is_dir']) { |
|
74 | 12 | $linkData = ['dir' => $info['path']]; |
|
75 | 12 | } else { |
|
76 | 17 | $parentDir = (substr_count($info['path'], '/') === 1) ? '/' : dirname($info['path']); |
|
77 | 17 | $fileName = basename($info['path']); |
|
78 | $linkData = [ |
||
79 | 17 | 'dir' => $parentDir, |
|
80 | 17 | 'scrollto' => $fileName, |
|
81 | 17 | ]; |
|
82 | } |
||
83 | |||
84 | 29 | if ($info['view'] !== '') { |
|
85 | 8 | $linkData['view'] = $info['view']; |
|
86 | 8 | } |
|
87 | |||
88 | 29 | $param = trim($param, '/'); |
|
89 | 29 | list($path, $name) = $this->splitPathFromFilename($param); |
|
90 | 29 | $fileLink = $this->urlGenerator->linkTo('files', 'index.php', $linkData); |
|
91 | |||
92 | 29 | if ($allowHtml === null) { |
|
93 | 4 | return '<file link="' . $fileLink . '" id="' . Util::sanitizeHTML($fileId) . '">' . Util::sanitizeHTML($param) . '</file>'; |
|
94 | } |
||
95 | |||
96 | 29 | if ($verbose || $path === '') { |
|
97 | 21 | if (!$allowHtml) { |
|
98 | 12 | return $param; |
|
99 | } |
||
100 | 13 | return '<a class="filename" href="' . $fileLink . '">' . Util::sanitizeHTML($param) . '</a>'; |
|
101 | } |
||
102 | |||
103 | 12 | if (!$allowHtml) { |
|
104 | 8 | return $name; |
|
105 | } |
||
106 | |||
107 | 8 | $title = ' title="' . $this->l->t('in %s', array(Util::sanitizeHTML($path))) . '"'; |
|
108 | 8 | return '<a class="filename has-tooltip" href="' . $fileLink . '"' . $title . '>' . Util::sanitizeHTML($name) . '</a>'; |
|
109 | } |
||
110 | |||
111 | /** |
||
112 | * Prepend leading slash to filenames of legacy activities |
||
113 | * @param string $filename |
||
114 | * @return string |
||
115 | */ |
||
116 | 8 | protected function fixLegacyFilename($filename) { |
|
122 | |||
123 | /** |
||
124 | * Split the path from the filename string |
||
125 | * |
||
126 | * @param string $filename |
||
127 | * @return array Array with path and filename |
||
128 | */ |
||
129 | 35 | protected function splitPathFromFilename($filename) { |
|
138 | } |
||
139 |