1
|
|
|
<?php namespace Anomaly\Streams\Platform\Ui\Table\Component\Header\Guesser; |
2
|
|
|
|
3
|
|
|
use Anomaly\Streams\Platform\Addon\Module\Module; |
4
|
|
|
use Anomaly\Streams\Platform\Addon\Module\ModuleCollection; |
5
|
|
|
use Anomaly\Streams\Platform\Stream\Contract\StreamInterface; |
6
|
|
|
use Anomaly\Streams\Platform\Support\Str; |
7
|
|
|
use Anomaly\Streams\Platform\Ui\Table\TableBuilder; |
8
|
|
|
use Illuminate\Contracts\Config\Repository; |
9
|
|
|
use Illuminate\Translation\Translator; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class HeadingsGuesser |
13
|
|
|
* |
14
|
|
|
* @link http://anomaly.is/streams-platform |
15
|
|
|
* @author AnomalyLabs, Inc. <[email protected]> |
16
|
|
|
* @author Ryan Thompson <[email protected]> |
17
|
|
|
* @package Anomaly\Streams\Platform\Ui\Table\Component\Button\Guesser |
18
|
|
|
*/ |
19
|
|
|
class HeadingsGuesser |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The string utility. |
24
|
|
|
* |
25
|
|
|
* @var Str |
26
|
|
|
*/ |
27
|
|
|
protected $string; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The config repository. |
31
|
|
|
* |
32
|
|
|
* @var Repository |
33
|
|
|
*/ |
34
|
|
|
protected $config; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The module collection. |
38
|
|
|
* |
39
|
|
|
* @var ModuleCollection |
40
|
|
|
*/ |
41
|
|
|
protected $modules; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* The translator utility. |
45
|
|
|
* |
46
|
|
|
* @var Translator |
47
|
|
|
*/ |
48
|
|
|
protected $translator; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Create a new HeadingsGuesser instance. |
52
|
|
|
* |
53
|
|
|
* @param Str $string |
54
|
|
|
* @param Repository $config |
55
|
|
|
* @param ModuleCollection $modules |
56
|
|
|
* @param Translator $translator |
57
|
|
|
*/ |
58
|
|
|
public function __construct(Str $string, Repository $config, ModuleCollection $modules, Translator $translator) |
59
|
|
|
{ |
60
|
|
|
$this->string = $string; |
61
|
|
|
$this->config = $config; |
62
|
|
|
$this->modules = $modules; |
63
|
|
|
$this->translator = $translator; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Guess the field for a column. |
68
|
|
|
* |
69
|
|
|
* @param TableBuilder $builder |
70
|
|
|
*/ |
71
|
|
|
public function guess(TableBuilder $builder) |
72
|
|
|
{ |
73
|
|
|
$columns = $builder->getColumns(); |
74
|
|
|
$stream = $builder->getTableStream(); |
75
|
|
|
|
76
|
|
|
$module = $this->modules->active(); |
77
|
|
|
|
78
|
|
|
foreach ($columns as &$column) { |
|
|
|
|
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* If the heading is already set then |
82
|
|
|
* we don't have anything to do. |
83
|
|
|
*/ |
84
|
|
|
if (isset($column['heading'])) { |
85
|
|
|
continue; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* If the heading is false, then no |
90
|
|
|
* header is desired at all. |
91
|
|
|
*/ |
92
|
|
|
if (isset($column['heading']) && $column['heading'] === false) { |
93
|
|
|
continue; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* No stream means we can't |
98
|
|
|
* really do much here. |
99
|
|
|
*/ |
100
|
|
|
if (!$stream instanceof StreamInterface) { |
101
|
|
|
continue; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* No module means we can't |
106
|
|
|
* really do much here. |
107
|
|
|
*/ |
108
|
|
|
if (!$module instanceof Module) { |
109
|
|
|
continue; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if (!isset($column['field']) && is_string($column['value'])) { |
113
|
|
|
$column['field'] = $column['value']; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* If the heading matches a field |
118
|
|
|
* with dot format then reduce it. |
119
|
|
|
*/ |
120
|
|
|
if (isset($column['field']) && preg_match("/^entry.([a-zA-Z\\_]+)/", $column['field'], $match)) { |
121
|
|
|
$column['field'] = $match[1]; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Detect some built in columns. |
126
|
|
|
*/ |
127
|
|
|
if (in_array($column['field'], ['id', 'created_at', 'created_by', 'updated_at', 'updated_by'])) { |
128
|
|
|
|
129
|
|
|
$column['heading'] = 'streams::entry.' . $column['field']; |
130
|
|
|
|
131
|
|
|
continue; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Detect entry title. |
136
|
|
|
*/ |
137
|
|
|
if (in_array($column['field'], ['view_link', 'edit_link']) && $field = $stream->getTitleField()) { |
138
|
|
|
|
139
|
|
|
$column['heading'] = $field->getName(); |
140
|
|
|
|
141
|
|
|
continue; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
$field = $stream->getField($column['value']); |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Detect the title column. |
148
|
|
|
*/ |
149
|
|
|
$title = $stream->getTitleField(); |
150
|
|
|
|
151
|
|
|
if ( |
152
|
|
|
$title && |
153
|
|
|
!$field && |
154
|
|
|
$column['field'] == 'title' && |
155
|
|
|
$this->translator->has($heading = $title->getName()) |
156
|
|
|
) { |
157
|
|
|
$column['heading'] = $heading; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Use the name from the field. |
162
|
|
|
*/ |
163
|
|
|
if ($field && $heading = $field->getName()) { |
164
|
|
|
$column['heading'] = $heading; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* If no field look for |
169
|
|
|
* a name anyways. |
170
|
|
|
*/ |
171
|
|
|
if (!$field && $this->translator->has( |
172
|
|
|
$heading = $module->getNamespace('field.' . $column['field'] . '.name') |
173
|
|
|
) |
174
|
|
|
) { |
175
|
|
|
$column['heading'] = $heading; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* If no translatable heading yet and |
180
|
|
|
* the heading matches the value (default) |
181
|
|
|
* then humanize the heading value. |
182
|
|
|
*/ |
183
|
|
View Code Duplication |
if (!isset($column['heading']) && $this->config->get('streams::locales.lazy')) { |
|
|
|
|
184
|
|
|
$column['heading'] = $this->string->humanize($column['field']); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* If we have a translatable heading and |
189
|
|
|
* the heading does not have a translation |
190
|
|
|
* then humanize the heading value. |
191
|
|
|
*/ |
192
|
|
View Code Duplication |
if ( |
|
|
|
|
193
|
|
|
isset($column['heading']) && |
194
|
|
|
str_is('*.*.*::*', $column['heading']) && |
195
|
|
|
!$this->translator->has($column['heading']) && |
196
|
|
|
$this->config->get('streams::locales.lazy') |
197
|
|
|
) { |
198
|
|
|
$column['heading'] = $this->string->humanize($column['field']); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Last resort. |
203
|
|
|
*/ |
204
|
|
|
if (!isset($column['heading'])) { |
205
|
|
|
$column['heading'] = $module->getNamespace('field.' . $column['field'] . '.name'); |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
$builder->setColumns($columns); |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.