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

RelationCountRenderer::renderForBackend()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 34
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 34
rs 8.439
cc 5
eloc 23
nc 8
nop 0
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 TYPO3\CMS\Core\Imaging\Icon;
19
use TYPO3\CMS\Core\Utility\GeneralUtility;
20
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
21
use Fab\Vidi\Tca\Tca;
22
23
/**
24
 * Class rendering relation
25
 */
26
class RelationCountRenderer extends ColumnRendererAbstract
27
{
28
29
    /**
30
     * Render a representation of the relation on the GUI.
31
     *
32
     * @return string
33
     */
34
    public function render()
35
    {
36
37
        $output = '';
38
        if ($this->isBackendMode()) {
39
            $output = $this->renderForBackend();
40
        }
41
42
        return $output;
43
    }
44
45
    /**
46
     * @return string
47
     */
48
    protected function renderForBackend()
49
    {
50
51
        $numberOfObjects = count($this->object[$this->fieldName]);
52
53
        if ($numberOfObjects > 1) {
54
            $label = 'LLL:EXT:vidi/Resources/Private/Language/locallang.xlf:items';
55
            if (isset($this->gridRendererConfiguration['labelPlural'])) {
56
                $label = $this->gridRendererConfiguration['labelPlural'];
57
            }
58
        } else {
59
            $label = 'LLL:EXT:vidi/Resources/Private/Language/locallang.xlf:item';
60
            if (isset($this->gridRendererConfiguration['labelSingular'])) {
61
                $label = $this->gridRendererConfiguration['labelSingular'];
62
            }
63
        }
64
65
        $template = '<a href="%s&returnUrl=%s&search=%s&query=%s:%s">%s %s<span class="invisible" style="padding-left: 5px">%s</span></a>';
66
67
        $foreignField = Tca::table($this->object)->field($this->fieldName)->getForeignField();
68
        $search = json_encode(array(array($foreignField => $this->object->getUid())));
69
70
        $moduleTarget = empty($this->gridRendererConfiguration['targetModule']) ? '' : $this->gridRendererConfiguration['targetModule'];
71
        return sprintf($template,
72
            BackendUtility::getModuleUrl($moduleTarget),
73
            rawurlencode(BackendUtility::getModuleUrl($this->gridRendererConfiguration['sourceModule'])),
74
            rawurlencode($search),
75
            rawurlencode($foreignField),
76
            rawurlencode($this->object->getUid()),
77
            htmlspecialchars($numberOfObjects),
78
            htmlspecialchars(LocalizationUtility::translate($label, '')),
79
            $this->getIconFactory()->getIcon('extensions-vidi-go', Icon::SIZE_SMALL)
80
        );
81
    }
82
83
    /**
84
     * Returns whether the current mode is Frontend
85
     *
86
     * @return bool
87
     */
88
    protected function isBackendMode()
89
    {
90
        return TYPO3_MODE === 'BE';
91
    }
92
93
}
94