1: <?php
2:
3: declare(strict_types=1);
4:
5: namespace Atk4\Ui\Form\Control;
6:
7: use Atk4\Ui\View;
8:
9: class UploadImage extends Upload
10: {
11: /** @var View|null The thumbnail view to add to this input. */
12: public $thumbnail;
13:
14: /** @var string The template region where to add the thumbnail view. */
15: public $thumbnailRegion = 'AfterAfterInput';
16:
17: /** @var string|null The default thumbnail source. */
18: public $defaultSrc;
19:
20: #[\Override]
21: protected function init(): void
22: {
23: parent::init();
24:
25: if (!$this->accept) {
26: $this->accept = ['.jpg', '.jpeg', '.png'];
27: }
28:
29: $this->add($this->getThumbnail(), $this->thumbnailRegion);
30: }
31:
32: public function getThumbnail(): View
33: {
34: if ($this->thumbnail === null) {
35: $this->thumbnail = (new View(['element' => 'img', 'class' => ['right', 'floated', 'image'], 'ui' => true]))
36: ->setAttr(['width' => 36, 'height' => 36]);
37:
38: if ($this->defaultSrc !== null) {
39: $this->thumbnail->setAttr(['src' => $this->defaultSrc]);
40: }
41: }
42:
43: return $this->thumbnail;
44: }
45:
46: public function setThumbnailSrc(string $src): void
47: {
48: $this->thumbnail->setAttr(['src' => $src]);
49: $js = $this->thumbnail->js();
50: $js->attr('src', $src);
51: $this->addJsAction($js);
52: }
53:
54: public function clearThumbnail(): void
55: {
56: $js = $this->thumbnail->js();
57: if ($this->defaultSrc !== null) {
58: $js->attr('src', $this->defaultSrc);
59: } else {
60: $js->removeAttr('src');
61: }
62: $this->addJsAction($js);
63: }
64: }
65: