Passed
Push — master ( bc7b5c...925ca7 )
by Georgi
03:13
created

FileView   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 58
c 1
b 0
f 0
dl 0
loc 133
rs 10
wmc 26

9 Methods

Rating   Name   Duplication   Size   Complexity  
A addFileRemoteLinks() 0 21 4
A setPeriodSelection() 0 13 5
A __construct() 0 3 2
A addContents() 0 18 2
A actionDisabled() 0 3 1
A addFileDetails() 0 5 2
A reload() 0 2 1
A renderView() 0 7 1
B addFileControlButtons() 0 34 8
1
<?php
2
3
namespace Epesi\FileStorage\Seeds;
4
5
use atk4\ui\jsExpression;
6
use Epesi\FileStorage\Database\Models\FileRemoteAccess;
7
use Epesi\FileStorage\Integration\Joints\FileStorageAccessJoint;
8
use atk4\ui\View;
9
use Epesi\FileStorage\Database\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 = is_numeric($file)? File::get($file): $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) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->periodSelection of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

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.

Loading history...
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') . ')': '';
0 ignored issues
show
Bug introduced by
Are you sure __('Default') of type array|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

45
			$default = $key == $this->defaultPeriod? ' (' . /** @scrutinizer ignore-type */ __('Default') . ')': '';
Loading history...
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();
0 ignored issues
show
Bug introduced by
The method addColumn() does not exist on atk4\ui\View. It seems like you code against a sub-type of atk4\ui\View such as atk4\ui\Columns or atk4\ui\Grid or atk4\ui\Table. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

60
		/** @scrutinizer ignore-call */ 
61
  $column = $columns->addColumn();
Loading history...
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()->get() as $link) {
82
			$container->add(['View', __('Remote access link expiring :expiry', ['expiry' => $link->expires_at])]);
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) {
0 ignored issues
show
Bug introduced by
function(...) { /* ... */ } of type callable is incompatible with the type array|string expected by parameter $arg1 of atk4\ui\View::set(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

92
		$container->on('click', '.delete-link', $this->add(['jsCallback', 'postTrigger' => 'link'])->set(/** @scrutinizer ignore-type */ function($j, $linkId) {
Loading history...
93
			if (! $link = FileRemoteAccess::find($linkId)) return;
94
			
95
			$link->delete();
96
			
97
			return $this->reload();
98
		}, ['link' => new jsExpression('$(this).data("id")')]));
0 ignored issues
show
Bug introduced by
array('link' => new atk4...('$(this).data("id")')) of type array<string,atk4\ui\jsExpression> is incompatible with the type null|string expected by parameter $arg2 of atk4\ui\View::set(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

98
		}, /** @scrutinizer ignore-type */ ['link' => new jsExpression('$(this).data("id")')]));
Loading history...
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) {
0 ignored issues
show
Bug introduced by
The method onChange() does not exist on atk4\ui\View. It seems like you code against a sub-type of atk4\ui\View such as atk4\ui\FormField\Generic or atk4\ui\Component\InlineEdit or atk4\ui\DropDown. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

125
			$dropdown->/** @scrutinizer ignore-call */ 
126
              onChange(function($value) {
Loading history...
126
				$period = array_search($value, $this->periodSelection)?: $this->defaultPeriod;
127
				
128
				FileRemoteAccess::grant($this->file, $period);
129
				
130
				return $this->reload();
131
			});
132
		}
133
			
134
		$linkButton->on('click', $dropdown->cb);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $linkButton does not seem to be defined for all execution paths leading up to this point.
Loading history...
Comprehensibility Best Practice introduced by
The variable $dropdown does not seem to be defined for all execution paths leading up to this point.
Loading history...
135
	}
136
	
137
	protected function actionDisabled($action) 
138
	{
139
		return in_array($action, (array) $this->disableActions);
140
	}
141
	
142
	protected function reload() {
143
		return new \atk4\ui\jsReload($this);
144
	}
145
}