Completed
Branch FET/attendee-importer (7f98c3)
by
unknown
45:19 queued 36:19
created

EE_File_Input::getFileDataNameParts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
use EventEspresso\core\services\loaders\LoaderFactory;
4
5
/**
6
 * EE_File_Input
7
 *
8
 * For uploading a file
9
 * Note: if you will be using this in a form, make sure it uses `enctype="multipart/form-data"`, and that the request
10
 * data is `array_merge($_REQUEST, $_FILES)`.
11
 *
12
 * @package         Event Espresso
13
 * @subpackage
14
 * @author              Mike Nelson
15
 *
16
 * This input has a default validation strategy of plaintext (which can be removed after construction)
17
 */
18
class EE_File_Input extends EE_Form_Input_Base
19
{
20
    /**
21
     * @var array
22
     */
23
    protected $allowed_file_extensions;
24
25
    /**
26
     * @var
27
     */
28
    protected $allowed_mime_types;
29
30
    /**
31
     * @param array $options
32
     * @throws InvalidArgumentException
33
     */
34
    public function __construct($options = array())
35
    {
36 View Code Duplication
        if (isset($options['allowed_file_extensions'])) {
37
            if (!is_array($options['allowed_file_extensions'])) {
38
                throw new InvalidArgumentException(esc_html__('A valid allowed_file_extensions array was not provided to EE_File_Input', 'event_espresso'));
39
            }
40
            $this->allowed_file_extensions = $options['allowed_file_extensions'];
41
        } else {
42
            $this->allowed_file_extensions = ['csv'];
43
        }
44 View Code Duplication
        if (isset($options['allowed_mime_types'])) {
45
            if (!is_array($options['allowed_mime_types'])) {
46
                throw new InvalidArgumentException(esc_html__('A valid allowed_mime_types array was not provided to EE_File_Input', 'event_espresso'));
47
            }
48
            $this->allowed_mime_types = $options['allowed_file_extensions'];
49
        } else {
50
            $this->allowed_mime_types = ['text/csv'];
51
        }
52
53
        $this->_set_display_strategy(new EE_File_Input_Display_Strategy());
54
        $this->_set_normalization_strategy(new EE_File_Normalization());
55
        $this->add_validation_strategy(
56
            new EE_Text_Validation_Strategy(
57
                sprintf(
58
                // translators: %1$s is a list of allowed file extensions.
59
                    esc_html__('Please provide a file of the requested filetype: %1$s', 'event_espresso'),
60
                    implode(', ', $this->allowed_file_extensions)
61
                ),
62
                '~.*\.(' . implode('|', $this->allowed_file_extensions) . ')$~'
63
            )
64
        );
65
        parent::__construct($options);
66
67
//        It would be great to add this HTML attribute, but jQuery validate chokes on it.
68
        $this->set_other_html_attributes(
69
            $this->other_html_attributes()
70
            . ' extension="'
71
            . implode(
72
                ',',
73
//                array_merge(
74
//                    array_map(
75
//                        function ($mime_type) {
76
//                            if(strpos($mime_type, '/') === false) {
77
//                                return $mime_type . '/*';
78
//                            } else {
79
//                                return $mime_type;
80
//                            }
81
//
82
//                        },
83
//                        $this->allowed_mime_types
84
//                    )
85
                    array_map(
86
                        function ($file_extension) {
87
                            return  $file_extension;
88
                        },
89
                        $this->allowed_file_extensions
90
                    )
91
//                )
92
            )
93
            . '"'
94
        );
95
    }
96
97
    /**
98
     * Takes into account that $_FILES does a weird thing when you have hierarchical form names (eg `<input type="file"
99
     * name="my[hierarchical][form]">`): it leaves the top-level form part alone, but replaces the SECOND part with
100
     * "name", "size", "temp_file", etc. So that file's data is located at "my[name][hierarchical][form]",
101
     * "my[size][hierarchical][form]", "my[temp_name][hierarchical][form]", etc. It's really weird.
102
     * @since $VID:$
103
     * @param array $req_data
104
     * @return array|mixed|NULL
105
     * @throws EE_Error
106
     */
107
    public function find_form_data_for_this_section($req_data)
108
    {
109
        // ignore $req_data. Files are in the files data handler.
110
        $fileDataHandler = LoaderFactory::getLoader()->getShared('EventEspresso\core\services\request\files\FilesDataHandler');
111
        return $fileDataHandler->getFileObject($this->html_name());
112
    }
113
}
114