Passed
Push — master ( f55a40...3301f5 )
by Timo
23:56
created

getTranslationOriginalUidIfTranslated()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 3
crap 2
1
<?php
2
3
namespace ApacheSolrForTypo3\Solr\System\TCA;
4
5
/***************************************************************
6
 *  Copyright notice
7
 *
8
 *  (c) 2010-2017 Timo Hund <[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 3 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
/**
29
 * Class to encapsulate TCA specific logic
30
 *
31
 * @author Timo Hund <[email protected]>
32
 */
33
class TCAService
34
{
35
    /**
36
     * @var array
37
     */
38
    protected $tca = [];
39
40
    /**
41
     * @var array
42
     */
43
    protected $visibilityAffectingFields = [];
44
45
    /**
46
     * TCAService constructor.
47
     * @param null $TCA
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $TCA is correct as it would always require null to be passed?
Loading history...
48
     */
49 61
    public function __construct($TCA = null)
50
    {
51 61
        $this->tca = (array)($TCA ?? $GLOBALS['TCA']);
52 61
    }
53
54
    /**
55
     * @return integer
56
     */
57 7
    protected function getTime()
58
    {
59 7
        return isset($GLOBALS['EXEC_TIME']) ? $GLOBALS['EXEC_TIME'] : time();
60
    }
61
62
    /**
63
     * Checks if a record is "enabled"
64
     *
65
     * A record is considered "enabled" if
66
     *  - it is not hidden
67
     *  - it is not deleted
68
     *  - as a page it is not set to be excluded from search
69
     *
70
     * @param string $table The record's table name
71
     * @param array $record The record to check
72
     * @return bool TRUE if the record is enabled, FALSE otherwise
73
     */
74 37
    public function isEnabledRecord($table, $record)
75
    {
76
        if (
77 37
            (empty($record))
78
            ||
79 36
            (isset($this->tca[$table]['ctrl']['enablecolumns']['disabled']) && !empty($record[$this->tca[$table]['ctrl']['enablecolumns']['disabled']]))
80
            ||
81 32
            (isset($this->tca[$table]['ctrl']['delete']) && !empty($record[$this->tca[$table]['ctrl']['delete']]))
82
            ||
83 37
            ($table === 'pages' && !empty($record['no_search']))
84
        ) {
85 7
            return false;
86
        }
87
88 30
        return true;
89
    }
90
91
    /**
92
     * Checks whether a end time field exists for the record's table and if so
93
     * determines if a time is set and whether that time is in the past,
94
     * making the record invisible on the website.
95
     *
96
     * @param string $table The table name.
97
     * @param array $record An array with record fields that may affect visibility.
98
     * @return bool True if the record's end time is in the past, FALSE otherwise.
99
     */
100 5
    public function isEndTimeInPast($table, $record)
101
    {
102 5
        $endTimeInPast = false;
103
104 5
        if (isset($this->tca[$table]['ctrl']['enablecolumns']['endtime'])) {
105 5
            $endTimeField = $this->tca[$table]['ctrl']['enablecolumns']['endtime'];
106 5
            if ($record[$endTimeField] > 0) {
107 3
                $endTimeInPast = $record[$endTimeField] < $this->getTime();
108
            }
109
        }
110
111 5
        return $endTimeInPast;
112
    }
113
114
    /**
115
     * This method can be used to check if there is a configured key in
116
     *
117
     * $GLOBALS['TCA']['mytable']['ctrl']['enablecolumns']
118
     *
119
     * Example:
120
     *
121
     * $GLOBALS['TCA']['mytable']]['ctrl']['enablecolumns']['fe_group'] = 'mygroupfield'
122
     *
123
     * ->isEnableColumn('mytable', 'fe_group') will return true, because 'mygroupfield' is
124
     * configured as column.
125
     *
126
     * @params string $table
127
     * @param string $columnName
128
     * @return bool
129
     */
130 5
    public function isEnableColumn($table, $columnName)
131
    {
132
        return (
133 5
            isset($GLOBALS['TCA'][$table]['ctrl']['enablecolumns']) &&
134 5
            array_key_exists($columnName, $GLOBALS['TCA'][$table]['ctrl']['enablecolumns'])
135
        );
136
    }
137
138
    /**
139
     * Checks whether a start time field exists for the record's table and if so
140
     * determines if a time is set and whether that time is in the future,
141
     * making the record invisible on the website.
142
     *
143
     * @param string $table The table name.
144
     * @param array $record An array with record fields that may affect visibility.
145
     * @return bool True if the record's start time is in the future, FALSE otherwise.
146
     */
147 5
    public function isStartTimeInFuture($table, $record)
148
    {
149 5
        $startTimeInFuture = false;
150
151 5
        if (isset($this->tca[$table]['ctrl']['enablecolumns']['starttime'])) {
152 5
            $startTimeField = $this->tca[$table]['ctrl']['enablecolumns']['starttime'];
153 5
            $startTimeInFuture = $record[$startTimeField] > $this->getTime();
154
        }
155
156 5
        return $startTimeInFuture;
157
    }
158
159
160
    /**
161
     * Checks whether a hidden field exists for the current table and if so
162
     * determines whether it is set on the current record.
163
     *
164
     * @param string $table The table name.
165
     * @param array $record An array with record fields that may affect visibility.
166
     * @return bool True if the record is hidden, FALSE otherwise.
167
     */
168 9
    public function isHidden($table, $record)
169
    {
170 9
        $hidden = false;
171
172 9
        if (isset($this->tca[$table]['ctrl']['enablecolumns']['disabled'])) {
173 9
            $hiddenField = $this->tca[$table]['ctrl']['enablecolumns']['disabled'];
174 9
            $hidden = (boolean)$record[$hiddenField];
175
        }
176
177 9
        return $hidden;
178
    }
179
180
    /**
181
     * Makes sure that "empty" frontend group fields are always the same value.
182
     *
183
     * @param string $table The record's table name.
184
     * @param array $record the record array.
185
     * @return array The cleaned record
186
     */
187 8
    public function normalizeFrontendGroupField($table, $record)
188
    {
189 8
        if (isset($this->tca[$table]['ctrl']['enablecolumns']['fe_group'])) {
190 8
            $frontendGroupsField = $this->tca[$table]['ctrl']['enablecolumns']['fe_group'];
191
192 8
            if ($record[$frontendGroupsField] == '') {
193 1
                $record[$frontendGroupsField] = '0';
194
            }
195
        }
196
197 8
        return $record;
198
    }
199
200
    /**
201
     * @param string $table
202
     * @param array $record
203
     * @return mixed
204
     */
205 37
    public function getTranslationOriginalUid($table, array $record)
206
    {
207 37
        return $record[$this->tca[$table]['ctrl']['transOrigPointerField']];
208
    }
209
210
    /**
211
     * Retrieves the uid that as marked as original if the record is a translation if not it returns the
212
     * originalUid.
213
     *
214
     * @param $table
215
     * @param array $record
216
     * @param $originalUid
217
     * @return integer
218
     */
219 33
    public function getTranslationOriginalUidIfTranslated($table, array $record, $originalUid)
220
    {
221 33
        if (!$this->isLocalizedRecord($table, $record)) {
222 30
            return $originalUid;
223
        }
224
225 4
        return $this->getTranslationOriginalUid($table, $record);
226
    }
227
228
    /**
229
     * Checks whether a record is a localization overlay.
230
     *
231
     * @param string $tableName The record's table name
232
     * @param array $record The record to check
233
     * @return bool TRUE if the record is a language overlay, FALSE otherwise
234
     */
235 34
    public function isLocalizedRecord($tableName, array $record)
236
    {
237 34
        $translationUid = $this->getTranslationOriginalUid($tableName, $record);
238 34
        if (is_null($translationUid)) {
239 31
            return false;
240
        }
241
242 5
        $hasTranslationReference = $translationUid > 0;
243 5
        if (!$hasTranslationReference) {
244 2
            return false;
245
        }
246
247 5
        return true;
248
    }
249
250
    /**
251
     * Compiles a list of visibility affecting fields of a table so that it can
252
     * be used in SQL queries.
253
     *
254
     * @param string $table Table name to retrieve visibility affecting fields for
255
     * @return string Comma separated list of field names that affect the visibility of a record on the website
256
     */
257 11
    public function getVisibilityAffectingFieldsByTable($table)
258
    {
259 11
        if (isset($this->visibilityAffectingFields[$table])) {
260 5
            return $this->visibilityAffectingFields[$table];
261
        }
262
263
        // we always want to get the uid and pid although they do not affect visibility
264 11
        $fields = ['uid', 'pid'];
265 11
        if (isset($this->tca[$table]['ctrl']['enablecolumns'])) {
266 8
            $fields = array_merge($fields, $this->tca[$table]['ctrl']['enablecolumns']);
267
        }
268
269 11
        if (isset($this->tca[$table]['ctrl']['delete'])) {
270 8
            $fields[] = $this->tca[$table]['ctrl']['delete'];
271
        }
272
273 11
        if ($table === 'pages') {
274 4
            $fields[] = 'no_search';
275 4
            $fields[] = 'doktype';
276
        }
277
278 11
        $this->visibilityAffectingFields[$table] = implode(', ', $fields);
279
280 11
        return $this->visibilityAffectingFields[$table];
281
    }
282
}
283