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