Passed
Pull Request — master (#135)
by Romain
03:12
created

ShowNotificationDetailsButton::getModuleTemplate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * Copyright (C) 2018
5
 * Nathan Boiron <[email protected]>
6
 * Romain Canon <[email protected]>
7
 *
8
 * This file is part of the TYPO3 NotiZ project.
9
 * It is free software; you can redistribute it and/or modify it
10
 * under the terms of the GNU General Public License, either
11
 * version 3 of the License, or any later version.
12
 *
13
 * For the full copyright and license information, see:
14
 * http://www.gnu.org/licenses/gpl-3.0.html
15
 */
16
17
namespace CuyZ\Notiz\Backend\FormEngine\ButtonBar;
18
19
use CuyZ\Notiz\Core\Definition\DefinitionService;
20
use CuyZ\Notiz\Core\Definition\Tree\Notification\NotificationDefinition;
21
use CuyZ\Notiz\Core\Notification\Viewable;
22
use CuyZ\Notiz\Domain\Notification\EntityNotification;
23
use CuyZ\Notiz\Service\LocalizationService;
24
use ReflectionClass;
25
use TYPO3\CMS\Backend\Controller\EditDocumentController;
26
use TYPO3\CMS\Backend\Template\Components\ButtonBar;
27
use TYPO3\CMS\Backend\Template\ModuleTemplate;
28
use TYPO3\CMS\Core\Imaging\Icon;
29
use TYPO3\CMS\Core\Imaging\IconFactory;
30
use TYPO3\CMS\Core\SingletonInterface;
31
32
/**
33
 * Adds a button "View details" to the button bar at the top of the screen when
34
 * editing a notification record.
35
 *
36
 * Clicking this button loads the NotiZ module showing more information about
37
 * the current notification.
38
 */
39
class ShowNotificationDetailsButton implements SingletonInterface
40
{
41
    /**
42
     * @var DefinitionService
43
     */
44
    protected $definitionService;
45
46
    /**
47
     * @var IconFactory
48
     */
49
    protected $iconFactory;
50
51
    /**
52
     * @param DefinitionService $definitionService
53
     * @param IconFactory $iconFactory
54
     */
55
    public function __construct(DefinitionService $definitionService, IconFactory $iconFactory)
56
    {
57
        $this->definitionService = $definitionService;
58
        $this->iconFactory = $iconFactory;
59
    }
60
61
    /**
62
     * @param EditDocumentController $controller
63
     */
64
    public function addButton(EditDocumentController $controller)
65
    {
66
        if ($this->definitionService->getValidationResult()->hasErrors()) {
67
            return;
68
        }
69
70
        foreach ($this->definitionService->getDefinition()->getNotifications() as $notificationDefinition) {
71
            $notification = $this->getNotification($notificationDefinition, $controller);
72
73
            if ($notification) {
74
                $this->addButtonForNotification($controller, $notification);
75
76
                break;
77
            }
78
        }
79
    }
80
81
    /**
82
     * @param NotificationDefinition $notificationDefinition
83
     * @param EditDocumentController $controller
84
     * @return Viewable
85
     */
86
    protected function getNotification(NotificationDefinition $notificationDefinition, EditDocumentController $controller)
87
    {
88
        /** @var EntityNotification|Viewable $className */
89
        $className = $notificationDefinition->getClassName();
90
91
        if (!in_array(Viewable::class, class_implements($className))
92
            || !in_array(EntityNotification::class, class_parents($className))
93
        ) {
94
            return null;
95
        }
96
97
        $tableName = $className::getTableName();
0 ignored issues
show
Bug introduced by
The method getTableName() does not exist on CuyZ\Notiz\Core\Notification\Viewable. Since it exists in all sub-types, consider adding an abstract or default implementation to CuyZ\Notiz\Core\Notification\Viewable. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

97
        /** @scrutinizer ignore-call */ 
98
        $tableName = $className::getTableName();
Loading history...
98
99
        if (!isset($controller->editconf[$tableName])) {
100
            return null;
101
        }
102
103
        $uid = reset(array_keys($controller->editconf[$tableName]));
104
105
        // We show the button only for existing records being edited.
106
        if ($controller->editconf[$tableName][$uid] !== 'edit') {
107
            return null;
108
        }
109
110
        /** @var Viewable $notification */
111
        $notification = $notificationDefinition->getProcessor()->getNotificationFromIdentifier($uid);
112
113
        return $notification;
114
    }
115
116
    /**
117
     * @param EditDocumentController $controller
118
     * @param Viewable $notification
119
     */
120
    protected function addButtonForNotification(EditDocumentController $controller, Viewable $notification)
121
    {
122
        $buttonBar = $this->getModuleTemplate($controller)
123
            ->getDocHeaderComponent()
124
            ->getButtonBar();
125
126
        $button = $buttonBar->makeLinkButton()
127
            ->setShowLabelText(true)
128
            ->setHref($notification->getViewUri())
129
            ->setTitle(LocalizationService::localize('Notification/Entity/ButtonBar:view_details'))
130
            ->setIcon($this->iconFactory->getIcon(
131
                'actions-view',
132
                Icon::SIZE_SMALL
133
            ));
134
135
        $buttonBar->addButton($button, ButtonBar::BUTTON_POSITION_LEFT, 50);
136
    }
137
138
    /**
139
     * Unfortunately TYPO3 doesn't provide a public API to access the module
140
     * template and add an icon to it, so we need to cheat a bit.
141
     *
142
     * @param EditDocumentController $controller
143
     * @return ModuleTemplate
144
     */
145
    protected function getModuleTemplate(EditDocumentController $controller)
146
    {
147
        $reflection = new ReflectionClass($controller);
148
        $property = $reflection->getProperty('moduleTemplate');
149
        $property->setAccessible(true);
150
151
        /** @var ModuleTemplate $moduleTemplate */
152
        $moduleTemplate = $property->getValue($controller);
153
154
        return $moduleTemplate;
155
    }
156
}
157