|
23 | 23 | use SilverStripe\Forms\ListboxField; |
24 | 24 | use SilverStripe\Forms\LiteralField; |
25 | 25 | use SilverStripe\Forms\RequiredFields; |
| 26 | +use SilverStripe\Forms\SearchableDropdownField; |
26 | 27 | use SilverStripe\Forms\Tab; |
27 | 28 | use SilverStripe\Forms\TabSet; |
28 | 29 | use SilverStripe\Forms\TextareaField; |
29 | 30 | use SilverStripe\Forms\TextField; |
| 31 | +use SilverStripe\Forms\TreeDropdownField; |
30 | 32 | use SilverStripe\ORM\ArrayList; |
31 | 33 | use SilverStripe\ORM\DataObject; |
32 | 34 | use SilverStripe\ORM\DataQuery; |
| 35 | +use SilverStripe\ORM\FieldType\DBForeignKey; |
33 | 36 | use SilverStripe\ORM\HasManyList; |
34 | 37 | use SilverStripe\ORM\Hierarchy\Hierarchy; |
35 | 38 | use SilverStripe\ORM\ManyManyList; |
@@ -83,6 +86,11 @@ class Group extends DataObject |
83 | 86 | Hierarchy::class, |
84 | 87 | ]; |
85 | 88 |
|
| 89 | + private static $searchable_fields = [ |
| 90 | + 'Title', |
| 91 | + 'Description', |
| 92 | + ]; |
| 93 | + |
86 | 94 | private static $table_name = "Group"; |
87 | 95 |
|
88 | 96 | private static $indexes = [ |
@@ -122,18 +130,28 @@ private function getDecodedBreadcrumbs() |
122 | 130 | */ |
123 | 131 | public function getCMSFields() |
124 | 132 | { |
| 133 | + $threshold = DBForeignKey::config()->get('dropdown_field_threshold'); |
| 134 | + $overThreshold = $groups->count() > $threshold; |
| 135 | + |
125 | 136 | $fields = new FieldList( |
126 | 137 | new TabSet( |
127 | 138 | "Root", |
128 | 139 | new Tab( |
129 | 140 | 'Members', |
130 | 141 | _t(__CLASS__ . '.MEMBERS', 'Members'), |
131 | 142 | new TextField("Title", $this->fieldLabel('Title')), |
132 | | - $parentidfield = DropdownField::create( |
| 143 | + $parentidfield = SearchableDropdownField::create( |
133 | 144 | 'ParentID', |
134 | 145 | $this->fieldLabel('Parent'), |
135 | | - $this->getDecodedBreadcrumbs() |
136 | | - )->setEmptyString(' '), |
| 146 | + Group::get(), |
| 147 | + null, |
| 148 | + 'BreadcrumbTitle' |
| 149 | + ) |
| 150 | + ->setIsSearchable(true) |
| 151 | + ->setUseSearchContext(true) |
| 152 | + ->setPlaceholder(' ') |
| 153 | + ->setIsLazyLoaded($overThreshold) |
| 154 | + ->setLazyLoadLimit($threshold), |
137 | 155 | new TextareaField('Description', $this->fieldLabel('Description')) |
138 | 156 | ), |
139 | 157 | $permissionsTab = new Tab( |
@@ -460,6 +478,14 @@ public function stageChildren() |
460 | 478 | ->sort('"Sort"'); |
461 | 479 | } |
462 | 480 |
|
| 481 | + /** |
| 482 | + * @return string |
| 483 | + */ |
| 484 | + public function getBreadcrumbTitle(): string |
| 485 | + { |
| 486 | + return $this->getBreadcrumbs(' > '); |
| 487 | + } |
| 488 | + |
463 | 489 | /** |
464 | 490 | * @return string |
465 | 491 | */ |
@@ -503,12 +529,12 @@ public function validate() |
503 | 529 | } |
504 | 530 | } |
505 | 531 |
|
506 | | - $currentGroups = Group::get() |
507 | | - ->filter('ID:not', $this->ID) |
508 | | - ->map('Code', 'Title') |
509 | | - ->toArray(); |
| 532 | + $hasGroupWithSameTitle = Group::get() |
| 533 | + ->exclude('ID', $this->ID) |
| 534 | + ->filter('Title', $this->Title) |
| 535 | + ->exists(); |
510 | 536 |
|
511 | | - if (in_array($this->Title, $currentGroups)) { |
| 537 | + if ($hasGroupWithSameTitle) { |
512 | 538 | $result->addError( |
513 | 539 | _t( |
514 | 540 | 'SilverStripe\\Security\\Group.ValidationIdentifierAlreadyExists', |
@@ -711,16 +737,19 @@ public function requireDefaultRecords() |
711 | 737 | */ |
712 | 738 | private function dedupeCode(): void |
713 | 739 | { |
714 | | - $currentGroups = Group::get() |
715 | | - ->exclude('ID', $this->ID) |
716 | | - ->map('Code', 'Title') |
717 | | - ->toArray(); |
718 | 740 | $code = $this->Code; |
719 | 741 | $count = 2; |
720 | | - while (isset($currentGroups[$code])) { |
| 742 | + |
| 743 | + while ($this->checkIfCodeExists($code)) { |
721 | 744 | $code = $this->Code . '-' . $count; |
722 | 745 | $count++; |
723 | 746 | } |
| 747 | + |
724 | 748 | $this->setField('Code', $code); |
725 | 749 | } |
| 750 | + |
| 751 | + private function checkIfCodeExists(string $code): bool |
| 752 | + { |
| 753 | + return Group::get()->filter('Code', $code)->exclude('ID', $this->ID)->exists(); |
| 754 | + } |
726 | 755 | } |
0 commit comments