symfony2admingenerator / AvocodeFormExtensionsBundle

(old-legacy) Symfony2 form extensions for Admingenerator project (also working standalone!)
Other
48 stars 31 forks source link

Drag and drop don't work #151

Open rflorent opened 9 years ago

rflorent commented 9 years ago

Hello,

I'm using this great bundle, wihtout admingenerator, and thanks for it. I try ti use upload collection but I ran into issues. I can drag and drop files, row are added, with preview and informations. But files are not uploaded, data['uploads'] is always empty. If I use file input to choose file, everything works, but I can't add more than one file.

I need somes help.

My config.yml

knp_gaufrette:
    stream_wrapper: ~

    adapters:
        object_file_adapter:
            local:
                directory: %kernel.root_dir%/../../web/uploads

    filesystems:
        object_file_fs:
            adapter:    object_file_adapter

vich_uploader:
    db_driver: orm

    gaufrette: true
    storage:   vich_uploader.storage.gaufrette

    mappings:
        object_files:
            uri_prefix:         /files
            upload_destination: object_file_fs
            namer: vich_uploader.namer_uniqid
            inject_on_load:       true
            delete_on_remove:     true
            delete_on_update:     true

avocode_form_extensions:
    upload_manager:        vich_uploader

Entity ObjectFile

<?php
/**
 * @Vich\UploadableField(mapping="object_files", fileNameProperty="path")
 * @var \Symfony\Component\HttpFoundation\File\File
 */
protected $file;

/**
 * @ORM\Column(type="string", length=255, nullable=true)
 */
protected $path;

Form OjectFile

<?php
$builder
    ->add('id', 'integer',  array(
        'required' => true,
        'label'    => 'Id',
    ))
    ->add('name', 'text', array(
        'label'    => 'Nom',
        'required' => false,
    ))
    ->add('description', 'textarea', array(
        'label'    => 'Description',
        'required' => false
    ))
    ->add('file', 'file');

Form Parent

<?php
 $builder->add('objectFiles', 'afe_collection_upload', array(
    'allow_add'         => true,
    'allow_delete'      => true,
    'by_reference'      => false,
    'type'              => "atm_object_file",
    'maxNumberOfFiles'  => 5,
    'maxFileSize'       => 500000,
    'acceptFileTypes'   => '/(\.|\/)(pdf|gif|jpe?g|png|doc|docx|xls|xlsx|odt|ppt|pptx)$/i',
    'error_bubbling'    => false,
    'options'           => array(
        'data_class' => 'Atm\\Bundle\\CoreBundle\\Entity\\ObjectFile',
    )
));
ioleo commented 9 years ago

@rflorent hi,

1) Thanks. I'm glad you like it :) 2) I''ve edited your post to add code highlighting. Please use github markdown to paste code for future issues, see doc section "Examples -> Code".

About your issue:

The data class does not need double backshasles. Try 'data_class' => 'Atm\Bundle\CoreBundle\Entity\ObjectFile'. Other than that, your configuration looks OK.

Does your ObjectFile entity implement Avocode\FormExtensionsBundle\Form\Model\UploadCollectionFileInterface? You should implement the setParent() method.

Did you add any custom eventlisteners which might kick in and modify the submitted data?

rflorent commented 9 years ago

Thanks for adding code highlighting, I will use it from now. Entity ObjectFile can have a relation with different entities, so there is a switch/case on class name to use the right setter. For now, there is juste one relation with entity BusinessDoc. This work when I use file input. DB row created and linked to businessDoc id 17 :

2 NULL 17 1 542954a19e27f.pdf mydoc.pdf NULL 2014-09-29 14:46:25 2014-09-29 14:46:25

I use entitylistener on BusinessDoc to add/remove acls when owner change, that's all. No listener in form. I add somes var_dump in CollectionUploadSubscriber, uploads array is empty on pre/on/post submit when I use drag and drop. It's like files are not added to atm_business_doc[objectFiles][uploads][]

This is my entity ObjectFile

<?php

namespace Atm\Bundle\CoreBundle\Entity;

use Avocode\FormExtensionsBundle\Form\Model\UploadCollectionFileInterface;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
use Symfony\Component\HttpFoundation\File\File;
use Atm\Bundle\CoreBundle\Entity\BusinessDoc;
/**
 * @ORM\Table(name="object_file")
 * @ORM\Entity()
 * @Vich\Uploadable
 */
class ObjectFile implements UploadCollectionFileInterface
{
    const PARENT_TYPE_BUSINESS_DOC = 1;
    const PARENT_TYPE_PRESENCE     = 2;

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @Vich\UploadableField(mapping="object_files", fileNameProperty="path")
     * @var \Symfony\Component\HttpFoundation\File\File
     */
    protected $file;

    /**
     *
     * @ORM\ManyToOne(targetEntity="ObjectFileGroup", inversedBy="objectFiles")
     * @ORM\JoinColumn(name="object_group_id", referencedColumnName="id", nullable=true)
     */
    protected $objectGroup;

    /**
     * @ORM\Column(type="integer", nullable=true)
     */
    protected $parentType;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    protected $path;

    /**
     * (Optional) nameable field
     *
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    protected $name;

    /**
     * (Optional) additional editable field
     *
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    protected $description;

    /**
     *
     * @ORM\ManyToOne(targetEntity="BusinessDoc", inversedBy="objectFiles")
     * @ORM\JoinColumn(name="business_doc_id", referencedColumnName="id")
     */
    protected $businessDoc;

        /**
     * @var DateTime $createdAt
     *
     * @Gedmo\Timestampable(on="create")
     * @ORM\Column(name="created_at",type="datetime")
     */
    protected $createdAt;

    /**
     * @var DateTime $updatedAt
     *
     * @Gedmo\Timestampable(on="update")
     * @ORM\Column(name="updated_at",type="datetime")
     */
    protected $updatedAt;

    public function setFile(\Symfony\Component\HttpFoundation\File\File $file) {
        $this->file = $file;
        $this->updated = new \DateTime();
        return $this;
    }

    public function getFile() {
        return $this->file;
    }

    public function getSize() {
        return $this->file->getFileInfo()->getSize();
    }

    /*
     * LIEN AUTO PAR VINCH
     */
    public function setParent($parent) { 

        $classArray = explode('\\',get_class($parent));
        $className = end($classArray);

        switch($className) {
            case 'BusinessDoc':
                $this->setBusinessDoc($parent);
                $this->setParentType(self::PARENT_TYPE_BUSINESS_DOC);
                break;
        }
    }

    public function getPreview() {
        return (preg_match('/image\/.*/i', $this->file->getMimeType()));
    }

    /**
     * Set id
     *
     * @param string $id
     * @return ObjectFile
     */
    public function setId($id)
    {
        $this->id = $id;

        return $this;
    }

    public function getId() {
        return $this->id;
    }

    public function setName($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }

    public function setBusinessDoc($businessDoc) {
        $this->businessDoc = $businessDoc;
    }

    public function getBusinessDoc() {
        return $this->businessDoc;
    }

    public function setDescription($description) {
        $this->description = $description;
    }

    public function getDescription() {
        return $this->description;
    }

    public function setPath($path) {
        $this->path = $path;
    }

    public function getPath() {
        return $this->path;
    }

    public function getMimeType() {
        return $this->file->getMimeType();
    }

      /**
     * Set parentType
     *
     * @param integer $parentType
     *
     * @return ObjectFile
     */
    public function setParentType($parentType)
    {
        $this->parentType = $parentType;

        return $this;
    }

    /**
     * Get parentType
     *
     * @return integer
     */
    public function getParentType()
    {
        return $this->parentType;
    }

        /**
     * Set updatedAt
     *
     * @param \DateTime $updatedAt
     * @return ObjectFile
     */
    public function setUpdatedAt($updatedAt)
    {
        $this->updatedAt = $updatedAt;

        return $this;
    }

    /**
     * Get updatedAt
     *
     * @return \DateTime 
     */
    public function getUpdatedAt()
    {
        return $this->updatedAt;
    }

        /**
     * Set createdAt
     *
     * @param \DateTime $createdAt
     * @return ObjectFile
     */
    public function setCreatedAt($createdAt)
    {
        $this->createdAt = $createdAt;

        return $this;
    }

    /**
     * Get createdAt
     *
     * @return \DateTime 
     */
    public function getCreatedAt()
    {
        return $this->createdAt;
    }
}
rflorent commented 9 years ago

I try with this sample : https://github.com/sescandell/CollectionUploadSample/tree/master/src/Acme/DemoBundle I've updated all bundle. Same problem, one by one with input file work but not with drag and drop. symfony/symfony v2.5.4 vich/uploader-bundle dev-master avocode/form-extensions-bundle dev-master

ioleo commented 9 years ago

@rflorent to make it clear, it only does not work when dragging and dropping the file? when selecting a file with the standard file browser it works?

rflorent commented 9 years ago

@loostro yes, it doesn't work when dragging and dropping. When selecting a file, it works.

sescandell commented 9 years ago

It's probably because events sent when drag'n'droping files are not the sames. I'll check that point and let you know.

rflorent commented 9 years ago

I've done more tests. It works when I use async mode with autoload:true when drag'n'droping But with autoload:false and no upload before submit, only one file is uploaded. If I start upload before submit, first file succeed, others return error 500 :

Catchable Fatal Error: Argument 1 passed to Avocode\FormExtensionsBundle\Storage\LocalFileStorage::storeFiles() must be an array, null given, called in /Users/Florent/Travail/www/ziggourat-admin-sf2/www/private/vendor/avocode/form-extensions-bundle/Avocode/FormExtensionsBundle/EventListener/CollectionUploadListener.php on line 66 and defined in /Users/Florent/Travail/www/ziggourat-admin-sf2/www/private/vendor/avocode/form-extensions-bundle/Avocode/FormExtensionsBundle/Storage/LocalFileStorage.php line 40