Passed
Push — master ( db7072...847c39 )
by Rafael
120:36 queued 75:29
created

Util::getSolrConfigurationFromPageId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 7
ccs 0
cts 5
cp 0
rs 10
cc 2
nc 2
nop 3
crap 6
1
<?php
2
3
/*
4
 * This file is part of the TYPO3 CMS project.
5
 *
6
 * It is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License, either version 2
8
 * of the License, or any later version.
9
 *
10
 * For the full copyright and license information, please read the
11
 * LICENSE.txt file that was distributed with this source code.
12
 *
13
 * The TYPO3 project - inspiring people to share!
14
 */
15
16
namespace ApacheSolrForTypo3\Solr;
17
18
use ApacheSolrForTypo3\Solr\Domain\Site\SiteRepository;
19
use ApacheSolrForTypo3\Solr\System\Configuration\ConfigurationManager;
20
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
21
use TYPO3\CMS\Core\Context\Context;
22
use TYPO3\CMS\Backend\Utility\BackendUtility;
23
use TYPO3\CMS\Core\Utility\GeneralUtility;
24
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
25
26
/**
27
 * Utility class for tx_solr
28
 *
29
 * @author Ingo Renner <[email protected]>
30
 * (c) 2009-2015 Ingo Renner <[email protected]>
31
 */
32
class Util
33
{
34
35
    /**
36
     * Generates a document id for documents representing page records.
37
     *
38
     * @param int $uid The page's uid
39
     * @param int $typeNum The page's typeNum
40
     * @param int $language the language id, defaults to 0
41
     * @param string $accessGroups comma separated list of uids of groups that have access to that page
42
     * @param string $mountPointParameter The mount point parameter that is used to access the page.
43
     * @return string The document id for that page
44
     */
45
    public static function getPageDocumentId($uid, $typeNum = 0, $language = 0, $accessGroups = '0,-1', $mountPointParameter = '')
46
    {
47
        $additionalParameters = $typeNum . '/' . $language . '/' . $accessGroups;
48
49
        if ((string)$mountPointParameter !== '') {
50
            $additionalParameters = $mountPointParameter . '/' . $additionalParameters;
51
        }
52
53
        $documentId = self::getDocumentId('pages', $uid, $uid, $additionalParameters);
54
55
        return $documentId;
56
    }
57
58
    /**
59
     * Generates a document id in the form $siteHash/$type/$uid.
60
     *
61
     * @param string $table The records table name
62
     * @param int $rootPageId The record's site root id
63
     * @param int $uid The record's uid
64
     * @param string $additionalIdParameters Additional ID parameters
65
     * @return string A document id
66
     */
67
    public static function getDocumentId($table, $rootPageId, $uid, $additionalIdParameters = '')
68
    {
69 31
            /** @var SiteRepository $siteRepository */
70
        $siteRepository = GeneralUtility::makeInstance(SiteRepository::class);
71 31
        $site = $siteRepository->getSiteByPageId($rootPageId);
72
        $siteHash = $site->getSiteHash();
73 31
74 2
        $documentId = $siteHash . '/' . $table . '/' . $uid;
75
        if (!empty($additionalIdParameters)) {
76
            $documentId .= '/' . $additionalIdParameters;
77 31
        }
78
79 31
        return $documentId;
80
    }
81
82
    /**
83
     * Shortcut to retrieve the TypoScript configuration for EXT:solr
84
     *
85
     * @return TypoScriptConfiguration
86
     */
87
    public static function getSolrConfiguration()
88
    {
89
        $configurationManager = GeneralUtility::makeInstance(ConfigurationManager::class);
90
        return $configurationManager->getTypoScriptConfiguration();
91 50
    }
92
93
    /**
94 50
     * Check if record ($table, $uid) is a workspace record
95 50
     *
96 50
     * @param string $table The table the record belongs to
97
     * @param int $uid The record's uid
98 50
     * @return bool TRUE if the record is in a draft workspace, FALSE if it's a LIVE record
99 50
     */
100 31
    public static function isDraftRecord($table, $uid)
101
    {
102
        $isWorkspaceRecord = false;
103 50
104
        if ((ExtensionManagementUtility::isLoaded('workspaces')) && (BackendUtility::isTableWorkspaceEnabled($table))) {
105
            $record = BackendUtility::getRecord($table, $uid, 'pid, t3ver_state');
106
107
            if ($record['pid'] == '-1' || $record['t3ver_state'] > 0) {
108
                $isWorkspaceRecord = true;
109
            }
110
        }
111 158
112
        return $isWorkspaceRecord;
113 158
    }
114 158
115
    /**
116
     * This function can be used to check if one of the strings in needles is
117
     * contained in the haystack.
118
     *
119
     *
120
     * Example:
121
     *
122
     * haystack: the brown fox
123
     * needles: ['hello', 'world']
124
     * result: false
125
     *
126
     * haystack: the brown fox
127
     * needles: ['is', 'fox']
128
     * result: true
129
     *
130
     * @param string $haystack
131
     * @param array $needles
132
     * @return bool
133
     */
134
    public static function containsOneOfTheStrings($haystack, array $needles)
135
    {
136
        foreach ($needles as $needle) {
137
            $position = strpos($haystack, $needle);
138
            if ($position !== false) {
139
                return true;
140
            }
141
        }
142
143
        return false;
144
    }
145
146
    /**
147
     * Returns the current language ID from the active context.
148
     * @return int
149 2
     */
150
    public static function getLanguageUid(): int
151 2
    {
152 2
        $context = GeneralUtility::makeInstance(Context::class);
153
        return (int)$context->getPropertyFromAspect('language', 'id');
154
    }
155 2
156
    /**
157
     * @return string
158
     */
159
    public static function getFrontendUserGroupsList(): string
160
    {
161
        return implode(',', self::getFrontendUserGroups());
162
    }
163
164
    /**
165
     * @return array
166
     */
167
    public static function getFrontendUserGroups(): array
168
    {
169
        $context = GeneralUtility::makeInstance(Context::class);
170
        return $context->getPropertyFromAspect('frontend.user', 'groupIds');
171
    }
172
}
173