Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 958aa91

Browse files
committedOct 29, 2016
Add FAQ list
1 parent d7ce1e6 commit 958aa91

File tree

10 files changed

+316
-0
lines changed

10 files changed

+316
-0
lines changed
 

‎app/Resources/views/base.html.twig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@
4646
<li>
4747
<a href="{{ path("homepage") }}">Domů</a>
4848
</li>
49+
<li>
50+
<a href="{{ path("faq") }}">FAQ</a>
51+
</li>
4952
</ul>
5053
<p class="navbar-text navbar-right">
5154
{% if user is defined and user %}

‎app/Resources/views/faq/faq.html.twig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{% extends 'base.html.twig' %}
2+
3+
{% block body %}
4+
{% for question in questions %}
5+
<div class="col-sm-12 col-lg-12 col-md-12">
6+
<h3>{{ question.text }}</h3>
7+
8+
{{ answers[question.id].text }}
9+
</div>
10+
{% endfor %}
11+
{% endblock %}
12+

‎app/config/services.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ services:
2020
class: AppBundle\Controller\UserController
2121
autowire: true
2222

23+
app.controller.faq_controller:
24+
class: AppBundle\Controller\FaqController
25+
autowire: true
26+
2327
app.facade.category_facade:
2428
class: AppBundle\Facade\CategoryFacade
2529
autowire: true
@@ -32,6 +36,14 @@ services:
3236
class: AppBundle\Facade\UserFacade
3337
autowire: true
3438

39+
app.facade.answer_facade:
40+
class: AppBundle\Facade\AnswerFacade
41+
autowire: true
42+
43+
app.facade.question_facade:
44+
class: AppBundle\Facade\QuestionFacade
45+
autowire: true
46+
3547
app.repository.category_repository:
3648
class: AppBundle\Repository\CategoryRepository
3749
factory: ['@doctrine.orm.default_entity_manager', getRepository]
@@ -42,6 +54,16 @@ services:
4254
factory: ['@doctrine.orm.default_entity_manager', getRepository]
4355
arguments: ['AppBundle\Entity\Product']
4456

57+
app.repository.answer_repository:
58+
class: AppBundle\Repository\AnswerRepository
59+
factory: ['@doctrine.orm.default_entity_manager', getRepository]
60+
arguments: ['AppBundle\Entity\Answer']
61+
62+
app.repository.question_repository:
63+
class: AppBundle\Repository\QuestionRepository
64+
factory: ['@doctrine.orm.default_entity_manager', getRepository]
65+
arguments: ['AppBundle\Entity\Question']
66+
4567
encoder:
4668
class: Symfony\Component\Security\Core\Encoder\BCryptPasswordEncoder
4769
arguments:
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace AppBundle\Controller;
4+
5+
use AppBundle\Facade\AnswerFacade;
6+
use AppBundle\Facade\QuestionFacade;
7+
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
8+
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
9+
use Symfony\Component\HttpFoundation\Request;
10+
11+
/**
12+
* @Route(service="app.controller.faq_controller")
13+
*/
14+
class FaqController
15+
{
16+
/** @var QuestionFacade */
17+
private $questionFacade;
18+
19+
/** @var AnswerFacade */
20+
private $answerFacade;
21+
22+
/**
23+
* @param QuestionFacade $questionFacade
24+
* @param AnswerFacade $answerFacade
25+
*/
26+
public function __construct(QuestionFacade $questionFacade, AnswerFacade $answerFacade)
27+
{
28+
$this->questionFacade = $questionFacade;
29+
$this->answerFacade = $answerFacade;
30+
}
31+
32+
/**
33+
* @Route("/faq", name="faq")
34+
* @Template("faq/faq.html.twig")
35+
*/
36+
public function faqAction(Request $request)
37+
{
38+
$questions = $this->questionFacade->findAll();
39+
$answers = $this->answerFacade->findByQuestions($questions);
40+
41+
return [
42+
"questions" => $questions,
43+
"answers" => $answers,
44+
];
45+
}
46+
}

‎src/AppBundle/Entity/Answer.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
namespace AppBundle\Entity;
4+
5+
use Doctrine\ORM\Mapping as ORM;
6+
7+
/**
8+
* @ORM\Entity
9+
*
10+
* @ORM\Entity(repositoryClass="AppBundle\Repository\AnswerRepository")
11+
*/
12+
class Answer
13+
{
14+
/**
15+
* @ORM\Id
16+
* @ORM\GeneratedValue
17+
* @ORM\Column(type="integer")
18+
* @var int
19+
*/
20+
private $id;
21+
22+
/**
23+
* @ORM\ManyToOne(targetEntity="Question")
24+
* @var Question
25+
*/
26+
private $question;
27+
28+
/**
29+
* @ORM\Column(type="string")
30+
* @var string
31+
*/
32+
private $text;
33+
34+
/**
35+
* @return int
36+
*/
37+
public function getId() : int
38+
{
39+
return $this->id;
40+
}
41+
42+
/**
43+
* @return Question
44+
*/
45+
public function getQuestion() : Question
46+
{
47+
return $this->question;
48+
}
49+
50+
/**
51+
* @param Question $question
52+
*/
53+
public function setQuestion(Question $question)
54+
{
55+
$this->question = $question;
56+
}
57+
58+
/**
59+
* @return string
60+
*/
61+
public function getText() : string
62+
{
63+
return $this->text;
64+
}
65+
66+
/**
67+
* @param string $text
68+
*/
69+
public function setText(string $text)
70+
{
71+
$this->text = $text;
72+
}
73+
}

‎src/AppBundle/Entity/Question.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace AppBundle\Entity;
4+
5+
use Doctrine\ORM\Mapping as ORM;
6+
7+
/**
8+
* @ORM\Entity
9+
*
10+
* @ORM\Entity(repositoryClass="AppBundle\Repository\QuestionRepository")
11+
*/
12+
class Question
13+
{
14+
/**
15+
* @ORM\Id
16+
* @ORM\GeneratedValue
17+
* @ORM\Column(type="integer")
18+
* @var int
19+
*/
20+
private $id;
21+
22+
/**
23+
* @ORM\Column(type="string")
24+
* @var string
25+
*/
26+
private $text;
27+
28+
/**
29+
* @return int
30+
*/
31+
public function getId() : int
32+
{
33+
return $this->id;
34+
}
35+
36+
/**
37+
* @return string
38+
*/
39+
public function getText() : string
40+
{
41+
return $this->text;
42+
}
43+
44+
/**
45+
* @param string $text
46+
*/
47+
public function setText(string $text)
48+
{
49+
$this->text = $text;
50+
}
51+
}

‎src/AppBundle/Facade/AnswerFacade.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace AppBundle\Facade;
4+
5+
use AppBundle\Entity\Answer;
6+
use AppBundle\Entity\Question;
7+
use AppBundle\Repository\AnswerRepository;
8+
9+
class AnswerFacade
10+
{
11+
/** @var AnswerRepository */
12+
private $answerRepository;
13+
14+
/**
15+
* @param AnswerRepository $answerRepository
16+
*/
17+
public function __construct(AnswerRepository $answerRepository)
18+
{
19+
$this->answerRepository = $answerRepository;
20+
}
21+
22+
/**
23+
* @param Question $question
24+
* @return Answer|null
25+
*/
26+
public function findByQuestion(Question $question)
27+
{
28+
return $this->answerRepository->findOneBy([
29+
"question" => $question,
30+
]);
31+
}
32+
33+
/**
34+
* @param Question[] $questions
35+
* @return Answer[]|null
36+
*/
37+
public function findByQuestions($questions)
38+
{
39+
/** @var Answer[] $answers */
40+
$answers = $this->answerRepository->findBy([
41+
"question" => $questions,
42+
]);
43+
44+
$result = [];
45+
foreach ($answers as $answer) {
46+
$result[$answer->getQuestion()->getId()] = $answer;
47+
}
48+
49+
return $result;
50+
}
51+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace AppBundle\Facade;
4+
5+
use AppBundle\Entity\Question;
6+
use AppBundle\Repository\QuestionRepository;
7+
8+
class QuestionFacade
9+
{
10+
/** @var QuestionRepository */
11+
private $questionRepository;
12+
13+
/**
14+
* @param QuestionRepository $questionRepository
15+
*/
16+
public function __construct(QuestionRepository $questionRepository)
17+
{
18+
$this->questionRepository = $questionRepository;
19+
}
20+
21+
/**
22+
* @return Question[]
23+
*/
24+
public function findAll()
25+
{
26+
return $this->questionRepository->findAll();
27+
}
28+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace AppBundle\Repository;
4+
5+
use AppBundle\Entity\Answer;
6+
use AppBundle\Entity\Question;
7+
use Doctrine\ORM\EntityRepository;
8+
9+
class AnswerRepository extends EntityRepository
10+
{
11+
public function findByQuestion(Question $question)
12+
{
13+
return $this->_em->createQueryBuilder()
14+
->select('*')
15+
->from(Answer::class, 'a')
16+
->where('a.question = :question')
17+
->setParameter("question", $question)
18+
->getQuery()
19+
->getResult();
20+
}
21+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace AppBundle\Repository;
4+
5+
use Doctrine\ORM\EntityRepository;
6+
7+
class QuestionRepository extends EntityRepository
8+
{
9+
}

0 commit comments

Comments
 (0)
Please sign in to comment.