Completed
Push — master ( 6623e1...0a591e )
by Fabien
03:37
created

RelationRenderer::getEditUri()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 11
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 11
loc 11
rs 9.4285
cc 1
eloc 7
nc 1
nop 1
1
<?php
2
namespace Fab\Vidi\Grid;
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\Backend\Utility\BackendUtility;
18
use Fab\Vidi\Domain\Model\Content;
19
use Fab\Vidi\Tca\Tca;
20
use TYPO3\CMS\Core\Imaging\Icon;
21
22
/**
23
 * Class rendering relation
24
 */
25
class RelationRenderer extends ColumnRendererAbstract
26
{
27
28
    /**
29
     * Render a representation of the relation on the GUI.
30
     *
31
     * @return string
32
     */
33
    public function render()
34
    {
35
        if ($this->isBackendMode()) {
36
            $output = $this->renderForBackend();
37
        } else {
38
            $output = $this->renderForFrontend();
39
        }
40
41
        return $output;
42
    }
43
44
    /**
45
     * @return string
46
     */
47
    protected function renderForBackend()
48
    {
49
50
        $output = '';
51
52
        // Get label of the foreign table.
53
        $foreignLabelField = $this->getForeignTableLabelField($this->fieldName);
54
55
        if (Tca::table($this->object)->field($this->fieldName)->hasOne()) {
56
57
            $foreignObject = $this->object[$this->fieldName];
58
59
            if ($foreignObject) {
60
                $output = sprintf(
61
                    '<a href="%s" data-uid="%s" class="btn-edit invisible">%s</a> <span>%s</span>',
62
                    $this->getEditUri($foreignObject),
63
                    $this->object->getUid(),
64
                    $this->getIconFactory()->getIcon('actions-document-open', Icon::SIZE_SMALL),
65
                    $foreignObject[$foreignLabelField]
66
                );
67
            }
68
        } elseif (Tca::table($this->object)->field($this->fieldName)->hasMany()) {
69
70
            if (!empty($this->object[$this->fieldName])) {
71
72
                /** @var $foreignObject \Fab\Vidi\Domain\Model\Content */
73
                foreach ($this->object[$this->fieldName] as $foreignObject) {
74
                    $output .= sprintf(
75
                        '<li><a href="%s" data-uid="%s" class="btn-edit invisible">%s</a> <span>%s</span></li>',
76
                        $this->getEditUri($foreignObject),
77
                        $this->object->getUid(),
78
                        $this->getIconFactory()->getIcon('actions-document-open', Icon::SIZE_SMALL),
79
                        $foreignObject[$foreignLabelField]
80
                    );
81
                }
82
                $output = sprintf('<ul class="list-unstyled">%s</ul>', $output);
83
            }
84
        }
85
        return $output;
86
    }
87
88
    /**
89
     * @return string
90
     */
91
    protected function renderForFrontend()
92
    {
93
94
        $output = '';
95
96
        // Get label of the foreign table.
97
        $foreignLabelField = $this->getForeignTableLabelField($this->fieldName);
98
99
        if (Tca::table($this->object)->field($this->fieldName)->hasOne()) {
100
101
            $foreignObject = $this->object[$this->fieldName];
102
103
            if ($foreignObject) {
104
                $output = sprintf(
105
                    '%s',
106
                    $foreignObject[$foreignLabelField]
107
                );
108
            }
109
        } elseif (Tca::table($this->object)->field($this->fieldName)->hasMany()) {
110
111
            if (!empty($this->object[$this->fieldName])) {
112
113
                /** @var $foreignObject \Fab\Vidi\Domain\Model\Content */
114
                foreach ($this->object[$this->fieldName] as $foreignObject) {
115
                    $output .= sprintf(
116
                        '<li>%s</li>',
117
                        $foreignObject[$foreignLabelField]
118
                    );
119
                }
120
                $output = sprintf('<ul class="list-unstyled">%s</ul>', $output);
121
            }
122
        }
123
        return $output;
124
    }
125
126
    /**
127
     * Render an edit URI given an object.
128
     *
129
     * @param Content $object
130
     * @return string
131
     */
132 View Code Duplication
    protected function getEditUri(Content $object)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
133
    {
134
        $uri = BackendUtility::getModuleUrl(
135
            'record_edit',
136
            array(
137
                $this->getEditParameterName($object) => 'edit',
138
                'returnUrl' => $this->getModuleLoader()->getModuleUrl()
139
            )
140
        );
141
        return $uri;
142
    }
143
144
    /**
145
     * @param Content $object
146
     * @return string
147
     */
148
    protected function getEditParameterName(Content $object)
149
    {
150
        return sprintf(
151
            'edit[%s][%s]',
152
            $object->getDataType(),
153
            $object->getUid()
154
        );
155
    }
156
157
158
    /**
159
     * Return the label field of the foreign table.
160
     *
161
     * @param string $fieldName
162
     * @return string
163
     */
164
    protected function getForeignTableLabelField($fieldName)
165
    {
166
167
        // Get TCA table service.
168
        $table = Tca::table($this->object);
169
170
        // Compute the label of the foreign table.
171
        $relationDataType = $table->field($fieldName)->relationDataType();
172
        return Tca::table($relationDataType)->getLabelField();
173
    }
174
175
    /**
176
     * Returns whether the current mode is Frontend
177
     *
178
     * @return bool
179
     */
180
    protected function isBackendMode()
181
    {
182
        return TYPO3_MODE === 'BE';
183
    }
184
185
}
186