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

TCAService::normalizeFrontendGroupField()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 2
crap 3
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 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
/**
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
48
     */
49 32
    public function __construct($TCA = null)
50
    {
51 32
        $this->tca = (array) (is_null($TCA) ? $GLOBALS['TCA'] : $TCA);
52 32
    }
53
54
    /**
55
     * @return integer
56
     */
57 4
    protected function getTime()
58
    {
59 4
        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 17
    public function isEnabledRecord($table, $record)
75
    {
76
        if (
77 17
            (isset($this->tca[$table]['ctrl']['enablecolumns']['disabled']) && !empty($record[$this->tca[$table]['ctrl']['enablecolumns']['disabled']]))
78
            ||
79 14
            (isset($this->tca[$table]['ctrl']['delete']) && !empty($record[$this->tca[$table]['ctrl']['delete']]))
80 14
            ||
81 13
            ($table == 'pages' && !empty($record['no_search']))
82 17
        ) {
83 5
            return false;
84
        }
85
86 12
        return true;
87
    }
88
89
    /**
90
     * Checks whether a end time field exists for the record's table and if so
91
     * determines if a time is set and whether that time is in the past,
92
     * making the record invisible on the website.
93
     *
94
     * @param string $table The table name.
95
     * @param array $record An array with record fields that may affect visibility.
96
     * @return bool True if the record's end time is in the past, FALSE otherwise.
97
     */
98 2
    public function isEndTimeInPast($table, $record)
99
    {
100 2
        $endTimeInPast = false;
101
102 2
        if (isset($this->tca[$table]['ctrl']['enablecolumns']['endtime'])) {
103 2
            $endTimeField = $this->tca[$table]['ctrl']['enablecolumns']['endtime'];
104 2
            $endTimeInPast = $record[$endTimeField] < $this->getTime();
105 2
        }
106
107 2
        return $endTimeInPast;
108
    }
109
110
    /**
111
     * This method can be used to check if there is a configured key in
112
     *
113
     * $GLOBALS['TCA']['mytable']['ctrl']['enablecolumns']
114
     *
115
     * Example:
116
     *
117
     * $GLOBALS['TCA']['mytable']]['ctrl']['enablecolumns']['fe_group'] = 'mygroupfield'
118
     *
119
     * ->isEnableColumn('mytable', 'fe_group') will return true, because 'mygroupfield' is
120
     * configured as column.
121
     *
122
     * @params string $table
123
     * @param string $columnName
124
     * @return bool
125
     */
126
    public function isEnableColumn($table, $columnName)
127
    {
128
        return (
129
            isset($GLOBALS['TCA'][$table]['ctrl']['enablecolumns']) &&
130
            array_key_exists($columnName, $GLOBALS['TCA'][$table]['ctrl']['enablecolumns'])
131
        );
132
    }
133
134
    /**
135
     * Checks whether a start time field exists for the record's table and if so
136
     * determines if a time is set and whether that time is in the future,
137
     * making the record invisible on the website.
138
     *
139
     * @param string $table The table name.
140
     * @param array $record An array with record fields that may affect visibility.
141
     * @return bool True if the record's start time is in the future, FALSE otherwise.
142
     */
143 2
    public function isStartTimeInFuture($table, $record)
144
    {
145 2
        $startTimeInFuture = false;
146
147 2
        if (isset($this->tca[$table]['ctrl']['enablecolumns']['starttime'])) {
148 2
            $startTimeField = $this->tca[$table]['ctrl']['enablecolumns']['starttime'];
149 2
            $startTimeInFuture = $record[$startTimeField] > $this->getTime();
150 2
        }
151
152 2
        return $startTimeInFuture;
153
    }
154
155
156
    /**
157
     * Checks whether a hidden field exists for the current table and if so
158
     * determines whether it is set on the current record.
159
     *
160
     * @param string $table The table name.
161
     * @param array $record An array with record fields that may affect visibility.
162
     * @return bool True if the record is hidden, FALSE otherwise.
163
     */
164 4
    public function isHidden($table, $record)
165
    {
166 4
        $hidden = false;
167
168 4
        if (isset($this->tca[$table]['ctrl']['enablecolumns']['disabled'])) {
169 4
            $hiddenField = $this->tca[$table]['ctrl']['enablecolumns']['disabled'];
170 4
            $hidden = (boolean)$record[$hiddenField];
171 4
        }
172
173 4
        return $hidden;
174
    }
175
176
    /**
177
     * Makes sure that "empty" frontend group fields are always the same value.
178
     *
179
     * @param string $table The record's table name.
180
     * @param array $record the record array.
181
     * @return array The cleaned record
182
     */
183 3
    public function normalizeFrontendGroupField($table, $record)
184
    {
185 3
        if (isset($this->tca[$table]['ctrl']['enablecolumns']['fe_group'])) {
186 3
            $frontendGroupsField = $this->tca[$table]['ctrl']['enablecolumns']['fe_group'];
187
188 3
            if ($record[$frontendGroupsField] == '') {
189 1
                $record[$frontendGroupsField] = '0';
190 1
            }
191 3
        }
192
193 3
        return $record;
194
    }
195
196
    /**
197
     * Compiles a list of visibility affecting fields of a table so that it can
198
     * be used in SQL queries.
199
     *
200
     * @param string $table Table name to retrieve visibility affecting fields for
201
     * @return string Comma separated list of field names that affect the visibility of a record on the website
202
     */
203 6
    public function getVisibilityAffectingFieldsByTable($table)
204
    {
205 6
        if (isset($this->visibilityAffectingFields[$table])) {
206
            return $this->visibilityAffectingFields[$table];
207
        }
208
209
        // we always want to get the uid and pid although they do not affect visibility
210 6
        $fields = ['uid', 'pid'];
211 6
        if (isset($this->tca[$table]['ctrl']['enablecolumns'])) {
212 3
            $fields = array_merge($fields, $this->tca[$table]['ctrl']['enablecolumns']);
213 3
        }
214
215 6
        if (isset($this->tca[$table]['ctrl']['delete'])) {
216 3
            $fields[] = $this->tca[$table]['ctrl']['delete'];
217 3
        }
218
219 6
        if ($table == 'pages') {
220 3
            $fields[] = 'no_search';
221 3
            $fields[] = 'doktype';
222 3
        }
223
224 6
        $this->visibilityAffectingFields[$table] = implode(', ', $fields);
225
226 6
        return $this->visibilityAffectingFields[$table];
227
    }
228
}
229