ArticleController.php 11.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php

namespace T3o\T3oPlanet\Controller;

/***
 *
 * This file is part of the "Planet TYPO3" Extension for TYPO3 CMS.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 *
 *  (c) 2019
 *
 ***/

Thomas Löffler's avatar
Thomas Löffler committed
16
17
18
use T3o\T3oPlanet\Domain\Model\Article;
use T3o\T3oPlanet\Domain\Repository\ArticleRepository;
use T3o\T3oPlanet\Domain\Repository\TagRepository;
19
use T3o\T3oPlanet\Property\TypeConverter\UploadedFileReferenceConverter;
Thomas Löffler's avatar
Thomas Löffler committed
20
use TYPO3\CMS\Core\Messaging\FlashMessage;
21
use TYPO3\CMS\Core\Utility\GeneralUtility;
Thomas Löffler's avatar
Thomas Löffler committed
22
23
24
use TYPO3\CMS\Extbase\Domain\Model\FrontendUser;
use TYPO3\CMS\Extbase\Domain\Repository\CategoryRepository;
use TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository;
25
26
use TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration;

27
class ArticleController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController
28
{
29
    protected array $contentElementData;
30

Thomas Löffler's avatar
Thomas Löffler committed
31
    protected ?ArticleRepository $articleRepository;
32

Thomas Löffler's avatar
Thomas Löffler committed
33
    public function injectArticleRepository(ArticleRepository $articleRepository): void
34
    {
Thomas Löffler's avatar
Thomas Löffler committed
35
        $this->articleRepository = $articleRepository;
36
    }
37

Thomas Löffler's avatar
Thomas Löffler committed
38
    protected ?TagRepository $tagRepository;
39

Thomas Löffler's avatar
Thomas Löffler committed
40
    public function injectTagRepository(TagRepository $tagRepository): void
41
    {
Thomas Löffler's avatar
Thomas Löffler committed
42
        $this->tagRepository = $tagRepository;
43
44
    }

Thomas Löffler's avatar
Thomas Löffler committed
45
    protected ?CategoryRepository $categoryRepository;
46

Thomas Löffler's avatar
Thomas Löffler committed
47
    public function injectCategoryRepository(CategoryRepository $categoryRepository): void
48
    {
Thomas Löffler's avatar
Thomas Löffler committed
49
        $this->categoryRepository = $categoryRepository;
50
51
    }

Thomas Löffler's avatar
Thomas Löffler committed
52
    protected FrontendUserRepository $userRepository;
53

Thomas Löffler's avatar
Thomas Löffler committed
54
    public function injectUserRepository(FrontendUserRepository $frontendUserRepository): void
55
    {
Thomas Löffler's avatar
Thomas Löffler committed
56
        $this->userRepository = $frontendUserRepository;
57
    }
58
59
60
61
62
63
64

    /**
     * action initialize
     */
    public function initializeAction()
    {
        parent::initializeAction();
65
        $this->contentElementData = $this->configurationManager->getContentObject()->data;
66
67
68
69
70
71
72
73
74
    }
    /**
     * action featuredList
     */
    public function featuredListAction()
    {
        // Records per page
        $limit = 4;
        if ($this->settings['articlesPerPage']) {
75
            $limit = (int)$this->settings['articlesPerPage'];
76
        }
77
        $t3planetArticles = $this->articleRepository->getFeaturedArticles($limit);
78
79
80
81
82
83
        $this->view->assign('t3planetArticles', $t3planetArticles);
        $this->view->assign('data', $this->contentElementData);
    }

    public function filterArticleAction()
    {
84
        $t3planetTags = $this->tagRepository->findAll();
85
86
        $argument = $this->request->getArguments();
        if ($argument['category']) {
87
            $category = (int)$this->request->getArgument('category');
88
            $t3planetArticles = $this->articleRepository->getAcceptedArticlesByCategoryId($category);
89
        } else {
90
            $t3planetArticles = $this->articleRepository->getAcceptedArticles();
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
        }

        $t3planetArticlesInfo = [];
        if ($t3planetArticles) {
            foreach ($t3planetArticles as $articleKey => $article) {
                $t3planetArticlesInfo[$articleKey]['articleDate'] = $article->getArticleDate();
                $t3planetArticlesInfo[$articleKey]['title'] = $article->getTitle();
                $t3planetArticlesInfo[$articleKey]['teasertext'] = $article->getTeasertext();
                $t3planetArticlesInfo[$articleKey]['link'] = $article->getLink();
                $t3planetArticlesInfo[$articleKey]['name'] = $article->getName();
                $t3planetArticlesInfo[$articleKey]['email'] = $article->getEmail();
                $t3planetArticlesInfo[$articleKey]['feUserid'] = $article->getFeUserid();
                $t3planetArticlesInfo[$articleKey]['image'] = $article->getImage();

                $t3planetArticlesInfo[$articleKey]['tags'] = '';
                $tags = $article->getTagId();
                foreach ($tags as $tag) {
                    $t3planetArticlesInfo[$articleKey]['tags'] = 'tag-' . $tag->getUid();
                }
            }
        }

        $assignArray = [
            't3planetArticles' => $t3planetArticlesInfo,
            't3planetTags' => $t3planetTags,
            'data' => $this->contentElementData,
        ];
        $this->view->assignMultiple($assignArray);
    }

    /**
     * action list
     */
    public function articlesManagementAction()
    {
        $statusArr = [
Thomas Löffler's avatar
Thomas Löffler committed
127
128
129
            Article::STATUS_PENDING => 'Pending',
            Article::STATUS_ACCEPTED => 'Accepted',
            Article::STATUS_REJECTED => 'Rejected',
130
131
132
133
134
        ];
        $panelType = $this->settings['panelType'];
        if ($panelType) {
            $request = $this->request->getArguments();
            if ($request['newT3planetArticles']['search'] != '') {
135
                $t3planetArticles = $this->articleRepository->getArticlesBysearch($request['newT3planetArticles']['search']);
136
            } else {
137
                $t3planetArticles = $this->articleRepository->findAll();
138
139
140
            }
        } else {
            $userID = $GLOBALS['TSFE']->fe_user->user['uid'];
141
            $t3planetArticles = $this->articleRepository->getArticlesByUser($userID);
142
143
144
145
146
147
148
149
150
151
152
153
154
        }

        $assignArray = [
            't3planetArticles' => $t3planetArticles,
            'statusArr' => $statusArr,
            'data' => $this->contentElementData,
        ];
        $this->view->assignMultiple($assignArray);
    }

    /**
     * action new
     */
155
    public function newAction(int $backPid = 0)
156
    {
157
158
159
160
        $userId = $GLOBALS['TSFE']->fe_user->user['uid'];
        $mainPlanetTypo3Category = $this->categoryRepository->findByUid((int)$this->settings['mainCategory']);
        $t3planetCategories = $this->categoryRepository->findByParent($mainPlanetTypo3Category);
        $t3planetTags = $this->tagRepository->findAll();
161
162
163
        $assignArray = [
            't3planetTags' => $t3planetTags,
            't3planetCategories' => $t3planetCategories,
164
165
            'backPID' => $backPid,
            'user' => $userId,
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
            'data' => $this->contentElementData,
        ];
        $this->view->assignMultiple($assignArray);
    }

    /**
     * Set TypeConverter option for image upload
     */
    public function initializeCreateAction()
    {
        $this->setTypeConverterConfigurationForImageUpload('newT3planetArticles');
        $getArguments = $this->request->getArguments();
        if ($getArguments['newT3planetArticles']['image']['error'] > 0) {
            unset($getArguments['newT3planetArticles']['image']);
        }
        if (isset($this->arguments['newT3planetArticles'])) {
            $this->arguments['newT3planetArticles']
                ->getPropertyMappingConfiguration()
                ->forProperty('articleDate')
                ->setTypeConverterOption('TYPO3\\CMS\\Extbase\\Property\\TypeConverter\\DateTimeConverter', \TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::CONFIGURATION_DATE_FORMAT, 'd-m-Y');
        }
        $this->request->setArguments($getArguments);
    }

    /**
     * action create
     *
193
     * @param \T3o\T3oPlanet\Domain\Model\Article $newT3planetArticles
194
     */
Thomas Löffler's avatar
Thomas Löffler committed
195
    public function createAction(Article $newT3planetArticles)
196
    {
197
        $backPid = (int)GeneralUtility::_GP('backPID');
198
199
200
201
202
203

        $user = $this->getCurrentUser();
        $storagePid = $this->configurationManager->getContentObject()->data['pages'];
        $date = \DateTime::createFromFormat('d-m-Y', date('d-m-Y'));

        $newT3planetArticles->setArticleDate($date);
204
        if ($user) {
205
            $newT3planetArticles->setName($user->getName());
206
            $newT3planetArticles->setEmail($user->getEmail());
207
208
            $newT3planetArticles->setFeUserid($user->getUsername());
            $newT3planetArticles->setPid($storagePid);
209

210
            $this->articleRepository->add($newT3planetArticles);
Thomas Löffler's avatar
Fix CGL    
Thomas Löffler committed
211
        } else {
212
213
            $this->addFlashMessage('You need to login first!', 'No access!', FlashMessage::ERROR);
        }
214

215
        if ($backPid) {
Thomas Löffler's avatar
Thomas Löffler committed
216
            $this->addFlashMessage('A new article inserted successfully!', 'Success!', FlashMessage::OK);
217
218
            $uriBuilder = $this->uriBuilder;
            $uri = $uriBuilder
219
                ->setTargetPageUid($backPid)
220
221
222
                ->build();
            $this->redirectToUri($uri, 0, 301);
        } else {
223
            $this->redirect('articlesManagement');
224
225
226
        }
    }

Thomas Löffler's avatar
Thomas Löffler committed
227
    public function getCurrentUser(): FrontendUser
228
    {
229
        if ($this->currentUser === null && $GLOBALS['TSFE']->fe_user->user['uid'] > 0) {
230
231
            $this->currentUser = $this->userRepository->findByUid($GLOBALS['TSFE']->fe_user->user['uid']);
        }
232

233
234
235
236
237
238
        return $this->currentUser;
    }

    /**
     * action edit
     *
239
     * @param \T3o\T3oPlanet\Domain\Model\Article $t3planetArticles
240
     */
Thomas Löffler's avatar
Thomas Löffler committed
241
    public function editAction(Article $t3planetArticles)
242
    {
243
244
        $t3planetCategories = $this->categoryRepository->findAll();
        $t3planetTags = $this->tagRepository->findAll();
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
        $assignArray = [
            't3planetTags' => $t3planetTags,
            't3planetCategories' => $t3planetCategories,
            't3planetArticles' => $t3planetArticles,
            'data' => $this->contentElementData,
        ];
        $this->view->assignMultiple($assignArray);
    }

    /**
     * Set TypeConverter option for image upload
     */
    public function initializeUpdateAction()
    {
        $this->setTypeConverterConfigurationForImageUpload('t3planetArticles');
    }

    /**
     * action update
     *
265
     * @param \T3o\T3oPlanet\Domain\Model\Article $t3planetArticles
266
     */
Thomas Löffler's avatar
Thomas Löffler committed
267
    public function updateAction(Article $t3planetArticles)
268
269
    {
        $this->addFlashMessage('The article updated successfully!', 'Success!', \TYPO3\CMS\Core\Messaging\AbstractMessage::OK);
270
        $this->articleRepository->update($t3planetArticles);
271
272
273
274
275
276
277
278
279
280
        $this->redirect('list');
    }

    /**
     * action updateStatus
     */
    public function updateStatusAction()
    {
        $request = $this->request->getArguments();
        if ($request['newT3planetArticles']) {
281
            $articles = $this->articleRepository->findByUid($request['newT3planetArticles']['uid']);
282
            $articles->setArticleStatus($request['newT3planetArticles']['articleStatus']);
283
            $this->articleRepository->update($articles);
284
285
286
287
288
289
290
291
            $this->addFlashMessage('The article updated successfully!', 'Success!', \TYPO3\CMS\Core\Messaging\AbstractMessage::OK);
            $this->redirect('articlesManagement');
        }
    }

    /**
     * action delete
     *
292
     * @param \T3o\T3oPlanet\Domain\Model\Article $t3planetArticles
293
     */
Thomas Löffler's avatar
Thomas Löffler committed
294
    public function deleteAction(Article $t3planetArticles)
295
    {
296
        $this->articleRepository->remove($t3planetArticles);
297
298
299
300
        $this->addFlashMessage('The article deleted successfully!', 'Success!', \TYPO3\CMS\Core\Messaging\AbstractMessage::OK);
        $this->redirect('list');
    }

301
    protected function setTypeConverterConfigurationForImageUpload(string $argumentName)
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
    {
        $uploadConfiguration = [
            UploadedFileReferenceConverter::CONFIGURATION_ALLOWED_FILE_EXTENSIONS => $GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'],
            UploadedFileReferenceConverter::CONFIGURATION_UPLOAD_FOLDER => '1:/Planet/',
        ];

        /** @var PropertyMappingConfiguration $newExampleConfiguration */
        $newExampleConfiguration = $this->arguments[$argumentName]->getPropertyMappingConfiguration();

        $newExampleConfiguration->forProperty('image')
            ->setTypeConverterOptions(
                'T3o\\T3oPlanet\\Property\\TypeConverter\\UploadedFileReferenceConverter',
                $uploadConfiguration
            );
    }
}