ruian / RuianUploadifyBundle

Add Uploadify support for symfony2
21 stars 8 forks source link

Multi Upload? #4

Open Blackskyliner opened 12 years ago

Blackskyliner commented 12 years ago

Hi there,

I wanted to use this bundle for my file upload but I don't know who I need to do the entity-wiring and form building to actually have this stored as a e.g. doctrine-array type or n-1 mapping... Could you provide a simple Example for this case?

Thanks in advance, Blackskyliner

umpirsky commented 12 years ago

I would like to have multiple file upload as well.

@Blackskyliner Did you solve this?

@ruian Do you have any plans adding this. I would be ready to contribute, just want to discus implementation with you first.

Blackskyliner commented 12 years ago

Yea... No I did just not used this package and build some not-so-fancy manual form upload through browser form elements...

umpirsky commented 12 years ago

@Blackskyliner Can I see your solution?

Blackskyliner commented 12 years ago

Entitiy:

<?php
/**
* @var string
* @ORM\Column(type="array", nullable=true)
*/
protected $bilder;

/**
 * @var array
 */
protected $bilder_upload;

Type (with entity as prop class):

<?php
        $builder->add(
            'bilder_upload', 'file', array(
                'label' => 'Bilder',
                'required' => false,
                'attr' => array(
                    'accept' => 'image/*',
                    'multiple' => 'multiple'
                )
            )
        );

Controller (mergin new and old images and set them in the ORM handles prop, also reset the upload one):

<?php
                /** @var $fum \SomeBundlePath\Service\FileUploadManager */
                $fum = $this->get('somebundlepath.service.fileuploadmanager');

                $realImages = array();
                foreach($entity->getBilder() as $bild)
                {
                    $realImages[] = $bild;
                }

                $uploads = $fum->uploadFiles($entity->getBilderUpload());

                foreach($uploads as $bild)
                {
                    $realImages[] = $bild;
                }

                $realImages = array_unique($realImages);
                $entity->setBilder($realImages);
                $entity->setBilderUpload(array());
//doing some flushing down here...

UploadService function (the commented stmts. could be used in some cases to check or whatever, but I just want to put it in ;) ):

<?php
    public function uploadFiles(array $files)
    {
        //if($files == null) return array();
        $retVal = array();
        foreach($files as $file){
            if($file != null && $file->isValid())
            {
                $fileHash = sha1_file($file->getRealPath());
                try{
                    $fileName = $fileHash.'.'.$file->guessExtension();
                    $file->move($this->upload_dir, $fileName);
                    $retVal[] = $fileName;
                }catch(\Exception $e)
                {
                    //$retVal[] = false;
                }
            }else{
                //$retVal[] = false;
            }
        }
        return $retVal;
    }
umpirsky commented 12 years ago

@Blackskyliner Thanks, I'll try if I fail with Uploadify :)

Blackskyliner commented 12 years ago

Ahh something I need to tell you though - what I forgot to write in the part above - is that after you created the formView you need to do this in the Controller before submitting it to the template:

<?php
$formview = $form->createView();
$formview->getChild('bilder_upload')->set('full_name', $formview->getChild('bilder_upload')->get('full_name').'[]');

I know its a bit hacky, but it seems to be the only way to mark its name as array, so you can multi-upload in modern browsers. It will be just a single filed though, but you can select multiple files in the dialog that appears for selecting the files, but thats kinda browser specific. e.g. the IE does not support multiple-choice upload. But in Chrome and FF it works like a charm. The merging mechanism can be used for sequential uploading though... I use this for my edit of the entity for those who just use an IE so they can upload more than one image in each step of profile editing, etc.

With some JS magic you could expand this method even more and add the uploadfile fields dynamically. Whereas the name would be the same for all fields I think... would need to have to do some testing there... But as my site needs to be fully functional without JS I didn't done it till now... maybe when the corefeatures are done I will beatufy sme controls here and there with JS, like the multiupload, etc.

ruian commented 12 years ago

I will write an example using uploadify for multi upload soon

umpirsky commented 12 years ago

@ruian Please define soon. :) Is it possible at all, you need to add new option, which is not used in bundle.

ruian commented 12 years ago

I already use it for multi upload in a backend apps, it's just i need some time to make it cleaner

umpirsky commented 12 years ago

Can community help? :)

ruian commented 12 years ago

sure :) you are welcome

umpirsky commented 12 years ago

@ruian We need your source to make it cleaned together.

ruian commented 12 years ago

the fact is i used it for multi upload with a previous version. So i think that a better way is to let start add new stuff to this bundle to allow user to switch between Single or Multi upload style. With FormType options, and it will choose the right script into the twig template associated to the type

umpirsky commented 12 years ago

@ruian Why not open full Uploadify interface by letting customer to pass array of options which will be directly converted to json and passed to Uploadify?

ruian commented 12 years ago

can you give me an example of what you mean ?

umpirsky commented 12 years ago

@ruian https://gist.github.com/2405545

ruian commented 12 years ago

hmm yeah it could be nice. I think a good way to make this bundle better is to add an extension, with all this allowed options from uploadify lib

umpirsky commented 12 years ago

It's important to be configurable per form, not from global config, since you want to use different option sets on different pages/forms.

ruian commented 12 years ago

the extension extend 'file' so it let you add every options (declared in the extension) you want to your type, and a listener who transformed the specific type to a hidden field and add a uploadifyType

umpirsky commented 12 years ago

Can you start implementing it, and I'll see how I can help, just give the initial idea. Maybe we can open separated branch for this?

ruian commented 12 years ago

yep i will create a dev branch

ruian commented 12 years ago

Tonight i will push some features into the dev branch, because during journey i have no time ;)

umpirsky commented 12 years ago

Great, thanks!

ruian commented 12 years ago

So i start a dev branch, but it still a garbage one. I will continue to work on it as soon as possible.

umpirsky commented 12 years ago

Great, thanks.

ruian commented 12 years ago

@umpirsky i have made an update on the dev branch you should have a look and give me your feedback :), there is some work to do but it's already works

Blackskyliner commented 12 years ago

Seems promising... Will test it as soon as possible :)

It needs to be patched for master though... As there is the FormType::buildForm() changed in the Way that you'll need to have a FormBuilderInterface as the first param.