BackendLayoutDataProvider   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 38
c 2
b 0
f 0
dl 0
loc 105
rs 10
wmc 14

5 Methods

Rating   Name   Duplication   Size   Complexity  
A typoscriptToString() 0 13 3
A getIconPath() 0 8 2
A getBackendLayout() 0 10 2
A addBackendLayouts() 0 15 4
A createBackendLayout() 0 22 3
1
<?php
2
namespace Qbus\Qbtools\Hooks\Options;
3
4
use TYPO3\CMS\Backend\View\BackendLayout\BackendLayout;
5
use TYPO3\CMS\Backend\View\BackendLayout\BackendLayoutCollection;
6
use TYPO3\CMS\Backend\View\BackendLayout\DataProviderContext;
7
8
/**
9
 * @todo deprecate
10
 */
11
class BackendLayoutDataProvider implements \TYPO3\CMS\Backend\View\BackendLayout\DataProviderInterface
12
{
13
    /**
14
     * @param  DataProviderContext     $dataProviderContext
15
     * @param  BackendLayoutCollection $backendLayoutCollection
16
     * @return void
17
     */
18
    public function addBackendLayouts(DataProviderContext $dataProviderContext, BackendLayoutCollection $backendLayoutCollection)
19
    {
20
        $BEfunc = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\CMS\Backend\Utility\BackendUtility');
21
        $pageTSconfig = $BEfunc->getPagesTSconfig(1);
22
23
        if (isset($pageTSconfig['tx_qbtools.']['backend_layout.'])) {
24
            foreach ($pageTSconfig['tx_qbtools.']['backend_layout.'] as $id => $layout) {
25
                if (substr($id, -1) != '.') {
26
                    continue;
27
                }
28
29
                $id = substr($id, 0, -1);
30
31
                $backendLayout = $this->createBackendLayout($id, $layout);
32
                $backendLayoutCollection->add($backendLayout);
33
            }
34
        }
35
    }
36
37
    /**
38
     * Gets a backend layout by (regular) identifier.
39
     *
40
     * @param  string             $identifier
41
     * @param  int                $pageId
42
     * @return NULL|BackendLayout
43
     */
44
    public function getBackendLayout($identifier, $pageId)
45
    {
46
        $BEfunc = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\CMS\Backend\Utility\BackendUtility');
47
        $pageTSconfig = $BEfunc->getPagesTSconfig(1);
48
49
        if (isset($pageTSconfig['tx_qbtools.']['backend_layout.'][$identifier . '.'])) {
50
            return $this->createBackendLayout($identifier, $pageTSconfig['tx_qbtools.']['backend_layout.'][$identifier . '.']);
51
        }
52
53
        return null;
54
    }
55
56
    protected function typoscriptToString(array $config)
57
    {
58
        $str = '';
59
60
        foreach ($config as $key => $value) {
61
            if (is_array($value)) {
62
                $str .= sprintf("%s {\n%s\n}\n", substr($key, 0, -1), $this->typoscriptToString($value));
63
            } else {
64
                $str .= sprintf("%s = %s\n", $key, $value);
65
            }
66
        }
67
68
        return $str;
69
    }
70
71
    /**
72
     * Creates a new backend layout using the given record data.
73
     *
74
     * @param  string        $id
75
     * @param  array         $data
76
     * @return BackendLayout
77
     */
78
    protected function createBackendLayout($id, $layout)
79
    {
80
        $layout['uid'] = $id;
81
82
        if (isset($layout['config.'])) {
83
            $layout['config'] = array('backend_layout.' => $layout['config.']);
84
            unset($layout['config.']);
85
86
            $layout['config'] = $this->typoscriptToString($layout['config']);
87
        }
88
89
        if (!isset($layout['title'])) {
90
            $layout['title'] = 'Untitled';
91
        }
92
93
        $layout['icon'] = $this->getIconPath($layout['icon']);
94
95
        $backendLayout = BackendLayout::create($layout['uid'], $layout['title'], $layout['config']);
96
        $backendLayout->setIconPath($this->getIconPath($layout['icon']));
97
        $backendLayout->setData($layout);
98
99
        return $backendLayout;
100
    }
101
102
    /**
103
     * Gets and sanitizes the icon path.
104
     *
105
     * @param  string $icon Name of the icon file
106
     * @return string
107
     */
108
    protected function getIconPath($icon)
109
    {
110
        $iconPath = '';
111
        if (!empty($icon)) {
112
            $iconPath = $icon;
113
        }
114
115
        return $iconPath;
116
    }
117
}
118