Passed
Pull Request — master (#1249)
by
unknown
19:13
created

HierarchicalFacetHelper   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 69.74%

Importance

Changes 0
Metric Value
wmc 13
c 0
b 0
f 0
lcom 1
cbo 3
dl 0
loc 134
ccs 53
cts 76
cp 0.6974
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getFacetOptionLabel() 0 10 1
B getMenuStructure() 0 41 6
B getSubMenu() 0 45 6
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Facet;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2012-2015 Ingo Renner <[email protected]>
8
 *  All rights reserved
9
 *
10
 *  This script is part of the TYPO3 project. The TYPO3 project is
11
 *  free software; you can redistribute it and/or modify
12
 *  it under the terms of the GNU General Public License as published by
13
 *  the Free Software Foundation; either version 2 of the License, or
14
 *  (at your option) any later version.
15
 *
16
 *  The GNU General Public License can be found at
17
 *  http://www.gnu.org/copyleft/gpl.html.
18
 *
19
 *  This script is distributed in the hope that it will be useful,
20
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 *  GNU General Public License for more details.
23
 *
24
 *  This copyright notice MUST APPEAR in all copies of the script!
25
 ***************************************************************/
26
27
use TYPO3\CMS\Core\Utility\GeneralUtility;
28
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
29
30
/**
31
 * Helper for the hierarchical menu structure.
32
 *
33
 * @author Ingo Renner <[email protected]>
34
 */
35
class HierarchicalFacetHelper
36
{
37
38
    /**
39
     * Parent content object, set when called by ContentObjectRenderer->callUserFunction()
40
     *
41
     * @var ContentObjectRenderer
42
     */
43
    public $cObj;
44
45
    /**
46
     * Builds a menu structure usable with HMENU and returns it.
47
     *
48
     * Starts with the top level menu entries and hands the sub menu building
49
     * off to a recursive method.
50
     *
51
     * @param string $content
52
     * @param array $configuration
53
     * @return array A menu structure usable for HMENU
54
     */
55 18
    public function getMenuStructure(
56
        /** @noinspection PhpUnusedParameterInspection */
57
        $content,
0 ignored issues
show
Unused Code introduced by
The parameter $content is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
58
        /** @noinspection PhpUnusedParameterInspection */
59
        array $configuration
0 ignored issues
show
Unused Code introduced by
The parameter $configuration is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
60
    ) {
61 18
        $menuStructure = [];
62 18
        $facetOptions = $this->cObj->data['facetOptions'];
63
64 18
        foreach ($facetOptions as $facetOptionKey => $facetOption) {
65
            // let's start with top level menu options
66 18
            if (substr($facetOptionKey, 0, 1) == '0') {
67
                $topLevelMenu = [
68 18
                    'title' => $this->getFacetOptionLabel($facetOptionKey,
69 18
                        $facetOption['numberOfResults']),
70 18
                    'facetKey' => HierarchicalFacetRenderer::getLastPathSegmentFromHierarchicalFacetOption($facetOptionKey),
71 18
                    'numberOfResults' => $facetOption['numberOfResults'],
72 18
                    '_OVERRIDE_HREF' => $facetOption['url'],
73 18
                    'ITEM_STATE' => $facetOption['selected'] ? 'ACT' : 'NO',
74 18
                    '_PAGES_OVERLAY' => ($GLOBALS['TSFE']->sys_language_uid > 0)
75 18
                ];
76
77 18
                list(, $mainMenuName) = explode('-', $facetOptionKey, 2);
78
79
                // build sub menus recursively
80 18
                $subMenu = $this->getSubMenu($facetOptions, $mainMenuName, 1);
81 18
                if (!empty($subMenu)) {
82 17
                    $topLevelMenu['_SUB_MENU'] = $subMenu;
83 17
                    if ($topLevelMenu['ITEM_STATE'] == 'ACT') {
84
                        $topLevelMenu['ITEM_STATE'] = 'ACTIFSUB';
85
                    } else {
86 17
                        $topLevelMenu['ITEM_STATE'] = 'IFSUB';
87
                    }
88 17
                }
89
90 18
                $menuStructure[] = $topLevelMenu;
91 18
            }
92 18
        }
93
94 18
        return $menuStructure;
95
    }
96
97
    /**
98
     * Generates a facet option label from the given facet option.
99
     *
100
     * @param string $facetOptionKey A hierachical facet option path
101
     * @param int $facetOptionResultCount
102
     * @return string The label for the facet option consisting of the last part of the path and the options result count
103
     */
104 18
    protected function getFacetOptionLabel(
105
        $facetOptionKey,
106
        $facetOptionResultCount
107
    ) {
108
        // use the last path segment and the result count to build the label
109 18
        $lastPathSegment = HierarchicalFacetRenderer::getLastPathSegmentFromHierarchicalFacetOption($facetOptionKey);
110 18
        $facetOptionLabel = $lastPathSegment . ' (' . $facetOptionResultCount . ')';
111
112 18
        return $facetOptionLabel;
113
    }
114
115
    /**
116
     * Recursively builds a sub menu structure for the current menu.
117
     *
118
     * @param array $facetOptions Array of facet options
119
     * @param string $menuName Name of the top level menu to build the sub menu structure for
120
     * @param int $level The sub level depth
121
     * @return array Returns an array sub menu structure if a sub menu exists, an empty array otherwise
122
     */
123 18
    protected function getSubMenu(array $facetOptions, $menuName, $level)
124
    {
125 18
        $menu = [];
126
127 18
        $subMenuEntryPrefix = $level . '-' . $menuName;
128
129 18
        foreach ($facetOptions as $facetOptionKey => $facetOption) {
130
            // find the sub menu items for the current menu
131 18
            if (GeneralUtility::isFirstPartOfStr($facetOptionKey,
132 18
                $subMenuEntryPrefix)
133 18
            ) {
134
                $currentMenu = [
135 17
                    'title' => $this->getFacetOptionLabel($facetOptionKey,
136 17
                        $facetOption['numberOfResults']),
137 17
                    'facetKey' => HierarchicalFacetRenderer::getLastPathSegmentFromHierarchicalFacetOption($facetOptionKey),
138 17
                    'numberOfResults' => $facetOption['numberOfResults'],
139 17
                    '_OVERRIDE_HREF' => $facetOption['url'],
140 17
                    'ITEM_STATE' => $facetOption['selected'] ? 'ACT' : 'NO',
141 17
                    '_PAGES_OVERLAY' => ($GLOBALS['TSFE']->sys_language_uid > 0)
142 17
                ];
143
144 17
                $lastPathSegment = HierarchicalFacetRenderer::getLastPathSegmentFromHierarchicalFacetOption($facetOptionKey);
145
146
                // move one level down (recursion)
147 17
                $subMenu = $this->getSubMenu(
148 17
                    $facetOptions,
149 17
                    $menuName . $lastPathSegment . '/',
150
                    $level + 1
151 17
                );
152 17
                if (!empty($subMenu)) {
153
                    $currentMenu['_SUB_MENU'] = $subMenu;
154
                    if ($currentMenu['ITEM_STATE'] == 'ACT') {
155
                        $currentMenu['ITEM_STATE'] = 'ACTIFSUB';
156
                    } else {
157
                        $currentMenu['ITEM_STATE'] = 'IFSUB';
158
                    }
159
                }
160
161 17
                $menu[] = $currentMenu;
162 17
            }
163 18
        }
164
165
        // return one level up
166 18
        return $menu;
167
    }
168
}
169