From cf920c6683e1551b14c11860be437dc4f9fa347b Mon Sep 17 00:00:00 2001 From: Pablo Reyes Date: Wed, 17 Aug 2011 14:16:22 +0200 Subject: [PATCH] Aregando a plugins el archivo media --- .../modules/monografia/config/generator.yml | 22 --- .../form/doctrine/PluginDmMediaForm.class.php | 152 ++++++++++++++++++ 2 files changed, 152 insertions(+), 22 deletions(-) create mode 100644 plugins/dmCorePlugin/lib/form/doctrine/PluginDmMediaForm.class.php diff --git a/apps/admin/modules/monografia/config/generator.yml b/apps/admin/modules/monografia/config/generator.yml index d480ed1..23bd05c 100644 --- a/apps/admin/modules/monografia/config/generator.yml +++ b/apps/admin/modules/monografia/config/generator.yml @@ -44,38 +44,16 @@ generator: sortable: true filter: display: - - id - aprobado - is_active - formato_duro - titulo - anno - - resumen - idioma - - pdf_id - palabras_claves - - pais - - institucion - - total_paginas - - num_internacional - - organismo - - tipo_dc - - cant_registros - - fecha_inicial - - fecha_final - ciudad_pub - editorial - isbn - - volumen - - num_revista - - pagina_inicial - - pagina_final - - issn - - num_diapositivas - - departamento - - ciudad - - tutor - - tipo - created_at - updated_at - created_by diff --git a/plugins/dmCorePlugin/lib/form/doctrine/PluginDmMediaForm.class.php b/plugins/dmCorePlugin/lib/form/doctrine/PluginDmMediaForm.class.php new file mode 100644 index 0000000..7aa30e9 --- /dev/null +++ b/plugins/dmCorePlugin/lib/form/doctrine/PluginDmMediaForm.class.php @@ -0,0 +1,152 @@ +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; + } +} \ No newline at end of file