定制 bummzack/sortablefile 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

bummzack/sortablefile

Composer 安装命令:

composer require bummzack/sortablefile

包简介

An extension for SilverStripe that adds sorting to UploadField.

README 文档

README

Scrutinizer Code Quality Code Coverage Build Status Latest Stable Version Latest Unstable Version Monthly Downloads

An extension for SilverStripe 4.1+ that allows sorting of files attached via UploadField.

This module decorates the existing UploadField and adds sorting capabilities to it. This is meant to be used with a many_many relation of Files or Images.

screen-capture

Installation

This module only works with SilverStripe 4.1+.

For a version compatible with SilverStripe 3, please use a 1.x release.

The easiest way is to use composer:

composer require bummzack/sortablefile ^2.0

Run dev/build afterwards.

Usage

Usage is pretty simple. Just use SortableUploadField instead of UploadField to manage your many_many relations. To persist the sort-order, an additional extra-field with the sort-order has to be added to the many_many relation. You can do so by specifying the sort column via many_many_extraFields (see example below).

By default the SortableUploadField assumes that the sort-column is named SortOrder. If you want to use another field-name (for example Sort), you have to explicitly set it:

SortableUploadField::create('Files')->setSortColumn('Sort');

Example setup for many_many

Let's assume we have a PortfolioPage that has multiple Images attached.

The PortfolioPage looks like this:

use SilverStripe\Assets\Image;
use SilverStripe\Forms\FieldList;
use Bummzack\SortableFile\Forms\SortableUploadField;

class PortfolioPage extends Page
{
    // This page can have many images
    private static $many_many = [
        'Images' => Image::class
    ];

    // this adds the SortOrder field to the relation table.
    // Please note that the key (in this case 'Images')
    // has to be the same key as in the $many_many definition!
    private static $many_many_extraFields = [
        'Images' => ['SortOrder' => 'Int']
    ];

    public function getCMSFields()
    {
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
            $fields->addFieldToTab('Root.Main', SortableUploadField::create(
                'Images', $this->fieldLabel('Images')
            ));
        });

        return parent::getCMSFields();
    }
}

Once this has been set up like described above, then you should be able to add images in the CMS and sort them by dragging them (use the handle on the left).

Templates

Sorting the Files via a relation table isn't easily achievable via a DataExtension. This is why it's currently up to the user to implement a getter that will return the sorted files, something along the lines of:

// Use this in your templates to get the correctly sorted images

public function Images()
{
    return $this->getManyManyComponents('Images')->sort('SortOrder');
}

// and/or
public function SortedImages()
{
    return $this->Images()->Sort('SortOrder');
}

And then in your templates use:

<% loop $SortedImages %>
  $ScaleWidth(500)
<% end_loop %>

Alternatively, you could simply use the sort statement in your template, which will remove the need for a special getter method in your page class.

<% loop $Images.Sort('SortOrder') %>
  $ScaleWidth(500)
<% end_loop %>

Many Many Through

The module supports editing many many through lists as well. The advantage of using many_many through is, that your relations can be versioned properly.

Example Setup

Here's an example setup with the same PortfolioPage as above, but now using many_many through instead. If this seems unclear, please also consult the official documentation.

First, the PortfolioPage class:

<?php

use Bummzack\SortableFile\Forms\SortableUploadField;
use SilverStripe\Forms\FieldList;

class PortfolioPage extends Page
{
    private static $many_many = [
        'Images' => [
            'through' => PortfolioImage::class,
            'from' => 'PortfolioPage',
            'to' => 'Image',
        ]
    ];

    // This is required to automatically publish your images
    // whenever you publish your page
    private static $owns = [
        'Images'
    ];

    // This is required to publish deletions as well,
    // as this will not happen by default!
    private static $cascade_deletes = [
        'Images'
    ];

    public function getCMSFields()
    {
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
            $fields->addFieldToTab('Root.Main', SortableUploadField::create(
                'Images', $this->fieldLabel('Images')
            ));
        });

        return parent::getCMSFields();
    }

    // This is a helper method, that is needed to display the items in the
    // correct order. Is required for proper CMS functionality and can be
    // used in templates as $getImages
    public function getImages()
    {
        return $this->Images()->sort('SortOrder');
    }
}

The idea of the many_many through relation is, that you have an intermediate DataObject, that connects your objects. So we add a DataObject named PortfolioImage:

<?php

use SilverStripe\Assets\Image;
use SilverStripe\ORM\DataObject;
use SilverStripe\Versioned\Versioned;

class PortfolioImage extends DataObject
{
    private static $db = [
        'SortOrder' => 'Int'
    ];

    private static $has_one = [
        'Image' => Image::class,
        'PortfolioPage' => PortfolioPage::class
    ];

    private static $default_sort = 'SortOrder';

    // It's important that you add the Versioned extension to this!
    private static $extensions = [
        Versioned::class
    ];
}

We should also add the belongs_many_many relation to the Image class. This is very easy to do via config. So in your mysite/_config/config.yml add:

SilverStripe\Assets\Image:
  belongs_many_many:
    PortfolioPages: PortfolioPage.Images

What happened to has_many support?

Support for has_many relations has been dropped, since it can lead to a very bad user experience if a file can only be added to a single page. Imagine a user added an image to Page A, then adds the same image via Add from files to Page B. The file would then be removed from Page A, without any warning or explanation, which is bad UX.

bummzack/sortablefile 适用场景与选型建议

bummzack/sortablefile 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 692.51k 次下载、GitHub Stars 达 67, 最近一次更新时间为 2013 年 05 月 05 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「file」 「cms」 「sortable」 「silverstripe」 「uploadfield」 「Many Files」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 bummzack/sortablefile 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 bummzack/sortablefile 我们能提供哪些服务?
定制开发 / 二次开发

基于 bummzack/sortablefile 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 692.51k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 70
  • 点击次数: 21
  • 依赖项目数: 65
  • 推荐数: 2

GitHub 信息

  • Stars: 67
  • Watchers: 11
  • Forks: 31
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2013-05-05