-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
152 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 152 additions & 0 deletions
152
plugins/dmCorePlugin/lib/form/doctrine/PluginDmMediaForm.class.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
<?php | ||
|
||
/** | ||
* PluginDmMedia form. | ||
* | ||
* @package form | ||
* @subpackage DmMedia | ||
* @version SVN: $Id: sfDoctrineFormTemplate.php 6174 2007-11-27 06:22:40Z fabien $ | ||
*/ | ||
abstract class PluginDmMediaForm extends BaseDmMediaForm | ||
{ | ||
|
||
public function setup() | ||
{ | ||
parent::setup(); | ||
|
||
$this->useFields(array('dm_media_folder_id', 'file', 'legend', 'author', 'license')); | ||
|
||
$this->widgetSchema['file'] = new sfWidgetFormDmInputFile(); | ||
$this->validatorSchema['file'] = new sfValidatorFile(array( | ||
'required' => $this->getObject()->isNew() | ||
)); | ||
|
||
$this->changeToHidden('dm_media_folder_id'); | ||
|
||
$this->mergePostValidator(new sfValidatorCallback(array('callback' => array($this, 'clearName')))); | ||
$this->mergePostValidator(new sfValidatorCallback(array('callback' => array($this, 'checkFolder')))); | ||
|
||
if(false !== $mimeTypes = $this->getOption('mime_types', false)) | ||
{ | ||
$this->setMimeTypeWhiteList($mimeTypes); | ||
} | ||
if(false !== $size = $this->getOption('max_size', false)) | ||
{ | ||
$this->setMimeTypeMaxSize($size); | ||
} | ||
} | ||
|
||
public function setMimeTypeWhiteList($mimeTypes) | ||
{ | ||
$this->validatorSchema['file']->setOption('mime_types', $mimeTypes); | ||
} | ||
public function setMimeTypeMaxSize($size) | ||
{ | ||
$this->validatorSchema['file']->setOption('max_size', $size); | ||
} | ||
|
||
protected function doUpdateObject($values) | ||
{ | ||
if (isset($values['file']) && $values['file'] instanceof sfValidatedFile) | ||
{ | ||
$validatedFile = $values['file']; | ||
} | ||
else | ||
{ | ||
$validatedFile = null; | ||
} | ||
|
||
unset($values['file']); | ||
|
||
if($this->object->exists() && $values['dm_media_folder_id'] != $this->object->dm_media_folder_id) | ||
{ | ||
$moveToFolderId = $values['dm_media_folder_id']; | ||
$values['dm_media_folder_id'] = $this->object->dm_media_folder_id; | ||
} | ||
|
||
parent::doUpdateObject($values); | ||
|
||
if ($validatedFile) | ||
{ | ||
$values = $this->handleValidatedFile($validatedFile, $values); | ||
} | ||
|
||
if(isset($moveToFolderId)) | ||
{ | ||
$this->object->move(dmDb::table('DmMediaFolder')->find($moveToFolderId)); | ||
} | ||
} | ||
|
||
/* | ||
* By default, when a file is uploaded | ||
* 1. If media is new, create the file | ||
* 2. If media already exists with another file, keep the media and replace the file | ||
*/ | ||
protected function handleValidatedFile(sfValidatedFile $file, array $values) | ||
{ | ||
if ($this->object->isNew()) | ||
{ | ||
if (!$this->object->create($file)) | ||
{ | ||
throw new dmException(sprintf('Can not create file for media %s', $this->object)); | ||
} | ||
} | ||
else | ||
{ | ||
if (!$this->object->replaceFile($file)) | ||
{ | ||
throw new dmException(sprintf('Can not replace file for media %s', $object)); | ||
} | ||
} | ||
|
||
return $values; | ||
} | ||
|
||
public function clearName($validator, $values) | ||
{ | ||
if (!empty($values['file'])) | ||
{ | ||
$filename = dmOs::sanitizeFileName($values['file']->getOriginalName()); | ||
if(empty($filename)) | ||
{ | ||
$error = new sfValidatorError($validator, 'This is a bad name'); | ||
|
||
// throw an error bound to the password field | ||
throw new sfValidatorErrorSchema($validator, array('file' => $error)); | ||
} | ||
} | ||
|
||
return $values; | ||
} | ||
|
||
public function checkFolder($validator, $values) | ||
{ | ||
if (!empty($values['file'])) | ||
{ | ||
if(!$folder = dmDb::table('DmMediaFolder')->find($values['dm_media_folder_id'])) | ||
{ | ||
throw new dmException('media has no folder'); | ||
} | ||
|
||
if(!is_dir($folder->fullPath)) | ||
{ | ||
if (!$this->getService('filesystem')->mkdir($folder->fullPath)) | ||
{ | ||
$error = new sfValidatorError($validator, dmProject::unRootify($folder->fullPath).' is not a directory'); | ||
|
||
throw new sfValidatorErrorSchema($validator, array('file' => $error)); | ||
} | ||
} | ||
|
||
if(!is_writable($folder->fullPath)) | ||
{ | ||
$error = new sfValidatorError($validator, dmProject::unRootify($folder->fullPath).' is not writable'); | ||
|
||
// throw an error bound to the file field | ||
throw new sfValidatorErrorSchema($validator, array('file' => $error)); | ||
} | ||
} | ||
|
||
return $values; | ||
} | ||
} |