1 <?php
2 /**
3 * FileInput.php
4 *
5 * @author Elvyrra SAS
6 * @license http://rem.mit-license.org/ MIT
7 */
8
9 namespace Hawk;
10
11 /**
12 * This class describes the file inputs behavior
13 *
14 * @package Form\Input
15 */
16 class FileInput extends FormInput{
17 const TYPE = "file";
18
19 // This type of file is independant, no data is get or set in the database
20 const INDEPENDANT = true;
21
22 /**
23 * Defines the 'multiple' attribute of the input
24 *
25 * @var boolean
26 */
27 public $multiple = false;
28
29 /**
30 * Defines which extensions are allowed to be uploaded with this input
31 *
32 * @var array
33 */
34 public $extensions = array();
35
36
37 /**
38 * Display the file input
39 *
40 * @return string The displayed HTML
41 */
42 public function display(){
43 $this->value = '';
44
45 return parent::display();
46 }
47
48 /**
49 * Check the submitted value of the input
50 *
51 * @param Form $form The form the input is associated with
52 *
53 * @return boolean true if the uploaded files are correct, else false
54 */
55 public function check(&$form = null){
56 if(empty($this->errorAt)) {
57 $this->errorAt = $this->name;
58 }
59
60 $basename = preg_replace("/^(\w+)(\[.*)?$/", "$1", $this->name);
61 $upload = Upload::getInstance($basename);
62
63 if($this->required && !$upload) {
64 // No file were uploaded
65 $form->error($this->errorAt, Lang::get('form.required-field'));
66 return false;
67 }
68
69 if($upload && $this->extensions) {
70 foreach($upload->getFiles() as $file){
71 if(!in_array($file->extension, $this->extensions)) {
72 // One of the uploaded files has no good extension
73 $form && $form->error($this->errorAt, Lang::get('form.invalid-file-extension'));
74 return false;
75 }
76 }
77 }
78 return true;
79 }
80 }
81