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