Developed with love by KnpLabs Hire us for your project!
39

sonata-multiupload-bundle

by silasjoisten

silasjoisten/sonata-multiupload-bundle

Build Status
Latest Stable Version
Total Downloads
Latest Unstable Version
License
codecov

Installation

Step 1: Download the Bundle

composer require silasjoisten/sonata-multiupload-bundle

Step 2: Enable the Bundle

Then, enable the bundle by adding it to the list of registered bundles
in the app/AppKernel.php file of your project:

<?php

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...
            new SilasJoisten\Sonata\MultiUploadBundle\SonataMultiUploadBundle(),
        );

        // ...
    }

    // ...
}

If you are using flex register bundle in config/bundles.php:
```php
<?php

return [
//...
SilasJoisten\Sonata\MultiUploadBundle\SonataMultiUploadBundle::class => ['all' => true]
];
```

Step 3: Configuration

First you need to override the default MediaAdminController.php via:

# config/services.yaml

parameters:
    sonata.media.admin.media.controller: SilasJoisten\Sonata\MultiUploadBundle\Controller\MultiUploadController

services:
    # ...

Now add to the service definition of your Provider and add to tag multi_upload: true like:

App\Provider\VideoProvider:
    arguments:
        - 'App\Provider\VideoProvider'
        # ...
    calls:
        # ...
    tags:
        - { name: sonata.media.provider, multi_upload: true }

If you are using default Sonata MediaProvider these arguments are already injected.
Otherwise be aware that the provider gets allowedMimeTypes and allowedExtensions injected.
This arguments will be used by JavaScript to validate MimeType and FileExtensions before the Request starts.

For Example:

App\Provider\VideoProvider:
    arguments:
        #- 'App\Provider\VideoProvider'
        #- '@sonata.media.filesystem.local'
        #- '@sonata.media.cdn.server'
        #- '@sonata.media.generator.default'
        #- '@sonata.media.video.thumbnail'
        - ['mp4', 'mov', 'flv', 'wmv', 'mxf', 'mkv','m4v','mpg']
        - ['video/mp4', 'video/quicktime', 'video/x-flv','video/x-ms-wmv','application/mxf','video/x-matroska','video/x-m4v','video/mpeg']
        # ...

In your provider where you want to use multiupload you need to add the
SilasJoisten\Sonata\MultiUploadBundle\Traits\MultiUploadTrait trait.
If you dont know how to use them take a look at
PHP trait definintion

In the following you see how to use the MultiUploadTrait:
```php
namespace App\Provider;

use SilasJoisten\Sonata\MultiUploadBundle\Traits\MultiUploadTrait;

final class VideoProvider extends FileProvider
{
use MultiUploadTrait;

// ...

}
```

Add JavaScript and CSS to SonataAdmin config:
```yaml

config/packages/sonata_admin.yaml

sonata_admin:
assets:
extra_stylesheets:
- bundles/sonatamultiupload/dist/sonata-multiupload.css

    extra_javascripts:
        - bundles/sonatamultiupload/dist/sonata-multiupload.js

#### OPTIONAL

```yaml
# config/packages/sonata_multi_upload.yaml

sonata_multi_upload:
    max_upload_filesize: 3000000 # 3MB the default value is 0 -> allow every size

There is an option redirect_to which allows you to redirect after complete upload to your configured page.

# config/packages/sonata_multi_upload.yaml

sonata_multi_upload:
    redirect_to: 'admin_sonata_media_media_list'

HINT: You can create a controller action in your MediaAdminController which is called like
createGalleryFromMultiUploadAction. The MultiUploadBundle passes automatically the id's from the uploaded medias
to the redirection route for example: /foo/bar?idx=%5B70%2C71%2C72%5D so you can take them and create
a gallery from uploaded medias.

In the following i provide an example:

In your MediaAdminController.php:
```php
namespace App\Controller;

use App\Application\Sonata\MediaBundle\Admin\GalleryAdmin;
use SilasJoisten\Sonata\MultiUploadBundle\Controller\MultiUploadController;
use Sonata\MediaBundle\Entity\MediaManager;
use Sonata\MediaBundle\Entity\GalleryManager;

final class MediaAdminController extends MultiUploadController
{
public function createGalleryAction(Request $request, MediaManager $mediaManager, GalleryManager $galleryManager, GalleryAdmin $galleryAdmin): RedirectResponse
{
$idx = $request->query->get('idx');
$idx = json_decode($idx);

    $gallery = $galleryManager->create();
    $gallery->setName('Auto Created Gallery');
    $gallery->setEnabled(false);
    $gallery->setContext('default');

    foreach ($idx as $id) {
        $media = $mediaManager->find($id);

        $galleryHasMedia = new GalleryHasMedia();
        $galleryHasMedia->setGallery($gallery);
        $galleryHasMedia->setMedia($media);
        $gallery->addGalleryHasMedia($galleryHasMedia);
    }

    $galleryManager->save($gallery);

    return $this->redirect($galleryAdmin->generateObjectUrl('edit', $gallery));
}

}
```

Maybe you need to create an alias for MediaManager and GalleryManager like:
```yaml

config/services.yaml

services:
Sonata\MediaBundle\Entity\MediaManager:
alias: sonata.media.manager.media

Sonata\MediaBundle\Entity\GalleryManager:
    alias: sonata.media.manager.gallery

App\Application\Sonata\MediaBundle\Admin\GalleryAdmin:
    alias: sonata.media.admin.gallery


Register Route in `MediaAdmin.php`:

```php
protected function configureRoutes(RouteCollection $collection): void
{
    $collection->add('create_gallery', 'create/gallery/uploaded/medias');
}

And update the config accordingly:
```yaml

config/packages/sonata_multi_upload.yaml

sonata_multi_upload:
redirect_to: 'admin_sonata_media_media_create_gallery'
```
This is how you can create a Gallery by uploaded Medias.

Notice that the uploader won't work for Providers like: YouTubeProvider, VimeoProvider!

4. Look & Feel

multiupload

Used Library:
* jQuery Upload

MIT License

Copyright (c) 2018 Silas Joisten

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
  • fixed template to keep SonataMediaBundle compatibility (#55)
    By web-flow, 10 months ago
  • use in test new interface (#54)
    By web-flow, 10 months ago
  • changed ManagerInterface (#49)
    By web-flow, 10 months ago
  • fixed deprecation message of treebuilder (#51)
    By web-flow, 10 months ago
  • dropped php 7.0 support (#50)
    By web-flow, 10 months ago
  • fixed video image not shown (#47)
    By web-flow, 10 months ago
  • updated docs (#45)
    By silasjoisten, 10 months ago
  • updated docs (#44)
    By web-flow, 10 months ago
  • renamed translations (#43)
    By web-flow, 10 months ago
  • updated docs provide an example for redirection (#42)
    By web-flow, 10 months ago
  • added rediect_to config option (#41)
    By web-flow, 10 months ago
  • added php7.3 to travis (#40)
    By web-flow, 10 months ago
  • Merge pull request #39 from tifabien/fix-image-preview
    By web-flow, 1 year ago
  • Fix the image provider preview
    By tifabien, 1 year ago
  • Merge pull request #36 from tifabien/replace-controller-image-path
    By web-flow, 1 year ago
  • Replace image preview path in controller
    By tifabien, 1 year ago
  • Merge pull request #34 from tifabien/fix-typo
    By web-flow, 1 year ago
  • Fix translation typo
    By tifabien, 1 year ago
  • Merge pull request #32 from silasjoisten/silasjoisten-patch-1
    By web-flow, 1 year ago
  • Update UPGRADE.md
    By web-flow, 1 year ago
  • Merge pull request #31 from malikzh/patch-2
    By web-flow, 1 year ago
  • Create SonataMultiUploadBundle.ru.yml
    By web-flow, 1 year ago
  • Merge pull request #28 from greg0ire/french_translations
    By web-flow, 1 year ago
  • Merge pull request #29 from silasjoisten/conf
    By web-flow, 1 year ago
  • added info node
    By web-flow, 1 year ago
  • Add french translations
    By greg0ire, 1 year ago
  • added upgrade note for 3.x
    By silasjoisten, 1 year ago
  • renamed upgrade
    By silasjoisten, 1 year ago
  • added upgrade note for 3.x
    By silasjoisten, 1 year ago
  • updated readme
    By silasjoisten, 1 year ago