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

SiteSelectorField::getAvailableSitesSelector()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 24
ccs 0
cts 19
cp 0
rs 8.6845
cc 4
eloc 15
nc 3
nop 2
crap 20
1
<?php
2
3
namespace ApacheSolrForTypo3\Solr\Backend;
4
5
/***************************************************************
6
 *  Copyright notice
7
 *
8
 *  (c) 2017 - Thomas Hohn <[email protected]>
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 2 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
28
use ApacheSolrForTypo3\Solr\Domain\Site\SiteRepository;
29
use ApacheSolrForTypo3\Solr\Site;
30
use TYPO3\CMS\Core\Utility\GeneralUtility;
31
32
/**
33
 * SiteSelectorField
34
 *
35
 * Responsible for generating SiteSelectorField
36
 *
37
 * @author Thomas Hohn <[email protected]>
38
 */
39
class SiteSelectorField
40
{
41
    /**
42
     * Creates a dropdown selector of available TYPO3 sites with Solr configured.
43
     *
44
     * @param string $selectorName Name to be used in the select's name attribute
45
     * @param Site $selectedSite Optional, currently selected site
46
     * @return string Site selector HTML code
47
     */
48
    public function getAvailableSitesSelector(
49
        $selectorName,
50
        Site $selectedSite = null
51
    ) {
52
        $siteRepository = GeneralUtility::makeInstance(SiteRepository::class);
53
54
        $sites = $siteRepository->getAvailableSites();
55
        $selector = '<select name="' . $selectorName . '" class="form-control">';
56
57
        foreach ($sites as $site) {
58
            $selectedAttribute = '';
59
            if ($selectedSite !== null && $site->getRootPageId() == $selectedSite->getRootPageId()) {
60
                $selectedAttribute = ' selected="selected"';
61
            }
62
63
            $selector .= '<option value="' . $site->getRootPageId() . '"' . $selectedAttribute . '>'
64
                . $site->getLabel()
65
                . '</option>';
66
        }
67
68
        $selector .= '</select>';
69
70
        return $selector;
71
    }
72
}
73