UploadTableForm.class.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * UploadTable form.
  4. *
  5. * @package usic
  6. * @subpackage form
  7. * @author Your name here
  8. * @version SVN: $Id: sfPropelFormTemplate.php 10377 2008-07-21 07:10:32Z dwhittle $
  9. */
  10. class UploadTableForm extends BaseUploadTableForm
  11. {
  12. static $supported_mime = array (
  13. // "application/octet-stream",
  14. "application/rtf","application/pdf","application/postscript","application/vnd.oasis.opendocument.text",
  15. "application/zip","application/x-zip","application/x-zip-compressed","application/x-gzip","application/bzip2","application/x-tar",
  16. "application/x-bzip2",
  17. /* audio */
  18. "audio/x-ogg",
  19. "audio/mpeg3",
  20. "audio/x-mpeg-3",
  21. "audio/mpeg",
  22. "audio/x-mpequrl",
  23. /* video */
  24. "application/x-troff-msvideo",
  25. "video/mpeg",
  26. "video/x-mpeg",
  27. "video/avi",
  28. "video/msvideo",
  29. "video/x-msvideo",
  30. "video/x-flv",
  31. /* "image/",*/
  32. "image/vnd.djvu",
  33. "image/vnd.djvu",
  34. "image/gif",
  35. "image/x.djvu", "image/vnd.djvu", "image/djvu",
  36. "audio/x-ogg",
  37. "image/gif",
  38. "image/tiff",
  39. "image/jpeg","image/jpg","image/pjpeg",
  40. "image/png","image/x-png",
  41. "image/svg","image/svg+xml","image/svg-xml","text/xml-svg","image/vnd.adobe.svg+xml",
  42. "text/rtf","text/html","text/plain"
  43. );
  44. public function configure()
  45. {
  46. $this->widgetSchema['file'] = new sfWidgetFormInputFile();
  47. $this->validatorSchema['file'] = new sfValidatorFile(array('required' => true, 'max_size' => 1024000000,
  48. 'mime_types' => self::$supported_mime
  49. // ,'mime_type_guessers' => array( 'guessFromFileBinary' )
  50. // ,'mime_type_guessers'=>array( 'guessFromFileinfo', 'guessFromFileBinary' )
  51. ), array('max_size' => 'your file must be less than %max_size%',
  52. 'mime_types'=>'such file format is not recognized'));
  53. /* uploading files cannot be done via editing */
  54. if (!$this->isNew()) {
  55. $this->validatorSchema['file']->setOption('required',false);
  56. }
  57. $validators = $this->validatorSchema->getFields();
  58. $validators['daytime']->setOption('required',false);
  59. $validators['user']->setOption('required',false);
  60. unset($this->validatorSchema['url']);
  61. unset($this->validatorSchema['filesize']);
  62. unset($this->validatorSchema['filename']);
  63. $this->widgetSchema['state'] = new sfWidgetFormChoice(array('choices' => UploadTablePeer::$states));
  64. $this->validatorSchema['state'] = new sfValidatorChoice(array('choices' => array_keys(UploadTablePeer::$states),'required' => true));
  65. $this->widgetSchema['category_id']->setOption('label', 'Category');
  66. $this->setDefaults(array('category_id'=>CategoryTablePeer::defaultCategory()));
  67. }
  68. protected function doSave($con = null) //$con's type is PropelPDO
  69. {
  70. if ($this->isNew())
  71. {
  72. $file = $this->getValue('file');
  73. $size = $file->getSize();
  74. $name = $file->getOriginalName();
  75. $dirname = sfConfig::get('sf_upload_dir').'/'.sha1($file->getOriginalName().rand(11111, 99999));
  76. if (!mkdir($dirname))
  77. throw new Exception('could not create the directory for your file');
  78. $path = $dirname . '/' . $file->getOriginalName();
  79. if (file_exists($path))
  80. {
  81. throw new Exception('such a file already exists');
  82. }
  83. $file->save($path);
  84. // setting fields for database
  85. $this->values['user'] = sfContext::getInstance()->getUser()->getAttribute('login');
  86. $this->values['url'] = $path;
  87. $this->values['filename'] = $name;
  88. $this->values['filesize'] = $size;
  89. }
  90. return parent::doSave($con);
  91. }
  92. public function save($con = null)
  93. {
  94. $this->values['user'] = sfContext::getInstance()->getUser()->getAttribute('login');
  95. return parent::save($con);
  96. }
  97. }