Skip to content

Commit

Permalink
Aregando a plugins el archivo media
Browse files Browse the repository at this point in the history
  • Loading branch information
PabloRN committed Aug 17, 2011
1 parent 9a4f25f commit cf920c6
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 22 deletions.
22 changes: 0 additions & 22 deletions apps/admin/modules/monografia/config/generator.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
152 changes: 152 additions & 0 deletions plugins/dmCorePlugin/lib/form/doctrine/PluginDmMediaForm.class.php
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;
}
}

0 comments on commit cf920c6

Please sign in to comment.