|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use EventEspresso\core\exceptions\InvalidDataTypeException; |
|
4
|
|
|
use EventEspresso\core\exceptions\InvalidInterfaceException; |
|
5
|
|
|
use EventEspresso\core\services\loaders\LoaderFactory; |
|
6
|
|
|
use EventEspresso\core\services\request\files\FileSubmissionInterface; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* EE_File_Input |
|
10
|
|
|
* |
|
11
|
|
|
* For uploading a file |
|
12
|
|
|
* Note: if you will be using this in a form, make sure it uses `enctype="multipart/form-data"`. |
|
13
|
|
|
* |
|
14
|
|
|
* @package Event Espresso |
|
15
|
|
|
* @subpackage |
|
16
|
|
|
* @author Mike Nelson |
|
17
|
|
|
* |
|
18
|
|
|
* This input has a default validation strategy of plaintext (which can be removed after construction) |
|
19
|
|
|
*/ |
|
20
|
|
|
class EE_File_Input extends EE_Form_Input_Base |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $allowed_file_extensions; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $allowed_mime_types; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param array $options |
|
34
|
|
|
* @throws InvalidArgumentException |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct($options = array()) |
|
37
|
|
|
{ |
|
38
|
|
View Code Duplication |
if (isset($options['allowed_file_extensions'])) { |
|
39
|
|
|
$this->allowed_file_extensions = (array) $options['allowed_file_extensions']; |
|
40
|
|
|
} else { |
|
41
|
|
|
$this->allowed_file_extensions = ['csv']; |
|
42
|
|
|
} |
|
43
|
|
View Code Duplication |
if (isset($options['allowed_mime_types'])) { |
|
44
|
|
|
$this->allowed_mime_types = (array) $options['allowed_file_extensions']; |
|
45
|
|
|
} else { |
|
46
|
|
|
$this->allowed_mime_types = ['text/csv']; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$this->_set_display_strategy(new EE_File_Input_Display_Strategy()); |
|
50
|
|
|
$this->_set_normalization_strategy(new EE_File_Normalization()); |
|
51
|
|
|
$this->add_validation_strategy( |
|
52
|
|
|
new EE_Text_Validation_Strategy( |
|
53
|
|
|
sprintf( |
|
54
|
|
|
// translators: %1$s is a list of allowed file extensions. |
|
55
|
|
|
esc_html__('Please provide a file of the requested filetype: %1$s', 'event_espresso'), |
|
56
|
|
|
implode(', ', $this->allowed_file_extensions) |
|
57
|
|
|
), |
|
58
|
|
|
'~.*\.(' . implode('|', $this->allowed_file_extensions) . ')$~' |
|
59
|
|
|
) |
|
60
|
|
|
); |
|
61
|
|
|
parent::__construct($options); |
|
62
|
|
|
|
|
63
|
|
|
// It would be great to add this HTML attribute, but jQuery validate chokes on it. |
|
64
|
|
|
$this->set_other_html_attributes( |
|
65
|
|
|
$this->other_html_attributes() |
|
66
|
|
|
. ' extension="' |
|
67
|
|
|
. implode( |
|
68
|
|
|
',', |
|
69
|
|
|
$this->allowed_file_extensions |
|
70
|
|
|
) |
|
71
|
|
|
. '"' |
|
72
|
|
|
); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* $_FILES has a really weird structure. So we let `FilesDataHandler` take care of finding the file info for |
|
77
|
|
|
* this input. |
|
78
|
|
|
* @since $VID:$ |
|
79
|
|
|
* @param array $req_data |
|
80
|
|
|
* @return FileSubmissionInterface |
|
81
|
|
|
* @throws InvalidArgumentException |
|
82
|
|
|
* @throws InvalidDataTypeException |
|
83
|
|
|
* @throws InvalidInterfaceException |
|
84
|
|
|
*/ |
|
85
|
|
|
public function find_form_data_for_this_section($req_data) |
|
86
|
|
|
{ |
|
87
|
|
|
// ignore $req_data. Files are in the files data handler. |
|
88
|
|
|
$fileDataHandler = LoaderFactory::getLoader()->getShared( |
|
89
|
|
|
'EventEspresso\core\services\request\files\FilesDataHandler' |
|
90
|
|
|
); |
|
91
|
|
|
return $fileDataHandler->getFileObject($this->html_name()); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Don't transform the file submission object into a string, thanks. |
|
96
|
|
|
* |
|
97
|
|
|
* @param string $value |
|
98
|
|
|
* @return null|string |
|
99
|
|
|
*/ |
|
100
|
|
|
protected function _sanitize($value) |
|
101
|
|
|
{ |
|
102
|
|
|
return $value; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|