forked from sjaakp/yii2-taggable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TagSuggestAction.php
54 lines (45 loc) · 1.16 KB
/
TagSuggestAction.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
<?php
/**
* MIT licence
* Version 1.0
* Sjaak Priester, Amsterdam 13-05-2015.
*
* Action for Yii 2.0
*
* Handles autocomplete requests from TagEditor.
*
*/
namespace sjaakp\taggable;
use yii\base\Action;
use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;
class TagSuggestAction extends Action {
/**
* @var string
* The (full) class name of the Tag class.
*/
public $tagClass;
/**
* @var string
* The name attribute of the Tag class.
*/
public $nameAttribute = 'name';
/**
* @var string
* The pattern used for searching suggestions.
* Default searches for Tag names beginning with the search term.
* Change this to '%{term}%' to search Tag names with the search term in any position.
*/
public $like = '{term}%';
public function run($term = '') {
/**
* @var $tc ActiveRecord
*/
$tc = $this->tagClass;
$r = ArrayHelper::getColumn($tc::find()->where(['like', $this->nameAttribute,
strtr($this->like, ['{term}' => $term]), false])->all(),
$this->nameAttribute);
return Json::encode($r);
}
}