refactor
This commit is contained in:
@@ -3,8 +3,115 @@
|
||||
namespace App\Modules\Front;
|
||||
|
||||
use App\Modules\Base\UnsecuredPresenter;
|
||||
use Nette;
|
||||
use Nette\Utils\Strings;
|
||||
use Nette\Application\UI;
|
||||
use Dict\OggResponse;
|
||||
use Nette\Application\Responses\JsonResponse;
|
||||
use AlesWita\VisualPaginator\VisualPaginator;
|
||||
use AlesWita\VisualPaginator\VisualPaginatorFactory;
|
||||
use App\Model\Database\EntityManager;
|
||||
use App\Model\Database\Entity\Dictionary;
|
||||
use App\Model\Database\Entity\Translation;
|
||||
use App\Model\Database\Repository\TranslationRepository;
|
||||
use App\Model\Database\Repository\TermsFullQuery;
|
||||
use App\Model\Database\Repository\TermsQuickQuery;
|
||||
use App\Model\Database\Facade\TermFacade;
|
||||
|
||||
abstract class BaseFrontPresenter extends UnsecuredPresenter
|
||||
{
|
||||
/** @inject @var EntityManager */
|
||||
public EntityManager $em;
|
||||
/** @inject */
|
||||
public VisualPaginatorFactory $visualPaginatorFactory;
|
||||
/** @inject */
|
||||
public TermFacade $termFacade;
|
||||
|
||||
/** @persistent */
|
||||
public $term;
|
||||
|
||||
/** @persistent */
|
||||
public $slug;
|
||||
|
||||
public $translation;
|
||||
public $translations;
|
||||
public $dictionaries;
|
||||
public $drsSelect;
|
||||
public $slugs;
|
||||
|
||||
protected function startup()
|
||||
{
|
||||
$trs = $this->em->getRepository(Translation::class)->findBy([], ['id' => 'ASC', 'direction' => 'ASC']);
|
||||
$drs = $this->em->getRepository(Dictionary::class)->findPairs('id', 'name');
|
||||
$select = [];
|
||||
$translations = [];
|
||||
foreach ($trs as $t) {
|
||||
$lang1 = $t->getLangName1();
|
||||
$lang2 = $t->getLangName2();
|
||||
|
||||
$lang = mb_substr($lang1, 0, -1);
|
||||
$lang .= "o-" . $lang2;
|
||||
$slug = Strings::webalize($lang);
|
||||
$select[$drs[$t->getDictionary()->getId()]][$t->getId()] = $lang;
|
||||
$translations[$t->getId()]["slug"] = $slug;
|
||||
$translations[$t->getId()]["lang"] = $lang;
|
||||
$translations[$t->getId()]["t"] = $t;
|
||||
$slugs[$slug] = $t;
|
||||
}
|
||||
$this->translations = $translations;
|
||||
$this->dictionaries = $drs;
|
||||
$this->drsSelect = $select;
|
||||
$this->slugs = $slugs;
|
||||
|
||||
parent::startup();
|
||||
}
|
||||
|
||||
public function handleGetInfo($id)
|
||||
{
|
||||
$term = $this->repoTerms->find($id);
|
||||
$termFlags = $this->repoTermFlags->findAssoc([], 'id');
|
||||
|
||||
$dictionary = $term->dictionary;
|
||||
|
||||
$query = new TermsFullQuery($dictionary, $term->string1, 1, false);
|
||||
$result = $this->repoTerms->fetch($query);
|
||||
|
||||
$this->template->result = $result;
|
||||
$this->template->termFlags = $termFlags;
|
||||
|
||||
$this->setView('info');
|
||||
}
|
||||
|
||||
public function beforeRender()
|
||||
{
|
||||
$dicttypes = $this->em->getDictTypeRepository()->findAssoc([], 'id');
|
||||
$this->template->dicttypes = $dicttypes;
|
||||
$this->template->dictionaries = $this->dictionaries;
|
||||
$this->template->translations = $this->translations;
|
||||
// $this->template->paginator = $this->paginator;
|
||||
}
|
||||
|
||||
protected function createComponentSearchForm()
|
||||
{
|
||||
$form = new UI\Form;
|
||||
$form->addText('string', 'Výraz')
|
||||
->setRequired('Zadajte výray');
|
||||
$form->addSelect('translation', 'Preklad', $this->drsSelect)
|
||||
->setDefaultValue(1)
|
||||
->setRequired('Vyberte slovnik');
|
||||
$form->addText('clen', 'Clen', 5);
|
||||
$form->addSubmit('search', 'Vyhaľadaj');
|
||||
$form->onSuccess[] = array($this, 'searchFormSucceeded');
|
||||
return $form;
|
||||
}
|
||||
|
||||
public function searchFormSucceeded(UI\Form $form, $values)
|
||||
{
|
||||
$translation = $values->translation;
|
||||
$clen = $values->clen;
|
||||
$term = $values->string;
|
||||
|
||||
$this->redirect('Search:default',$translation,$term,$clen);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,74 +22,6 @@ final class HomePresenter extends BaseFrontPresenter
|
||||
{
|
||||
|
||||
|
||||
/** @inject @var EntityManager */
|
||||
public EntityManager $em;
|
||||
/** @inject */
|
||||
public VisualPaginatorFactory $visualPaginatorFactory;
|
||||
/** @inject */
|
||||
public TermFacade $termFacade;
|
||||
|
||||
|
||||
private $translations;
|
||||
private $dictionaries;
|
||||
private $drsSelect;
|
||||
private $slugs;
|
||||
public $translation;
|
||||
|
||||
/** @persistent */
|
||||
public $term;
|
||||
|
||||
/** @persistent */
|
||||
public $slug;
|
||||
|
||||
protected function startup()
|
||||
{
|
||||
$trs = $this->em->getRepository(Translation::class)->findBy([], ['id' => 'ASC', 'direction' => 'ASC']);
|
||||
$drs = $this->em->getRepository(Dictionary::class)->findPairs('id', 'name');
|
||||
$select = [];
|
||||
foreach ($trs as $t) {
|
||||
$lang1 = $t->getLangName1();
|
||||
$lang2 = $t->getLangName2();
|
||||
|
||||
$lang = mb_substr($lang1, 0, -1);
|
||||
$lang .= "o-" . $lang2;
|
||||
$slug = Strings::webalize($lang);
|
||||
$select[$drs[$t->getDictionary()->getId()]][$t->getId()] = $lang;
|
||||
$translations[$t->getId()]["slug"] = $slug;
|
||||
$translations[$t->getId()]["lang"] = $lang;
|
||||
$translations[$t->getId()]["t"] = $t;
|
||||
$slugs[$slug] = $t;
|
||||
}
|
||||
$this->translations = $translations;
|
||||
$this->dictionaries = $drs;
|
||||
$this->drsSelect = $select;
|
||||
$this->slugs = $slugs;
|
||||
|
||||
parent::startup();
|
||||
}
|
||||
|
||||
protected function doSearch($query, $translation, $term, $clen = null, $paginate = true)
|
||||
{
|
||||
$t = $this->em->getTranslationRepository()->find($translation);
|
||||
|
||||
if ($term) $this->term = $term;
|
||||
if ($translation) $this->translation = $translation;
|
||||
$this->slug = $this->translations[$translation]["slug"];
|
||||
|
||||
$this->termFacade->setDirection(1);
|
||||
|
||||
if ($paginate) {
|
||||
$this["paginator"]->setItemCount(500);
|
||||
$offset = $this["paginator"]->getOffset();
|
||||
$itemsPerPage = $this['paginator']->getItemsPerPage();
|
||||
}
|
||||
$q = $this->termFacade->findByString($term, $t->dictionary, $clen);
|
||||
$q->setMaxResults(500)->setFirstResult($offset)->getMaxResults($itemsPerPage);
|
||||
|
||||
$this->template->translation = $t;
|
||||
|
||||
return $q->getResult();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return AlesWita\Components\VisualPaginator
|
||||
@@ -107,31 +39,7 @@ final class HomePresenter extends BaseFrontPresenter
|
||||
return $paginator;
|
||||
}
|
||||
|
||||
protected function createComponentSearchForm()
|
||||
{
|
||||
$form = new UI\Form;
|
||||
$form->addText('string', 'Výraz')
|
||||
->setRequired('Zadajte výray');
|
||||
$form->addSelect('translation', 'Preklad', $this->drsSelect)
|
||||
->setDefaultValue(1)
|
||||
->setRequired('Vyberte slovnik');
|
||||
$form->addText('clen', 'Clen', 5);
|
||||
$form->addSubmit('search', 'Vyhaľadaj');
|
||||
$form->onSuccess[] = array($this, 'searchFormSucceeded');
|
||||
return $form;
|
||||
}
|
||||
|
||||
public function searchFormSucceeded(UI\Form $form, $values)
|
||||
{
|
||||
$translation = $values->translation;
|
||||
$clen = $values->clen;
|
||||
$term = $values->string;
|
||||
|
||||
$result = $this->doSearch('TermsQuery', $translation, $term, $clen);
|
||||
$this->term = $term;
|
||||
dump($result);
|
||||
$this->template->result = $result;
|
||||
}
|
||||
|
||||
public function searchFormSucceededIpa(UI\Form $form, $values)
|
||||
{
|
||||
@@ -162,14 +70,6 @@ final class HomePresenter extends BaseFrontPresenter
|
||||
return $this->sendResponse($oggResp);
|
||||
}
|
||||
|
||||
public function beforeRender()
|
||||
{
|
||||
$dicttypes = $this->em->getDictTypeRepository()->findAssoc([], 'id');
|
||||
$this->template->dicttypes = $dicttypes;
|
||||
$this->template->dictionaries = $this->dictionaries;
|
||||
$this->template->translations = $this->translations;
|
||||
// $this->template->paginator = $this->paginator;
|
||||
}
|
||||
|
||||
public function actionDefault()
|
||||
{
|
||||
@@ -179,7 +79,6 @@ final class HomePresenter extends BaseFrontPresenter
|
||||
$result = $this->doSearch('TermsQuery',$t->id,$this->term);
|
||||
if ($result) $this->template->result = $result;
|
||||
} else {
|
||||
//dumpe($this);
|
||||
$this->term = $this['searchForm']['string']->getValue();
|
||||
}
|
||||
$this['searchForm']->onSuccess[] = array($this, 'searchFormSucceeded');
|
||||
@@ -213,21 +112,7 @@ final class HomePresenter extends BaseFrontPresenter
|
||||
$this->setView('ipa');
|
||||
}
|
||||
|
||||
public function handleGetInfo($id)
|
||||
{
|
||||
$term = $this->repoTerms->find($id);
|
||||
$termFlags = $this->repoTermFlags->findAssoc([], 'id');
|
||||
|
||||
$dictionary = $term->dictionary;
|
||||
|
||||
$query = new TermsFullQuery($dictionary, $term->string1, 1, false);
|
||||
$result = $this->repoTerms->fetch($query);
|
||||
|
||||
$this->template->result = $result;
|
||||
$this->template->termFlags = $termFlags;
|
||||
|
||||
$this->setView('info');
|
||||
}
|
||||
|
||||
|
||||
public function renderDefault()
|
||||
{
|
||||
|
||||
@@ -1,75 +1 @@
|
||||
{block head}
|
||||
<script>
|
||||
$( document ).ready(function() {
|
||||
console.log( "ready!" );
|
||||
$("tr[id*='term']").on("click",function () {
|
||||
var id = this.id + '-info';
|
||||
if ($('#' + id).length == 0) {
|
||||
$(this).after('<tr id=' + '"' + id + '"></tr>');
|
||||
console.log('Append ' + id);
|
||||
$.ajax({
|
||||
url: $(this).attr('data-url'),
|
||||
context: $('#' + id)
|
||||
}).done(function (response) {
|
||||
console.log(response);
|
||||
$(this).html("<td colspan='4'>" + response + "</td>");
|
||||
$("img.ajax").on("click", function (event) {
|
||||
event.preventDefault();
|
||||
id = $(this).attr("data-sound-id");
|
||||
var audioElement = document.createElement('audio');
|
||||
var link = {link Home:play 1234567};
|
||||
audioElement.setAttribute('src', link.replace('1234567',id));
|
||||
console.log("Play sound:" + id);
|
||||
audioElement.play();
|
||||
});
|
||||
});
|
||||
|
||||
}
|
||||
});
|
||||
});
|
||||
</script>
|
||||
{/block}
|
||||
{block content}
|
||||
<div id="content">
|
||||
<h2>Zadaj vyhladavany vyraz.</h2>
|
||||
|
||||
<div class="boxes">
|
||||
{form searchForm}
|
||||
<table>
|
||||
<tr>
|
||||
<th>{label string /}</th><th>{label translation /}</th><th>{label clen /}</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>{input string}</td><td>{input translation}</td><td>{input clen}</td><td>{input search}</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
{ifset $result}
|
||||
|
||||
<table class="e-table" n:snippet="termsComnainer">
|
||||
<tr>
|
||||
<th>Vyraz</th><th>Preklad</th><th>Typ</th><th>Member</th>
|
||||
</tr>
|
||||
{foreach $result as $term}
|
||||
|
||||
<tr data-url="{link getInfo! $term->id}" n:snippet="term-$term->id">
|
||||
{if $translation->getDirection() == 1}
|
||||
<td>{$term->string1}{if $term->suffix1} {$term->suffix1->text}{/if}</td>
|
||||
<td>{$term->string2}{if $term->suffix2} {$term->suffix2->text}{/if}</td>
|
||||
{else}
|
||||
<td>{$term->string2}{if $term->suffix2} {$term->suffix2->text}{/if}</td>
|
||||
<td>{$term->string1}{if $term->suffix1} {$term->suffix1->text}{/if}</td>
|
||||
{/if}
|
||||
<td n:attr="title => $term->type ? $dicttypes[$term->type->id]->fullName : null">{$term->type ? $dicttypes[$term->type->id]->shortName : ''}</td>
|
||||
<td>{$term->member}</td>
|
||||
|
||||
</tr>
|
||||
{/foreach}
|
||||
</table>
|
||||
{control paginator}
|
||||
{/ifset}
|
||||
{/form}
|
||||
</div>
|
||||
{/block}
|
||||
|
||||
|
||||
{block #content}
|
||||
@@ -51,8 +51,6 @@ $(function() {
|
||||
{/block}
|
||||
|
||||
{block content}
|
||||
<div id="content">
|
||||
<h2>Zadaj vyhladavany vyraz.</h2>
|
||||
|
||||
<div class="boxes">
|
||||
{form searchForm}
|
||||
|
||||
@@ -15,20 +15,11 @@ $( document ).ready(function() {
|
||||
</script>
|
||||
{/block}
|
||||
|
||||
{block content}
|
||||
<div id="content">
|
||||
<h2>Zadaj vyhladavany vyraz.</h2>
|
||||
|
||||
<div class="boxes">
|
||||
{form searchForm}
|
||||
<table>
|
||||
<tr>
|
||||
<th>{label string /}</th><th>{label translation /}</th><th>{label clen /}</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>{input string}</td><td>{input translation}</td><td>{input clen}</td><td>{input search}</td>
|
||||
</tr>
|
||||
</table>
|
||||
{block content}
|
||||
{block form}
|
||||
|
||||
{/block}
|
||||
{ifset $result}
|
||||
<table class="trans">
|
||||
<tr>
|
||||
|
||||
64
app/modules/Front/Search/templates/default.latte
Normal file
64
app/modules/Front/Search/templates/default.latte
Normal file
@@ -0,0 +1,64 @@
|
||||
{block head}
|
||||
<script>
|
||||
$( document ).ready(function() {
|
||||
console.log( "ready!" );
|
||||
$("tr[id*='term']").on("click",function () {
|
||||
var id = this.id + '-info';
|
||||
if ($('#' + id).length == 0) {
|
||||
$(this).after('<tr id=' + '"' + id + '"></tr>');
|
||||
console.log('Append ' + id);
|
||||
$.ajax({
|
||||
url: $(this).attr('data-url'),
|
||||
context: $('#' + id)
|
||||
}).done(function (response) {
|
||||
console.log(response);
|
||||
$(this).html("<td colspan='4'>" + response + "</td>");
|
||||
$("img.ajax").on("click", function (event) {
|
||||
event.preventDefault();
|
||||
id = $(this).attr("data-sound-id");
|
||||
var audioElement = document.createElement('audio');
|
||||
var link = {link Home:play 1234567};
|
||||
audioElement.setAttribute('src', link.replace('1234567',id));
|
||||
console.log("Play sound:" + id);
|
||||
audioElement.play();
|
||||
});
|
||||
});
|
||||
|
||||
}
|
||||
});
|
||||
});
|
||||
</script>
|
||||
{/block}
|
||||
{block content}
|
||||
|
||||
|
||||
<div class="boxes">
|
||||
|
||||
{ifset $result}
|
||||
|
||||
<table class="e-table" n:snippet="termsComnainer">
|
||||
<tr>
|
||||
<th>Vyraz</th><th>Preklad</th><th>Typ</th><th>Member</th>
|
||||
</tr>
|
||||
{foreach $result as $term}
|
||||
|
||||
<tr data-url="{link getInfo! $term->id}" n:snippet="term-$term->id">
|
||||
{if $translation->getDirection() == 1}
|
||||
<td>{$term->string1}{if $term->suffix1} {$term->suffix1->text}{/if}</td>
|
||||
<td>{$term->string2}{if $term->suffix2} {$term->suffix2->text}{/if}</td>
|
||||
{else}
|
||||
<td>{$term->string2}{if $term->suffix2} {$term->suffix2->text}{/if}</td>
|
||||
<td>{$term->string1}{if $term->suffix1} {$term->suffix1->text}{/if}</td>
|
||||
{/if}
|
||||
<td n:attr="title => $term->type ? $dicttypes[$term->type->id]->fullName : null">{$term->type ? $dicttypes[$term->type->id]->shortName : ''}</td>
|
||||
<td>{$term->member}</td>
|
||||
|
||||
</tr>
|
||||
{/foreach}
|
||||
</table>
|
||||
|
||||
{/ifset}
|
||||
</div>
|
||||
{/block}
|
||||
|
||||
|
||||
@@ -39,8 +39,22 @@
|
||||
<h1 n:ifset="$title">{$title}</h1>
|
||||
|
||||
<div n:foreach="$flashes as $flash" n:class="flash, $flash->type">{$flash->message}</div>
|
||||
|
||||
<div id="content">
|
||||
{block form}
|
||||
<h2>Zadaj vyhladavany vyraz.</h2>
|
||||
{form searchForm}
|
||||
<table>
|
||||
<tr>
|
||||
<th>{label string /}</th><th>{label translation /}</th><th>{label clen /}</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>{input string}</td><td>{input translation}</td><td>{input clen}</td><td>{input search}</td>
|
||||
</tr>
|
||||
</table>
|
||||
{/form}
|
||||
{/block}
|
||||
{include content}
|
||||
</div>
|
||||
</article>
|
||||
|
||||
<section>
|
||||
|
||||
Reference in New Issue
Block a user