Passed
Push — master ( 0143aa...ef0bb0 )
by Florian
02:01
created

EventController::displayNewForm()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 17
rs 9.9666
cc 2
nc 2
nop 4
1
<?php
2
3
/*
4
 * This file is part of the vseth-semesterly-reports project.
5
 *
6
 * (c) Florian Moser <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace App\Controller\Organisation;
13
14
use App\Controller\Administration\Base\BaseController;
15
use App\Entity\Event;
16
use App\Entity\Organisation;
17
use App\Form\Type\SemesterType;
18
use App\Model\Breadcrumb;
19
use Symfony\Component\HttpFoundation\Request;
20
use Symfony\Component\HttpFoundation\Response;
21
use Symfony\Component\Routing\Annotation\Route;
22
use Symfony\Contracts\Translation\TranslatorInterface;
23
24
/**
25
 * @Route("/event")
26
 */
27
class EventController extends BaseController
28
{
29
    /**
30
     * @var Organisation
31
     */
32
    private $organisation;
33
34
    /**
35
     * @Route("/new", name="organisation_event_new")
36
     *
37
     * @return Response
38
     */
39
    public function newAction(Organisation $organisation, Request $request, TranslatorInterface $translator)
40
    {
41
        //create the event
42
        $event = new Event();
43
        $event->setSemester(SemesterType::getCurrentSemester());
44
        $event->setOrganisation($organisation);
45
        $event->setLocation('');
46
        $event->setBudget(0);
47
        $event->setNeedFinancialSupport(false);
48
        $event->setStartDate(new \DateTime());
49
50
        return $this->displayNewForm($request, $translator, $organisation, $event);
51
    }
52
53
    /**
54
     * @return \Symfony\Component\Form\FormInterface|Response
55
     */
56
    private function displayNewForm(Request $request, TranslatorInterface $translator, Organisation $organisation, Event $event)
57
    {
58
        //process form
59
        $myForm = $this->handleCreateForm(
60
            $request,
61
            $event,
62
            function () use ($event, $translator) {
63
                return $this->validateEvent($event, $translator);
64
            }
65
        );
66
        if ($myForm instanceof Response) {
67
            return $myForm;
68
        }
69
70
        $this->organisation = $organisation;
71
72
        return $this->render('organisation/event/new.html.twig', ['form' => $myForm->createView()]);
73
    }
74
75
    /**
76
     * @Route("/{event}/clone", name="organisation_event_copy")
77
     *
78
     * @return Response
79
     */
80
    public function copyAction(Request $request, Organisation $organisation, Event $event, TranslatorInterface $translator)
81
    {
82
        $clonedEvent = clone $event;
83
84
        return $this->displayNewForm($request, $translator, $organisation, $clonedEvent);
85
    }
86
87
    /**
88
     * @Route("/{event}/edit", name="organisation_event_edit")
89
     *
90
     * @return Response
91
     */
92
    public function editAction(Organisation $organisation, Request $request, Event $event, TranslatorInterface $translator)
93
    {
94
        //process form
95
        $myForm = $this->handleUpdateForm(
96
            $request,
97
            $event,
98
            function () use ($event, $translator) {
99
                return $this->validateEvent($event, $translator);
100
            }
101
        );
102
103
        if ($myForm instanceof Response) {
104
            return $myForm;
105
        }
106
107
        $this->organisation = $organisation;
108
109
        return $this->render('organisation/event/edit.html.twig', ['form' => $myForm->createView(), 'event' => $event]);
110
    }
111
112
    /**     *
113
     * @Route("/{event}/remove", name="organisation_event_remove")
114
     *
115
     * @return Response
116
     */
117
    public function removeAction(Organisation $organisation, Request $request, Event $event)
118
    {
119
        //process form
120
        $form = $this->handleDeleteForm($request, $event);
121
        if ($form === null) {
122
            return $this->redirectToRoute('organisation_view', ['organisation' => $organisation->getId()]);
123
        }
124
125
        $this->organisation = $organisation;
126
127
        return $this->render('organisation/event/remove.html.twig', ['form' => $form->createView(), 'event' => $event]);
128
    }
129
130
    private function validateEvent(Event $event, TranslatorInterface $translator): bool
131
    {
132
        if (mb_strlen($event->getNameDe()) === 0 && mb_strlen($event->getNameEn()) === 0) {
133
            $this->displayError($translator->trans('new.error.no_name', [], 'organisation_event'));
134
135
            return false;
136
        }
137
138
        return true;
139
    }
140
141
    /**
142
     * get the breadcrumbs leading to this controller.
143
     *
144
     * @return Breadcrumb[]
145
     */
146
    protected function getIndexBreadcrumbs()
147
    {
148
        // test in frontend
149
        return array_merge(parent::getIndexBreadcrumbs(), [
150
            new Breadcrumb(
151
                $this->generateUrl('organisation_view', ['organisation' => $this->organisation->getId()]),
152
                $this->getTranslator()->trans('view.title', [], 'organisation')
153
            ),
154
        ]);
155
    }
156
}
157