Repository navigation
Expand file tree
/
Copy pathAutoComplete.php
More file actions
65 lines (52 loc) · 1.54 KB
/
Copy pathAutoComplete.php
File metadata and controls
65 lines (52 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
namespace Example;
class AutoComplete extends Page
{
private \Example\Model\Country $model;
public function __construct()
{
parent::__construct();
$this->model = new \Example\Model\Country();
$this->addBody(new \PHPFUI\Header('Auto Complete Country Example'));
$this->addBody('<b>AutoComplete</b> offers a backend callback that can be leveraged for various user experiences. ');
$this->addBody(new \PHPFUI\Link('http://' . $_SERVER['SERVER_NAME'] . '/countries.csv', 'Data Source'));
$form = new \PHPFUI\Form($this);
$form->setAreYouSure(false);
$control = new \PHPFUI\Input\AutoComplete($this, [$this, 'callback', ], 'text', 'country', 'Pick a country, any country');
$control->setToolTip('Start typing to find a country');
$form->add($control);
$form->add(new \PHPFUI\Submit('Save', 'save'));
$this->addBody("{$form}");
}
/**
* @param array<string, string> $parameters
*
* @return array<string, array<array<string, string>>>
*/
public function callback(array $parameters) : array
{
$returnValue = [];
if (empty($parameters['save']))
{
$names = \explode(' ', \trim($parameters['AutoComplete']));
foreach ($this->model->getCountries() as $row)
{
$pos = 0;
$country = $row['Country'];
foreach ($names as $part)
{
$pos = \stripos($country, $part, $pos);
if (false === $pos)
{
break;
}
}
if (false !== $pos)
{
$returnValue[] = ['value' => $country, 'data' => $country];
}
}
}
return ['suggestions' => $returnValue];
}
}