Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ukol 4 - Vojtěch Bartoš #37

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
15 changes: 15 additions & 0 deletions app/Resources/views/base.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@
<li>
<a href="{{ path("homepage") }}">Domů</a>
</li>
<li>
<a href="{{ path("faq") }}">FAQ</a>
</li>
<li>
<a href="{{ path("contact") }}">Kontakt</a>
</li>
</ul>
<p class="navbar-text navbar-right">
{% if user is defined and user %}
Expand All @@ -72,6 +78,15 @@
</div>

<div class="col-md-9">

{% for flash_message in app.session.flashBag.get('notice') %}


<div class="alert alert-info">
{{ flash_message }}
</div>
{% endfor %}

{% block body %}
{% endblock %}
</div>
Expand Down
11 changes: 11 additions & 0 deletions app/Resources/views/contact/contact.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% extends 'base.html.twig' %}

{% block body %}
{{ form_start(form) }}
{{ form_row(form.email) }}
{{ form_row(form.text) }}
<br />
<button class="btn btn-lg btn-primary btn-block" type="submit">Odeslat</button>
{{ form_end(form) }}

{% endblock %}
11 changes: 11 additions & 0 deletions app/Resources/views/faq/add.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% extends 'base.html.twig' %}

{% block body %}
{{ form_start(form) }}
{{ form_row(form.question) }}
{{ form_row(form.answer) }}
<br />
<button class="btn btn-lg btn-primary btn-block" type="submit">Přidat</button>
{{ form_end(form) }}
{% endblock %}

11 changes: 11 additions & 0 deletions app/Resources/views/faq/edit.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% extends 'base.html.twig' %}

{% block body %}
{{ form_start(form) }}
{{ form_row(form.question) }}
{{ form_row(form.answer) }}
<br />
<button class="btn btn-lg btn-primary btn-block" type="submit">Upravit</button>
{{ form_end(form) }}
{% endblock %}

12 changes: 12 additions & 0 deletions app/Resources/views/faq/faq.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{% extends 'base.html.twig' %}

{% block body %}
{% for question in questions %}
<div class="col-sm-12 col-lg-12 col-md-12">
<h3>{{ question.text }}</h3>

{{ answers[question.id].text }}
</div>
{% endfor %}
{% endblock %}

13 changes: 13 additions & 0 deletions app/Resources/views/faq/list.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% extends 'base.html.twig' %}

{% block body %}
<a href="{{ path("faq_add") }}">Přidat</a>
{% for question in questions %}
<div class="col-sm-12 col-lg-12 col-md-12">
<h3>{{ question.text }} <a href="{{ path("faq_edit", {"id": question.id }) }}">Upravit</a></h3>

{{ answers[question.id].text }}
</div>
{% endfor %}
{% endblock %}

16 changes: 15 additions & 1 deletion app/config/security.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
security:
role_hierarchy:
ROLE_ADMIN: ROLE_USER

encoders:
Symfony\Component\Security\Core\User\User: plaintext
AppBundle\Entity\User:
algorithm: bcrypt
cost: 13 #cost is 13 by default, just so you know why and how to set it

providers:
chain_provider:
chain:
providers: [in_memory, database_users]
in_memory:
memory:
users:
- { name: admin@shop.cz, password: admin, roles: 'ROLE_ADMIN' }

database_users:
entity: { class: AppBundle:User, property: username }

Expand All @@ -24,4 +36,6 @@ security:
# The route name the user can go to in order to logout
path: user_logout
# The name of the route to redirect to after logging out
target: homepage
target: homepage
access_control:
- { path: ^/faq/, roles: ROLE_ADMIN }
34 changes: 34 additions & 0 deletions app/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ services:
class: AppBundle\Controller\UserController
autowire: true

app.controller.faq_controller:
class: AppBundle\Controller\FaqController
autowire: true

app.controller.contact_controller:
class: AppBundle\Controller\ContactController
autowire: true

app.facade.category_facade:
class: AppBundle\Facade\CategoryFacade
autowire: true
Expand All @@ -32,6 +40,22 @@ services:
class: AppBundle\Facade\UserFacade
autowire: true

app.facade.answer_facade:
class: AppBundle\Facade\AnswerFacade
autowire: true

app.facade.question_facade:
class: AppBundle\Facade\QuestionFacade
autowire: true

app.facade.message_facade:
class: AppBundle\Facade\MessageFacade
autowire: true

app.facade.faq_facade:
class: AppBundle\Facade\FaqFacade
autowire: true

app.repository.category_repository:
class: AppBundle\Repository\CategoryRepository
factory: ['@doctrine.orm.default_entity_manager', getRepository]
Expand All @@ -42,6 +66,16 @@ services:
factory: ['@doctrine.orm.default_entity_manager', getRepository]
arguments: ['AppBundle\Entity\Product']

app.repository.answer_repository:
class: AppBundle\Repository\AnswerRepository
factory: ['@doctrine.orm.default_entity_manager', getRepository]
arguments: ['AppBundle\Entity\Answer']

app.repository.question_repository:
class: AppBundle\Repository\QuestionRepository
factory: ['@doctrine.orm.default_entity_manager', getRepository]
arguments: ['AppBundle\Entity\Question']

encoder:
class: Symfony\Component\Security\Core\Encoder\BCryptPasswordEncoder
arguments:
Expand Down
69 changes: 69 additions & 0 deletions src/AppBundle/Controller/ContactController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace AppBundle\Controller;

use AppBundle\Facade\MessageFacade;
use AppBundle\FormType\ContactFormType;
use AppBundle\FormType\VO\MessageVO;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\Form\FormFactory;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
use Symfony\Component\Routing\RouterInterface;

/**
* @Route(service="app.controller.contact_controller")
*/
class ContactController
{
/** @var FormFactory */
private $formFactory;

/** @var MessageFacade */
private $messageFacade;

/** @var RouterInterface */
private $router;

/** @var FlashBag */
private $flashBag;

/**
* @param FormFactory $formFactory
* @param MessageFacade $messageFacade
* @param RouterInterface $router
* @param FlashBag $flashBag
*/
public function __construct(FormFactory $formFactory, MessageFacade $messageFacade, RouterInterface $router, FlashBag $flashBag)
{
$this->formFactory = $formFactory;
$this->messageFacade = $messageFacade;
$this->router = $router;
$this->flashBag = $flashBag;
}

/**
* @Route("/contact", name="contact")
* @Template("contact/contact.html.twig")
*/
public function contactAction(Request $request)
{
$messageVO = new MessageVO();
$form = $this->formFactory->create(ContactFormType::class, $messageVO);

$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->messageFacade->save($messageVO);

$this->flashBag->add('notice', 'Message send.');

return RedirectResponse::create($this->router->generate("contact"));
}

return [
"form" => $form->createView()
];
}
}
142 changes: 142 additions & 0 deletions src/AppBundle/Controller/FaqController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<?php

namespace AppBundle\Controller;

use AppBundle\Facade\AnswerFacade;
use AppBundle\Facade\FaqFacade;
use AppBundle\Facade\QuestionFacade;
use AppBundle\FormType\FaqFormType;
use AppBundle\FormType\VO\FaqVO;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\Form\FormFactory;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
use Symfony\Component\Routing\RouterInterface;

/**
* @Route(service="app.controller.faq_controller")
*/
class FaqController
{
/** @var FormFactory */
private $formFactory;

/** @var QuestionFacade */
private $questionFacade;

/** @var AnswerFacade */
private $answerFacade;

/** @var FaqFacade */
private $faqFacade;

/** @var RouterInterface */
private $router;

/** @var FlashBag */
private $flashBag;

/**
* @param FormFactory $formFactory
* @param QuestionFacade $questionFacade
* @param AnswerFacade $answerFacade
* @param FaqFacade $faqFacade
* @param RouterInterface $router
* @param FlashBag $flashBag
*/
public function __construct(FormFactory $formFactory, QuestionFacade $questionFacade, AnswerFacade $answerFacade, FaqFacade $faqFacade, RouterInterface $router, FlashBag $flashBag)
{
$this->formFactory = $formFactory;
$this->questionFacade = $questionFacade;
$this->answerFacade = $answerFacade;
$this->faqFacade = $faqFacade;
$this->router = $router;
$this->flashBag = $flashBag;
}

/**
* @Route("/faq", name="faq")
* @Template("faq/faq.html.twig")
*/
public function faqAction(Request $request)
{
$questions = $this->questionFacade->findAll();
$answers = $this->answerFacade->findByQuestions($questions);

return [
"questions" => $questions,
"answers" => $answers,
];
}

/**
* @Route("/faq/list", name="faq_list")
* @Template("faq/list.html.twig")
*/
public function listAction()
{
$questions = $this->questionFacade->findAll();
$answers = $this->answerFacade->findByQuestions($questions);

return [
"questions" => $questions,
"answers" => $answers,
];
}

/**
* @Route("/faq/add", name="faq_add")
* @Template("faq/add.html.twig")
* @param Request $request
* @return array
*/
public function addAction(Request $request)
{
$faqVO = new FaqVO();
$form = $this->formFactory->create(FaqFormType::class, $faqVO);

$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->faqFacade->insert($faqVO);

$this->flashBag->add('notice', 'Question added.');

return RedirectResponse::create($this->router->generate("faq_list"));
}

return [
"form" => $form->createView()
];
}

/**
* @Route("/faq/edit/{id}", name="faq_edit", requirements={"id": "\d+"})
* @Template("faq/edit.html.twig")
* @param int $id
* @param Request $request
* @return array
*/
public function editAction($id, Request $request)
{
$question = $this->questionFacade->findById($id);
$answer = $this->answerFacade->findByQuestion($question);

$faqVO = FaqVO::fromEntity($question, $answer);
$form = $this->formFactory->create(FaqFormType::class, $faqVO);

$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->faqFacade->update($question, $answer, $faqVO);

$this->flashBag->add('notice', 'Question uppdated.');

return RedirectResponse::create($this->router->generate("faq_list"));
}

return [
"form" => $form->createView()
];
}
}
Loading