2015-08-11 22:57:19 +02:00
|
|
|
<?php
|
|
|
|
|
2017-08-09 01:55:09 +02:00
|
|
|
namespace SilverStripe\UserForms\Modifier;
|
|
|
|
|
|
|
|
|
2015-08-11 22:57:19 +02:00
|
|
|
use SilverStripe\Forms\SegmentFieldModifier\AbstractSegmentFieldModifier;
|
|
|
|
|
2017-08-09 01:55:09 +02:00
|
|
|
use EditableformField;
|
|
|
|
use SilverStripe\Forms\Form;
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
class DisambiguationSegmentFieldModifier extends AbstractSegmentFieldModifier
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*
|
|
|
|
* @param string $value
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getPreview($value)
|
|
|
|
{
|
|
|
|
if ($this->form instanceof Form && $record = $this->form->getRecord()) {
|
|
|
|
$parent = $record->Parent();
|
|
|
|
|
|
|
|
$try = $value;
|
|
|
|
|
|
|
|
$sibling = EditableformField::get()
|
|
|
|
->filter('ParentID', $parent->ID)
|
|
|
|
->filter('Name', $try)
|
|
|
|
->where('"ID" != ' . $record->ID)
|
|
|
|
->first();
|
|
|
|
|
|
|
|
$counter = 1;
|
|
|
|
|
|
|
|
while ($sibling !== null) {
|
|
|
|
$try = $value . '_' . $counter++;
|
|
|
|
|
|
|
|
$sibling = EditableformField::get()
|
|
|
|
->filter('ParentID', $parent->ID)
|
|
|
|
->filter('Name', $try)
|
|
|
|
->first();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($try !== $value) {
|
|
|
|
return $try;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*
|
|
|
|
* @param string $value
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getSuggestion($value)
|
|
|
|
{
|
|
|
|
return $this->getPreview($value);
|
|
|
|
}
|
2015-08-11 22:57:19 +02:00
|
|
|
}
|