Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions app/Components/Forms/DocRequestFormFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace App\Components\Forms;

use App\Model\Howdo\HowdoRequestFactory;
use Instante\Bootstrap3Renderer\BootstrapRenderer;
use Nette\Application\UI\Form;

class DocRequestFormFactory
{
/** @var HowdoRequestFactory */
private $howdoRequestFactory;

public function __construct(HowdoRequestFactory $howdoRequestFactory)
{
$this->howdoRequestFactory = $howdoRequestFactory;
}

public function create(array $values = [])
{
$form = new Form();
$form->setRenderer(new BootstrapRenderer());

$form->addText('title', 'Request title:')
->setRequired();

$form->addTextArea('description', 'Description:')
->setRequired();

$form->addSubmit('submit', 'Save');

$form->onSuccess[] = function (Form $form) {
$values = $form->getValues();

$this->howdoRequestFactory->create($values->title, $values->description);
};

if (count($values) > 0) {
$form->setDefaults([
'title' => $values['title'],
'description' => $values['description'],
]);
}

return $form;
}
}

92 changes: 90 additions & 2 deletions app/Model/Howdo/HowdoDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@

namespace App\Model\Howdo;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/** @ORM\Entity */
/**
* @ORM\Entity
* @ORM\Table(indexes={
* @ORM\Index(columns={"title"}, flags={"fulltext"}),
* @ORM\Index(columns={"document"}, flags={"fulltext"})
* })
*/
class HowdoDocument
{
/**
Expand All @@ -16,7 +23,7 @@ class HowdoDocument
private $id;

/**
* @ORM\Column(type="text")
* @ORM\Column(type="string")
* @var string
*/
private $title;
Expand All @@ -26,9 +33,90 @@ class HowdoDocument
*/
private $document;

/**
* @var HowdoKeyword[]|ArrayCollection
* @ORM\OneToMany(targetEntity="App\Model\Howdo\HowdoKeyword", mappedBy="document")
*/
private $keywords;

/**
* @param string $title
* @param string $document
*/
public function __construct($title, $document)
{
$this->title = $title;
$this->document = $document;
$this->keywords = new ArrayCollection();
}


/** @return int */
public function getId()
{
return $this->id;
}

/**
* @return string
*/
public function getTitle()
{
return $this->title;
}

/**
* @param string $title
*
* @return self
*/
public function setTitle($title)
{
$this->title = $title;

return $this;
}

/**
* @return string
*/
public function getDocument()
{
return $this->document;
}

/**
* @param string $document
*
* @return self
*/
public function setDocument($document)
{
$this->document = $document;

return $this;
}

/**
* @return HowdoKeyword[]
*/
public function getKeywords()
{
return $this->keywords;
}

/**
* @param HowdoKeyword $keyword
*
* @return self
*/
public function addKeyword(HowdoKeyword $keyword)
{
if (!$this->keywords->contains($keyword)) {
$this->keywords->add($keyword);
}

return $this;
}
}

24 changes: 24 additions & 0 deletions app/Model/Howdo/HowdoDocumentDao.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace App\Model\Howdo;


use App\Model\TEntityManager;

class HowdoDocumentDao
{
use TEntityManager;

public function findByFulltext($keyword)
{
return $this->em->getRepository(HowdoDocument::class)->createQueryBuilder('d')
->leftJoin(HowdoKeyword::class, 'k', 'WITH', 'k.document = d.id')
->where('d.title LIKE :keyword')
->orWhere('d.document LIKE :keyword')
->orWhere('k.keyword LIKE :keyword')
->setParameter('keyword', '%' . $keyword . '%')
->getQuery()
->getResult();
}
}

63 changes: 63 additions & 0 deletions app/Model/Howdo/HowdoFinder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace App\Model\Howdo;

class HowdoFinder
{
/** @var HowdoDocumentDao */
private $howdoDocumentDao;

/** @var HowdoRequestDao */
private $howdoRequestDao;

/** @var RequestUpVoter */
private $requestUpVoter;

public function __construct(HowdoDocumentDao $howdoDocumentDao, HowdoRequestDao $howdoRequestDao, RequestUpVoter $requestUpVoter)
{
$this->howdoDocumentDao = $howdoDocumentDao;
$this->howdoRequestDao = $howdoRequestDao;
$this->requestUpVoter = $requestUpVoter;
}

/**
* Returns array of found documents and requests
*
* @param string $keyword
* @return array
*/
public function findByKeyword($keyword)
{
return [
'documents' => $this->findDocuments($keyword),
'requests' => $this->findRequests($keyword),
];
}

private function findDocuments($keyword)
{
$documentEntities = $this->howdoDocumentDao->findByFulltext($keyword);

return array_map(function (HowdoDocument $item) {
return [
'title' => $item->getTitle(),
'content' => $item->getDocument(),
];
}, $documentEntities);
}

private function findRequests($keyword)
{
$requestEntities = $this->howdoRequestDao->findByFulltext($keyword);

return array_map(function (HowdoRequest $item) {
return [
'title' => $item->getTitle(),
'content' => $item->getDescription(),
'votes' => $item->getVotes(),
'voted' => $this->requestUpVoter->alreadyVoted($item->getId()),
];
}, $requestEntities);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

/**
* @ORM\Entity
* @ORM\Table(indexes={@ORM\Index(columns={"key_phrase"}, flags={"fulltext"})})
* @ORM\Table(indexes={@ORM\Index(columns={"keyword"}, flags={"fulltext"})})
*/
class HowdoIndex
class HowdoKeyword
{
/**
* @ORM\Column(type="integer")
Expand All @@ -19,14 +19,15 @@ class HowdoIndex
private $id;

/**
* @ORM\Column(type="text")
* @ORM\Column(type="string")
* @var string
*/
private $keyPhrase;
private $keyword;

/**
* @ORM\ManyToOne(targetEntity="App\Model\Howdo\HowdoDocument")
* @ORM\ManyToOne(targetEntity="App\Model\Howdo\HowdoDocument", inversedBy="keywords")
* @var HowdoDocument
*/
private $document;
}

55 changes: 51 additions & 4 deletions app/Model/Howdo/HowdoRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@

use Doctrine\ORM\Mapping as ORM;

/** @ORM\Entity */
/**
* @ORM\Entity
* @ORM\Table(indexes={
* @ORM\Index(columns={"description"}, flags={"fulltext"}),
* @ORM\Index(columns={"title"}, flags={"fulltext"})})
*/
class HowdoRequest
{
/**
Expand All @@ -16,24 +21,66 @@ class HowdoRequest
private $id;

/**
* @ORM\Column(type="text")
* @ORM\Column(type="string")
* @var string
*/
private $searchedQuery;
private $title;

/**
* @ORM\Column(type="text")
* @var string
*/
private $description;

/**
* @ORM\Column(type="integer")
* @var int
*/
private $votes;
private $votes = 1;

/**
* @param string $title
* @param string $description
*/
public function __construct($title, $description)
{
$this->title = $title;
$this->description = $description;
}

/** @return int */
public function getId()
{
return $this->id;
}

/**
* @return string
*/
public function getTitle()
{
return $this->title;
}

/**
* @return string
*/
public function getDescription()
{
return $this->description;
}

/**
* @return int
*/
public function getVotes()
{
return $this->votes;
}

public function addVote()
{
$this->votes++;
}
}

Loading