DisambiguationSegmentFieldModifier   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 37
rs 10
c 0
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getSuggestion() 0 3 1
A getPreview() 0 30 5
1
<?php
2
3
namespace SilverStripe\UserForms\Modifier;
4
5
use SilverStripe\Forms\Form;
6
use SilverStripe\Forms\SegmentFieldModifier\AbstractSegmentFieldModifier;
7
use SilverStripe\UserForms\Model\EditableFormField;
8
9
class DisambiguationSegmentFieldModifier extends AbstractSegmentFieldModifier
10
{
11
    public function getPreview($value)
12
    {
13
        if ($this->form instanceof Form && $record = $this->form->getRecord()) {
14
            $parent = $record->Parent();
15
16
            $try = $value;
17
18
            $sibling = EditableFormField::get()
19
                ->filter('ParentID', $parent->ID)
20
                ->filter('Name', $try)
21
                ->where('"ID" != ' . $record->ID)
22
                ->first();
23
24
            $counter = 1;
25
26
            while ($sibling !== null) {
27
                $try = $value . '_' . $counter++;
28
29
                $sibling = EditableFormField::get()
30
                    ->filter('ParentID', $parent->ID)
31
                    ->filter('Name', $try)
32
                    ->first();
33
            }
34
35
            if ($try !== $value) {
36
                return $try;
37
            }
38
        }
39
40
        return $value;
41
    }
42
43
    public function getSuggestion($value)
44
    {
45
        return $this->getPreview($value);
46
    }
47
}
48