1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sunnysideup\CMSNiceties\Traits; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Forms\FieldList; |
6
|
|
|
use SilverStripe\Forms\Tab; |
7
|
|
|
use SilverStripe\Versioned\Versioned; |
8
|
|
|
|
9
|
|
|
trait CMSNicetiesTraitForTabs |
10
|
|
|
{ |
11
|
|
|
public function addSeparator(FieldList $fields, string $name, ?string $after = 'Main') |
12
|
|
|
{ |
13
|
|
|
if (false !== $after) { |
|
|
|
|
14
|
|
|
$tab = Tab::create($name, '|'); |
15
|
|
|
$fields->insertAfter($after, $tab); |
16
|
|
|
} else { |
17
|
|
|
$fields->addFieldsToTab( |
18
|
|
|
'Root.' . $name, |
19
|
|
|
[] |
20
|
|
|
); |
21
|
|
|
$fields->fieldByName('Root.' . $name)->setTitle('|'); |
22
|
|
|
} |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function reorderTabs(FieldList $fields, array $tabOrder): FieldList |
26
|
|
|
{ |
27
|
|
|
$tabs = []; |
28
|
|
|
foreach ($tabOrder as $tabName => $title) { |
29
|
|
|
// non-associative array.. |
30
|
|
|
if ((int) $tabName === $tabName) { |
31
|
|
|
$tabName = $title; |
32
|
|
|
$items = preg_split('#(?=[A-Z])#', $tabName); |
33
|
|
|
$title = is_array($items) ? trim(implode(' ', $items)) : $tabName; |
|
|
|
|
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$tabNamePlus = $tabName . 'Tab'; |
37
|
|
|
|
38
|
|
|
// fixd existing existing tab |
39
|
|
|
$tab = $fields->fieldByName('Root.' . $tabName); |
|
|
|
|
40
|
|
|
if (! $tab) { |
41
|
|
|
$tab = $fields->fieldByName('Root.' . $tabNamePlus); |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
if (! $tab) { |
45
|
|
|
$tab = new Tab($tabNamePlus, $tabName); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$fields->removeByName(['Root.' . $tabName]); |
49
|
|
|
$fields->removeByName(['Root.' . $tabNamePlus]); |
50
|
|
|
$fields->removeFieldFromTab('Root', $tabName); |
51
|
|
|
$fields->removeFieldFromTab('Root', $tabNamePlus); |
52
|
|
|
$fields->removeFieldsFromTab('Root', [$tabName]); |
53
|
|
|
$fields->removeFieldsFromTab('Root', [$tabNamePlus]); |
54
|
|
|
$tab->setTitle($tabName); |
55
|
|
|
$tab->setName($tabNamePlus); |
56
|
|
|
$tabs[] = $tab; |
57
|
|
|
// $fields->addFieldsToTab('Root', $tab); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// $tabs = array_reverse($tabs); |
61
|
|
|
foreach ($tabs as $tab) { |
62
|
|
|
$fields->addFieldToTab('Root', $tab); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $fields; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function addTab(FieldList $fields, string $name, ?string $after = 'Main', ?string $title = '') |
69
|
|
|
{ |
70
|
|
|
// add spaces between capitals |
71
|
|
|
if (! $title) { |
72
|
|
|
$items = preg_split('#(?=[A-Z])#', $name); |
73
|
|
|
$title = is_array($items) ? trim(implode(' ', $items)) : $name; |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if (null !== $after) { |
77
|
|
|
if ($this->hasExtension(Versioned::class) && $this->isArchived()) { |
|
|
|
|
78
|
|
|
// do nothing |
79
|
|
|
} else { |
80
|
|
|
$tab = $fields->fieldByName('Root.' . $name); |
|
|
|
|
81
|
|
|
$fields->removeFieldFromTab( |
82
|
|
|
'Root', |
83
|
|
|
$name |
84
|
|
|
); |
85
|
|
|
if (! $tab) { |
|
|
|
|
86
|
|
|
$tab = Tab::create($name, $title); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$fields->insertAfter($after, $tab); |
90
|
|
|
} |
91
|
|
|
} else { |
92
|
|
|
$fields->addFieldsToTab( |
93
|
|
|
'Root.' . $name, |
94
|
|
|
[] |
95
|
|
|
); |
96
|
|
|
$fields->fieldByName('Root.' . $name)->setTitle($title); |
|
|
|
|
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|