Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class PhotoForm extends BasePhotoForm
- {
- public function configure()
- {
- unset($this['product_id'] ,$this['width'], $this['height']);
- $this->widgetSchema['filename'] = new sfWidgetFormInputFileEditable(array(
- 'file_src' => '/uploads/products/photos/'.$this->getObject()->filename,
- 'edit_mode' => !$this->isNew(),
- 'is_image' => true,
- 'with_delete' => true,
- ));
- $this->validatorSchema['filename'] = new sfValidatorFile(array(
- 'mime_types' => 'web_images',
- 'path' => sfConfig::get('sf_upload_dir').'/products/photos',
- 'required' => false,
- ));
- $this->validatorSchema['filename_delete'] = new sfValidatorBoolean();
- }
- protected function saveFile($field, $filename = null, sfValidatedFile $file = null)
- {
- if (null === $file)
- {
- $file = $this->getValue($field);
- }
- $filename = $this->getUniqueFilename($this->getObject()->Product->getName() . $file->getExtension($file->getOriginalExtension()));
- $savedname = parent::saveFile($field, $filename, $file);
- return $this->createThumbNail($field , $savedname);
- }
- protected function removeFile($field)
- {
- $this->deleteThumbnail($field);
- parent::removeFile($field);
- }
- protected function createThumbnail($field , $savedname)
- {
- $thumbnail = new sfThumbnail(150, 150);
- $thumbnail->loadFile($this->validatorSchema[$field]->getOption('path') . DIRECTORY_SEPARATOR . $savedname);
- $thumbname = $this->generateThumbname( $savedname );
- $thumbnail->save($this->validatorSchema[$field]->getOption('path') . DIRECTORY_SEPARATOR . $thumbname);
- return $savedname;
- }
- protected function deleteThumbnail($field)
- {
- $thumbname = $this->generateThumbname( $this->getObject()->$field );
- unlink($this->validatorSchema[$field]->getOption('path') .'/'. $thumbname);
- }
- protected function generateThumbname($name)
- {
- $split = explode('.' , $name);
- return $split[0] . '_thumb.' . $split[1];
- }
- protected function getUniqueFilename($name)
- {
- $record = $this->getObject();
- $fieldname = $this->getObject()->getTable()->getFieldName('filename');
- // Split basename & extension
- $name = explode ('.' , $name);
- // TODO : provide builder option
- $baseFilename = Doctrine_Inflector::urlize($name[0]);;
- $extension = $name[1];
- $whereString = 'r.' . $fieldname . ' LIKE ?';
- $whereParams = array($baseFilename.'%');
- // Disable indexby to ensure we get all records
- $originalIndexBy = $record->getTable()->getBoundQueryPart('indexBy');
- $record->getTable()->bindQueryPart('indexBy', null);
- $query = $record->getTable()
- ->createQuery('r')
- ->select('r.' . $fieldname)
- ->where($whereString , $whereParams)
- ->setHydrationMode(Doctrine_Core::HYDRATE_ARRAY);
- $similarFilenameResult = $query->execute();
- $query->free();
- // Change indexby back
- $record->getTable()->bindQueryPart('indexBy', $originalIndexBy);
- $similarFilenames = array();
- foreach ($similarFilenameResult as $key => $value)
- {
- $similarFilenames[$key] = $value[$fieldname];
- }
- // Define filename with extension to compare with similar filename
- $filename = $baseFilename.'.'.$extension;
- $i = 1;
- while (in_array($filename, $similarFilenames)) {
- $filename = $baseFilename.'-'.$i.'.'.$extension;
- $i++;
- }
- // If filename is longer then the column length then we need to trim it
- // and try to generate a unique filename again
- $length = $record->getTable()->getFieldLength('filename');
- if (strlen($baseFilename) > $length) {
- $filename = substr($baseFilename, 0, $length - (strlen($i)+ strlen($extension) + 1 ));
- $filename = $this->getUniqueFilename($filename);
- }
- return $filename;
- }
- }
Add Comment
Please, Sign In to add comment