ngscz/nette-elfinder
Composer 安装命令:
composer require ngscz/nette-elfinder
包简介
Elfinder implementation to Nette framework
关键字:
README 文档
README
Installation
- Add this options to composer.json
"require": {
"ngscz/nette-elfinder": "dev-master"
},
"repositories": [
{
"type": "git",
"url": "https://github.com/ngscz/nette-elfinder.git"
}
],
- Add script options to composer.json
"scripts": {
"ngs-elfinder-move-assets": [
"cp -r vendor/ngscz/nette-elfinder/assets www",
"mkdir -p www/assets/vendor/elfinder",
"cp -r vendor/studio-42/elfinder/css www/assets/vendor/elfinder",
"cp -r vendor/studio-42/elfinder/js www/assets/vendor/elfinder",
"cp -r vendor/studio-42/elfinder/img www/assets/vendor/elfinder"
],
"post-install-cmd": [
"@ngs-elfinder-move-assets"
]
}
- Update your composer dependencies
composer update
Script above will copy required assets to public (www) directory. If it is not run automatically, you should run:
composer run-script ngs-elfinder-move-assets
Add extension configuration to config.neon
extensions:
ngs.elfinder: Ngscz\Elfinder\DI\ElfinderExtension
Create Elfinder presenter and use trait ElfinderPresenter
<?php
namespace App\Presenters;
use Nette;
class ElfinderPresenter extends Nette\Application\UI\Presenter
{
use \Ngscz\Elfinder\Presenters\ElfinderPresenter;
public function renderDefault()
{
$template = $this->getTemplate();
$template->includePath = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR .
'vendor' . DIRECTORY_SEPARATOR .
'ngscz' . DIRECTORY_SEPARATOR .
'nette-elfinder' . DIRECTORY_SEPARATOR .
'src' . DIRECTORY_SEPARATOR .
'Presenters' . DIRECTORY_SEPARATOR .
'templates' . DIRECTORY_SEPARATOR .
'Elfinder' . DIRECTORY_SEPARATOR .
'default.latte';
}
}
Add Elfinder presenter to your Router
$router[] = new Route('elfinder/<action>', 'Elfinder:default');
create template templates/Elfinder/default.latte and include core default.latte file (use $includePath variable created in renderDefault method)
etc:
{include $includePath}
also you have to add JS hook to bind filemanager to your CMS @layout.latte
<script src="assets/vendor/ngscz-elfinder/js/ElfinderInputHook.js"></script>
Example, how to add elfinder to Form
<?php
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;
}
}
Example, how to add filters based on file mimeType
$control = new ElfinderInput($label, $assetTable);
$control->onlyMimes(['image', 'audio']); //to show only images and audio files
list of possible filters:
['image', 'audio', 'application', 'text', 'video']
see main types: https://cs.wikipedia.org/wiki/Typ_internetov%C3%A9ho_m%C3%A9dia#N%C4%9Bkter%C3%A9_%C4%8Dasto_pou%C5%BE%C3%ADvan%C3%A9_typy_m%C3%A9di%C3%AD
Custom Elfinder input with custom attributes
<?php 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;
}
}
and then bind onSuccess event
$this->onSuccess[] = function (Form $form) use ($name): void {
$form[$name]->onSave();
};
@todo
Add example how to save data to DB (Uploader)
ngscz/nette-elfinder 适用场景与选型建议
ngscz/nette-elfinder 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 17.91k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2021 年 01 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「elfinder」 「nette」 「filemanager」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ngscz/nette-elfinder 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ngscz/nette-elfinder 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ngscz/nette-elfinder 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
The file manager intended for using Laravel with CKEditor / TinyMCE / Colorbox
Simple, fast, standalone PHP lib, that helps You to define, if any of your files were modified since last time You check - Is this file FRESH?
A Flysystem Driver for elFinder extended version
Nette Framework adapter for I18n package
Effortless file management with Symfony UX and Mezcalito UX FileManager
Build forms from schema
统计信息
- 总下载量: 17.91k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 11
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-01-06