geniv / nette-front-editor
Nette 框架的前端编辑扩展
v1.0.9
2019-02-01 23:33 UTC
Requires
- php: >=7.0.0
- geniv/nette-general-form: >=1.0.0
- geniv/nette-identity: >=1.0.0
- nette/nette: >=2.4.0
README
安装
$ composer require geniv/nette-front-editor
或
"geniv/nette-front-editor": ">=1.0.0"
require
"php": ">=7.0.0", "nette/nette": ">=2.4.0", "geniv/nette-general-form": ">=1.0.0", "geniv/nette-identity": ">=1.0.0"
包含在应用中
neon 配置
# front editor frontEditor: # autowired: true # formContainer: FrontEditor\FormContainer # adminLink: "admin/%routerPrefix.adminBaseUrl%"
neon 配置扩展
extensions: frontEditor: FrontEditor\Bridges\Nette\Extension
前端编辑 v.1
演示者
use FrontEditorControl; //$this->template->frontEditorEnable = $this->isFrontEditorEnable(); //$frontEditor->setAcl($this->isFrontEditorEnable()); //$frontEditor->getFrontEditorEnableHash(); //<a n:href="FrontEditorDisable!" n:if="$frontEditorEnable" class="ajax">odhlasi se z edit modu</a> //{control frontEditor.'-identText1'} protected function createComponentFrontEditor(FrontEditor $frontEditor): FrontEditor { $frontEditor->setTemplatePath(__DIR__ . '/templates/frontEditor.latte'); $frontEditor->setAcl($this->user->isAllowed($this->getName(), 'edit')); $frontEditor->setData($this['config']->getEditor(static::IDENTIFIER)); $frontEditor->onSuccess[] = function (Form $form, array $values) { try { if ($this['config']->setEditor(static::IDENTIFIER, $values['content'])) { $this->flashMessage($this->translator->translate('front-editor#onsuccess'), 'success'); } } catch (\Dibi\Exception $e) { $this->flashMessage($e->getMessage(), 'danger'); } $this->redirect('this'); }; return $frontEditor; }
使用方法
{control frontEditor}
前端编辑 v.2
前端演示者
use FrontEditorControl; protected function startup() { parent::startup(); $this->template->frontEditorEnable = $this->isFrontEditorEnable(); } protected function createComponentFrontEditor(FrontEditor $frontEditor): Multiplier { $frontEditor->setTemplatePath(__DIR__ . '/templates/frontEditor.latte'); $frontEditor->setAcl($this->isFrontEditorEnable()); return new Multiplier(function ($indexName) use ($frontEditor) { $data = $this['config']->getDataByIdent($indexName); if (!$data) { $this['config']->setEditor($indexName, $indexName); // create if not exists return $frontEditor; } // set type and add variable to frontEditor $frontEditor->getFormContainer()->setType($data['type']); $frontEditor->addVariableTemplate('type', $data['type']); $frontEditor->setData($data['content']); $frontEditor->onSuccess[] = function (Form $form, array $values) use ($data) { try { if ($this['config']->editData($data['id'], ['content' => $values['content']])) { $this->flashMessage('done', 'success'); } } catch (\Dibi\Exception $e) { $this->flashMessage($e->getMessage(), 'danger'); } $this->redirect('this'); }; return clone $frontEditor; }); }
前端使用方法
<a n:href="FrontEditorDisable!" n:if="$frontEditorEnable" class="ajax">logout edit mode</a> {control frontEditor.'-identText1'}
前端编辑 v.3
前端演示者
use FrontEditorControl; protected function startup() { parent::startup(); $this->template->frontEditorEnable = $this->isFrontEditorEnable(); } protected function createComponentFrontEditor(IFrontEditor $frontEditor): IFrontEditor { $frontEditor->setTemplatePath(__DIR__ . '/templates/frontEditor.latte'); $frontEditor->setTemplatePathLink(__DIR__ . '/templates/frontEditorLink.latte'); $frontEditor->setAcl($this->isFrontEditorEnable()); $frontEditor->onLoadData = function ($identification) use ($frontEditor) { if ($identification) { $data = $this['config']->getDataByIdent($identification); $frontEditor->getFormContainer()->setType($data['type']); $frontEditor->addVariableTemplate('type', $data['type']); return $data; } return null; }; $frontEditor->onLogout[] = function () { $this->handleFrontEditorDisable(); }; $frontEditor->onSuccess[] = function (Form $form, array $values) { try { if ($this['config']->editData((int) $values['id'], ['content' => $values['content']])) { $this['config']->cleanCache(); $this->flashMessage($this->translator->translate('front-editor#onsuccess'), 'success'); } else { $this->flashMessage($this->translator->translate('front-editor#onsuccess-fail'), 'danger'); } } catch (\Dibi\Exception $e) { $this->flashMessage($e->getMessage(), 'danger'); } $this->redirect('this'); }; return $frontEditor; }
前端使用方法
{control frontEditor:link 'identText1'} {control config:editor 'identText1'} {control frontEditor}
前端编辑管理员
管理员演示者
use FrontEditorControl; $this->template->frontEditorEnableLink = $this->getFrontEditorEnableLink();
管理员使用方法
<a href="{$baseUrl}/../{$frontEditorEnableLink}" title="{_'layout-front-editor-enable'}">link</a>