|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Epesi\FileStorage\View; |
|
4
|
|
|
|
|
5
|
|
|
use atk4\ui\jsExpression; |
|
6
|
|
|
use Epesi\FileStorage\Models\FileRemoteAccess; |
|
7
|
|
|
use Epesi\FileStorage\Integration\Joints\FileStorageAccessJoint; |
|
8
|
|
|
use atk4\ui\View; |
|
9
|
|
|
use Epesi\FileStorage\Models\File; |
|
10
|
|
|
|
|
11
|
|
|
class FileView extends View |
|
12
|
|
|
{ |
|
13
|
|
|
public $defaultPeriod = '1 weeks'; |
|
14
|
|
|
|
|
15
|
|
|
public $periodSelection = []; |
|
16
|
|
|
|
|
17
|
|
|
public $disableActions = []; |
|
18
|
|
|
|
|
19
|
|
|
protected $file; |
|
20
|
|
|
|
|
21
|
|
|
public function __construct($file) |
|
22
|
|
|
{ |
|
23
|
|
|
$this->file = File::retrieve($file); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function renderView() |
|
27
|
|
|
{ |
|
28
|
|
|
$this->setPeriodSelection(); |
|
29
|
|
|
|
|
30
|
|
|
$this->addContents(); |
|
31
|
|
|
|
|
32
|
|
|
parent::renderView(); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
protected function setPeriodSelection() |
|
36
|
|
|
{ |
|
37
|
|
|
if (! $this->periodSelection) { |
|
|
|
|
|
|
38
|
|
|
$this->periodSelection = []; |
|
39
|
|
|
foreach ([1, 2, 3, 4] as $count) { |
|
40
|
|
|
$this->periodSelection[$count . ' weeks'] = trans_choice('{1} :count week |[2,*] :count weeks', $count); |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
foreach ($this->periodSelection as $key => $period) { |
|
45
|
|
|
$default = $key == $this->defaultPeriod? ' (' . __('Default') . ')': ''; |
|
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
$this->periodSelection[$key] = $period . $default; |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
protected function addContents() |
|
52
|
|
|
{ |
|
53
|
|
|
if (! $this->file) { |
|
54
|
|
|
$this->set(__('Wrong parameters for file')); |
|
55
|
|
|
return; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$columns = $this->add('Columns'); |
|
59
|
|
|
|
|
60
|
|
|
$column = $columns->addColumn(); |
|
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
$this->addFileDetails($column); |
|
63
|
|
|
|
|
64
|
|
|
$column = $columns->addColumn(); |
|
65
|
|
|
|
|
66
|
|
|
$this->addFileRemoteLinks($column); |
|
67
|
|
|
|
|
68
|
|
|
$this->addFileControlButtons(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
protected function addFileDetails($container = null) |
|
72
|
|
|
{ |
|
73
|
|
|
$container = $container?: $this; |
|
74
|
|
|
|
|
75
|
|
|
$container->add(new FileDetailsLister($this->file)); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
protected function addFileRemoteLinks($container = null) { |
|
79
|
|
|
$container = $container?: $this; |
|
80
|
|
|
|
|
81
|
|
|
foreach ($this->file->userActiveLinks() as $link) { |
|
82
|
|
|
$container->add(['View', __('Remote access link expiring :expiry', ['expiry' => $link['expires_at']->format('Y-m-d H:i:s')])]); |
|
83
|
|
|
$linkInput = $container->add([new \atk4\ui\FormField\Input(['readonly' => true, 'iconLeft' => 'linkify'])])->set($link['href'])->setStyle(['width' => '100%', 'margin-bottom' => '15px']); |
|
84
|
|
|
|
|
85
|
|
|
$linkInput->action = new \atk4\ui\Button([_('Copy'), 'iconRight' => 'copy', 'attr' => ['data-clipboard-target' => "#{$linkInput->id}_input", 'title' => __('Click to copy link')], 'class' => ['basic copy-button']]); |
|
86
|
|
|
|
|
87
|
|
|
$linkInput->add(['Button', 'icon'=>'red x icon', 'class' => ['basic delete-link']], 'AfterAfterInput')->setAttr(['data-id' => $link['id'], 'title' => __('Disable link')]); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$container->js(true, new jsExpression('new ClipboardJS(".copy-button")')); |
|
91
|
|
|
|
|
92
|
|
|
$container->on('click', '.delete-link', $this->add(['jsCallback', 'postTrigger' => 'link'])->set(function($j, $linkId) { |
|
|
|
|
|
|
93
|
|
|
if (! $link = FileRemoteAccess::create()->load($linkId)) return; |
|
94
|
|
|
|
|
95
|
|
|
$link->delete(); |
|
96
|
|
|
|
|
97
|
|
|
return $this->jsReload(); |
|
98
|
|
|
}, ['link' => new jsExpression('$(this).data("id")')])); |
|
|
|
|
|
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
protected function addFileControlButtons() { |
|
102
|
|
|
$urls = FileStorageAccessJoint::getActionUrls($this->file); |
|
103
|
|
|
|
|
104
|
|
|
if (! $this->actionDisabled('preview') && ($urls['preview']?? null)) { |
|
105
|
|
|
$this->add(['Button', 'class' => ['basic'], 'icon' => 'file alternate outline'])->set(__('View'))->link($urls['preview'])->setAttr(['target' => '_blank']); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
if (! $this->actionDisabled('download') && ($urls['download']?? null)) { |
|
109
|
|
|
$this->add(['Button', 'class' => ['basic'], 'icon' => 'file download'])->set(__('Download'))->link($urls['download']); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
if (! $this->actionDisabled('history')) { |
|
113
|
|
|
$this->add(['Button', 'class' => ['basic'], 'icon' => 'history'])->set(__('Access History'))->link(url('view/filestorage:file-access-history/body') . '?' . http_build_query(['id' => $this->file->id])); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
if (! $this->actionDisabled('remote')) { |
|
117
|
|
|
$linkControl = $this->add(['View', 'ui' => 'basic buttons']); |
|
118
|
|
|
|
|
119
|
|
|
$linkButton = $linkControl->add(['Button', 'class' => ['basic'], 'icon' => 'linkify'])->set(__('Get Remote Link')); |
|
120
|
|
|
|
|
121
|
|
|
$dropdown = $linkControl->add(['DropDownButton', 'class' => ['floating icon button']]); |
|
122
|
|
|
|
|
123
|
|
|
$dropdown->setSource($this->periodSelection); |
|
124
|
|
|
|
|
125
|
|
|
$dropdown->onChange(function($value) { |
|
|
|
|
|
|
126
|
|
|
$period = array_search($value, $this->periodSelection)?: $this->defaultPeriod; |
|
127
|
|
|
|
|
128
|
|
|
FileRemoteAccess::grant($this->file, $period); |
|
129
|
|
|
|
|
130
|
|
|
return $this->jsReload(); |
|
131
|
|
|
}); |
|
132
|
|
|
|
|
133
|
|
|
$linkButton->on('click', $dropdown->cb); |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
protected function actionDisabled($action) |
|
138
|
|
|
{ |
|
139
|
|
|
return in_array($action, (array) $this->disableActions); |
|
140
|
|
|
} |
|
141
|
|
|
} |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.