-
Notifications
You must be signed in to change notification settings - Fork 0
/
Select2Action.php
56 lines (46 loc) · 1.37 KB
/
Select2Action.php
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
<?php
/**
* @link https://github.com/indicalabs/yii2-select2
* @copyright Copyright (c) 2016 Venu Narukulla
* @license https://github.com/indicalabs/yii2-select2/blob/master/LICENSE
*/
namespace indicalabs\select2;
use yii\web\Response;
use yii\base\InvalidConfigException;
/**
*
* @author Venu Narukulla
*/
class Select2Action extends \yii\base\Action
{
/**
* Name of the GET parameter
* @var string
*/
public $paramName = 'q';
/**
* @var callable PHP callback function to retrieve filtered data
* @example function ($q) { return ['results' => [['id'=>1,'text'=>'First Element'], ['id'=>2,'text'=>'Second Element']]]; }
*/
public $dataCallback;
/**
* @inheritdoc
*/
public function init()
{
if (!is_callable($this->dataCallback)) {
throw new InvalidConfigException('"' . get_class($this) . '::dataCallback" should be a valid callback.');
}
\Yii::$app->response->format = Response::FORMAT_JSON;
$this->controller->enableCsrfValidation = false;
}
public function run()
{
$q = \Yii::$app->request->get($this->paramName);
$data = call_user_func($this->dataCallback, $q);
if (is_array($data) && (!isset($data['results']))) {
$data = ['results' => $data];
}
return $data;
}
}