BeElectorController.php 7.73 KB
Newer Older
Oliver Eglseder's avatar
Oliver Eglseder committed
1
<?php
2
namespace T3o\Election\Controller;
Oliver Eglseder's avatar
Oliver Eglseder committed
3
4
5
6
7
8
9
10
11
12
13
14
15

/*
 * This file is part of the TYPO3 CMS project.
 *
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 *
 * The TYPO3 project - inspiring people to share!
 */
16
17
18
19

use T3o\Election\Domain\Model\ElectionInvitation;
use T3o\Election\Service\ElectionInvitationService;
use TYPO3\CMS\Core\Messaging\AbstractMessage;
Oliver Eglseder's avatar
Oliver Eglseder committed
20
use TYPO3\CMS\Extbase\Persistence\ObjectStorage;
21
22
23
24
25
26
use T3o\Election\Domain\Model\Dto\ElectorImport as ElectorImportDto;
use T3o\Election\Domain\Model\Election;
use T3o\Election\Domain\Model\Elector;
use T3o\Election\Domain\Model\ElectorImport;
use T3o\Election\Utility\CsvUtility;
use T3o\Election\Utility\FileUtility;
27
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
Oliver Eglseder's avatar
Oliver Eglseder committed
28
29
30
31
32
33
34

/**
 * Class BeConfigurationController
 */
class BeElectorController extends AbstractProtectedBeController
{
    const CONTROLLER_NAME = 'BeElector';
35
36
37
38
39
40
    const ACTION_NEW = 'new';
    const ACTION_CREATE = 'create';
    const ACTION_EDIT = 'edit';
    const ACTION_UPDATE = 'update';
    const ACTION_DELETE = 'delete';
    const ACTION_LIST = 'list';
Oliver Eglseder's avatar
Oliver Eglseder committed
41
    const ACTION_IMPORT = 'import';
42
43
44
    const ACTION_EXPORT = 'export';
    const ACTION_UPLOAD = 'upload';
    const ACTION_PROCESS_IMPORT = 'processImport';
Oliver Eglseder's avatar
Oliver Eglseder committed
45
    const ACTION_OVERWRITE_ALL = 'overwriteAll';
46
    const ACTION_RESEND_ELECTION_INVITATION = 'resendElectionInvitation';
Oliver Eglseder's avatar
Oliver Eglseder committed
47
48

    /**
49
     * @var \T3o\Election\Domain\Repository\ElectorateRepository
Oliver Eglseder's avatar
Oliver Eglseder committed
50
51
     * @inject
     */
52
53
54
    protected $electorateRepository = null;

    /**
55
     * @var \T3o\Election\Domain\Repository\ElectorRepository
56
57
58
59
60
     * @inject
     */
    protected $electorRepository = null;

    /**
61
     * @var \T3o\Election\Domain\Repository\ElectorImportRepository
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
     * @inject
     */
    protected $electorImportRepository = null;

    /**
     *
     */
    public function newAction()
    {
        $this->view->assign('elector', $this->objectManager->get(Elector::class));
        $this->view->assign('electorates', $this->electorateRepository->findAll());
    }

    /**
     * @param Elector $elector
     */
    public function createAction(Elector $elector)
    {
        $this->electorRepository->add($elector);
        $this->redirect(self::ACTION_LIST);
    }

    /**
     * @param Elector $elector
     */
    public function editAction(Elector $elector)
    {
        $this->view->assign('elector', $elector);
        $this->view->assign('electorates', $this->electorateRepository->findAll());
    }

    /**
     * @param Elector $elector
     */
    public function updateAction(Elector $elector)
    {
        $this->electorRepository->update($elector);
        $this->redirect(self::ACTION_LIST);
    }

    /**
     * @param Elector $elector
     */
    public function deleteAction(Elector $elector)
    {
        $this->electorRepository->remove($elector);
        $this->redirect(self::ACTION_LIST);
    }
Oliver Eglseder's avatar
Oliver Eglseder committed
110
111
112
113
114
115

    /**
     * @param Election|null $election Optional Election which any imported Elector will be assigned to
     */
    public function importAction(Election $election = null)
    {
116
        $this->view->assign('electorImport', new ElectorImportDto($election, $this->electorateRepository->findAll()));
Oliver Eglseder's avatar
Oliver Eglseder committed
117
118
    }

Oliver Eglseder's avatar
Oliver Eglseder committed
119
120
121
    /**
     *
     */
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
    public function listAction()
    {
        $this->view->assign('electors', $this->electorRepository->findAll());
    }

    /**
     *
     */
    public function exportAction()
    {
        FileUtility::pushFile(
            $this->objectManager->get(CsvUtility::class)->convertFromArray(
                $this->electorRepository->findAll()->toArray(),
                array('firstName', 'middleName', 'lastName', 'gender', 'email')
            )
        );
    }

    /**
     * @param ElectorImportDto $electorImportDto
     */
    public function uploadAction(ElectorImportDto $electorImportDto)
Oliver Eglseder's avatar
Oliver Eglseder committed
144
    {
145
146
147
148
        if (FileUtility::isUploadedFileGood($electorImportDto->getImportInformation())) {
            $this->electorImportRepository->removeAll();
            $fileContents = FileUtility::getUploadedFileContents($electorImportDto->getImportInformation());
            $electorImports = $this->objectManager->get(CsvUtility::class)->convertToArray($fileContents);
Oliver Eglseder's avatar
Oliver Eglseder committed
149
            foreach ($electorImports as $electorImport) {
150
151
152
153
                $electorImportObject = new ElectorImport();
                $electorImportObject->setFromArray($electorImport);
                $electorImportObject->setElectorate($electorImportDto->getChosenElectorate());
                $this->electorImportRepository->add($electorImportObject);
Oliver Eglseder's avatar
Oliver Eglseder committed
154
155
            }
        }
156
        $this->redirect(self::ACTION_PROCESS_IMPORT);
Oliver Eglseder's avatar
Oliver Eglseder committed
157
158
    }

Oliver Eglseder's avatar
Oliver Eglseder committed
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
    /**
     *
     */
    public function processImportAction()
    {
        $this->view->assign('electorImports', $this->electorImportRepository->findAll());
    }

    /**
     *
     */
    public function overwriteAllAction()
    {
        $this->electorRepository->removeAll();
        /** @var ElectorImport $electorImport */
        foreach ($this->electorImportRepository->findAll() as $electorImport) {
            $elector = $this->objectManager->get(Elector::class);
            $elector->setFirstName($electorImport->getFirstName());
            $elector->setMiddleName($electorImport->getMiddleName());
            $elector->setLastName($electorImport->getLastName());
            $elector->setGender($electorImport->getGender());
            $elector->setEmail($electorImport->getEmail());
            $elector->addElectorate($electorImport->getElectorate());
            $this->electorRepository->add($elector);
            $this->electorImportRepository->remove($electorImport);
        }
        $this->redirect(self::ACTION_LIST);
    }

188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
    /**
     * @param Elector $elector
     * @param ElectionInvitation $electionInvitation
     */
    public function resendElectionInvitationAction(Elector $elector, ElectionInvitation $electionInvitation){
        /** @var ElectionInvitationService $electionInvitationService */
        $electionInvitationService = $this->objectManager->get(ElectionInvitationService::class);
        $electionInvitationService->resendElectionInvitation($this->configuration, $electionInvitation);

        $labelKey = $electionInvitation->isSuccess() ? 'view.be.elector.edit.election_invitaion_resend_body_success' : 'view.be.elector.edit.election_invitaion_resend_body_failure';
        $severity = $electionInvitation->isSuccess() ? AbstractMessage::OK : AbstractMessage::ERROR;
        $this->addFlashMessage(
            LocalizationUtility::translate($labelKey, $this->extensionName),
            LocalizationUtility::translate('view.be.elector.edit.election_invitaion_resend_title', $this->extensionName),
            $severity
        );

        $this->redirect(self::ACTION_EDIT, self::CONTROLLER_NAME, 'election', ['elector' => $elector]);
    }

Oliver Eglseder's avatar
Oliver Eglseder committed
208
209
210
211
212
    /**
     * @return string
     */
    public static function getActionsForModuleConfiguration()
    {
213
214
215
216
217
218
219
220
221
222
223
224
225
        return implode(
            ',',
            [
                self::ACTION_NEW,
                self::ACTION_CREATE,
                self::ACTION_EDIT,
                self::ACTION_UPDATE,
                self::ACTION_DELETE,
                self::ACTION_IMPORT,
                self::ACTION_LIST,
                self::ACTION_EXPORT,
                self::ACTION_UPLOAD,
                self::ACTION_PROCESS_IMPORT,
Oliver Eglseder's avatar
Oliver Eglseder committed
226
                self::ACTION_OVERWRITE_ALL,
227
                self::ACTION_RESEND_ELECTION_INVITATION,
228
229
            ]
        );
Oliver Eglseder's avatar
Oliver Eglseder committed
230
231
232
    }

    /**
233
     * @return int
Oliver Eglseder's avatar
Oliver Eglseder committed
234
     */
235
    protected function getRequiredUserGroup()
Oliver Eglseder's avatar
Oliver Eglseder committed
236
    {
237
        return $this->configuration->getElectionManagerGroup();
Oliver Eglseder's avatar
Oliver Eglseder committed
238
239
    }
}