Giter Club home page Giter Club logo

nette-front-editor's Introduction

Front editor

Installation

$ composer require geniv/nette-front-editor

or

"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"

Include in application

neon configure:

# front editor
frontEditor:
#   autowired: true
#   formContainer: FrontEditor\FormContainer
#   adminLink: "admin/%routerPrefix.adminBaseUrl%"

neon configure extension:

extensions:
    frontEditor: FrontEditor\Bridges\Nette\Extension

front editor v.1:

presenters:

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;
}

usage:

{control frontEditor}

front editor v.2:

presenters front:

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;
    });
}

usage front:

<a n:href="FrontEditorDisable!" n:if="$frontEditorEnable" class="ajax">logout edit mode</a>

{control frontEditor.'-identText1'}

front editor v.3:

presenters front:

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;
}

usage front:

{control frontEditor:link 'identText1'} 
{control config:editor 'identText1'}

{control frontEditor}

front editor admin:

presenters admin:

use FrontEditorControl;

$this->template->frontEditorEnableLink = $this->getFrontEditorEnableLink();

usage admin:

<a href="{$baseUrl}/../{$frontEditorEnableLink}" title="{_'layout-front-editor-enable'}">link</a>

nette-front-editor's People

Contributors

geniv avatar martinfugess avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.