1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AbterPhp\Files\Grid\Factory; |
6
|
|
|
|
7
|
|
|
use AbterPhp\Admin\Grid\Factory\BaseFactory; |
8
|
|
|
use AbterPhp\Admin\Grid\Factory\GridFactory; |
9
|
|
|
use AbterPhp\Admin\Grid\Factory\PaginationFactory; |
10
|
|
|
use AbterPhp\Admin\Helper\DateHelper; |
11
|
|
|
use AbterPhp\Files\Constant\Route; |
12
|
|
|
use AbterPhp\Files\Domain\Entities\File as Entity; |
13
|
|
|
use AbterPhp\Files\Grid\Factory\Table\Header\File as HeaderFactory; |
14
|
|
|
use AbterPhp\Files\Grid\Factory\Table\File as TableFactory; |
15
|
|
|
use AbterPhp\Files\Grid\Filters\File as Filters; |
16
|
|
|
use AbterPhp\Framework\Constant\Html5; |
17
|
|
|
use AbterPhp\Framework\Grid\Action\Action; |
18
|
|
|
use AbterPhp\Framework\Grid\Component\Actions; |
19
|
|
|
use AbterPhp\Framework\Html\Component; |
20
|
|
|
use Opulence\Routing\Urls\UrlGenerator; |
21
|
|
|
|
22
|
|
|
class File extends BaseFactory |
23
|
|
|
{ |
24
|
|
|
private const GETTER_PUBLIC_NAME = 'getPublicName'; |
25
|
|
|
private const GETTER_CATEGORY = 'getCategory'; |
26
|
|
|
private const GETTER_DESCRIPTION = 'getDescription'; |
27
|
|
|
|
28
|
|
|
private const LABEL_DOWNLOAD = 'files:download'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* File constructor. |
32
|
|
|
* |
33
|
|
|
* @param UrlGenerator $urlGenerator |
34
|
|
|
* @param PaginationFactory $paginationFactory |
35
|
|
|
* @param TableFactory $tableFactory |
36
|
|
|
* @param GridFactory $gridFactory |
37
|
|
|
* @param Filters $filters |
38
|
|
|
*/ |
39
|
|
|
public function __construct( |
40
|
|
|
UrlGenerator $urlGenerator, |
41
|
|
|
PaginationFactory $paginationFactory, |
42
|
|
|
TableFactory $tableFactory, |
43
|
|
|
GridFactory $gridFactory, |
44
|
|
|
Filters $filters |
45
|
|
|
) { |
46
|
|
|
parent::__construct($urlGenerator, $paginationFactory, $tableFactory, $gridFactory, $filters); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return array |
51
|
|
|
*/ |
52
|
|
|
public function getGetters(): array |
53
|
|
|
{ |
54
|
|
|
return [ |
55
|
|
|
HeaderFactory::GROUP_FILENAME => static::GETTER_PUBLIC_NAME, |
56
|
|
|
HeaderFactory::GROUP_CATEGORY => static::GETTER_CATEGORY, |
57
|
|
|
HeaderFactory::GROUP_DESCRIPTION => static::GETTER_DESCRIPTION, |
58
|
|
|
/** @see File::getUploadedAt */ |
59
|
|
|
HeaderFactory::GROUP_UPLOADED_AT => [$this, 'getUploadedAt'], |
60
|
|
|
]; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param Entity $entity |
65
|
|
|
* |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
|
|
public function getUploadedAt(Entity $entity): string |
69
|
|
|
{ |
70
|
|
|
return DateHelper::mysqlDateTime($entity->getUploadedAt()); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return Actions |
75
|
|
|
*/ |
76
|
|
|
protected function getRowActions(): Actions |
77
|
|
|
{ |
78
|
|
|
$attributeCallbacks = $this->getAttributeCallbacks(); |
79
|
|
|
$downloadCallbacks = $this->getDownloadCallbacks(); |
80
|
|
|
|
81
|
|
|
$downloadAttributes = [ |
82
|
|
|
Html5::ATTR_HREF => Route::PUBLIC_FILE, |
83
|
|
|
]; |
84
|
|
|
$editAttributes = [ |
85
|
|
|
Html5::ATTR_HREF => Route::FILES_EDIT, |
86
|
|
|
]; |
87
|
|
|
$deleteAttributes = [ |
88
|
|
|
Html5::ATTR_HREF => Route::FILES_DELETE, |
89
|
|
|
]; |
90
|
|
|
|
91
|
|
|
$cellActions = new Actions(); |
92
|
|
|
$cellActions[] = new Action( |
93
|
|
|
static::LABEL_DOWNLOAD, |
94
|
|
|
$this->downloadIntents, |
95
|
|
|
$downloadAttributes, |
96
|
|
|
$downloadCallbacks, |
97
|
|
|
Html5::TAG_A |
98
|
|
|
); |
99
|
|
|
$cellActions[] = new Action( |
100
|
|
|
static::LABEL_EDIT, |
101
|
|
|
$this->editIntents, |
102
|
|
|
$editAttributes, |
103
|
|
|
$attributeCallbacks, |
104
|
|
|
Html5::TAG_A |
105
|
|
|
); |
106
|
|
|
$cellActions[] = new Action( |
107
|
|
|
static::LABEL_DELETE, |
108
|
|
|
$this->deleteIntents, |
109
|
|
|
$deleteAttributes, |
110
|
|
|
$attributeCallbacks, |
111
|
|
|
Html5::TAG_A |
112
|
|
|
); |
113
|
|
|
|
114
|
|
|
return $cellActions; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return callable[] |
119
|
|
|
*/ |
120
|
|
|
protected function getAttributeCallbacks(): array |
121
|
|
|
{ |
122
|
|
|
$attributeCallbacks = parent::getAttributeCallbacks(); |
123
|
|
|
|
124
|
|
|
$attributeCallbacks[Html5::ATTR_CLASS] = function ($attribute, Entity $entity) { |
125
|
|
|
return $entity->isWritable() ? $attribute : Component::INTENT_HIDDEN; |
126
|
|
|
}; |
127
|
|
|
|
128
|
|
|
return $attributeCallbacks; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return callable[] |
133
|
|
|
*/ |
134
|
|
|
protected function getDownloadCallbacks(): array |
135
|
|
|
{ |
136
|
|
|
$urlGenerator = $this->urlGenerator; |
137
|
|
|
|
138
|
|
|
$closure = function ($attribute, Entity $entity) use ($urlGenerator) { |
139
|
|
|
try { |
140
|
|
|
// @phan-suppress-next-line PhanTypeMismatchArgument |
141
|
|
|
return $urlGenerator->createFromName($attribute, $entity->getFilesystemName()); |
142
|
|
|
} catch (\Exception $e) { |
143
|
|
|
return ''; |
144
|
|
|
} |
145
|
|
|
}; |
146
|
|
|
|
147
|
|
|
$attributeCallbacks = [ |
148
|
|
|
Html5::ATTR_HREF => $closure, |
149
|
|
|
]; |
150
|
|
|
|
151
|
|
|
return $attributeCallbacks; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|