|
1
|
|
|
<?php |
|
2
|
|
|
namespace Fab\Vidi\Facet; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* This file is part of the TYPO3 CMS project. |
|
6
|
|
|
* |
|
7
|
|
|
* It is free software; you can redistribute it and/or modify it under |
|
8
|
|
|
* the terms of the GNU General Public License, either version 2 |
|
9
|
|
|
* of the License, or any later version. |
|
10
|
|
|
* |
|
11
|
|
|
* For the full copyright and license information, please read the |
|
12
|
|
|
* LICENSE.txt file that was distributed with this source code. |
|
13
|
|
|
* |
|
14
|
|
|
* The TYPO3 project - inspiring people to share! |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
use Fab\Vidi\Persistence\Matcher; |
|
18
|
|
|
use Fab\Vidi\Tca\Tca; |
|
19
|
|
|
use TYPO3\CMS\Extbase\Utility\LocalizationUtility; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class for configuring a custom Facet item. |
|
23
|
|
|
*/ |
|
24
|
|
|
class StandardFacet implements FacetInterface |
|
25
|
|
|
{ |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $name; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $label; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var array |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $suggestions = array(); |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var string |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $dataType; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var bool |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $canModifyMatcher = false; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var bool |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $forceKeyValueObject = false; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Constructor of a Generic Facet in Vidi. |
|
59
|
|
|
* |
|
60
|
|
|
* @param string $name |
|
61
|
|
|
* @param string $label |
|
62
|
|
|
* @param array $suggestions |
|
63
|
|
|
* @param bool $forceKeyValueObject |
|
64
|
|
|
*/ |
|
65
|
|
|
public function __construct($name, $label = '', array $suggestions = array(), $forceKeyValueObject = false) |
|
66
|
|
|
{ |
|
67
|
|
|
$this->name = $name; |
|
68
|
|
|
if (empty($label)) { |
|
69
|
|
|
$label = $this->name; |
|
70
|
|
|
} |
|
71
|
|
|
$this->label = $label; |
|
72
|
|
|
$this->suggestions = $suggestions; |
|
73
|
|
|
$this->forceKeyValueObject = $forceKeyValueObject; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @return string |
|
78
|
|
|
*/ |
|
79
|
|
|
public function getName() |
|
80
|
|
|
{ |
|
81
|
|
|
return $this->name; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @return string |
|
86
|
|
|
*/ |
|
87
|
|
|
public function getLabel() |
|
88
|
|
|
{ |
|
89
|
|
|
if ($this->label === $this->name) { |
|
90
|
|
|
$label = Tca::table($this->dataType)->field($this->getName())->getLabel(); |
|
91
|
|
|
} else { |
|
92
|
|
|
$label = LocalizationUtility::translate($this->label, ''); |
|
93
|
|
|
if (empty($label)) { |
|
94
|
|
|
$label = $this->label; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return $label; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @return array |
|
103
|
|
|
*/ |
|
104
|
|
|
public function getSuggestions() |
|
105
|
|
|
{ |
|
106
|
|
|
|
|
107
|
|
|
$values = array(); |
|
108
|
|
|
foreach ($this->suggestions as $key => $label) { |
|
109
|
|
|
$localizedLabel = $this->getLanguageService()->sL($label); |
|
110
|
|
|
if (!empty($localizedLabel)) { |
|
111
|
|
|
$label = $localizedLabel; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
if ($this->forceKeyValueObject) { |
|
115
|
|
|
$values[] = [$key => $label]; |
|
116
|
|
|
} else { |
|
117
|
|
|
|
|
118
|
|
|
// Hack to have object correctly json encoded. |
|
119
|
|
|
$values[$key] = $label; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
return $values; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @return \TYPO3\CMS\Lang\LanguageService |
|
128
|
|
|
*/ |
|
129
|
|
|
protected function getLanguageService() |
|
|
|
|
|
|
130
|
|
|
{ |
|
131
|
|
|
return $GLOBALS['LANG']; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @return bool |
|
136
|
|
|
*/ |
|
137
|
|
|
public function hasSuggestions() |
|
138
|
|
|
{ |
|
139
|
|
|
return !empty($this->suggestions); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @param string $dataType |
|
144
|
|
|
* @return $this |
|
145
|
|
|
*/ |
|
146
|
|
|
public function setDataType($dataType) |
|
147
|
|
|
{ |
|
148
|
|
|
$this->dataType = $dataType; |
|
149
|
|
|
return $this; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @return bool |
|
154
|
|
|
*/ |
|
155
|
|
|
public function canModifyMatcher() |
|
156
|
|
|
{ |
|
157
|
|
|
return $this->canModifyMatcher; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @param Matcher $matcher |
|
162
|
|
|
* @param $value |
|
163
|
|
|
* @return Matcher |
|
164
|
|
|
*/ |
|
165
|
|
|
public function modifyMatcher(Matcher $matcher, $value) |
|
166
|
|
|
{ |
|
167
|
|
|
return $matcher; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Magic method implementation for retrieving state. |
|
172
|
|
|
* |
|
173
|
|
|
* @param array $states |
|
174
|
|
|
* @return StandardFacet |
|
175
|
|
|
*/ |
|
176
|
|
|
static public function __set_state($states) |
|
|
|
|
|
|
177
|
|
|
{ |
|
178
|
|
|
return new StandardFacet($states['name'], $states['label'], $states['suggestions']); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
} |
|
182
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: