<commit_before># Upload
---------------------------------------

[go back to Table of contents][back-to-index]

[back-to-index]: https://github.com/symfony2admingenerator/AdmingeneratorGeneratorBundle/blob/master/Resources/doc/documentation.md#5-form-extensions

### 1. Usage

Upload is a widget for [collection](http://symfony.com/doc/current/reference/forms/types/collection.html) of files and entities associated with them.

It combines two forms into one:

* collection of already uploaded files
* file input to allow submitting new files

It transforms uploaded files into entities and allows easy manipulation of existing entities.

#### Features:

* Allow multiple file selection
* Drag & drop support
* Copy & paste support
* Display list of selected files
* Remove selected file from the list
* Display file size (browsers with HTML5 File API support only)
* Preview images
* Sort uploaded files
* Additional form fields (like name, description, etc)

#### Requirements:

* [VichUploaderBundle](https://github.com/dustin10/VichUploaderBundle)
* (Optional) [AvalancheImagineBundle](https://github.com/avalanche123/AvalancheImagineBundle)

### 2. Support

Upload supports **Doctrine ORM** and *may* support (untested) Doctrine ODM and Propel.

### 3. Configuration

Upload widget configuration:

```yaml
images:
  label:            Images
  dbType:           collection
  formType:         upload
  addFormOptions:
    nameable:         name
    sortable:         position
    editable:         [ name, description, position ]
    type:             \Acme\GalleryBundle\Form\Type\Image\EditType
    maxNumberOfFiles:           5
    maxFileSize:                500000
    minFileSize:                1000
    acceptFileTypes:            /(\.|\/)(gif|jpe?g|png)$/i
    previewSourceFileTypes:     /^image\/(gif|jpeg|png)$/
    previewSourceMaxFileSize:   250000
    previewMaxWidth:            100
    previewMaxHeight:           100
    previewAsCanvas:            true
    prependFiles:               false
    allow_add:        true
    allow_delete:     true
    error_bubbling:   false
    options:
      data_class:     Acme\GalleryBundle\Entity\Image
```

### 4. Edit your entity class:

* entity class must implement `Fileinterface`
* if you want sortable behaviour, you must use Gedmo
* use Vich to handle file uploads and injection

```php
<?php

namespace Acme\GalleryBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

use Vich\UploaderBundle\Mapping\Annotation as Vich;
use Gedmo\Mapping\Annotation as Gedmo;

use Admingenerator\GeneratorBundle\Model\FileInterface;

/**
 * @ORM\Table(name="acme_image") 
 * @ORM\Entity(repositoryClass="Gedmo\Sortable\Entity\Repository\SortableRepository")
 * @Vich\Uploadable
 */
class Image implements FileInterface
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;
    
    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    protected $name;          // used for nameable option
    
    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    protected $description;   // simple editable field

    /**
     * @Vich\UploadableField(mapping="product_image", fileNameProperty="path")
     */
    protected $file;          // file container
    
    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    protected $path;          // used to store file path
    
    /**
     * @Gedmo\SortableGroup
     * @ORM\ManyToOne(targetEntity="Album", inversedBy="images")
     * @ORM\JoinColumn(name="album_id", referencedColumnName="id")
     */
    protected $album;         // here i want to group images per related gallery album, 
                              // so i use album's ID as unique sortable group

    /**
     * @Gedmo\SortablePosition
     * @ORM\Column(name="position", type="integer")
     */
    protected $position;      // used to store sortable position
    
    // ...
```

### 5. Options

#### nameable

**type:** `string` **default:** `null`

If specified, normalized filenames will be autoloaded into this property upon upload.

#### sortable

**type:** `string` **default:** `null`

If specified, enables sortable behavior and uses this property to store position.

> **Note:** This property must be added to builder in formType and added to **editable** option.

#### editable

**type:** `array` **default:** `[]`<commit_msg>Update upload.md<commit_after># Upload
---------------------------------------

[go back to Table of contents][back-to-index]

[back-to-index]: https://github.com/symfony2admingenerator/AdmingeneratorGeneratorBundle/blob/master/Resources/doc/documentation.md#5-form-extensions

### 1. Usage

Upload is a widget for [collection](http://symfony.com/doc/current/reference/forms/types/collection.html) of files and entities associated with them.

It combines two forms into one:

* collection of already uploaded files
* file input to allow submitting new files

It transforms uploaded files into entities and allows easy manipulation of existing entities.

#### Features:

* Allow multiple file selection
* Drag & drop support
* Copy & paste support
* Display list of selected files
* Remove selected file from the list
* Display file size (browsers with HTML5 File API support only)
* Preview images
* Sort uploaded files
* Additional form fields (like name, description, etc)

#### Requirements:

* [VichUploaderBundle](https://github.com/dustin10/VichUploaderBundle)
* (Optional) [AvalancheImagineBundle](https://github.com/avalanche123/AvalancheImagineBundle)

### 2. Support

Upload supports **Doctrine ORM** and *may* support (untested) Doctrine ODM and Propel.

### 3. Configuration

Upload widget configuration:

```yaml
images:
  label:            Images
  dbType:           collection
  formType:         upload
  addFormOptions:
    nameable:         name
    sortable:         position
    editable:         [ name, description, position ]
    type:             \Acme\GalleryBundle\Form\Type\Image\EditType
    maxNumberOfFiles:           5
    maxFileSize:                500000
    minFileSize:                1000
    acceptFileTypes:            /(\.|\/)(gif|jpe?g|png)$/i
    previewSourceFileTypes:     /^image\/(gif|jpeg|png)$/
    previewSourceMaxFileSize:   250000
    previewMaxWidth:            100
    previewMaxHeight:           100
    previewAsCanvas:            true
    prependFiles:               false
    allow_add:        true
    allow_delete:     true
    error_bubbling:   false
    options:
      data_class:     Acme\GalleryBundle\Entity\Image
```

### 4. Edit your entity class:

* entity class must implement `Fileinterface`
* if you want sortable behaviour, you must use Gedmo
* use Vich to handle file uploads and injection

```php
<?php

namespace Acme\GalleryBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

use Vich\UploaderBundle\Mapping\Annotation as Vich;
use Gedmo\Mapping\Annotation as Gedmo;

use Admingenerator\GeneratorBundle\Model\FileInterface;

/**
 * @ORM\Table(name="acme_image") 
 * @ORM\Entity(repositoryClass="Gedmo\Sortable\Entity\Repository\SortableRepository")
 * @Vich\Uploadable
 */
class Image implements FileInterface
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;
    
    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    protected $name;          // used for nameable option
    
    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    protected $description;   // simple editable field

    /**
     * @Vich\UploadableField(mapping="product_image", fileNameProperty="path")
     */
    protected $file;          // file container
    
    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    protected $path;          // used to store file path
    
    /**
     * @Gedmo\SortableGroup
     * @ORM\ManyToOne(targetEntity="Album", inversedBy="images")
     * @ORM\JoinColumn(name="album_id", referencedColumnName="id")
     */
    protected $album;         // here i want to group images per related gallery album, 
                              // so i use album's ID as unique sortable group

    /**
     * @Gedmo\SortablePosition
     * @ORM\Column(name="position", type="integer")
     */
    protected $position;      // used to store sortable position
    
    // ...
```

#### 4.1. Implementing functions
  
  As you are implementing your class from an interface(FileInterface), you have to implement all abstract
  functions in that interface which are :
  
*  `public function getSize()`
*  `public function setParent($parent)`
*  `public function setFile(\Symfony\Component\HttpFoundation\File\File $file)`
*  `public function getFile()`
*  `public function getPreview()`
  
**What exactly should be in these functions?** The short answer is: that depends.
  
  For example in this *Image* class, you can implement them like this :
  
```php
  <?php
  //...

    /**
     * Set file
     *
     * @param Symfony\Component\HttpFoundation\File\File $file
     * @return AlbumImage
     */
    public function setFile(\Symfony\Component\HttpFoundation\File\File $file)
    {
        $this->file = $file;
        return $this;
    }

    /**
     * Get file
     *
     * @return string 
     */
    public function getFile()
    {
        return $this->file;
    }

    /**
     * Get size
     *
     * @return string 
     */
    public function getSize()
    {
        return $this->file->getFileInfo()->getSize();
    }

    /**
     * @inheritDoc
     */
    public function setParent($parent) {
        $this->setAlbum($parent);
    }

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

### 5. Options

#### nameable

**type:** `string` **default:** `null`

If specified, normalized filenames will be autoloaded into this property upon upload.

#### sortable

**type:** `string` **default:** `null`

If specified, enables sortable behavior and uses this property to store position.

> **Note:** This property must be added to builder in formType and added to **editable** option.

#### editable

**type:** `array` **default:** `[]`
{'fraction_non_alphanumeric': 0.12493946731234867, 'fraction_numerical': 0.00891041162227603, 'mean_word_length': 2.7157250809643756, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 12, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2634892', 'n_tokens_mistral': 3070, 'n_tokens_neox': 2947, 'n_words': 817}