aboutsummaryrefslogtreecommitdiff
path: root/app/controllers/files_dashboard/helpers.php
blob: d86fc9f815503266f826137d44e59cfd49775659 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php

namespace FilesDashboard;

use Icon;
use URLHelper;

trait Helpers
{
    /**
     * Create an action menu for a file. This method is used by the template.
     *
     * @param FileRef    $fileRef the file whose action shall be created
     * @param FolderType $folder  the file's folder
     * @param User       $user    the user for whom the actions shall be created
     *
     * @return string the HTML fragment of the action menu
     */
    public function getActionMenu($fileRef, $folder, $user)
    {
        $actionMenu = \ActionMenu::get();

        $actionMenu->addLink(
            URLHelper::getURL('dispatch.php/file/details/'.$fileRef->id),
            _('Info'),
            Icon::create('info-circle'),
            ['data-dialog' => 1]
        );

        require_once 'app/controllers/files.php';

        if ($rangeLink = \FilesController::getRangeLink($folder)) {
            $actionMenu->addLink(
                $rangeLink,
                _('Ordner öffnen'),
                Icon::create('folder-empty')
            );
        }

        if ($folder->isFileEditable($fileRef->id, $user->id)) {
            $actionMenu->addLink(
                URLHelper::getURL('dispatch.php/file/edit/'.$fileRef->id),
                _('Datei bearbeiten'),
                Icon::create('edit'),
                ['data-dialog' => '']
            );
            $actionMenu->addLink(
                URLHelper::getURL('dispatch.php/file/update/'.$fileRef->id),
                _('Datei aktualisieren'),
                Icon::create('refresh'),
                ['data-dialog' => '']
            );
        }

        if ($folder->isFileWritable($fileRef->id, $user->id)) {
            $actionMenu->addLink(
                URLHelper::getURL('dispatch.php/file/choose_destination/move/'.$fileRef->id),
                _('Datei verschieben'),
                Icon::create('arr_1right'),
                ['data-dialog' => 'size=auto']
            );
        }

        if ($folder->isFileDownloadable($fileRef, $user->id)) {
            $actionMenu->addLink(
                URLHelper::getURL('dispatch.php/file/choose_destination/copy/'.$fileRef->id),
                _('Datei kopieren'),
                Icon::create('clipboard'),
                ['data-dialog' => 'size=auto']
            );
        }

        if ($folder->isFileWritable($fileRef->id, $user->id)) {
            $actionMenu->addLink(
                URLHelper::getURL('dispatch.php/file/delete/'.$fileRef->id),
                _('Datei löschen'),
                Icon::create('trash'),
                ['onclick' => "return STUDIP.Dialog.confirmAsPost('".sprintf(_('Soll die Datei "%s" wirklich gelöscht werden?'), jsReady($fileRef->name))."', this.href);"]
            );
        }

        return $actionMenu->render();
    }

    /**
     * Takes a `text`, marks the first occurence of a `phrase` and
     * limits the length of this to `lim` characters. Used in the template.
     *
     * @param string $text   the text to be marked and cut
     * @param string $phrase the phrase to mark
     * @param int    $lim    the length of the abbreviated text
     *
     * @return string the marked and cut text
     */
    public function markPhrase($text, $phrase, $lim = 150)
    {
        if (mb_strlen($text) > $lim) {
            $text = mb_substr($text, max([0, $this->findWordPosition($phrase, $text) - $lim * 0.5]), $lim);
        }

        $words = str_replace(' ', '|', preg_quote(htmlReady($phrase), '/'));

        return preg_replace("/($words)/i", '<mark>\\1</mark>', htmlReady($text));
    }

    private function findWordPosition($word, $phrase)
    {
        return mb_stripos($phrase, $word) ?: 0;
    }
}