upload-magic-bundle
Symfony2 Bundle - Easy integration for DropzoneJS and OneupUploaderBundle, (*1)
Prerequisites
This bundle is tested using Symfony2 versions 2.3+., (*2)
Installation:
Step 1: Add Bundle
- Add to composer:
"sopinet/upload-magic-bundle": "dev-master"
- Add to AppKernel:
new Oneup\UploaderBundle\OneupUploaderBundle(),
new Sopinet\Bundle\UploadMagicBundle\SopinetUploadMagicBundle(),
- Add routing for delete
upload:
resource: "@SopinetUploadMagicBundle/Controller/UploadController.php"
type: annotation
- Add configuration (config.yml) any like:
oneup_uploader:
mappings:
gallery:
frontend: dropzone
- Configure routing
oneup_uploader:
resource: .
type: uploader
- More options from OneupUploaderBundle,https://github.com/1up-lab/OneupUploaderBundle/blob/master/Resources/doc/index.md but upload-magic-bundle only support dropzone frontend method.
Step 3: Define your Entity
- You can use UploadMagic trait, so:
namespace yourBundle\BaseBundle\Entity;
use ...
/**
* @ORM\Entity
* @ORM\Table(name="file")
* @DoctrineAssert\UniqueEntity("id")
*/
class File
{
use \Sopinet\Bundle\UploadMagicBundle\Model\UploadMagic;
...
}
- You can add relation and another attributes to your entity:
...
/**
* @ORM\ManyToOne(targetEntity="Category", inversedBy="files")
* @ORM\JoinColumn(name="category_id", referencedColumnName="id")
*/
protected $category;
...
- Add CSS
{% include 'SopinetUploadMagicBundle:Upload:simple_css.html.twig' %}
- Add form
{% include 'SopinetUploadMagicBundle:Upload:simple.html.twig'
with {
'name': 'demo',
'type': 'gallery',
'id': 0,
'entity': 'myBundle_BaseBundle_File',
'param1': 'title',
'value1': 'A title for...',
'param2': 'myBundle_BaseBundle_Category',
'value2': guild.id,
'icon': 'fa fa-plus-circle'
}
%}
In param1 i use "title", and Listener will do "setTitle"., (*3)
With param2 it use an entity, and it will do findbyoneid and "set" again., (*4)
More documentation about params form is coming... ;), (*5)
- Add js, one time
{% include 'SopinetUploadMagicBundle:Upload:simple_js.html.twig' %}
If you have errors about load time, you can use:, (*6)
{% include 'SopinetUploadMagicBundle:Upload:simple_js.timeout.html.twig' %}
- Add blockjs, for each form
{% include 'SopinetUploadMagicBundle:Upload:simple_blockjs.html.twig'
with {
'name': 'demo',
'type': 'gallery',
'files': files_already_saved_array,
'limit': 1,
'entity': 'myBundle_BaseBundle_File'
# Optional
'preview': true
}
%}
More JS, trigger
What file is uploading or file is removed, you can capture this event in JQuery and do anything., (*7)
Events
$( "body" ).on( "removedFile", function( event, data) {
// data, "ok" or "ko"
});
$("body").on("uploadedFile", function(event, data) {
// data, "ok"
});
Sample
$( "body" ).on( "removedFile", function( event, data) {
if (data == "ok") {
$.gritter.add({
title: '<i class="fa fa-check-circle"></i> Acción realizada con éxito',
text: 'Has eliminado un fichero',
sticky: false,
time: '',
class_name: 'gritter-success'
});
} else {
$.gritter.add({
title: '<i class="fa fa-times-circle"></i> Ha ocurrido un error',
text: 'No se ha podido eliminar el fichero',
sticky: false,
time: '',
class_name: 'gritter-danger'
});
}
});
$("body").on("uploadedFile", function(event, data) {
$.gritter.add({
title: '<i class="fa fa-check-circle"></i> Acción realizada con éxito',
text: 'Has guardado un fichero',
sticky: false,
time: '',
class_name: 'gritter-success'
});
});
TODO
- Better documentation about params to Form
- More is coming...