1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13: namespace Pry\Form\Element;
14:
15: use Pry\Form\Input;
16: use Pry\Form\Error;
17:
18: 19: 20: 21: 22: 23: 24: 25: 26: 27:
28: class File extends Input
29: {
30:
31: 32: 33: 34: 35: 36:
37: private $maxFileSize;
38:
39: 40: 41: 42: 43: 44:
45: private $extension;
46:
47: 48: 49: 50: 51:
52: private $name;
53:
54: 55: 56: 57: 58: 59:
60: private $nbInput;
61:
62: 63: 64: 65: 66: 67:
68: private $multiple;
69:
70: 71: 72: 73: 74: 75: 76:
77: public function __construct($nom, $form)
78: {
79: parent::__construct($nom, $form);
80: $this->name = $nom;
81: $this->nbInput = 1;
82: $this->multiple = false;
83: $this->attrs['type'] = 'file';
84: $this->form->enctype('multipart/form-data');
85: $this->maxFileSize = 2097152;
86: $this->extension = array('jpg', 'gif', 'png');
87: }
88:
89: 90: 91: 92: 93: 94: 95:
96: public function maxFileSize($size)
97: {
98: $this->maxFileSize = $size;
99: return $this;
100: }
101:
102: 103: 104: 105: 106: 107:
108: public function flushAllowedFileType()
109: {
110: $this->extension = array();
111: return $this;
112: }
113:
114: 115: 116: 117: 118: 119: 120:
121: public function multiple($nb = 1)
122: {
123: $this->nbInput = intval($nb);
124: $this->multiple = true;
125: $this->attrs['name'] = $this->attrs['name'] . '[]';
126: return $this;
127: }
128:
129: 130: 131: 132: 133: 134: 135:
136: public function allowFileType($newExt)
137: {
138: if (is_array($newExt))
139: {
140: foreach ($newExt as $value) {
141: if (!in_array($value, $this->extension))
142: $this->extension[] = $value;
143: }
144: }
145: else
146: if (!in_array($newExt, $this->extension))
147: $this->extension[] = $newExt;
148:
149: return $this;
150: }
151:
152: 153: 154: 155: 156: 157: 158:
159: public function isValid($value)
160: {
161: if (isset($_FILES[$this->name]))
162: {
163: if (!$this->multiple)
164: {
165: if (parent::isValid($_FILES[$this->name]['name']))
166: {
167: if (!$this->required && $_FILES[$this->name]['tmp_name'] == '')
168: {
169: return true;
170: }
171:
172: if ($this->maxFileSize < $_FILES[$this->name]['size'])
173: {
174: $this->errorMsg = Error::TOOBIG;
175: return false;
176: }
177:
178: if (!is_uploaded_file($_FILES[$this->name]['tmp_name']) || $_FILES[$this->name]['error'] != 0)
179: {
180: $this->errorMsg = Error::UPLOAD;
181: return false;
182: }
183:
184: if (!empty($this->extension))
185: {
186: $extension = pathinfo($_FILES[$this->name]['name'], PATHINFO_EXTENSION);
187: if (!in_array($extension, $this->extension))
188: {
189: $this->errorMsg = Error::EXT;
190: return false;
191: }
192: }
193: return true;
194: }
195: }
196: else
197: {
198: $return = true;
199: for ($i = 0; $i < $this->nbInput; $i++) {
200: if (parent::isValid($_FILES[$this->name]['name'][$i]))
201: {
202: if (!$this->required && $_FILES[$this->name]['tmp_name'][$i] == '')
203: {
204: $return = $return && true;
205: break;
206: }
207: else if ($this->required && $_FILES[$this->name]['tmp_name'][$i] == '')
208: {
209: $return = $return && false;
210: $this->errorMsg = Error::REQUIRED;
211: }
212:
213: if ($this->maxFileSize < $_FILES[$this->name]['size'][$i])
214: {
215: $this->errorMsg = Error::TOOBIG;
216: $return = $return && false;
217: }
218:
219: if (!is_uploaded_file($_FILES[$this->name]['tmp_name'][$i]) || $_FILES[$this->name]['error'][$i] != 0)
220: {
221: $this->errorMsg = Error::UPLOAD;
222: $return = $return && false;
223: }
224:
225: if (!empty($this->extension))
226: {
227: $extension = pathinfo($_FILES[$this->name]['name'][$i], PATHINFO_EXTENSION);
228: if (!in_array($extension, $this->extension))
229: {
230: $this->errorMsg = Error::EXT;
231: $return = $return && false;
232: }
233: }
234: $return = $return && true;
235: }
236: else
237: {
238: $return = $return && false;
239: }
240: }
241: return $return;
242: }
243: }
244: return false;
245: }
246:
247: 248: 249: 250: 251: 252:
253: public function __toString()
254: {
255: $css = $this->cssClass();
256: $label = '';
257:
258: if (!empty($this->label))
259: {
260: $label = "\t" . '<label for="' . $this->attrs['id'] . '" class="' . $this->cssLabel . '">' . $this->label . '</label>' . "\n";
261: if ($this->labelNewLine)
262: $label.="\t" . '<br />' . "\n";
263: }
264:
265: $attributs = $this->attrsToString();
266: $field = "\t" . '<input ' . $css . ' ' . $attributs . ' />' . "\n";
267:
268: if (!empty($this->info))
269: $field.="\t" . '<img src="' . $this->imgInfo . '" id="' . $this->attrs['name'] . '_tooltip" class="form_tooltip" title="' . $this->info . '" alt="" style="cursor:help;" />';
270:
271: if ($this->multiple && $this->nbInput > 1)
272: {
273: for ($i = 0; $i < ($this->nbInput - 1); $i++) {
274: $field.="\t" . '<br /><input ' . $css . ' ' . $attributs . ' />' . "\n";
275: }
276: }
277:
278: $field.="\t" . '<input type="hidden" name="MAX_FILE_SIZE" value="' . $this->maxFileSize . '" />' . "\n";
279: if ($this->fieldNewLine)
280: $field.="\t" . '<br />' . "\n";
281: $error = '';
282:
283: if (!is_null($this->errorMsg))
284: {
285: $error = '<span class="' . $this->errorClass . '">' . $this->errorMsg . '</span><br />';
286: }
287: return $label . $field . $error;
288: }
289:
290: }
291:
292: ?>