Commit | Line | Data |
---|---|---|
8a6c1667 | 1 | <?php |
a9229942 | 2 | |
8a6c1667 | 3 | namespace wcf\system\form\builder\field\media; |
a9229942 | 4 | |
8a6c1667 MS |
5 | use wcf\data\media\ViewableMedia; |
6 | use wcf\system\form\builder\field\AbstractFormField; | |
7 | use wcf\system\form\builder\field\IImmutableFormField; | |
8 | use wcf\system\form\builder\field\TImmutableFormField; | |
c0614ad3 | 9 | use wcf\system\form\builder\field\validation\FormFieldValidationError; |
8a6c1667 MS |
10 | |
11 | /** | |
12 | * Implementation of a form field to select a single media file. | |
a9229942 TD |
13 | * |
14 | * @author Matthias Schmidt | |
15 | * @copyright 2001-2019 WoltLab GmbH | |
16 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> | |
17 | * @package WoltLabSuite\Core\System\Form\Builder\Field | |
18 | * @since 5.2 | |
8a6c1667 | 19 | */ |
a9229942 TD |
20 | class SingleMediaSelectionFormField extends AbstractFormField implements IImmutableFormField |
21 | { | |
22 | use TImmutableFormField; | |
23 | ||
24 | /** | |
25 | * is `true` if only images can be selected and `false` otherwise | |
26 | * @var bool | |
27 | */ | |
28 | protected $imageOnly = false; | |
29 | ||
30 | /** | |
31 | * media object with the current value as id | |
32 | * @var null|ViewableMedia | |
33 | */ | |
34 | protected $media; | |
35 | ||
36 | /** | |
37 | * @inheritDoc | |
38 | */ | |
39 | protected $javaScriptDataHandlerModule = 'WoltLabSuite/Core/Form/Builder/Field/Value'; | |
40 | ||
41 | /** | |
42 | * @inheritDoc | |
43 | */ | |
44 | protected $templateName = '__singleMediaSelectionFormField'; | |
45 | ||
46 | /** | |
47 | * Returns the media object with the current value as id. | |
48 | * | |
49 | * @return ViewableMedia | |
50 | * | |
51 | * @throws \InvalidArgumentException if no or an invalid media id is set as value | |
52 | * @throws \UnexpectedValueException if no or an invalid media id is set as value | |
53 | */ | |
54 | public function getMedia() | |
55 | { | |
56 | if ($this->media === null) { | |
57 | if (!$this->getValue()) { | |
58 | throw new \BadMethodCallException("Cannot be media object if no valid media id is set as value."); | |
59 | } | |
60 | ||
61 | $this->media = ViewableMedia::getMedia($this->getValue()); | |
62 | if ($this->media === null) { | |
63 | throw new \UnexpectedValueException("Cannot load media with id '{$this->getValue()}'."); | |
64 | } | |
65 | } | |
66 | ||
67 | return $this->media; | |
68 | } | |
69 | ||
70 | /** | |
71 | * Sets if only images can be selected and returns this field. | |
72 | * | |
73 | * @param bool $imageOnly | |
74 | * @return static this field | |
75 | */ | |
76 | public function imageOnly($imageOnly = true) | |
77 | { | |
78 | $this->imageOnly = $imageOnly; | |
79 | ||
80 | return $this; | |
81 | } | |
82 | ||
83 | /** | |
84 | * Returns `true` if only images can be selected and `false` otherwise. | |
85 | * | |
86 | * By default, all images can be selected. | |
87 | * | |
88 | * @return bool | |
89 | */ | |
90 | public function isImageOnly() | |
91 | { | |
92 | return $this->imageOnly; | |
93 | } | |
94 | ||
95 | /** | |
96 | * @inheritDoc | |
97 | */ | |
98 | public function readValue() | |
99 | { | |
100 | if ($this->getDocument()->hasRequestData($this->getPrefixedId())) { | |
101 | $value = $this->getDocument()->getRequestData($this->getPrefixedId()); | |
102 | ||
103 | if ($value) { | |
104 | $this->value = $value; | |
105 | } | |
106 | } | |
107 | ||
108 | return $this; | |
109 | } | |
110 | ||
111 | /** | |
112 | * @inheritDoc | |
113 | */ | |
114 | public function validate() | |
115 | { | |
116 | parent::validate(); | |
117 | ||
118 | try { | |
119 | $media = $this->getMedia(); | |
120 | if (!$media->isAccessible() || ($this->isImageOnly() && !$media->isImage)) { | |
121 | $this->value = null; | |
122 | } | |
123 | } catch (\BadMethodCallException $e) { | |
124 | $this->value = null; | |
125 | } catch (\UnexpectedValueException $e) { | |
126 | $this->value = null; | |
127 | } | |
128 | ||
129 | if (!$this->getValue() && $this->isRequired()) { | |
130 | $this->addValidationError(new FormFieldValidationError('empty')); | |
131 | } | |
132 | } | |
8a6c1667 | 133 | } |