Search code examples
phppluginsckeditordirectoryckfinder

CKFinder Plugin - PHP - Remove Space if Found when Renaming a Folder


I need the communities help, I need to create a plugin that checks users input when Renaming a Folder. The plugin should check the new Renamed folder and before saving should remove any space that is found.

I am stuck in the removeFolderSpace function and I am not sure how to complete it. If anyone is willing to help I appreciate greatly!

<?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'];
    }


}

Solution

  • To achieve this result you will need to create a small plugin for both: frontend (JavaScript) and connector (PHP).

    PHP plugin bootstrap code:

    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 code:

    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, '_'));
                });
            });
        }
    });