PluginProbe
Packeta / 2.0.9
Packeta v2.0.9
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
packeta / deps / nette / forms / src / Forms / Controls / UploadControl.php

UploadControl.php in Packeta 2.0.9, at deps/nette/forms/src/Forms/Controls/UploadControl.php

91 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * This file is part of the Nette Framework (https://nette.org)
5 * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6 */
7 declare (strict_types=1);
8 namespace Packetery\Nette\Forms\Controls;
9
10 use Packetery\Nette;
11 use Packetery\Nette\Forms;
12 use Packetery\Nette\Forms\Form;
13 use Packetery\Nette\Http\FileUpload;
14 /**
15 * Text box and browse button that allow users to select a file to upload to the server.
16 */
17 class UploadControl extends BaseControl
18 {
19 /** validation rule */
20 public const VALID = ':uploadControlValid';
21 /**
22 * @param string|object $label
23 */
24 public function __construct($label = null, bool $multiple = \false)
25 {
26 parent::__construct($label);
27 $this->control->type = 'file';
28 $this->control->multiple = $multiple;
29 $this->setOption('type', 'file');
30 $this->addCondition(\true)->addRule([$this, 'isOk'], Forms\Validator::$messages[self::VALID]);
31 $this->addRule(Form::MAX_FILE_SIZE, null, Forms\Helpers::iniGetSize('upload_max_filesize'));
32 $this->monitor(Form::class, function (Form $form) : void {
33 if (!$form->isMethod('post')) {
34 throw new \Packetery\Nette\InvalidStateException('File upload requires method POST.');
35 }
36 $form->getElementPrototype()->enctype = 'multipart/form-data';
37 });
38 }
39 public function loadHttpData() : void
40 {
41 $this->value = $this->getHttpData(Form::DATA_FILE);
42 if ($this->value === null) {
43 $this->value = new FileUpload(null);
44 }
45 }
46 public function getHtmlName() : string
47 {
48 return parent::getHtmlName() . ($this->control->multiple ? '[]' : '');
49 }
50 /**
51 * @return static
52 * @internal
53 */
54 public function setValue($value)
55 {
56 return $this;
57 }
58 /**
59 * Has been any file uploaded?
60 */
61 public function isFilled() : bool
62 {
63 return $this->value instanceof FileUpload ? $this->value->getError() !== \UPLOAD_ERR_NO_FILE : (bool) $this->value;
64 }
65 /**
66 * Have been all files succesfully uploaded?
67 */
68 public function isOk() : bool
69 {
70 return $this->value instanceof FileUpload ? $this->value->isOk() : $this->value && \array_reduce($this->value, function (bool $carry, FileUpload $fileUpload) : bool {
71 return $carry && $fileUpload->isOk();
72 }, \true);
73 }
74 /** @return static */
75 public function addRule($validator, $errorMessage = null, $arg = null)
76 {
77 if ($validator === Form::IMAGE) {
78 $this->control->accept = \implode(', ', FileUpload::IMAGE_MIME_TYPES);
79 } elseif ($validator === Form::MIME_TYPE) {
80 $this->control->accept = \implode(', ', (array) $arg);
81 } elseif ($validator === Form::MAX_FILE_SIZE) {
82 if ($arg > Forms\Helpers::iniGetSize('upload_max_filesize')) {
83 $ini = \ini_get('upload_max_filesize');
84 \trigger_error("Value of MAX_FILE_SIZE ({$arg}) is greater than value of directive upload_max_filesize ({$ini}).", \E_USER_WARNING);
85 }
86 $this->getRules()->removeRule($validator);
87 }
88 return parent::addRule($validator, $errorMessage, $arg);
89 }
90 }
91