1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sunnysideup\CMSNiceties\Extensions; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Admin\ModelAdmin; |
6
|
|
|
use SilverStripe\Control\Director; |
7
|
|
|
use SilverStripe\Core\Config\Config; |
8
|
|
|
use SilverStripe\Core\Extension; |
9
|
|
|
use SilverStripe\Core\Injector\Injector; |
10
|
|
|
use SilverStripe\Forms\GridField\GridField; |
11
|
|
|
use SilverStripe\Lumberjack\Forms\GridFieldSiteTreeState; |
|
|
|
|
12
|
|
|
use SilverStripe\Security\Permission; |
13
|
|
|
use SilverStripe\Versioned\Versioned; |
14
|
|
|
use UndefinedOffset\SortableGridField\Forms\GridFieldSortableRows; |
|
|
|
|
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class \Sunnysideup\CMSNiceties\Extensions\ModelAdminExtension. |
18
|
|
|
* |
19
|
|
|
* @property ModelAdmin|ModelAdminExtension $owner |
20
|
|
|
*/ |
21
|
|
|
class ModelAdminExtension extends Extension |
22
|
|
|
{ |
23
|
|
|
private static $excluded_models_from_ssu_extension = []; |
24
|
|
|
|
25
|
|
|
private static $excluded_modeladmins_from_ssu_extension = []; |
26
|
|
|
|
27
|
|
|
private static $sort_fields_from_ssu_extension = ['SortNumber', 'Sort', 'SortOrder']; |
28
|
|
|
|
29
|
|
|
private static array $assume_to_allow_all = [ |
30
|
|
|
'ADMIN', |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
private static int $max_records_to_check_for_can_view = 10000; |
34
|
|
|
|
35
|
|
|
public function updateEditForm($form) |
36
|
|
|
{ |
37
|
|
|
$owner = $this->getOwner(); |
38
|
|
|
if ($this->IsIncludedInExtension()) { |
39
|
|
|
$obj = Injector::inst()->get($owner->modelClass); |
40
|
|
|
if ($obj) { |
41
|
|
|
$sortFields = $owner->config()->get('sort_fields_from_ssu_extension'); |
42
|
|
|
$gridField = $form->Fields()->dataFieldByName($this->sanitiseClassNameHelper($owner->modelClass)); |
43
|
|
|
if ($gridField instanceof GridField) { |
44
|
|
|
$config = $gridField->getConfig(); |
45
|
|
|
$dbFields = $obj->config()->get('db'); |
46
|
|
|
foreach ($sortFields as $sortField) { |
47
|
|
|
if (isset($dbFields[$sortField])) { |
48
|
|
|
// This is just a precaution to ensure we got a GridField from dataFieldByName() which you should have |
49
|
|
|
if (! $config->getComponentByType(GridFieldSortableRows::class)) { |
50
|
|
|
$obj = $owner->modelClass::singleton(); |
51
|
|
|
if ($obj->hasExtension(Versioned::class)) { |
52
|
|
|
$sorter = (new GridFieldSortableRows($sortField)) |
53
|
|
|
->setUpdateVersionedStage(Versioned::LIVE); |
54
|
|
|
} else { |
55
|
|
|
$sorter = new GridFieldSortableRows($sortField); |
56
|
|
|
} |
57
|
|
|
$config->addComponent($sorter); |
58
|
|
|
} |
59
|
|
|
break; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if ($obj->hasExtension(Versioned::class) && $obj->hasStages() && class_exists(GridFieldSiteTreeState::class)) { |
64
|
|
|
$config->addComponent(new GridFieldSiteTreeState()); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
return $form; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Sanitise a model class' name for inclusion in a link |
77
|
|
|
* |
78
|
|
|
* @param string $class |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
|
|
protected function sanitiseClassNameHelper(string $class): string |
82
|
|
|
{ |
83
|
|
|
return str_replace('\\', '-', $class); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
protected function updateList(&$list) |
87
|
|
|
{ |
88
|
|
|
$owner = $this->getOwner(); |
89
|
|
|
if ($this->IsIncludedInExtension()) { |
90
|
|
|
if (!Permission::check($owner->config()->get('assume_to_allow_all'))) { |
91
|
|
|
$count = $list->count(); |
92
|
|
|
$limit = $owner->config()->get('max_records_to_check_for_can_view'); |
93
|
|
|
$ids = [0 => 0]; |
94
|
|
|
if ($count > $limit) { |
95
|
|
|
$list = $list->limit($limit); |
96
|
|
|
} |
97
|
|
|
foreach ($list as $record) { |
98
|
|
|
if ($record->canView()) { |
99
|
|
|
$ids[] = $record->ID; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
$list = $list->filter(['ID' => $ids]); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
protected function IsIncludedInExtension(): bool |
108
|
|
|
{ |
109
|
|
|
$owner = $this->getOwner(); |
110
|
|
|
$excludedModelAdmins = $owner->config()->get('excluded_modeladmins_from_ssu_extension'); |
111
|
|
|
if (! in_array(get_class($owner), $excludedModelAdmins, true)) { |
112
|
|
|
$excludedModels = $owner->config()->get('excluded_models_from_ssu_extension'); |
113
|
|
|
if (! in_array($owner->modelClass, $excludedModels, true)) { |
114
|
|
|
return true; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
return false; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
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