|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sunnysideup\CollectElementalContent\Extensions; |
|
4
|
|
|
|
|
5
|
|
|
use DNADesign\Elemental\Models\ElementalArea; |
|
6
|
|
|
use Page; |
|
|
|
|
|
|
7
|
|
|
use SilverStripe\CMS\Model\SiteTreeExtension; |
|
8
|
|
|
use SilverStripe\Core\Config\Config; |
|
9
|
|
|
|
|
10
|
|
|
class SiteTreeContentWriter extends SiteTreeExtension |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var string[] |
|
14
|
|
|
*/ |
|
15
|
|
|
private const BASE_UNSEARCHABLE_FIELDS = [ |
|
16
|
|
|
'Content', |
|
17
|
|
|
'ParentID', |
|
18
|
|
|
'Parent', |
|
19
|
|
|
'Type', |
|
20
|
|
|
'Created', |
|
21
|
|
|
'LastEdited', |
|
22
|
|
|
'ID', |
|
23
|
|
|
'ClassName', |
|
24
|
|
|
'Owner', |
|
25
|
|
|
'ElementalArea', |
|
26
|
|
|
'SortBy', |
|
27
|
|
|
'FileTracking', |
|
28
|
|
|
'LinkTracking', |
|
29
|
|
|
'ExtraClass', |
|
30
|
|
|
'Sort', |
|
31
|
|
|
'Version', |
|
32
|
|
|
]; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var string[] |
|
36
|
|
|
*/ |
|
37
|
|
|
private const BASE_UNSEARCHABLE_TYPES = [ |
|
38
|
|
|
'Boolean', |
|
39
|
|
|
'Boolean(0)', |
|
40
|
|
|
'Boolean(1)', |
|
41
|
|
|
'Int', |
|
42
|
|
|
'Date', |
|
43
|
|
|
]; |
|
44
|
|
|
|
|
45
|
|
|
public function onBeforeWrite() |
|
46
|
|
|
{ |
|
47
|
|
|
$this->updateSearchContentFromElementals(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function updateSearchContentFromElementals() |
|
51
|
|
|
{ |
|
52
|
|
|
//populate search |
|
53
|
|
|
$owner = $this->getOwner(); |
|
54
|
|
|
$myContent = ''; |
|
55
|
|
|
$myContent .= $owner->updateSearchContentFromElementalsExtractData($owner); |
|
56
|
|
|
if ($owner->hasMethod('ElementalArea')) { |
|
57
|
|
|
$myElementalArea = $owner->ElementalArea(); |
|
58
|
|
|
$ids = []; |
|
59
|
|
|
if ($myElementalArea && $myElementalArea instanceof ElementalArea) { |
|
60
|
|
|
$elements = $myElementalArea->Elements(); |
|
61
|
|
|
foreach ($elements as $element) { |
|
62
|
|
|
$myContent .= $owner->updateSearchContentFromElementalsExtractData($element); |
|
63
|
|
|
$ids[$element->ID] = $element->Title; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$this->owner->Content = $myContent . '. '; |
|
68
|
|
|
|
|
69
|
|
|
//for anchor links only! |
|
70
|
|
|
foreach ($ids as $id => $title) { |
|
71
|
|
|
$this->owner->Content .= '<br /><a id="e' . $id . '">' . $title . '</a>'; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function updateSearchContentFromElementalsExtractData($object): string |
|
77
|
|
|
{ |
|
78
|
|
|
$owner = $this->getOwner(); |
|
79
|
|
|
$badTypes = $owner->getUnsearchableTypes(); |
|
80
|
|
|
$unsetFields = $owner->getUnsearchableFields(); |
|
81
|
|
|
$string = ''; |
|
82
|
|
|
foreach (['db', 'has_one', 'belongs', 'has_many', 'many_many', 'belongs_many_many'] as $relType) { |
|
83
|
|
|
$fields = Config::inst()->get($object->ClassName, $relType); |
|
84
|
|
|
if (! empty($fields)) { |
|
85
|
|
|
foreach ($fields as $name => $type) { |
|
86
|
|
|
if (! in_array($type, $badTypes, true) && ! in_array($name, $unsetFields, true)) { |
|
87
|
|
|
if (0 === stripos($type, 'Enum')) { |
|
88
|
|
|
continue; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$endValue = ''; |
|
92
|
|
|
switch ($relType) { |
|
93
|
|
|
case 'db': |
|
94
|
|
|
$endValue = $object->{$name}; |
|
95
|
|
|
$isList = false; |
|
96
|
|
|
|
|
97
|
|
|
break; |
|
98
|
|
|
case 'belongs': |
|
99
|
|
|
case 'has_one': |
|
100
|
|
|
$values = $object->{$name}(); |
|
101
|
|
|
$endValue = $values && $values->exists() ? $values->getTitle() : ''; |
|
102
|
|
|
$isList = false; |
|
103
|
|
|
|
|
104
|
|
|
break; |
|
105
|
|
|
default: |
|
106
|
|
|
$isList = true; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
if ($isList) { |
|
110
|
|
|
$listValues = []; |
|
111
|
|
|
$values = $object->{$name}(); |
|
112
|
|
|
if ($values && $values->exists() && $values->count() < 13) { |
|
113
|
|
|
foreach ($values as $item) { |
|
114
|
|
|
if ($item && $item->exists()) { |
|
115
|
|
|
$listValues[] = $item->getTitle(); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
$endValue = implode('; ', array_filter($listValues)); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
$data = trim(strip_tags((string) $endValue)); |
|
124
|
|
|
// $string .= $data ? $name . ': '.$data . '; ' : $name.' missing; '; |
|
125
|
|
|
$string .= $data ? $data . '; ' : ''; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
return $string; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
public function getUnsearchableFields(): array |
|
135
|
|
|
{ |
|
136
|
|
|
$owner = $this->getOwner(); |
|
137
|
|
|
$array = self::BASE_UNSEARCHABLE_FIELDS; |
|
138
|
|
|
if ($owner->hasMethod('getUnsearchableFieldsExtras')) { |
|
139
|
|
|
$extraArray = $owner->getUnsearchableFieldsExtras(); |
|
140
|
|
|
} else { |
|
141
|
|
|
$extraArray = Config::inst()->get(Page::class, 'unsearchable_fields_extra'); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
if (! empty($extraArray) && is_array($extraArray)) { |
|
145
|
|
|
$array = array_merge($array, $extraArray); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
return $array; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
public function getUnsearchableTypes(): array |
|
152
|
|
|
{ |
|
153
|
|
|
$owner = $this->getOwner(); |
|
154
|
|
|
$array = self::BASE_UNSEARCHABLE_TYPES; |
|
155
|
|
|
if ($owner->hasMethod('getUnsearchableTypesExtras')) { |
|
156
|
|
|
$extraArray = $owner->getUnsearchableTypesExtras(); |
|
157
|
|
|
} else { |
|
158
|
|
|
$extraArray = Config::inst()->get(Page::class, 'unsearchable_types_extra'); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
if (! empty($extraArray) && is_array($extraArray)) { |
|
162
|
|
|
$array = array_merge($array, $extraArray); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
return $array; |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths