sopinet / uploadfiles-bundle

该包最新版本(1.5)没有可用的许可证信息。

Symfony2 Bundle - 用于简化One Upload Bundle使用的文件字段

安装: 279

依赖者: 0

建议者: 0

安全: 0

星级: 4

观察者: 11

分支: 3

开放问题: 5

语言:JavaScript

类型:symfony-bundle

1.5 2018-02-06 13:14 UTC

This package is not auto-updated.

Last update: 2024-09-22 07:34:21 UTC


README

安装

  1. 将此包添加到您的composer.json文件中
//composer.json
require:{
  ....
  "sopinet/uploadfiles-bundle": "dev-master"
  ....
 }
  1. 将此包添加到您的AppKernel.php文件中
//app/AppKernel.php
$bundles = array(
  ....
  new Sopinet\UploadFilesBundle\SopinetUploadFilesBundle(),
  new Oneup\UploaderBundle\OneupUploaderBundle(),
  ....
 )
  1. 配置oneuploader-bundle

  2. 基本配置: https://github.com/1up-lab/OneupUploaderBundle/blob/master/Resources/doc/index.md#step-3-configure-the-bundle

  3. 使用孤儿配置(目前配置为画廊): https://github.com/1up-lab/OneupUploaderBundle/blob/master/Resources/doc/orphanage.md

  4. 将表单类型添加到您的配置中,并将此包添加到资产配置中

//app/config/config.yml ... twig: form: resources: - 'SopinetUploadFilesBundle:Form:file.html.twig' assetic: bundles: [ SopinetUploadFilesBundle ] ...

5. Add the routing to your routing.yml:

  ```
//app/config/routing.yml
...
sopinet_uploadfiles:
    resource: @SopinetUploadFilesBundle/Resources/config/routing.yml
...
  1. 创建您的文件实体,例如
<?php
/**
 * Example File Entity from https://github.com/sopinet/uploadfiles-bundle
 */
namespace AppBundle\Entity;

use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
use Doctrine\ORM\Mapping as ORM;
use Knp\DoctrineBehaviors\Model as ORMBehaviors;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Bridge\Doctrine\Validator\Constraints as DoctrineAssert;


/**
 * @ORM\Entity
 * @ORM\Table(name="file")
 * @DoctrineAssert\UniqueEntity("id")
 * @ORM\HasLifecycleCallbacks
 */
class File
{
    use ORMBehaviors\Timestampable\Timestampable;

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

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

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

    /**
     * @return mixed
     */
    public function getPath()
    {
        return $this->path;
    }

    /**
     * @param $path
     * @return $this
     */
    public function setPath($path)
    {
        $this->path = $path;

        return $this;
    }


    public function getAbsolutePath()
    {
        return null === $this->path
            ? null
            : $this->getUploadRootDir().'/'.$this->path;
    }

    public function getWebPath()
    {
        return null === $this->path
            ? null
            : $this->getUploadDir().'/'.$this->path;
    }

    public function getFrontPath()
    {
        return null === $this->path
            ? null
            : $this->getUploadDir().'/'.$this->path;
    }

    protected function getUploadRootDir()
    {
        // the absolute directory path where uploaded
        // documents should be saved
        return __DIR__.'/../../../web'.$this->getUploadDir();
    }

    protected function getUploadDir()
    {
        // get rid of the __DIR__ so it doesn't screw up
        // when displaying uploaded doc/image in the view.
        return '';
    }

    public function getName()
    {
        return explode('/', $this->getPath())[2];
    }

    /**
     * @ORM\PostRemove
     */
    public function deleteFile(LifecycleEventArgs $event)
    {
        $fs = new Filesystem();
        //$fs->remove($this->getAbsolutePath());
    }
}

用法

  • 在您的项目实体和新的实体字段之间创建关系(注意所有者侧的生命周期回调和级联)
/**
 * @ORM\Entity
 * @ORM\Table(name="file")
 * @DoctrineAssert\UniqueEntity("id")
 * @ORM\HasLifecycleCallbacks
 */
class File
{
    ...
    /**
     * @ORM\OneToOne(targetEntity="Application\Sopinet\UserBundle\Entity\User", mappedBy="file")
     */
    protected $user;
    
        /**
     * @ORM\ManyToOne(targetEntity="Oil", inversedBy="files")
     * @ORM\JoinColumn(name="oil_id", referencedColumnName="id")
     */
    protected $oil;

    ...
}

/**
 * Entity User
 *
 * @ORM\Table("fos_user_user")
 * @ORM\Entity(repositoryClass="Application\Sopinet\UserBundle\Entity\UserRepository")
 * @ORM\HasLifecycleCallbacks
 */
class User extends BaseUser
{
 ...
     /**
     * @ORM\OneToOne(targetEntity="AppBundle\Entity\File", inversedBy="user")
     */
    protected $file;
  
 ...
}

//src/AppBundle/Entity/Oil.php
use Sopinet\UploadFilesBundle\Entity\HasFilesTrait;
/**
 * Oil
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="AppBundle\Entity\OilRepository")
 * @ORM\HasLifecycleCallbacks
 */
class Oil
{
    use HasFilesTrait;
    ...
        /**
     * @ORM\OneToMany(targetEntity="File", mappedBy="oil", cascade={"persist"})
     */
    protected $files;
    ...
}
  • 将HasFileTrait或HasFilesTrait添加到您的实体中
use Sopinet\UploadFilesBundle\Entity\HasFileTrait;
/**
 * Entity User
 *
 * @ORM\Table("fos_user_user")
 * @ORM\Entity(repositoryClass="Application\Sopinet\UserBundle\Entity\UserRepository")
 * @ORM\HasLifecycleCallbacks
 */
class User extends BaseUser
{
 ...
     use HasFileTrait;
     /**
     * @ORM\OneToOne(targetEntity="AppBundle\Entity\File", mappedBy="user")
     */
    protected $file;
 ...
}

//src/AppBundle/Entity/Oil.php
use Sopinet\UploadFilesBundle\Entity\HasFilesTrait;
/**
 * Oil
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="AppBundle\Entity\OilRepository")
 * @ORM\HasLifecycleCallbacks
 */
class Oil
{
    use HasFilesTrait;
    ...
        /**
     * @ORM\OneToMany(targetEntity="File", mappedBy="oil", cascade={"persist"})
     */
    protected $files;
    ...
}
  • 现在您可以在表单中使用此字段类型,如下所示
//src/AppBundle/Form/OilType.php
...
class OilType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
      $builder
        ->add('files', 'dropzone_file_gallery', array(
          'maxFiles'=> 4,//default 8
          'required' => false
          //if the name of mappedBy attribute is different for the className use this:
          //'mappedBy'=>'mappedByAttributeName'
        ))
...
  • 如果您想为dropbox使用其他样式或模板,可以像以下示例那样覆盖字段样式(此示例使用默认宽高比渲染dropbox)
//src/AppBundle/Form/OilType.php
...
class OilType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
      $builder
        ->add('files', 'dropzone_file_gallery', array(
          'maxFiles'=> 4,//default 8
          'required' => false,
          'type' => my_custom,
          'uploaderText' => 'your_custom_text',
        ))
        
//src/AppBundle/Resources/path_to/_template
{% form_theme oil_form _self %}
{% block my_custom %}
    <div class="modal-body">
        <div {{ block('widget_container_attributes') }}>
            {%- if form.parent is empty -%}
                {{ form_errors(form) }}
            {%- endif -%}
            {{- block('form_rows') -}}
            {{- form_rest(form) -}}
        </div>
    </div>
{% endblock %}

与sonata一起使用

  1. 对于一对一关系,将以下内容添加到您的配置中
//app/config/config.yml
...
sonata_doctrine_orm_admin:
    templates:
        types:
            list:
                ...
                dropzone_file_gallery:   SopinetUploadFilesBundle:Admin:file.html.twig
            show:
                ...
                dropzone_file_gallery:   SopinetUploadFilesBundle:Admin:file.html.twig
...

然后在表单中使用它

  1. 对于一对一关系,在添加时为字段设置自定义模板
//AppBundle/Admin/AcmeEntityAdmin.php
...
    /**
     * @param ListMapper $listMapper
     */
    protected function configureListFields(ListMapper $listMapper)
    {
        //...
        $listMapper
        ->add('files', 'dropzone_file_gallery', array(
            'template' => 'SopinetUploadFilesBundle:Admin:file_list.html.twig'
            )
        )

    }
...

如果您想自定义sonata渲染,可以创建自己的块,如下所示

//AppBundle/Admin/AcmeEntityAdmin.php
...
    public $fileUploadType='myblockName';
    /**
     * @param ListMapper $listMapper
     */
    protected function configureListFields(ListMapper $listMapper)
    {
        //...
        $listMapper
        ->add('file', 'dropzone_file_gallery', array(
            'template' => 'SopinetUploadFilesBundle:Admin:file.html.twig'
            )
        )

    }
...
//mytemplate.twig
...
{% block sonata_uploadfile_myblockName %}
    //your custom preview template here
{% endblock %
...

已添加到包中的类型和样式

  1. 类型

    modal: 用按钮替换dropbox框以显示包含dropbox框的模态框 pdf_preview: 用pdf预览替换dropbox(用于多个文件的预览),建议与style_none样式一起使用。

  2. 样式

    style_default: 正常的dropbox样式 style_iconic: 用+图标替换dropbox框 style_none: 无样式

  3. sonata预览类型

    pdf: 与pdf预览相同,仅适用于一对一实体与文件关系,使用add添加

    //AppBundle/Admin/AcmeEntityAdmin.php
    ...
        public $yourFieldNameUploadType='pdf';
    ...