/ Gists / Nette-Tricks

Gists - Nette-Tricks

On gists

Auto komponenty (Parent -> Children)

Nette Nette-Controls Nette-Tricks

ParentControl.php #

<?php

class OrderAction extends FrontControl 
{
  
  	public function __construct(, 
		Order $order, 
		Model\OrderAction $orderAction,
		Container $container
	) 
	{
		parent::__construct();

		$this->order = $order;
		$this->orderAction = $orderAction;
		$this->actions = $this->orderAction->getActions($this->pageByState);
	}
	
	
	
	public function render(array $config = [])
	{
		$config = $this->getCurrentConfig($config);

		$this->template->order = $this->order;
		$this->template->actions = $this->actions;

		$this->vueRender($config);
	}

	public function createComponent(string $name): ?Nette\ComponentModel\IComponent
	{
		if (!Strings::startsWith($name, 'action')) {
			return parent::createComponent($name);
		}

		return $this->container->createInstance('App\\FrontModule\\Components\\' . $this->actions[Strings::after($name, 'action')]->action, [
			'orderAction' => $this->actions[Strings::after($name, 'action')],
			'order' => $this->order
		]);
	}
	

}

On gists

Automatické vytvoření komponenty s @autowiringem // AW

Nette-Controls Nette-Tricks AW

auto-create.php #

<?php

	public function createComponentOrderReview()
	{
		return $this->getPresenter()->getControlFactory()->create($this, 'orderReview', [
			'order' => $this->getOrder()
		]);
	}
	
	
	    // from same category
    public function createComponentSameCategoryProducts()
    {
        $config = $this->getCurrentConfig();
        $product = $this->getProduct();
        $component = parent::createComponent($config['category_list_component']);
        $component->setConfig([
            'except_ids' => [
                $product::INHERITED ? $product->product_id : $product->id,
                $product->item__product_id,
                $product->inherit__product_id
            ],
            'navigation_id' => $product->getMainCat()->id,
            'default_filter' => []
        ]);
        return $component;
    }

On gists

Dohledani dat z jine kontrolky (template)

Nette-Controls Nette-Tricks Nette-Latte

some-file.latte #

{var $mailData = $control->lookup('App\FrontModule\Components\MailTemplateWrapper')->template}
{var $settings = $presenter->getContext()->getByType(App\EshopModule\Model\Settings::class)->getSetting()}



On gists

CheckboxList - booleaned all values

PHP Nette-Forms Nette-Tricks

example.php #

<?php
/* https://forum.nette.org/cs/35218-muze-checkboxlist-vracet-associativni-pole-misto-indexoveho#p220072 */

0 => 'po'
1 => 'st'
2 => 'ct'


po => true
ut => false
st => true
ct => true
pa => false


array_walk($arr, fn(&$item, $key, $values) => $item = in_array($key, $values), $values);

On gists

Abort exception

Nette Nette-Tricks

abort.php #

<?php


try
		{
		  // ...
			$this->redirect('PresenterWhatEver:default');

		}
		catch (\Exception $e)
		{
			// posle se to dal kdyz premserujeme - redirect vyhazuje abortException ...
			if ($e instanceof Nette\Application\AbortException)
			{
				throw $e;
			}
		}

On gists

Store/Restore Request

Nette Nette-Tricks

Presenter.php #

<?php

	public function actionDefault($back = null) 
	{
		

		if ($back)
			$this->restoreRequest($back);

		if ($this->presenter->navigation->navItem['alias'] == 'allOpenCalls')
		{
			
			$parentId = $this->presenter->navigation->navItem['parent__navigation_id'];
			$nav = $this->navigation->getById($parentId);
			$children = $nav->getChildren();

			$this->config['navigation_id'] = array_keys($children);

		}


		$this->template->back = $this->storeRequest();

	}

On gists

Přihlášení backend usera do frontu a obráceně

Nette Nette-Tricks

backend-frontend-user.php #

<?php

// https://forum.nette.org/cs/34290-prihlaseni-admina-jako-uzivatel
// bez přepisování $user proměné 
public function handleKlientLogin($hash)
{
	overim hash a nactu data klienta

	$user = $this->getUser();
	$user->getStorage()->setNamespace('frontend');
	$user->login(new Identity($user->id, $user->role, ['username' => $user->username]));
	$this->redirect(....);
}

On gists

Translator - null

Nette-Forms Nette-Tricks

translator.php #

<?php

$form['yearofbirth']->setTranslator(NULL);

On gists

Neon - konfigurace přes službu a metody

Nette Nette-Neon Nette-Tricks

config.neon #

services:
    - Config
reCaptcha:
    siteKey: @Config::getSiteKey()
    secretKey: @Config::getSecretKey()
    methodName: 'addReCaptcha' # optional

On gists

Jakým tlačítkem byl odeslán formulář? SubmittedBy() ?

Nette Nette-Forms Nette-Tricks

submitted-by.php #

<?php

if ($form['submit']->isSubmittedBy()) {
    // ...
}
or

if ($form->isSubmitted() === $form['submit']) {
    // ...
}

or

public function validate($form)
{
	if ($form->isSubmitted()->getName() == 'reloadSelect')
		return;
}