|
1 <?php |
|
2 /** |
|
3 * Zend Framework |
|
4 * |
|
5 * LICENSE |
|
6 * |
|
7 * This source file is subject to the new BSD license that is bundled |
|
8 * with this package in the file LICENSE.txt. |
|
9 * It is also available through the world-wide-web at this URL: |
|
10 * http://framework.zend.com/license/new-bsd |
|
11 * If you did not receive a copy of the license and are unable to |
|
12 * obtain it through the world-wide-web, please send an email |
|
13 * to license@zend.com so we can send you a copy immediately. |
|
14 * |
|
15 * @category Zend |
|
16 * @package Zend_Form |
|
17 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
18 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
19 */ |
|
20 |
|
21 /** Zend_Form_Element_Xhtml */ |
|
22 require_once 'Zend/Form/Element/Xhtml.php'; |
|
23 |
|
24 /** |
|
25 * Zend_Form_Element |
|
26 * |
|
27 * @category Zend |
|
28 * @package Zend_Form |
|
29 * @subpackage Element |
|
30 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
31 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
32 * @version $Id: File.php 22371 2010-06-04 20:09:44Z thomas $ |
|
33 */ |
|
34 class Zend_Form_Element_File extends Zend_Form_Element_Xhtml |
|
35 { |
|
36 /** |
|
37 * Plugin loader type |
|
38 */ |
|
39 const TRANSFER_ADAPTER = 'TRANSFER_ADAPTER'; |
|
40 |
|
41 /** |
|
42 * @var string Default view helper |
|
43 */ |
|
44 public $helper = 'formFile'; |
|
45 |
|
46 /** |
|
47 * @var Zend_File_Transfer_Adapter_Abstract |
|
48 */ |
|
49 protected $_adapter; |
|
50 |
|
51 /** |
|
52 * @var boolean Already validated ? |
|
53 */ |
|
54 protected $_validated = false; |
|
55 |
|
56 /** |
|
57 * @var boolean Disable value to be equal to file content |
|
58 */ |
|
59 protected $_valueDisabled = false; |
|
60 |
|
61 /** |
|
62 * @var integer Internal multifile counter |
|
63 */ |
|
64 protected $_counter = 1; |
|
65 |
|
66 /** |
|
67 * @var integer Maximum file size for MAX_FILE_SIZE attribut of form |
|
68 */ |
|
69 protected static $_maxFileSize = -1; |
|
70 |
|
71 /** |
|
72 * Load default decorators |
|
73 * |
|
74 * @return void |
|
75 */ |
|
76 public function loadDefaultDecorators() |
|
77 { |
|
78 if ($this->loadDefaultDecoratorsIsDisabled()) { |
|
79 return $this; |
|
80 } |
|
81 |
|
82 $decorators = $this->getDecorators(); |
|
83 if (empty($decorators)) { |
|
84 $this->addDecorator('File') |
|
85 ->addDecorator('Errors') |
|
86 ->addDecorator('Description', array('tag' => 'p', 'class' => 'description')) |
|
87 ->addDecorator('HtmlTag', array('tag' => 'dd')) |
|
88 ->addDecorator('Label', array('tag' => 'dt')); |
|
89 } |
|
90 return $this; |
|
91 } |
|
92 |
|
93 /** |
|
94 * Set plugin loader |
|
95 * |
|
96 * @param Zend_Loader_PluginLoader_Interface $loader |
|
97 * @param string $type |
|
98 * @return Zend_Form_Element_File |
|
99 */ |
|
100 public function setPluginLoader(Zend_Loader_PluginLoader_Interface $loader, $type) |
|
101 { |
|
102 $type = strtoupper($type); |
|
103 |
|
104 if ($type != self::TRANSFER_ADAPTER) { |
|
105 return parent::setPluginLoader($loader, $type); |
|
106 } |
|
107 |
|
108 $this->_loaders[$type] = $loader; |
|
109 return $this; |
|
110 } |
|
111 |
|
112 /** |
|
113 * Get Plugin Loader |
|
114 * |
|
115 * @param string $type |
|
116 * @return Zend_Loader_PluginLoader_Interface |
|
117 */ |
|
118 public function getPluginLoader($type) |
|
119 { |
|
120 $type = strtoupper($type); |
|
121 |
|
122 if ($type != self::TRANSFER_ADAPTER) { |
|
123 return parent::getPluginLoader($type); |
|
124 } |
|
125 |
|
126 if (!array_key_exists($type, $this->_loaders)) { |
|
127 require_once 'Zend/Loader/PluginLoader.php'; |
|
128 $loader = new Zend_Loader_PluginLoader(array( |
|
129 'Zend_File_Transfer_Adapter' => 'Zend/File/Transfer/Adapter/', |
|
130 )); |
|
131 $this->setPluginLoader($loader, self::TRANSFER_ADAPTER); |
|
132 } |
|
133 |
|
134 return $this->_loaders[$type]; |
|
135 } |
|
136 |
|
137 /** |
|
138 * Add prefix path for plugin loader |
|
139 * |
|
140 * @param string $prefix |
|
141 * @param string $path |
|
142 * @param string $type |
|
143 * @return Zend_Form_Element_File |
|
144 */ |
|
145 public function addPrefixPath($prefix, $path, $type = null) |
|
146 { |
|
147 $type = strtoupper($type); |
|
148 if (!empty($type) && ($type != self::TRANSFER_ADAPTER)) { |
|
149 return parent::addPrefixPath($prefix, $path, $type); |
|
150 } |
|
151 |
|
152 if (empty($type)) { |
|
153 $pluginPrefix = rtrim($prefix, '_') . '_Transfer_Adapter'; |
|
154 $pluginPath = rtrim($path, DIRECTORY_SEPARATOR) . '/Transfer/Adapter/'; |
|
155 $loader = $this->getPluginLoader(self::TRANSFER_ADAPTER); |
|
156 $loader->addPrefixPath($pluginPrefix, $pluginPath); |
|
157 return parent::addPrefixPath($prefix, $path, null); |
|
158 } |
|
159 |
|
160 $loader = $this->getPluginLoader($type); |
|
161 $loader->addPrefixPath($prefix, $path); |
|
162 return $this; |
|
163 } |
|
164 |
|
165 /** |
|
166 * Set transfer adapter |
|
167 * |
|
168 * @param string|Zend_File_Transfer_Adapter_Abstract $adapter |
|
169 * @return Zend_Form_Element_File |
|
170 */ |
|
171 public function setTransferAdapter($adapter) |
|
172 { |
|
173 if ($adapter instanceof Zend_File_Transfer_Adapter_Abstract) { |
|
174 $this->_adapter = $adapter; |
|
175 } elseif (is_string($adapter)) { |
|
176 $loader = $this->getPluginLoader(self::TRANSFER_ADAPTER); |
|
177 $class = $loader->load($adapter); |
|
178 $this->_adapter = new $class; |
|
179 } else { |
|
180 require_once 'Zend/Form/Element/Exception.php'; |
|
181 throw new Zend_Form_Element_Exception('Invalid adapter specified'); |
|
182 } |
|
183 |
|
184 foreach (array('filter', 'validate') as $type) { |
|
185 $loader = $this->getPluginLoader($type); |
|
186 $this->_adapter->setPluginLoader($loader, $type); |
|
187 } |
|
188 |
|
189 return $this; |
|
190 } |
|
191 |
|
192 /** |
|
193 * Get transfer adapter |
|
194 * |
|
195 * Lazy loads HTTP transfer adapter when no adapter registered. |
|
196 * |
|
197 * @return Zend_File_Transfer_Adapter_Abstract |
|
198 */ |
|
199 public function getTransferAdapter() |
|
200 { |
|
201 if (null === $this->_adapter) { |
|
202 $this->setTransferAdapter('Http'); |
|
203 } |
|
204 return $this->_adapter; |
|
205 } |
|
206 |
|
207 /** |
|
208 * Add Validator; proxy to adapter |
|
209 * |
|
210 * @param string|Zend_Validate_Interface $validator |
|
211 * @param bool $breakChainOnFailure |
|
212 * @param mixed $options |
|
213 * @return Zend_Form_Element_File |
|
214 */ |
|
215 public function addValidator($validator, $breakChainOnFailure = false, $options = array()) |
|
216 { |
|
217 $adapter = $this->getTransferAdapter(); |
|
218 $adapter->addValidator($validator, $breakChainOnFailure, $options, $this->getName()); |
|
219 $this->_validated = false; |
|
220 |
|
221 return $this; |
|
222 } |
|
223 |
|
224 /** |
|
225 * Add multiple validators at once; proxy to adapter |
|
226 * |
|
227 * @param array $validators |
|
228 * @return Zend_Form_Element_File |
|
229 */ |
|
230 public function addValidators(array $validators) |
|
231 { |
|
232 $adapter = $this->getTransferAdapter(); |
|
233 $adapter->addValidators($validators, $this->getName()); |
|
234 $this->_validated = false; |
|
235 |
|
236 return $this; |
|
237 } |
|
238 |
|
239 /** |
|
240 * Add multiple validators at once, overwriting; proxy to adapter |
|
241 * |
|
242 * @param array $validators |
|
243 * @return Zend_Form_Element_File |
|
244 */ |
|
245 public function setValidators(array $validators) |
|
246 { |
|
247 $adapter = $this->getTransferAdapter(); |
|
248 $adapter->setValidators($validators, $this->getName()); |
|
249 $this->_validated = false; |
|
250 |
|
251 return $this; |
|
252 } |
|
253 |
|
254 /** |
|
255 * Retrieve validator by name; proxy to adapter |
|
256 * |
|
257 * @param string $name |
|
258 * @return Zend_Validate_Interface|null |
|
259 */ |
|
260 public function getValidator($name) |
|
261 { |
|
262 $adapter = $this->getTransferAdapter(); |
|
263 return $adapter->getValidator($name); |
|
264 } |
|
265 |
|
266 /** |
|
267 * Retrieve all validators; proxy to adapter |
|
268 * |
|
269 * @return array |
|
270 */ |
|
271 public function getValidators() |
|
272 { |
|
273 $adapter = $this->getTransferAdapter(); |
|
274 $validators = $adapter->getValidators($this->getName()); |
|
275 if ($validators === null) { |
|
276 $validators = array(); |
|
277 } |
|
278 |
|
279 return $validators; |
|
280 } |
|
281 |
|
282 /** |
|
283 * Remove validator by name; proxy to adapter |
|
284 * |
|
285 * @param string $name |
|
286 * @return Zend_Form_Element_File |
|
287 */ |
|
288 public function removeValidator($name) |
|
289 { |
|
290 $adapter = $this->getTransferAdapter(); |
|
291 $adapter->removeValidator($name); |
|
292 $this->_validated = false; |
|
293 |
|
294 return $this; |
|
295 } |
|
296 |
|
297 /** |
|
298 * Remove all validators; proxy to adapter |
|
299 * |
|
300 * @return Zend_Form_Element_File |
|
301 */ |
|
302 public function clearValidators() |
|
303 { |
|
304 $adapter = $this->getTransferAdapter(); |
|
305 $adapter->clearValidators(); |
|
306 $this->_validated = false; |
|
307 |
|
308 return $this; |
|
309 } |
|
310 |
|
311 /** |
|
312 * Add Filter; proxy to adapter |
|
313 * |
|
314 * @param string|array $filter Type of filter to add |
|
315 * @param string|array $options Options to set for the filter |
|
316 * @return Zend_Form_Element_File |
|
317 */ |
|
318 public function addFilter($filter, $options = null) |
|
319 { |
|
320 $adapter = $this->getTransferAdapter(); |
|
321 $adapter->addFilter($filter, $options, $this->getName()); |
|
322 |
|
323 return $this; |
|
324 } |
|
325 |
|
326 /** |
|
327 * Add Multiple filters at once; proxy to adapter |
|
328 * |
|
329 * @param array $filters |
|
330 * @return Zend_Form_Element_File |
|
331 */ |
|
332 public function addFilters(array $filters) |
|
333 { |
|
334 $adapter = $this->getTransferAdapter(); |
|
335 $adapter->addFilters($filters, $this->getName()); |
|
336 |
|
337 return $this; |
|
338 } |
|
339 |
|
340 /** |
|
341 * Sets a filter for the class, erasing all previous set; proxy to adapter |
|
342 * |
|
343 * @param string|array $filter Filter to set |
|
344 * @return Zend_Form_Element_File |
|
345 */ |
|
346 public function setFilters(array $filters) |
|
347 { |
|
348 $adapter = $this->getTransferAdapter(); |
|
349 $adapter->setFilters($filters, $this->getName()); |
|
350 |
|
351 return $this; |
|
352 } |
|
353 |
|
354 /** |
|
355 * Retrieve individual filter; proxy to adapter |
|
356 * |
|
357 * @param string $name |
|
358 * @return Zend_Filter_Interface|null |
|
359 */ |
|
360 public function getFilter($name) |
|
361 { |
|
362 $adapter = $this->getTransferAdapter(); |
|
363 return $adapter->getFilter($name); |
|
364 } |
|
365 |
|
366 /** |
|
367 * Returns all set filters; proxy to adapter |
|
368 * |
|
369 * @return array List of set filters |
|
370 */ |
|
371 public function getFilters() |
|
372 { |
|
373 $adapter = $this->getTransferAdapter(); |
|
374 $filters = $adapter->getFilters($this->getName()); |
|
375 |
|
376 if ($filters === null) { |
|
377 $filters = array(); |
|
378 } |
|
379 return $filters; |
|
380 } |
|
381 |
|
382 /** |
|
383 * Remove an individual filter; proxy to adapter |
|
384 * |
|
385 * @param string $name |
|
386 * @return Zend_Form_Element_File |
|
387 */ |
|
388 public function removeFilter($name) |
|
389 { |
|
390 $adapter = $this->getTransferAdapter(); |
|
391 $adapter->removeFilter($name); |
|
392 |
|
393 return $this; |
|
394 } |
|
395 |
|
396 /** |
|
397 * Remove all filters; proxy to adapter |
|
398 * |
|
399 * @return Zend_Form_Element_File |
|
400 */ |
|
401 public function clearFilters() |
|
402 { |
|
403 $adapter = $this->getTransferAdapter(); |
|
404 $adapter->clearFilters(); |
|
405 |
|
406 return $this; |
|
407 } |
|
408 |
|
409 /** |
|
410 * Validate upload |
|
411 * |
|
412 * @param string $value File, can be optional, give null to validate all files |
|
413 * @param mixed $context |
|
414 * @return bool |
|
415 */ |
|
416 public function isValid($value, $context = null) |
|
417 { |
|
418 if ($this->_validated) { |
|
419 return true; |
|
420 } |
|
421 |
|
422 $adapter = $this->getTransferAdapter(); |
|
423 $translator = $this->getTranslator(); |
|
424 if ($translator !== null) { |
|
425 $adapter->setTranslator($translator); |
|
426 } |
|
427 |
|
428 if (!$this->isRequired()) { |
|
429 $adapter->setOptions(array('ignoreNoFile' => true), $this->getName()); |
|
430 } else { |
|
431 $adapter->setOptions(array('ignoreNoFile' => false), $this->getName()); |
|
432 if ($this->autoInsertNotEmptyValidator() && !$this->getValidator('NotEmpty')) { |
|
433 $this->addValidator = array('validator' => 'NotEmpty', 'breakChainOnFailure' => true); |
|
434 } |
|
435 } |
|
436 |
|
437 if($adapter->isValid($this->getName())) { |
|
438 $this->_validated = true; |
|
439 return true; |
|
440 } |
|
441 |
|
442 $this->_validated = false; |
|
443 return false; |
|
444 } |
|
445 |
|
446 /** |
|
447 * Receive the uploaded file |
|
448 * |
|
449 * @return boolean |
|
450 */ |
|
451 public function receive() |
|
452 { |
|
453 if (!$this->_validated) { |
|
454 if (!$this->isValid($this->getName())) { |
|
455 return false; |
|
456 } |
|
457 } |
|
458 |
|
459 $adapter = $this->getTransferAdapter(); |
|
460 if ($adapter->receive($this->getName())) { |
|
461 return true; |
|
462 } |
|
463 |
|
464 return false; |
|
465 } |
|
466 |
|
467 /** |
|
468 * Retrieve error codes; proxy to transfer adapter |
|
469 * |
|
470 * @return array |
|
471 */ |
|
472 public function getErrors() |
|
473 { |
|
474 return parent::getErrors() + $this->getTransferAdapter()->getErrors(); |
|
475 } |
|
476 |
|
477 /** |
|
478 * Are there errors registered? |
|
479 * |
|
480 * @return bool |
|
481 */ |
|
482 public function hasErrors() |
|
483 { |
|
484 return (parent::hasErrors() || $this->getTransferAdapter()->hasErrors()); |
|
485 } |
|
486 |
|
487 /** |
|
488 * Retrieve error messages; proxy to transfer adapter |
|
489 * |
|
490 * @return array |
|
491 */ |
|
492 public function getMessages() |
|
493 { |
|
494 return parent::getMessages() + $this->getTransferAdapter()->getMessages(); |
|
495 } |
|
496 |
|
497 /** |
|
498 * Set the upload destination |
|
499 * |
|
500 * @param string $path |
|
501 * @return Zend_Form_Element_File |
|
502 */ |
|
503 public function setDestination($path) |
|
504 { |
|
505 $this->getTransferAdapter()->setDestination($path, $this->getName()); |
|
506 return $this; |
|
507 } |
|
508 |
|
509 /** |
|
510 * Get the upload destination |
|
511 * |
|
512 * @return string |
|
513 */ |
|
514 public function getDestination() |
|
515 { |
|
516 return $this->getTransferAdapter()->getDestination($this->getName()); |
|
517 } |
|
518 |
|
519 /** |
|
520 * Get the final filename |
|
521 * |
|
522 * @param string $value (Optional) Element or file to return |
|
523 * @param boolean $path (Optional) Return also the path, defaults to true |
|
524 * @return string |
|
525 */ |
|
526 public function getFileName($value = null, $path = true) |
|
527 { |
|
528 if (empty($value)) { |
|
529 $value = $this->getName(); |
|
530 } |
|
531 |
|
532 return $this->getTransferAdapter()->getFileName($value, $path); |
|
533 } |
|
534 |
|
535 /** |
|
536 * Get internal file informations |
|
537 * |
|
538 * @param string $value (Optional) Element or file to return |
|
539 * @return array |
|
540 */ |
|
541 public function getFileInfo($value = null) |
|
542 { |
|
543 if (empty($value)) { |
|
544 $value = $this->getName(); |
|
545 } |
|
546 |
|
547 return $this->getTransferAdapter()->getFileInfo($value); |
|
548 } |
|
549 |
|
550 /** |
|
551 * Set a multifile element |
|
552 * |
|
553 * @param integer $count Number of file elements |
|
554 * @return Zend_Form_Element_File Provides fluent interface |
|
555 */ |
|
556 public function setMultiFile($count) |
|
557 { |
|
558 if ((integer) $count < 2) { |
|
559 $this->setIsArray(false); |
|
560 $this->_counter = 1; |
|
561 } else { |
|
562 $this->setIsArray(true); |
|
563 $this->_counter = (integer) $count; |
|
564 } |
|
565 |
|
566 return $this; |
|
567 } |
|
568 |
|
569 /** |
|
570 * Returns the multifile element number |
|
571 * |
|
572 * @return integer |
|
573 */ |
|
574 public function getMultiFile() |
|
575 { |
|
576 return $this->_counter; |
|
577 } |
|
578 |
|
579 /** |
|
580 * Sets the maximum file size of the form |
|
581 * |
|
582 * @return integer |
|
583 */ |
|
584 public function getMaxFileSize() |
|
585 { |
|
586 if (self::$_maxFileSize < 0) { |
|
587 $ini = $this->_convertIniToInteger(trim(ini_get('post_max_size'))); |
|
588 $max = $this->_convertIniToInteger(trim(ini_get('upload_max_filesize'))); |
|
589 $min = max($ini, $max); |
|
590 if ($ini > 0) { |
|
591 $min = min($min, $ini); |
|
592 } |
|
593 |
|
594 if ($max > 0) { |
|
595 $min = min($min, $max); |
|
596 } |
|
597 |
|
598 self::$_maxFileSize = $min; |
|
599 } |
|
600 |
|
601 return self::$_maxFileSize; |
|
602 } |
|
603 |
|
604 /** |
|
605 * Sets the maximum file size of the form |
|
606 * |
|
607 * @param integer $size |
|
608 * @return integer |
|
609 */ |
|
610 public function setMaxFileSize($size) |
|
611 { |
|
612 $ini = $this->_convertIniToInteger(trim(ini_get('post_max_size'))); |
|
613 $max = $this->_convertIniToInteger(trim(ini_get('upload_max_filesize'))); |
|
614 |
|
615 if (($max > -1) && ($size > $max)) { |
|
616 trigger_error("Your 'upload_max_filesize' config setting limits the maximum filesize to '$max'. You tried to set '$size'.", E_USER_NOTICE); |
|
617 $size = $max; |
|
618 } |
|
619 |
|
620 if (($ini > -1) && ($size > $ini)) { |
|
621 trigger_error("Your 'post_max_size' config setting limits the maximum filesize to '$ini'. You tried to set '$size'.", E_USER_NOTICE); |
|
622 $size = $ini; |
|
623 } |
|
624 |
|
625 self::$_maxFileSize = $size; |
|
626 return $this; |
|
627 } |
|
628 |
|
629 /** |
|
630 * Converts a ini setting to a integer value |
|
631 * |
|
632 * @param string $setting |
|
633 * @return integer |
|
634 */ |
|
635 private function _convertIniToInteger($setting) |
|
636 { |
|
637 if (!is_numeric($setting)) { |
|
638 $type = strtoupper(substr($setting, -1)); |
|
639 $setting = (integer) substr($setting, 0, -1); |
|
640 |
|
641 switch ($type) { |
|
642 case 'K' : |
|
643 $setting *= 1024; |
|
644 break; |
|
645 |
|
646 case 'M' : |
|
647 $setting *= 1024 * 1024; |
|
648 break; |
|
649 |
|
650 case 'G' : |
|
651 $setting *= 1024 * 1024 * 1024; |
|
652 break; |
|
653 |
|
654 default : |
|
655 break; |
|
656 } |
|
657 } |
|
658 |
|
659 return (integer) $setting; |
|
660 } |
|
661 |
|
662 /** |
|
663 * Set if the file will be uploaded when getting the value |
|
664 * This defaults to false which will force receive() when calling getValues() |
|
665 * |
|
666 * @param boolean $flag Sets if the file is handled as the elements value |
|
667 * @return Zend_Form_Element_File |
|
668 */ |
|
669 public function setValueDisabled($flag) |
|
670 { |
|
671 $this->_valueDisabled = (bool) $flag; |
|
672 return $this; |
|
673 } |
|
674 |
|
675 /** |
|
676 * Returns if the file will be uploaded when calling getValues() |
|
677 * |
|
678 * @return boolean Receive the file on calling getValues()? |
|
679 */ |
|
680 public function isValueDisabled() |
|
681 { |
|
682 return $this->_valueDisabled; |
|
683 } |
|
684 |
|
685 /** |
|
686 * Processes the file, returns null or the filename only |
|
687 * For the complete path, use getFileName |
|
688 * |
|
689 * @return null|string |
|
690 */ |
|
691 public function getValue() |
|
692 { |
|
693 if ($this->_value !== null) { |
|
694 return $this->_value; |
|
695 } |
|
696 |
|
697 $content = $this->getTransferAdapter()->getFileName($this->getName()); |
|
698 if (empty($content)) { |
|
699 return null; |
|
700 } |
|
701 |
|
702 if (!$this->isValid(null)) { |
|
703 return null; |
|
704 } |
|
705 |
|
706 if (!$this->_valueDisabled && !$this->receive()) { |
|
707 return null; |
|
708 } |
|
709 |
|
710 return $this->getFileName(null, false); |
|
711 } |
|
712 |
|
713 /** |
|
714 * Disallow setting the value |
|
715 * |
|
716 * @param mixed $value |
|
717 * @return Zend_Form_Element_File |
|
718 */ |
|
719 public function setValue($value) |
|
720 { |
|
721 return $this; |
|
722 } |
|
723 |
|
724 /** |
|
725 * Set translator object for localization |
|
726 * |
|
727 * @param Zend_Translate|null $translator |
|
728 * @return Zend_Form_Element_File |
|
729 */ |
|
730 public function setTranslator($translator = null) |
|
731 { |
|
732 $adapter = $this->getTransferAdapter(); |
|
733 $adapter->setTranslator($translator); |
|
734 parent::setTranslator($translator); |
|
735 |
|
736 return $this; |
|
737 } |
|
738 |
|
739 /** |
|
740 * Retrieve localization translator object |
|
741 * |
|
742 * @return Zend_Translate_Adapter|null |
|
743 */ |
|
744 public function getTranslator() |
|
745 { |
|
746 if ($this->translatorIsDisabled()) { |
|
747 return null; |
|
748 } |
|
749 |
|
750 $translator = $this->getTransferAdapter()->getTranslator(); |
|
751 if (null === $translator) { |
|
752 require_once 'Zend/Form.php'; |
|
753 return Zend_Form::getDefaultTranslator(); |
|
754 } |
|
755 |
|
756 return $translator; |
|
757 } |
|
758 |
|
759 /** |
|
760 * Indicate whether or not translation should be disabled |
|
761 * |
|
762 * @param bool $flag |
|
763 * @return Zend_Form_Element_File |
|
764 */ |
|
765 public function setDisableTranslator($flag) |
|
766 { |
|
767 $adapter = $this->getTransferAdapter(); |
|
768 $adapter->setDisableTranslator($flag); |
|
769 $this->_translatorDisabled = (bool) $flag; |
|
770 |
|
771 return $this; |
|
772 } |
|
773 |
|
774 /** |
|
775 * Is translation disabled? |
|
776 * |
|
777 * @return bool |
|
778 */ |
|
779 public function translatorIsDisabled() |
|
780 { |
|
781 $adapter = $this->getTransferAdapter(); |
|
782 return $adapter->translatorIsDisabled(); |
|
783 } |
|
784 |
|
785 /** |
|
786 * Was the file received? |
|
787 * |
|
788 * @return bool |
|
789 */ |
|
790 public function isReceived() |
|
791 { |
|
792 $adapter = $this->getTransferAdapter(); |
|
793 return $adapter->isReceived($this->getName()); |
|
794 } |
|
795 |
|
796 /** |
|
797 * Was the file uploaded? |
|
798 * |
|
799 * @return bool |
|
800 */ |
|
801 public function isUploaded() |
|
802 { |
|
803 $adapter = $this->getTransferAdapter(); |
|
804 return $adapter->isUploaded($this->getName()); |
|
805 } |
|
806 |
|
807 /** |
|
808 * Has the file been filtered? |
|
809 * |
|
810 * @return bool |
|
811 */ |
|
812 public function isFiltered() |
|
813 { |
|
814 $adapter = $this->getTransferAdapter(); |
|
815 return $adapter->isFiltered($this->getName()); |
|
816 } |
|
817 |
|
818 /** |
|
819 * Returns the hash for this file element |
|
820 * |
|
821 * @param string $hash (Optional) Hash algorithm to use |
|
822 * @return string|array Hashstring |
|
823 */ |
|
824 public function getHash($hash = 'crc32') |
|
825 { |
|
826 $adapter = $this->getTransferAdapter(); |
|
827 return $adapter->getHash($hash, $this->getName()); |
|
828 } |
|
829 |
|
830 /** |
|
831 * Returns the filesize for this file element |
|
832 * |
|
833 * @return string|array Filesize |
|
834 */ |
|
835 public function getFileSize() |
|
836 { |
|
837 $adapter = $this->getTransferAdapter(); |
|
838 return $adapter->getFileSize($this->getName()); |
|
839 } |
|
840 |
|
841 /** |
|
842 * Returns the mimetype for this file element |
|
843 * |
|
844 * @return string|array Mimetype |
|
845 */ |
|
846 public function getMimeType() |
|
847 { |
|
848 $adapter = $this->getTransferAdapter(); |
|
849 return $adapter->getMimeType($this->getName()); |
|
850 } |
|
851 |
|
852 /** |
|
853 * Render form element |
|
854 * Checks for decorator interface to prevent errors |
|
855 * |
|
856 * @param Zend_View_Interface $view |
|
857 * @return string |
|
858 */ |
|
859 public function render(Zend_View_Interface $view = null) |
|
860 { |
|
861 $marker = false; |
|
862 foreach ($this->getDecorators() as $decorator) { |
|
863 if ($decorator instanceof Zend_Form_Decorator_Marker_File_Interface) { |
|
864 $marker = true; |
|
865 } |
|
866 } |
|
867 |
|
868 if (!$marker) { |
|
869 require_once 'Zend/Form/Element/Exception.php'; |
|
870 throw new Zend_Form_Element_Exception('No file decorator found... unable to render file element'); |
|
871 } |
|
872 |
|
873 return parent::render($view); |
|
874 } |
|
875 |
|
876 /** |
|
877 * Retrieve error messages and perform translation and value substitution |
|
878 * |
|
879 * @return array |
|
880 */ |
|
881 protected function _getErrorMessages() |
|
882 { |
|
883 $translator = $this->getTranslator(); |
|
884 $messages = $this->getErrorMessages(); |
|
885 $value = $this->getFileName(); |
|
886 foreach ($messages as $key => $message) { |
|
887 if (null !== $translator) { |
|
888 $message = $translator->translate($message); |
|
889 } |
|
890 |
|
891 if ($this->isArray() || is_array($value)) { |
|
892 $aggregateMessages = array(); |
|
893 foreach ($value as $val) { |
|
894 $aggregateMessages[] = str_replace('%value%', $val, $message); |
|
895 } |
|
896 |
|
897 if (!empty($aggregateMessages)) { |
|
898 $messages[$key] = $aggregateMessages; |
|
899 } |
|
900 } else { |
|
901 $messages[$key] = str_replace('%value%', $value, $message); |
|
902 } |
|
903 } |
|
904 |
|
905 return $messages; |
|
906 } |
|
907 } |