Completed
Branch master (b9fc31)
by Timo
05:19
created

HierarchicalFacetHelper::getMenuStructure()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 37
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 6.288

Importance

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