Completed
Push — master ( a30450...a106a1 )
by
unknown
42s
created

TCAService::isEnableColumn()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 3
cts 3
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
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 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 47
    public function __construct($TCA = null)
50
    {
51 47
        $this->tca = (array) (is_null($TCA) ? $GLOBALS['TCA'] : $TCA);
52 47
    }
53
54
    /**
55
     * @return integer
56
     */
57 6
    protected function getTime()
58
    {
59 6
        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 30
    public function isEnabledRecord($table, $record)
75
    {
76
        if (
77 30
            (isset($this->tca[$table]['ctrl']['enablecolumns']['disabled']) && !empty($record[$this->tca[$table]['ctrl']['enablecolumns']['disabled']]))
78
            ||
79 26
            (isset($this->tca[$table]['ctrl']['delete']) && !empty($record[$this->tca[$table]['ctrl']['delete']]))
80
            ||
81 30
            ($table == 'pages' && !empty($record['no_search']))
82
        ) {
83 6
            return false;
84
        }
85
86 24
        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 3
    public function isEndTimeInPast($table, $record)
99
    {
100 3
        $endTimeInPast = false;
101
102 3
        if (isset($this->tca[$table]['ctrl']['enablecolumns']['endtime'])) {
103 3
            $endTimeField = $this->tca[$table]['ctrl']['enablecolumns']['endtime'];
104 3
            $endTimeInPast = $record[$endTimeField] < $this->getTime();
105
        }
106
107 3
        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 4
    public function isEnableColumn($table, $columnName)
127
    {
128
        return (
129 4
            isset($GLOBALS['TCA'][$table]['ctrl']['enablecolumns']) &&
130 4
            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 4
    public function isStartTimeInFuture($table, $record)
144
    {
145 4
        $startTimeInFuture = false;
146
147 4
        if (isset($this->tca[$table]['ctrl']['enablecolumns']['starttime'])) {
148 4
            $startTimeField = $this->tca[$table]['ctrl']['enablecolumns']['starttime'];
149 4
            $startTimeInFuture = $record[$startTimeField] > $this->getTime();
150
        }
151
152 4
        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 8
    public function isHidden($table, $record)
165
    {
166 8
        $hidden = false;
167
168 8
        if (isset($this->tca[$table]['ctrl']['enablecolumns']['disabled'])) {
169 8
            $hiddenField = $this->tca[$table]['ctrl']['enablecolumns']['disabled'];
170 8
            $hidden = (boolean)$record[$hiddenField];
171
        }
172
173 8
        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 7
    public function normalizeFrontendGroupField($table, $record)
184
    {
185 7
        if (isset($this->tca[$table]['ctrl']['enablecolumns']['fe_group'])) {
186 7
            $frontendGroupsField = $this->tca[$table]['ctrl']['enablecolumns']['fe_group'];
187
188 7
            if ($record[$frontendGroupsField] == '') {
189 1
                $record[$frontendGroupsField] = '0';
190
            }
191
        }
192
193 7
        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 10
    public function getVisibilityAffectingFieldsByTable($table)
204
    {
205 10
        if (isset($this->visibilityAffectingFields[$table])) {
206 4
            return $this->visibilityAffectingFields[$table];
207
        }
208
209
        // we always want to get the uid and pid although they do not affect visibility
210 10
        $fields = ['uid', 'pid'];
211 10
        if (isset($this->tca[$table]['ctrl']['enablecolumns'])) {
212 7
            $fields = array_merge($fields, $this->tca[$table]['ctrl']['enablecolumns']);
213
        }
214
215 10
        if (isset($this->tca[$table]['ctrl']['delete'])) {
216 7
            $fields[] = $this->tca[$table]['ctrl']['delete'];
217
        }
218
219 10
        if ($table == 'pages') {
220 4
            $fields[] = 'no_search';
221 4
            $fields[] = 'doktype';
222
        }
223
224 10
        $this->visibilityAffectingFields[$table] = implode(', ', $fields);
225
226 10
        return $this->visibilityAffectingFields[$table];
227
    }
228
}
229