Skip to content

Instantly share code, notes, and snippets.

@duskohu
Last active December 29, 2015 10:49
Show Gist options
  • Save duskohu/7659704 to your computer and use it in GitHub Desktop.
Save duskohu/7659704 to your computer and use it in GitHub Desktop.
BaseForm
<?php
namespace Nas;
use Kdyby\BootstrapFormRenderer\BootstrapRenderer;
use NasExt\Forms\Controls\RangeSlider;
use NasExt\Templating\ITemplateFilesFormatter;
use Nette\Application\UI\Control;
use Nette\ComponentModel\IContainer;
use Nette\Forms\Controls\SuggestionInput;
use Nette\Latte\Engine;
use Nette\Templating\ITemplate;
use Nette\Templating\Template;
class BaseFormControl extends Control
{
/** @var string */
protected $templateFile;
/** @var ITemplateFilesFormatter */
protected $templateFilesFormatter;
/**
* @param IContainer $parent
* @param string|null $name
*/
public function __construct(IContainer $parent = NULL, $name = NULL)
{
parent::__construct($parent, $name);
// SuggestionInput
SuggestionInput::register();
// RangeSlider
RangeSlider::register();
}
/**
* INJECT TemplateFilesFormatter
* @param ITemplateFilesFormatter $templateFilesFormatter
*/
public function injectTemplateFilesFormatter(ITemplateFilesFormatter $templateFilesFormatter)
{
$this->templateFilesFormatter = $templateFilesFormatter;
}
/**
* @param string $templateFile
* @return $this
*/
public function setFileTemplate($templateFile)
{
$this->templateFile = $templateFile;
return $this;
}
/**
* @param Template $template
*/
public function templatePrepareFilters($template)
{
/** @var Engine $latte */
$latte = $this->getPresenter()->getContext()->createService('nette.latte');
\Kdyby\BootstrapFormRenderer\Latte\FormMacros::install($latte->getCompiler());
$template->registerFilter($latte);
}
/**
* @param string|NULL
* @return ITemplate
*/
protected function createTemplate($class = NULL)
{
$template = parent::createTemplate($class);
$template->_form = $template->form = $this['form'];
// Implementacia \Kdyby\BootstrapFormRenderer
$this['form']->setRenderer(new BootstrapRenderer());
$template->setFile($this->templateFilesFormatter(....));
return $template;
}
}
<?php
namespace Nas;
use ...
class BlockFormControl extends BaseFormControl
{
/** @var BlockRepository */
private $blockRepository;
/** @var ActiveRow */
private $blockEntity;
/**
* @param BlockRepository $blockRepository
*/
public function __construct(BlockRepository $blockRepository
{
parent::__construct();
$this->blockRepository = $blockRepository;
}
/**
* setBlockEntity
* @param bool|ActiveRow $blockEntity
* @return BlockFormControl provides fluent interface
*/
public function setBlockEntity($blockEntity = FALSE)
{
$this->blockEntity = $blockEntity;
return $this;
}
/**
* FORM block
* @return Form
*/
protected function createComponentForm()
{
$form = new Form;
......
return $form;
}
/**
* PROCESS-SUBMIT-ADD-FORM - add new block
* @param Form $form
*/
public function processAddSubmit(Form $form)
{
.....
}
/**
* PROCESS-SUBMIT-EDIT-FORM - edit block item
* @param Form $form
*/
public function processEditSubmit(Form $form)
{
.....
}
/**
* HANDLE - DeleteBlock
*/
public function handleDeleteBlock()
{
.....
}
/**
* RENDER
*/
public function render()
{
$this->template->blockEntity = $this->blockEntity;
$this->template->render();
}
}
/**
* IBlockFormControl
*/
interface IBlockFormControl
{
/**
* @return BlockFormControl
*/
function create();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment