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

/*
 * 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!
 */
Oliver Eglseder's avatar
Oliver Eglseder committed
16
use TYPO3\CMS\Extbase\Persistence\ObjectStorage;
17
use TYPO3\Election\Domain\Model\Dto\ElectorImport as ElectorImportDto;
Oliver Eglseder's avatar
Oliver Eglseder committed
18
use TYPO3\Election\Domain\Model\Election;
19
20
21
22
use TYPO3\Election\Domain\Model\Elector;
use TYPO3\Election\Domain\Model\ElectorImport;
use TYPO3\Election\Utility\CsvUtility;
use TYPO3\Election\Utility\FileUtility;
Oliver Eglseder's avatar
Oliver Eglseder committed
23
24
25
26
27
28
29

/**
 * Class BeConfigurationController
 */
class BeElectorController extends AbstractProtectedBeController
{
    const CONTROLLER_NAME = 'BeElector';
30
31
32
33
34
35
    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
36
    const ACTION_IMPORT = 'import';
37
38
39
    const ACTION_EXPORT = 'export';
    const ACTION_UPLOAD = 'upload';
    const ACTION_PROCESS_IMPORT = 'processImport';
Oliver Eglseder's avatar
Oliver Eglseder committed
40
    const ACTION_OVERWRITE_ALL = 'overwriteAll';
Oliver Eglseder's avatar
Oliver Eglseder committed
41
42

    /**
43
     * @var \TYPO3\Election\Domain\Repository\ElectorateRepository
Oliver Eglseder's avatar
Oliver Eglseder committed
44
45
     * @inject
     */
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
    protected $electorateRepository = null;

    /**
     * @var \TYPO3\Election\Domain\Repository\ElectorRepository
     * @inject
     */
    protected $electorRepository = null;

    /**
     * @var \TYPO3\Election\Domain\Repository\ElectorImportRepository
     * @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
104
105
106
107
108
109

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

Oliver Eglseder's avatar
Oliver Eglseder committed
113
114
115
    /**
     *
     */
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
    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
138
    {
139
140
141
142
        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
143
            foreach ($electorImports as $electorImport) {
144
145
146
147
                $electorImportObject = new ElectorImport();
                $electorImportObject->setFromArray($electorImport);
                $electorImportObject->setElectorate($electorImportDto->getChosenElectorate());
                $this->electorImportRepository->add($electorImportObject);
Oliver Eglseder's avatar
Oliver Eglseder committed
148
149
            }
        }
150
        $this->redirect(self::ACTION_PROCESS_IMPORT);
Oliver Eglseder's avatar
Oliver Eglseder committed
151
152
    }

Oliver Eglseder's avatar
Oliver Eglseder committed
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
    /**
     *
     */
    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);
    }

Oliver Eglseder's avatar
Oliver Eglseder committed
182
183
184
185
186
    /**
     * @return string
     */
    public static function getActionsForModuleConfiguration()
    {
187
188
189
190
191
192
193
194
195
196
197
198
199
        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
200
                self::ACTION_OVERWRITE_ALL,
201
202
            ]
        );
Oliver Eglseder's avatar
Oliver Eglseder committed
203
204
205
    }

    /**
Oliver Eglseder's avatar
Oliver Eglseder committed
206
     * @return int
Oliver Eglseder's avatar
Oliver Eglseder committed
207
208
209
210
211
212
     */
    protected function getRequiredUserGroup()
    {
        return $this->configuration->getElectionManagerGroup();
    }
}