Skip to content

Commit 75c8e2f

Browse files
committed
add UploadFormField that supports strings and instances of UploadFile for getting the file's location
see also https://community.woltlab.com/thread/288853-uploadformfield-bild-vorschau-ohne-darstellbares-bild/?postID=1841066#post1841066
1 parent 33039ff commit 75c8e2f

1 file changed

Lines changed: 80 additions & 0 deletions

File tree

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
namespace wcf\system\form\builder\field;
4+
5+
use wcf\data\IStorableObject;
6+
use wcf\system\file\upload\UploadFile;
7+
use wcf\util\ImageUtil;
8+
9+
/**
10+
* Implementation of a form field for to uploads.
11+
* This extension supports UploadFile-objects and strings for file-locations.
12+
*
13+
* @author Joshua Ruesweg, Florian Gail
14+
* @copyright 2001-2019 WoltLab GmbH
15+
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
16+
* @package WoltLabSuite\Core\System\Form\Builder\Field
17+
* @since 5.2
18+
*/
19+
class PreviewedUploadFormField extends UploadFormField {
20+
/**
21+
* @inheritDoc
22+
*
23+
* @throws \InvalidArgumentException if the getter for the value provides invalid values
24+
*/
25+
public function updatedObject(array $data, IStorableObject $object, $loadValues = true) {
26+
if ($loadValues) {
27+
// first check, whether an getter for the field exists
28+
if (\method_exists($object, 'get' . \ucfirst($this->getObjectProperty()) . 'UploadFileLocations')) {
29+
$value = \call_user_func([
30+
$object,
31+
'get' . \ucfirst($this->getObjectProperty()) . 'UploadFileLocations',
32+
]);
33+
$method = "method '" . \get_class($object) . "::get" . \ucfirst($this->getObjectProperty()) . "UploadFileLocations()'";
34+
}
35+
elseif (\method_exists($object, 'get' . \ucfirst($this->getObjectProperty()))) {
36+
$value = \call_user_func([
37+
$object,
38+
'get' . \ucfirst($this->getObjectProperty())
39+
]);
40+
$method = "method '" . \get_class($object) . "::get" . \ucfirst($this->getObjectProperty()) . "()'";
41+
}
42+
else {
43+
$value = $data[$this->getObjectProperty()];
44+
$method = "variable '" . \get_class($object) . "::$" . $this->getObjectProperty() . "'";
45+
}
46+
47+
if (\is_array($value)) {
48+
$value = \array_map(function ($v) use ($method) {
49+
if ($v instanceof UploadFile) {
50+
if (!file_exists($v->getLocation())) {
51+
throw new \InvalidArgumentException("The " . $method . " must return an array of strings or object of class '" . UploadFile::class . "' with the valid file locations.");
52+
}
53+
54+
return $v;
55+
}
56+
else {
57+
if (!\is_string($v) || !\file_exists($v)) {
58+
throw new \InvalidArgumentException("The " . $method . " must return an array of strings or object of class '" . UploadFile::class . "' with the valid file locations.");
59+
}
60+
61+
return new UploadFile(
62+
$v,
63+
\basename($v),
64+
ImageUtil::isImage($v, \basename($v), $this->svgImageAllowed()),
65+
true,
66+
$this->svgImageAllowed()
67+
);
68+
}
69+
}, $value);
70+
71+
$this->value($value);
72+
}
73+
else {
74+
throw new \InvalidArgumentException("The " . $method . " must return an array of strings with the file locations.");
75+
}
76+
}
77+
78+
return $this;
79+
}
80+
}

0 commit comments

Comments
 (0)