PHP code example of ngscz / nette-elfinder

1. Go to this page and download the library: Download ngscz/nette-elfinder library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ngscz / nette-elfinder example snippets




namespace App\Presenters;

use Nette;

class ElfinderPresenter extends Nette\Application\UI\Presenter
{
    use \Ngscz\Elfinder\Presenters\ElfinderPresenter;

    public function renderDefault()
    {
        $template = $this->getTemplate();

        $template->SEPARATOR .
            'Elfinder' . DIRECTORY_SEPARATOR .
            'default.latte';
    }    

}



namespace App\Presenters;

use Nette\Application\UI;
use Ngscz\Elfinder\Forms\ElfinderInput;

class HomepagePresenter extends FrontendPresenter
{

    protected function createComponentForm()
    {
        $form = new UI\Form;
        $form->addComponent(new ElfinderInput, 'file');

        $form->addSubmit('submit');

        // how to set default values
        $qb = $this->assetTable->prepareQueryBuilder();
        $files = [];
        foreach ($qb->getQuery()->getResult() as $file) {
            $files[] = [
                'hash' => $file->getHash(),
                'url' => '/uploads' . $file->getPath(),
            ];
        }

        $form->setDefaults([
            'file' => $files,
        ]);

        $form->onSuccess[] = function($form, $values) {
            dumpe($values);
            //will return array of hashe
        };

        return $form;
    }

}

 declare(strict_types=1);

namespace App\CmsModule\Forms\Controls;

use App\Model\Asset\Asset;
use Nette\Utils\Json;
use Ngscz\Elfinder\Forms\ElfinderInput as BaseElfinderInput;

class ElfinderInput extends BaseElfinderInput
{
    public function __construct($caption, $assetTable)
    {
        parent::__construct($caption, $assetTable);

        $this->setOption(self::OPTION_LOCALES, [
            [
                'locale' => 'cs',
                'label' => 'Česky',
            ],
            [
                'locale' => 'en',
                'label' => 'Anglicky',
            ],
        ]);

        $this->setOption(self::OPTION_FIELDS, [
            [
                'name' => 'title',
                'type' => 'text',
                'label' => 'Název',
            ],
            [
                'name' => 'description',
                'type' => 'textarea',
                'label' => 'Popis',
            ],
        ]);
    }

    public function setValue($value)
    {
        parent::setValue($value);

        if ($this->files !== []) {
            $this->value = Json::encode($this->onLoad());
        }

        return $this;
    }

    private function onLoad(): array
    {
        $values = [];
        foreach ($this->files as $asset) {
            $formValue = [
                'hash' => $asset->getHash(),
                'url' => '/uploads' . $asset->getPath(),
            ];

            foreach ($this->getOption(self::OPTION_LOCALES) as $locale) {
                $translation = $asset->translate($locale['locale'], false);
                $formValue[$locale['locale']]['title'] = $translation->getTitle();
                $formValue[$locale['locale']]['description'] = $translation->getDescription();
            }

            $values[] = $formValue;
        }

        return $values;
    }

    public function onSave(): void
    {
        $values = $this->getValues();

        foreach ($values as $key => $formValue) {
            /** @var Asset $asset */
            $asset = $formValue['file'];

            foreach ($this->getOption(self::OPTION_LOCALES) as $locale) {
                $translation = $asset->translate($locale['locale'], false);
                $translation->setTitle($formValue[$locale['locale']]['title'] ?? null);
                $translation->setDescription($formValue[$locale['locale']]['description'] ?? null);
            }

            $asset->mergeNewTranslations();

            $values[$key]['file'] = $asset;
        }

        $this->values = $values;
    }
}


        $this->onSuccess[] = function (Form $form) use ($name): void {
            $form[$name]->onSave();
        };