Skip to content
Snippets Groups Projects
Select Git revision
  • a28e7fa5494031eb607c484cd60f8c087ec1c89f
  • develop default
  • master protected
  • unicaen_authentification
  • 5.x
  • 4.x
  • release_4.0.0
  • bootstrap4_migration
  • laminas_migration
  • zf-3.x
  • 7.1.0
  • 7.0.0
  • 6.1.0
  • 6.0.1
  • 6.0.0
  • 5.0.1
  • 5.0.0
  • 4.0.2
  • 4.0.1
  • 4.0.0
  • 3.0.0
  • 1.0.5
  • 1.0.4
  • 1.0.3
  • 1.0.2
  • 1.0.1
  • 1.0.0
27 results

FaqController.php

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    FaqController.php 3.48 KiB
    <?php
    
    namespace UnicaenFaq\Controller;
    
    use Doctrine\ORM\Tools\Pagination\Paginator;
    use DoctrineORMModule\Paginator\Adapter\DoctrinePaginator;
    use UnicaenFaq\Entity\Db\Faq;
    use UnicaenFaq\Form\FaqForm;
    use UnicaenFaq\Service\FaqServiceAwareInterface;
    use UnicaenFaq\Service\FaqServiceAwareTrait;
    use Zend\Mvc\Controller\AbstractActionController;
    use Zend\View\Model\ViewModel;
    
    class FaqController extends AbstractActionController implements FaqServiceAwareInterface
    {
        use FaqServiceAwareTrait;
    
        /**
         * @var FaqForm
         */
        protected $faqForm;
    
        /**
         * FaqController constructor.
         *
         * @param FaqForm $faqForm
         */
        public function __construct(FaqForm $faqForm)
        {
            $this->faqForm = $faqForm;
        }
    
        public function indexAction()
        {
            $qb = $this->faqService->getRepository()->createQueryBuilder('f');
            $qb->addOrderBy('f.ordre');
    
            $paginator = new \Zend\Paginator\Paginator(new DoctrinePaginator(new Paginator($qb, true)));
            $paginator->setItemCountPerPage(-1);
    
            $viewModel = new ViewModel([
                'faqs' => $paginator,
            ]);
            $viewModel->setTemplate('unicaen-faq/index');
    
            return $viewModel;
        }
    
        public function modifierAction()
        {
            $faq = $this->requestedFaq();
            $this->faqForm->bind($faq);
    
            if ($data = $this->params()->fromPost()) {
                $this->faqForm->setData($data);
                if ($this->faqForm->isValid()) {
                    /** @var Faq $faq */
                    $faq = $this->faqForm->getData();
                    $this->faqService->update($faq);
    
                    $this->flashMessenger()->addSuccessMessage("Question-réponse modifiée avec succès");
    
                    return $this->redirect()->toRoute('faq', [], ['query' => ['selected' => $faq->getId()]], true);
                }
            }
    
            $this->faqForm->setAttribute('action', $this->url()->fromRoute('faq/modifier', [], [], true));
    
            $viewModel = new ViewModel([
                'form' => $this->faqForm,
            ]);
            $viewModel->setTemplate('unicaen-faq/modifier');
    
            return $viewModel;
        }
    
        public function ajouterAction()
        {
            if ($data = $this->params()->fromPost()) {
                $this->faqForm->setData($data);
                if ($this->faqForm->isValid()) {
                    /** @var Faq $faq */
                    $faq = $this->faqForm->getData();
                    $this->faqService->create($faq);
    
                    $this->flashMessenger()->addSuccessMessage("Question-réponse ajoutée avec succès");
    
                    return $this->redirect()->toRoute('faq', [], ['query' => ['selected' => $faq->getId()]], true);
                }
            }
    
            $this->faqForm->setAttribute('action', $this->url()->fromRoute('faq/ajouter'));
    
            $viewModel = new ViewModel([
                'form' => $this->faqForm,
            ]);
            $viewModel->setTemplate('unicaen-faq/modifier');
    
            return $viewModel;
        }
    
        public function supprimerAction()
        {
            $faq = $this->requestedFaq();
            $this->faqService->delete($faq);
    
            $this->flashMessenger()->addSuccessMessage("Question-réponse supprimée avec succès");
    
            return $this->redirect()->toRoute('faq', [], [], true);
        }
    
        /**
         * @return Faq
         */
        protected function requestedFaq()
        {
            $id = $this->params()->fromRoute('faq');
            if (!$id) {
                return null;
            }
    
            /** @var Faq $faq */
            $faq = $this->faqService->getRepository()->find($id);
    
            return $faq;
        }
    }