CKFinder插件 - PHP - 重命名文件夹时找到删除空格

时间:2017-12-11 22:21:08

标签: php plugins ckeditor directory ckfinder

我需要社区帮助,我需要创建一个插件,用于在重命名文件夹时检查用户输入。该插件应检查新的Renamed文件夹,在保存之前应删除找到的任何空间。

我被困在removeFolderSpace函数中,我不知道如何完成它。如果有人愿意帮助我,我会非常感激!

<?php
namespace CKSource\CKFinder\Plugin\FolderSpace;

use CKSource\CKFinder\Acl\Permission;
use CKSource\CKFinder\CKFinder;
use CKSource\CKFinder\Config;
use CKSource\CKFinder\Command\CommandAbstract;
use CKSource\CKFinder\Event\CKFinderEvent;
use CKSource\CKFinder\Event\RenameFolderEvent;
use CKSource\CKFinder\Filesystem\Folder\Folder;
use CKSource\CKFinder\Filesystem\Folder\WorkingFolder;
use CKSource\CKFinder\Plugin\PluginInterface;
use CKSource\CKFinder\Filesystem\Path;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class FolderSpace implements PluginInterface, EventSubscriberInterface
{
    protected $app;

    public function setContainer(CKFinder $app) {
        $this->app = $app;
    }

    protected $requires = [
        Permission::FOLDER_RENAME,
    ];

    public function getDefaultConfig() {
        return [];
    }

    public function removeFolderSpace(RenameFolderEvent $event) {
        $config = $this->app['config'];
        //$dispatcher = $this->app['dispatcher'];

        // $dispatcher->addListener(CKFinderEvent::AFTER_COMMAND_RENAME_FILE, function(AfterCommandEvent $e) {

        // });

        $request = $event->getRequest();


        $workingFolder = $this->app['working_folder'];



    }


    public static function getSubscribedEvents()
    {
        return [CKFinderEvent::AFTER_COMMAND_RENAME_FILE => 'removeFolderSpace'];
    }


}

1 个答案:

答案 0 :(得分:0)

要实现此结果,您需要为以下两者创建一个小插件:前端(JavaScript)和连接器(PHP)。

PHP插件引导代码:

namespace CKSource\CKFinder\Plugin\SanitizeFolderName;

use CKSource\CKFinder\CKFinder;
use CKSource\CKFinder\Event\CKFinderEvent;
use CKSource\CKFinder\Event\RenameFolderEvent;
use CKSource\CKFinder\Plugin\PluginInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class SanitizeFolderName implements PluginInterface, EventSubscriberInterface
{
    protected $app;

    public function setContainer(CKFinder $app)
    {
        $this->app = $app;
    }

    public function getDefaultConfig()
    {
        return [];
    }

    public function onFolderRename(RenameFolderEvent $event)
    {
        $event->setNewFolderName(str_replace(' ', '_', $event->getNewFolderName()));
    }

    public static function getSubscribedEvents()
    {
        return [
            CKFinderEvent::RENAME_FOLDER => 'onFolderRename'
        ];
    }
}

JavaScript代码:

CKFinder.start({
    onInit: function(finder) {
        finder.on('command:before:RenameFolder', function() {
            finder.once('command:before:GetFiles', function(evt) {
                var folder = evt.data.folder;
                folder.set('name', folder.get('name').replace(/ /g, '_'));
            });
        });
    }
});