nassau / kunstmaan-import-bundle

为 Kunstmaan Bundles CMS 提供灵活的 Excel 导入模块

0.2 2017-03-02 16:06 UTC

This package is auto-updated.

Last update: 2024-09-23 18:57:40 UTC


README

允许在 Kunstmaan Bundles CMS 上轻松灵活地导入 Excel 文件。

安装

composer require nassau/kunstmaan-import-bundle

运行(生成)迁移以更新您的数据库模式。

配置

示例配置

kunstmaan_import:
  foobar:
    entity: AcmeBundle:Foobar
    excel:
      format: "rows" # or "columns"
    
    # you may send a zip with an Excel file and some other files
    # the files referenced from columns specified in `file_attributes` will be
    # processed and uploaded to the Media module
    zip:
      enabled: true
      data_file_extension: ['xls', 'xlsx']
      file_attributes:
        - packshot

    # use your custom handler implementing `ImportHandlerInterface`
    handler_id: ~
    
    # after an entity is imported it may be postprocessed by given services
    # to create a postprocessor register a class implementing `PostProcessorInterface` in the container
    # and tag it with "kunstmaan_import.post_processor" name, and add an `alias` attribute to this tag
    # you may then list those aliases here
    post_processors: []

    # each attribute will have those defaults:
    default_attributes:
      # don’t attempt to import cells with empty value
      ignore_empty: true
    attributes:
      # the key is a field on the entity (or a setter/getter pair)
      id:
        # label is matched to the first row/column for each imported item
        # i.e. search for this header value in Excel file
        label: External ID
      name:
        label: Foobar name
      photo:
        label: Photo
        # either provide media id, or filename (when uploading a zip file)
        # the result will be Media instance (or null if not found)
        type: media
      active:
        label: Active
        # understand human readable values like „false” or „No”:
        type: boolean

设置

在您的实体的 AdminListConfigurator 上实现 ImportWizardAdminListConfiguratorInterfacegetImportType 方法需要返回配置中指定的类型。由该管理员列表管理的实体需要实现 ImportedEntity 接口。

例如

class FoobarAdminListConfigurator extends AbstractDoctrineORMAdminListConfigurator implements ImportWizardAdminListConfiguratorInterface
{
    /**
     * Add a button to the admin list pointing to the import module
     */
    public function buildListActions()
    {
        $this->addListAction(new SimpleListAction([
            'path' => 'acmebundle_admin_foobar_import_upload',
            'params' => [],
        ], 'Upload Excel file', 'upload'));
    }

    public function getImportType()
    {
        return 'foobar';
    }

在您的 FooBarAdminListController 中,您需要添加两个动作。请调整路由名称。

    /**
     * @Route("/import/{id}", name="acmebundle_admin_foobar_import_edit")
     * @param Request $request
     * @param Import $import
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function importAction(Request $request, Import $import)
    {
        $configurator = $this->getAdminListConfigurator();

        $result = $this->get('nassau.kunstmaan_import.import_wizard_action')->import($request, $import, $configurator);

        if (false === is_array($result)) {
            if ($result) {
                $this->addFlash('success', $result);
            }

            return $this->redirectToRoute('acmebundle_admin_foobar_import_edit', ['id' => $import->getId()]);
        }

        return $this->render('KunstmaanImportBundle::Import.html.twig', $result);
    }

    /**
     * @Route("/import", name="acmebundle_admin_foobar_import_upload")
     * @param Request $request
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function uploadAction(Request $request)
    {
        $configurator = $this->getAdminListConfigurator();

        $result = $this->get('nassau.kunstmaan_import.import_wizard_action')->upload($configurator, $request);

        if (null === $result) {
            $this->addFlash('success', 'nassau.import.flash.successfull_import');

            return $this->redirectToRoute('acmebundle_admin_foobar');
        }

        if ($result instanceof Import) {
            $this->addFlash('warning', 'nassau.import.flash.import_errors');

            return $this->redirectToRoute('acmebundle_admin_foobar_import_edit', ['id' => $result->getId(), 'errors' => true]);
        }

        return $this->render($configurator->getAddTemplate(), $result);

    }

扩展

创建您自己的格式化程序/类型

  1. 实现 AttributeFormatter 接口
  2. 使用 kunstmaan_import.formatter 标签和 type 属性将其注册到容器中

例如

services:
    acme.services.import_formatter.money_formatter:
        class: 'AcmeBundle\Services\ImportFormatter\MoneyFormatter'
        public: false
        tags:
            - name: kunstmaan_import.formatter
              alias: money