Last active
December 18, 2015 02:59
-
-
Save Kedrigern/5715087 to your computer and use it in GitHub Desktop.
Lokalizace: repository, presenter, formuláře
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {var $robots = noindex} | |
| {var $isAjax = $presenter->isAjax()} | |
| {block content} | |
| <h1 n:inner-block=title>Přidání způsobu platby</h1> | |
| {snippet modal} | |
| <fieldset> | |
| {form editForm style => "pading-bottom:0;margin-bottom:0"} | |
| {if $isAjax} | |
| <style> | |
| .form-error-message { | |
| display: block; | |
| color: red; | |
| } | |
| </style> | |
| <div class="modal-header"> | |
| <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> | |
| <h3 id="modalLabel">{include #title}</h3> | |
| </div> | |
| {/if} | |
| <div class="modal-body"> | |
| <div class="row-fluid"> | |
| {control $form errors} | |
| <dl class="dl-horizontal"> | |
| <dt>{label name /}</dt> | |
| <dd>{input name size => 30, autofocus => true}</dd> | |
| <dt>{label domain_id /}</dt> | |
| <dd>{input domain_id}</dd> | |
| </dl> | |
| <legend> | |
| Lokalizace | |
| <small>{input autotranslate} prázdné přeložit automaticky</small> {* Label se zalomí *} | |
| </legend> | |
| <div class="tabbable tabs-below"> | |
| <ul class="nav nav-tabs" n:inner-foreach="$languagesCodes as $lang"> | |
| <li n:class="$iterator->isFirst() ? active"> | |
| <a href="#{$lang}" data-toggle="tab">{$lang}</a> | |
| </li> | |
| </ul> | |
| <div class="tab-content" n:inner-foreach="$languagesCodes as $lang"> | |
| <div n:class="true ? tab-pane, $iterator->isFirst() ? active" id="{$lang}"> | |
| {var $name = 'locale-' . $lang . '-name'} | |
| {var $desc = 'locale-' . $lang . '-description'} | |
| <dl class="dl-horizontal"> | |
| <dt>{label $name /}</dt> | |
| <dd>{input $name size => 30, autofocus => true}</dd> | |
| <dt>{label $desc /}</dt> | |
| <dd>{input $desc}</dd> | |
| </dl> | |
| </div> | |
| </div> | |
| </div> | |
| </div> | |
| </div> | |
| <div class="modal-footer text-left"> | |
| {input save, class => "btn btn-primary"} | |
| {input cancel, class => "btn"} | |
| </div> | |
| {/form} | |
| </fieldset> | |
| {/snippet} | |
| {/block} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace BackendModule; | |
| class LocaleForm extends BaseForm | |
| { | |
| /** @var */ | |
| protected $repository; | |
| /** @var */ | |
| protected $langRep; | |
| /** @var */ | |
| protected $domainRep; | |
| /** @var callback*/ | |
| protected $onSucccessFunction; | |
| /** | |
| * @param $repository | |
| * @param $langRep | |
| * @param $domainRep | |
| */ | |
| public function __construct($repository, $langRep, $domainRep, $onSuccess = null) | |
| { | |
| parent::__construct(); | |
| $this->repository = $repository; | |
| $this->langRep = $langRep; | |
| $this->domainRep = $domainRep; | |
| $this->onSucccessFunction = $onSuccess; | |
| } | |
| /** | |
| * @param \Nette\Forms\Controls\SubmitButton $button | |
| */ | |
| public function formSuccess(\Nette\Forms\Controls\SubmitButton $button) | |
| { | |
| $values = $this->getValues(); | |
| if(!is_null($this->onSucccessFunction)) { | |
| return $this->onSucccessFunction($values); | |
| } else { | |
| return $this->defaultFormSuccess($values); | |
| } | |
| } | |
| public function defaultFormSuccess($values) | |
| { | |
| $id = (int)$this->presenter->getParameter('id'); | |
| $translate = $values['autotranslate']; | |
| unset($values['autotranslate']); | |
| try { | |
| if ($id) { | |
| $this->repository->update($values, $id); | |
| $this->presenter->flashMessage('Zaktualizovali jste položku.', 'success'); | |
| } else { | |
| if ($translate) { | |
| $this->translateForm($values['locale']); | |
| } | |
| $this->repository->insert($values); | |
| $this->presenter->flashMessage('Přidali jste položku.', 'success'); | |
| } | |
| $this->presenter->prepareAjaxDefault(); | |
| } catch (\Carty\KeyAlreadyUsedException $e) { | |
| $this->addError('Položku nelze uložit, protože doména s touto položkou již existuje.'); | |
| $this->presenter->setModal(); | |
| if ($this->presenter->isAjax()) { | |
| $this->presenter->invalidateControl("modal"); | |
| } | |
| } | |
| } | |
| /** @param \Nette\Forms\Controls\SubmitButton $button */ | |
| public function formCancelled(\Nette\Forms\Controls\SubmitButton $button) | |
| { | |
| $this->presenter->renderAjaxDefault(); | |
| } | |
| /** | |
| * @param array $fields like: | |
| * array( | |
| * array( | |
| * 'text' => '...' | |
| * 'name' => '...', | |
| * 'label' => '...' | |
| * ), | |
| * array(...) | |
| * ); | |
| */ | |
| protected function addLocFields(array $fields) | |
| { | |
| $this->addCheckbox('autotranslate', 'Prázdné přeložit automaticky') | |
| ->setDefaultValue(false); | |
| $defaultLang = $this->langRep->default; | |
| $locale = $this->addContainer('locale'); | |
| foreach ($this->langRep->codes as $lang) { | |
| $sub = $locale->addContainer($lang); | |
| foreach( $fields as $field) { | |
| if($field['type'] == 'text') { | |
| $f = $sub->addText($field['name'], $field['label']); | |
| } else { | |
| $f = $sub->addTextArea($field['name'], $field['label']); | |
| } | |
| if ($defaultLang == $lang) { | |
| $f->setRequired("Defaultní jazyk ($defaultLang) je potřeba vyplnit."); | |
| } else { | |
| $f ->addConditionOn($this['autotranslate'], self::EQUAL, FALSE) | |
| ->addRule(\Nette\Forms\Form::FILLED, 'Je potřeba vyplnit %label'); | |
| } | |
| } | |
| } | |
| } | |
| /** | |
| * @param string[] $values like: | |
| * array( | |
| * 'cs' => array('name' => 'jméno, 'description' => 'Nějaký popis'), | |
| * 'en' => array('name' => 'name', 'description' => 'Some description', | |
| * 'de' => array(...) | |
| * ); | |
| * @param string|null $default language, if null then default language is used | |
| */ | |
| protected function translateForm($values, $default = null) | |
| { | |
| $default = is_null($default) ? $this->langRep->getDefault() : $default; | |
| $translator = new \gTranslator('AIzaSyCaawHOjgMbZOEOeOxdPdXn_TSQ_Bg85OA'); | |
| foreach ($this->langRep->codes as $code) { | |
| if ($code == $default) continue; | |
| foreach ($values[$code] as $column => $value) { | |
| if (empty($values[$code][$column]) and !empty($values[$default][$column])) { | |
| $values[$code][$column] = $translator->translate($values[$default][$column], $code); | |
| } | |
| } | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace BackendModule; | |
| class DeliveryForm extends LocaleForm | |
| { | |
| /** | |
| * @param $repository | |
| * @param $langRep | |
| * @param $domainRep | |
| */ | |
| public function __construct($repository, $langRep, $domainRep) | |
| { | |
| parent::__construct($repository, $langRep, $domainRep); | |
| /****** Form ******/ | |
| $this->addText('name', 'Jméno pro admin:') | |
| ->isRequired(); | |
| $this->addSelect('domain_id', 'Doména:', $this->domainRep->names) | |
| ->isRequired(); | |
| /****** Form localization ******/ | |
| $this->addLocFields(array( | |
| array( | |
| 'type' => 'text', | |
| 'name' => 'name', | |
| 'label' => 'Jméno' | |
| ), | |
| array( | |
| 'type' => 'textarea', | |
| 'name' => 'description', | |
| 'label' => 'Popis' | |
| ) | |
| )); | |
| /****** Form buttons ******/ | |
| $this->addSubmit('save', 'Uložit') | |
| ->setAttribute('class', 'default') | |
| ->onClick[] = $this->formSuccess; | |
| $this->addSubmit('cancel', 'Zrušit') | |
| ->setValidationScope(NULL) | |
| ->onClick[] = $this->formCancelled; | |
| $this->addProtection(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace BackendModule; | |
| final class SomePresenter extends BasePresenter | |
| { | |
| /** @var **/ | |
| private $repository; | |
| /** @var \Nette\Database\Tabe\ActiveRow **/ | |
| private $row; | |
| /** @return \Nette\Application\UI\Form */ | |
| protected function createComponentEditForm() | |
| { | |
| return new DeliveryForm($this->deliveryPaymentRepository, $this->languageRepository, $this->domainRepository); | |
| } | |
| /** @param int $id */ | |
| private function findItem($id) | |
| { | |
| $this->row = $this->deliveryShippingRepository->getItemLocale($id); | |
| if(!$this->row) { | |
| $this->error('Položka nenalezena.'); | |
| } | |
| return $this->row; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace Some; | |
| class LocaleRepository extends BaseRepository | |
| { | |
| /** | |
| * @param array|Traversable $data | |
| * @return \Nette\Database\Table\ActiveRow|void | |
| */ | |
| public function insert($data) | |
| { | |
| $locales = clone $data['locale']; | |
| unset($data['locale']); | |
| parent::insert($data); | |
| $this->upsertLocale($locales, $id); | |
| return $this->getItemLocale($id); | |
| } | |
| /** | |
| * @param array|Traversable $data | |
| * @param int $id | |
| */ | |
| public function update($data, $id) | |
| { | |
| $locales = clone $data['locale']; | |
| unset($data['locale']); | |
| $row = parent::update($data, $id); | |
| $this->upsertLocale($locales, $row->id); | |
| } | |
| /** | |
| * @param int $id | |
| * @return \Nette\Database\Table\ActiveRow | |
| */ | |
| public function getItemLocale($id) | |
| { | |
| $row = $this->getTable()->get($id); | |
| $row['locale'] = $this | |
| ->connection | |
| ->table($this->getNamespace() . '_locale') | |
| ->select('*') | |
| ->where($this->getNamespace() . '_id = ?', $id) | |
| ->fetchPairs('language_code'); | |
| return $row; | |
| } | |
| /** | |
| * @param array $data | |
| * @param null|int $id | |
| */ | |
| public function upsertLocale($dataLoc, $id = null) | |
| { | |
| foreach($dataLoc as $lang => $columns) { | |
| $row = $this->getLocaleTable() | |
| ->where($this->getNamespace() . '_id = ?', $id) | |
| ->where('language_code = ?', $lang) | |
| ->fetch(); | |
| if( $row ) { | |
| $row->update($columns); | |
| } else { | |
| $this->getLocaleTable()->insert( | |
| array_merge( | |
| (array) $columns, | |
| array( | |
| 'language_code' => $lang, | |
| $this->getNamespace() . '_id' => $id | |
| ) | |
| ) | |
| ); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment