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 - Jan Suchánek #40

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions app/Resources/views/base.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
<li>
<a href="{{ path("homepage") }}">Domů</a>
</li>
<li>
<a href="{{ path("faq_list") }}">Faq</a>
</li>
</ul>
<p class="navbar-text navbar-right">
{% if user is defined and user %}
Expand Down
1 change: 0 additions & 1 deletion app/Resources/views/components/list.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
<img src="{{ product.image }}" alt="{{ product.title }}">
<div class="caption">
<h4 class="pull-right">{{ product.price }} Kč</h4>
</h4>
<p>{{ product.title }}</p>
</div>
</div>
Expand Down
7 changes: 7 additions & 0 deletions app/Resources/views/components/list_faq.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<div class="col-sm-4 col-lg-4 col-md-4">
<ul>
{% for faq in faqs %}
<li>{{ faq.email }}<br>{{ faq.question }}</li>
{% endfor %}
</ul>
</div>
3 changes: 3 additions & 0 deletions app/Resources/views/components/paginator.html.twig
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{% if currentPage > 1 %}
a
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a?

{% endif %}
<nav aria-label="Page navigation">
<ul class="pagination">
{% if currentPage > 1 %}
Expand Down
17 changes: 17 additions & 0 deletions app/Resources/views/faq/list.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{% extends 'base.html.twig' %}

{% block body %}
<h1>Faq - Časté dotazy</h1>

{{ form_start(form) }}
{{ form_row(form.email) }}
{{ form_row(form.question) }}
<br />
<button class="btn btn-lg btn-primary btn-block" type="submit">Vložit dotaz</button>
{{ form_end(form) }}

<div class="row">
{% include 'components/list_faq.html.twig' %}
</div>
{% include ":components:paginator.html.twig" %}
{% endblock %}
13 changes: 13 additions & 0 deletions app/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ services:
class: AppBundle\Controller\UserController
autowire: true

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

app.facade.category_facade:
class: AppBundle\Facade\CategoryFacade
autowire: true
Expand All @@ -32,6 +36,10 @@ services:
class: AppBundle\Facade\UserFacade
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 +50,11 @@ services:
factory: ['@doctrine.orm.default_entity_manager', getRepository]
arguments: ['AppBundle\Entity\Product']

app.repository.faq_repository:
class: AppBundle\Repository\FaqRepository
factory: ['@doctrine.orm.default_entity_manager', getRepository]
arguments: ['AppBundle\Entity\Faq']

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

namespace AppBundle\Controller;

use AppBundle\Entity\Faq;
use AppBundle\Facade\FaqFacade;
use AppBundle\Service\Paginator;
use Doctrine\ORM\EntityManager;
use Symfony\Component\Form\FormFactory;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use AppBundle\FormType\FaqFormType;
use Symfony\Component\Routing\RouterInterface;

/**
* @author Vašek Boch <[email protected]>
* @author Jan Klat <[email protected]>
* @Route(service="app.controller.faq_controller")
*/
class FaqController
{
private $faqFacade;
private $entityManager;
private $formFactory;
private $router;

public function __construct(
FaqFacade $faqFacade,
FormFactory $formFactory,
RouterInterface $router,
EntityManager $entityManager
) {
$this->faqFacade = $faqFacade;
$this->entityManager = $entityManager;
$this->formFactory = $formFactory;
$this->router = $router;
}
/**
* @Route("/faq", name="faq_list")
* @Template("faq/list.html.twig")
*
* @param Request $request
* @return RedirectResponse|array
*/
public function listAction(Request $request)
{
if(!$page = $request->query->get("page")){
$page = 1;
}


$faq = new Faq();
$form = $this->formFactory->create(FaqFormType::class, $faq);

$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {

$this->entityManager->persist($faq);
$this->entityManager->flush([$faq]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pryč z controlleru


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

$countByFaq = $this->faqFacade->countAll();
$paginator = new Paginator($countByFaq, 10);
$paginator->setCurrentPage($page);

return [
"faqs" => $this->faqFacade->getAll($paginator->getLimit(), $paginator->getOffset()),
"form" => $form->createView(),
"currentPage" => $page,
"totalPages" => $paginator->getTotalPageCount(),
"pageRange" => $paginator->getPageRange(5),
];
}

}
82 changes: 82 additions & 0 deletions src/AppBundle/Entity/Faq.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace AppBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
* @author Vašek Boch <[email protected]>
* @author Jan Klat <[email protected]>
*
* @ORM\Entity(repositoryClass="AppBundle\Repository\FaqRepository")
*/
class Faq
{

/**
* @var int
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;

/**
* @ORM\Column(type="string", length=255, name="email")
* @Assert\NotBlank()
* @Assert\Email()
*/
private $email;

/**
* @var string
* @ORM\Column(type="text", length=255)
*/
private $question;

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

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

/**
* @param string $email
* @return self
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}

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

/**
* @param string $description
* @return self
*/
public function setQuestion($question)
{
$this->question = $question;
return $this;
}
}
34 changes: 34 additions & 0 deletions src/AppBundle/Facade/FaqFacade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace AppBundle\Facade;

use AppBundle\Entity\Faq;
use AppBundle\Repository\FaqRepository;

/**
* @author Vašek Boch <[email protected]>
* @author Jan Klat <[email protected]>
*/
class FaqFacade {

private $faqRepository;

public function __construct(FaqRepository $faqRepository) {
$this->faqRepository = $faqRepository;
}

public function getAll($limit, $offset) {
return $this->faqRepository->findBy(
[],
[
"id" => "desc"
],
$limit,
$offset
);
}

public function countAll() {
return $this->faqRepository->countAll();
}
}
45 changes: 45 additions & 0 deletions src/AppBundle/FormType/FaqFormType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
namespace AppBundle\FormType;

use AppBundle\Entity\Faq;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* @author Vašek Boch <[email protected]>
* @author Jan Klat <[email protected]>
*/
class FaqFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add("email", EmailType::class, [
"label" => "E-mail",
"attr" => [
"class" => "form-control",
],
])->add("question", TextareaType::class, [
"label" => "Question",
"attr" => [
"class" => "form-control",
],
"constraints" => [
new NotBlank(["message" => "Prosím vyplňte Váš dotaz"]),
],
]);
}

public function configureOptions(OptionsResolver $resolver)
{

$resolver->setDefaults(array(
"data_class" => Faq::class,
));

}
}
32 changes: 32 additions & 0 deletions src/AppBundle/Repository/FaqRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace AppBundle\Repository;

use AppBundle\Entity\Faq;
use Doctrine\ORM\EntityRepository;

/**
* @author Vašek Boch <[email protected]>
* @author Jan Klat <[email protected]>
*/
class FaqRepository extends EntityRepository
{
/**
* @param Faq $faq
* @return QueryBuilder
*/
public function findByFaq(Faq $faq)
{
$builder = $this->_em->createQueryBuilder()
->select('f')
->from('AppBundle\Entity\Faq', 'f');
return $builder;
}

public function countAll() {
return $this->_em->createQueryBuilder()
->select('COUNT(f.id)')
->from('AppBundle\Entity\Faq', 'f')
->getQuery()->getSingleScalarResult();
}
}
Loading