You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

76 lines
2.5 KiB

  1. <?php
  2. /**
  3. * File upload field
  4. */
  5. class helper_plugin_bureaucracy_fieldfile extends helper_plugin_bureaucracy_field {
  6. /**
  7. * Arguments:
  8. * - cmd
  9. * - label
  10. * - ^ (optional)
  11. *
  12. * @param array $args The tokenized definition, only split at spaces
  13. */
  14. function initialize($args) {
  15. $this->init($args);
  16. //default namespace for file upload (pagepath:file_name)
  17. $this->opt['namespace'] = '.';
  18. //check whenever the first argument is an upload namespace
  19. if (isset($args[0]) && preg_match('/^[a-z.\-_:]+$/', $args[0])) {
  20. $this->opt['namespace'] = array_shift($args);
  21. }
  22. $this->standardArgs($args);
  23. $attr = array();
  24. if(!isset($this->opt['optional'])) {
  25. $attr['required'] = 'required';
  26. }
  27. $this->tpl = form_makeFileField('@@NAME@@', '@@DISPLAY@@', '@@ID@@', '@@CLASS@@', $attr);
  28. if(!isset($this->opt['optional'])){
  29. $this->tpl['class'] .= ' required';
  30. }
  31. }
  32. /**
  33. * Handle a post to the field
  34. *
  35. * Accepts and validates a posted value.
  36. *
  37. * @param array $value The passed value or array or null if none given
  38. * @param helper_plugin_bureaucracy_field[] $fields (reference) form fields (POST handled upto $this field)
  39. * @param int $index index number of field in form
  40. * @param int $formid unique identifier of the form which contains this field
  41. * @return bool Whether the passed filename is valid
  42. */
  43. public function handle_post($value, &$fields, $index, $formid) {
  44. $this->opt['file'] = $value;
  45. return parent::handle_post($value['name'], $fields, $index, $formid);
  46. }
  47. /**
  48. * @throws Exception max size, required or upload error
  49. */
  50. protected function _validate() {
  51. global $lang;
  52. parent::_validate();
  53. $file = $this->getParam('file');
  54. if($file['error'] == 1 || $file['error'] == 2) {
  55. throw new Exception(sprintf($lang['uploadsize'],filesize_h(php_to_byte(ini_get('upload_max_filesize')))));
  56. } else if($file['error'] == 4) {
  57. if(!isset($this->opt['optional'])) {
  58. throw new Exception(sprintf($this->getLang('e_required'),hsc($this->opt['label'])));
  59. }
  60. } else if( $file['error'] || !is_uploaded_file($file['tmp_name'])) {
  61. throw new Exception(hsc($this->opt['label']) .' '. $lang['uploadfail'] . ' (' .$file['error'] . ')' );
  62. }
  63. }
  64. }