22
33namespace Vormkracht10 \UploadcareField ;
44
5- class UploadcareField {}
5+ use Filament \Forms ;
6+ use Filament \Facades \Filament ;
7+ use Illuminate \Database \Eloquent \Model ;
8+ use Vormkracht10 \Backstage \Models \Field ;
9+ use Vormkracht10 \Uploadcare \Enums \Style ;
10+ use Vormkracht10 \MediaPicker \Models \Media ;
11+ use Vormkracht10 \Backstage \Fields \FieldBase ;
12+ use Vormkracht10 \Backstage \Contracts \FieldContract ;
13+ use Vormkracht10 \Uploadcare \Forms \Components \Uploadcare as Input ;
14+
15+ class UploadcareField extends FieldBase implements FieldContract
16+ {
17+ public static function getDefaultConfig (): array
18+ {
19+ return [
20+ ...parent ::getDefaultConfig (),
21+ 'uploaderStyle ' => Style::INLINE ->value ,
22+ 'multiple ' => false ,
23+ 'imagesOnly ' => false ,
24+ ];
25+ }
26+
27+ public static function make (string $ name , Field $ field ): Input
28+ {
29+ $ input = self ::applyDefaultSettings (Input::make ($ name )->withMetadata (), $ field );
30+
31+ $ input = $ input ->label ($ field ->name ?? self ::getDefaultConfig ()['label ' ] ?? null )
32+ ->uploaderStyle (Style::tryFrom ($ field ->config ['uploaderStyle ' ] ?? null ) ?? Style::tryFrom (self ::getDefaultConfig ()['uploaderStyle ' ]))
33+ ->multiple ($ field ->config ['multiple ' ] ?? self ::getDefaultConfig ()['multiple ' ]);
34+
35+ if ($ field ->config ['imagesOnly ' ] ?? self ::getDefaultConfig ()['imagesOnly ' ]) {
36+ $ input ->imagesOnly ();
37+ }
38+
39+ return $ input ;
40+ }
41+
42+ public function getForm (): array
43+ {
44+ return [
45+ Forms \Components \Tabs::make ()
46+ ->schema ([
47+ Forms \Components \Tabs \Tab::make ('General ' )
48+ ->label (__ ('General ' ))
49+ ->schema ([
50+ ...parent ::getForm (),
51+ ]),
52+ Forms \Components \Tabs \Tab::make ('Field specific ' )
53+ ->label (__ ('Field specific ' ))
54+ ->schema ([
55+ Forms \Components \Grid::make (2 )->schema ([
56+ Forms \Components \Toggle::make ('config.multiple ' )
57+ ->label (__ ('Multiple ' ))
58+ ->inline (false ),
59+ Forms \Components \Toggle::make ('config.imagesOnly ' )
60+ ->label (__ ('Images only ' ))
61+ ->inline (false ),
62+ Forms \Components \Select::make ('config.uploaderStyle ' )
63+ ->label (__ ('Uploader style ' ))
64+ ->options ([
65+ Style::INLINE ->value => __ ('Inline ' ),
66+ Style::MINIMAL ->value => __ ('Minimal ' ),
67+ Style::REGULAR ->value => __ ('Regular ' ),
68+ ])
69+ ->required (),
70+ ]),
71+ ]),
72+ ])->columnSpanFull (),
73+ ];
74+ }
75+
76+ public static function mutateFormDataCallback (Model $ record , Field $ field , array $ data ): array
77+ {
78+ if (! isset ($ record ->values [$ field ->slug ])) {
79+ return $ data ;
80+ }
81+
82+ $ media = Media::whereIn ('ulid ' , $ record ->values [$ field ->slug ])
83+ ->get ()
84+ ->map (function ($ media ) {
85+ if (! isset ($ media ->metadata ['cdnUrl ' ])) {
86+ throw new \Exception ('Uploadcare file does not have a CDN URL ' );
87+ }
88+
89+ return $ media ->metadata ['cdnUrl ' ];
90+ })->toArray ();
91+
92+ $ data ['setting ' ][$ field ->slug ] = json_encode ($ media );
93+
94+ return $ data ;
95+ }
96+
97+ public static function mutateBeforeSaveCallback (Model $ record , Field $ field , array $ data ): array
98+ {
99+ if ($ field ->field_type !== 'uploadcare ' ) {
100+ return $ data ;
101+ }
102+
103+ if (!isset ($ data ['setting ' ][$ field ->slug ])) {
104+ return $ data ;
105+ }
106+
107+ $ values = $ data ['setting ' ][$ field ->slug ];
108+
109+ if (is_string ($ values )) {
110+ $ values = json_decode ($ values , true );
111+ }
112+
113+ if (!is_array ($ values )) {
114+ return $ data ;
115+ }
116+
117+ $ media = [];
118+
119+ foreach ($ values as $ file ) {
120+ $ info = $ file ['fileInfo ' ];
121+ $ detailedInfo = !empty ($ info ['imageInfo ' ])
122+ ? $ info ['imageInfo ' ]
123+ : (!empty ($ info ['videoInfo ' ])
124+ ? $ info ['videoInfo ' ]
125+ : (!empty ($ info ['contentInfo ' ])
126+ ? $ info ['contentInfo ' ]
127+ : []));
128+
129+ $ media [] = Media::updateOrCreate ([
130+ 'site_ulid ' => Filament::getTenant ()->ulid ,
131+ 'disk ' => 'uploadcare ' ,
132+ 'original_filename ' => $ info ['name ' ],
133+ 'checksum ' => md5_file ($ info ['cdnUrl ' ]),
134+ ], [
135+ 'filename ' => $ info ['uuid ' ],
136+ 'uploaded_by ' => auth ()->user ()->id ,
137+ 'extension ' => $ detailedInfo ['format ' ] ?? null ,
138+ 'mime_type ' => $ info ['mimeType ' ],
139+ 'size ' => $ info ['size ' ],
140+ 'width ' => isset ($ detailedInfo ['width ' ]) ? $ detailedInfo ['width ' ] : null ,
141+ 'height ' => isset ($ detailedInfo ['height ' ]) ? $ detailedInfo ['height ' ] : null ,
142+ 'public ' => config ('media-picker.visibility ' ) === 'public ' ,
143+ 'metadata ' => $ info ,
144+ ]);
145+ }
146+
147+ $ data ['setting ' ][$ field ->slug ] = collect ($ media )->map (function ($ media ) {
148+ return $ media ->ulid ;
149+ })->toArray ();
150+
151+ return $ data ;
152+ }
153+ }
0 commit comments