Guest User

Untitled

a guest
Jul 22nd, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 KB | None | 0 0
  1. class PhotoForm extends BasePhotoForm
  2. {
  3. public function configure()
  4. {
  5. unset($this['product_id'] ,$this['width'], $this['height']);
  6.  
  7. $this->widgetSchema['filename'] = new sfWidgetFormInputFileEditable(array(
  8. 'file_src' => '/uploads/products/photos/'.$this->getObject()->filename,
  9. 'edit_mode' => !$this->isNew(),
  10. 'is_image' => true,
  11. 'with_delete' => true,
  12. ));
  13.  
  14. $this->validatorSchema['filename'] = new sfValidatorFile(array(
  15. 'mime_types' => 'web_images',
  16. 'path' => sfConfig::get('sf_upload_dir').'/products/photos',
  17. 'required' => false,
  18. ));
  19.  
  20. $this->validatorSchema['filename_delete'] = new sfValidatorBoolean();
  21. }
  22.  
  23. protected function saveFile($field, $filename = null, sfValidatedFile $file = null)
  24. {
  25. if (null === $file)
  26. {
  27. $file = $this->getValue($field);
  28. }
  29.  
  30. $filename = $this->getUniqueFilename($this->getObject()->Product->getName() . $file->getExtension($file->getOriginalExtension()));
  31.  
  32. $savedname = parent::saveFile($field, $filename, $file);
  33. return $this->createThumbNail($field , $savedname);
  34. }
  35.  
  36. protected function removeFile($field)
  37. {
  38. $this->deleteThumbnail($field);
  39. parent::removeFile($field);
  40. }
  41.  
  42. protected function createThumbnail($field , $savedname)
  43. {
  44. $thumbnail = new sfThumbnail(150, 150);
  45. $thumbnail->loadFile($this->validatorSchema[$field]->getOption('path') . DIRECTORY_SEPARATOR . $savedname);
  46. $thumbname = $this->generateThumbname( $savedname );
  47. $thumbnail->save($this->validatorSchema[$field]->getOption('path') . DIRECTORY_SEPARATOR . $thumbname);
  48. return $savedname;
  49. }
  50.  
  51. protected function deleteThumbnail($field)
  52. {
  53. $thumbname = $this->generateThumbname( $this->getObject()->$field );
  54. unlink($this->validatorSchema[$field]->getOption('path') .'/'. $thumbname);
  55. }
  56.  
  57. protected function generateThumbname($name)
  58. {
  59. $split = explode('.' , $name);
  60. return $split[0] . '_thumb.' . $split[1];
  61. }
  62.  
  63. protected function getUniqueFilename($name)
  64. {
  65. $record = $this->getObject();
  66. $fieldname = $this->getObject()->getTable()->getFieldName('filename');
  67.  
  68. // Split basename & extension
  69. $name = explode ('.' , $name);
  70.  
  71. // TODO : provide builder option
  72. $baseFilename = Doctrine_Inflector::urlize($name[0]);;
  73. $extension = $name[1];
  74.  
  75. $whereString = 'r.' . $fieldname . ' LIKE ?';
  76. $whereParams = array($baseFilename.'%');
  77.  
  78. // Disable indexby to ensure we get all records
  79. $originalIndexBy = $record->getTable()->getBoundQueryPart('indexBy');
  80. $record->getTable()->bindQueryPart('indexBy', null);
  81.  
  82. $query = $record->getTable()
  83. ->createQuery('r')
  84. ->select('r.' . $fieldname)
  85. ->where($whereString , $whereParams)
  86. ->setHydrationMode(Doctrine_Core::HYDRATE_ARRAY);
  87.  
  88. $similarFilenameResult = $query->execute();
  89. $query->free();
  90.  
  91. // Change indexby back
  92. $record->getTable()->bindQueryPart('indexBy', $originalIndexBy);
  93.  
  94. $similarFilenames = array();
  95. foreach ($similarFilenameResult as $key => $value)
  96. {
  97. $similarFilenames[$key] = $value[$fieldname];
  98. }
  99.  
  100. // Define filename with extension to compare with similar filename
  101. $filename = $baseFilename.'.'.$extension;
  102.  
  103. $i = 1;
  104. while (in_array($filename, $similarFilenames)) {
  105. $filename = $baseFilename.'-'.$i.'.'.$extension;
  106. $i++;
  107. }
  108.  
  109. // If filename is longer then the column length then we need to trim it
  110. // and try to generate a unique filename again
  111. $length = $record->getTable()->getFieldLength('filename');
  112. if (strlen($baseFilename) > $length) {
  113. $filename = substr($baseFilename, 0, $length - (strlen($i)+ strlen($extension) + 1 ));
  114. $filename = $this->getUniqueFilename($filename);
  115. }
  116.  
  117. return $filename;
  118. }
  119.  
  120.  
  121. }
Add Comment
Please, Sign In to add comment