Completed
Push — master ( a54493...36663b )
by Fabien
05:49
created

ClipboardController::flushAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
namespace Fab\Vidi\Controller;
3
4
/**
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
 * It is free software; you can redistribute it and/or modify it under
8
 * the terms of the GNU General Public License, either version 2
9
 * of the License, or any later version.
10
 *
11
 * For the full copyright and license information, please read the
12
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
 * The TYPO3 project - inspiring people to share!
15
 */
16
17
use TYPO3\CMS\Core\Utility\GeneralUtility;
18
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
19
use Fab\Vidi\Persistence\MatcherObjectFactory;
20
21
/**
22
 * Controller which handles actions related to Vidi in the Backend.
23
 */
24
class ClipboardController extends ActionController
25
{
26
27
    /**
28
     * Save data into the clipboard.
29
     *
30
     * @param array $matches
31
     * @return string
32
     */
33
    public function saveAction(array $matches = array())
34
    {
35
36
        $matcher = MatcherObjectFactory::getInstance()->getMatcher($matches);
37
        $this->getClipboardService()->save($matcher);
38
39
        // Fetch objects via the Content Service.
40
        $contentService = $this->getContentService()->findBy($matcher);
41
        $numberOfObjects = $contentService->getNumberOfObjects();
42
43
        if ($numberOfObjects === 0) {
44
            $this->getClipboardService()->flush();
45
        }
46
47
        # Json header is not automatically sent in the BE...
48
        $this->response->setHeader('Content-Type', 'application/json');
49
        $this->response->sendHeaders();
50
        return json_encode($numberOfObjects);
51
    }
52
53
    /**
54
     * Completely flush the clipboard.
55
     *
56
     * @return string
57
     */
58
    public function flushAction()
59
    {
60
        $this->getClipboardService()->flush();
61
62
        # Json header is not automatically sent in the BE...
63
        $this->response->setHeader('Content-Type', 'application/json');
64
        $this->response->sendHeaders();
65
        return json_encode(TRUE);
66
    }
67
68
    /**
69
     * Show the content of the clipboard.
70
     *
71
     * @return string
72
     */
73
    public function showAction()
74
    {
75
76
        // Retrieve matcher object from clipboard.
77
        $matcher = $this->getClipboardService()->getMatcher();
78
79
        // Fetch objects via the Content Service.
80
        $contentService = $this->getContentService()->findBy($matcher);
81
82
        // count number of items and display it.
83
        $this->view->assign('target', GeneralUtility::_GP('id'));
84
        $this->view->assign('numberOfObjects', $contentService->getNumberOfObjects());
85
        $this->view->assign('objects', $contentService->getObjects());
86
    }
87
88
    /**
89
     * @return \Fab\Vidi\Service\ClipboardService
90
     */
91
    protected function getClipboardService()
92
    {
93
        return GeneralUtility::makeInstance('Fab\Vidi\Service\ClipboardService');
94
    }
95
96
    /**
97
     * @return \Fab\Vidi\Service\ContentService
98
     */
99
    protected function getContentService()
100
    {
101
        return GeneralUtility::makeInstance('Fab\Vidi\Service\ContentService');
102
    }
103
104
}
105