|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace app\models; |
|
4
|
|
|
|
|
5
|
|
|
use Yii; |
|
6
|
|
|
use yii\data\ActiveDataProvider; |
|
7
|
|
|
use yii\helpers\ArrayHelper; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* This is the model class for table "spam_checker". |
|
11
|
|
|
* @property integer $id |
|
12
|
|
|
* @property string $behavior |
|
13
|
|
|
* @property string $api_key |
|
14
|
|
|
* @property string $name |
|
15
|
|
|
* @property string $author_field |
|
16
|
|
|
* @property string $content_field |
|
17
|
|
|
*/ |
|
18
|
|
|
class SpamChecker extends \yii\db\ActiveRecord |
|
19
|
|
|
{ |
|
20
|
|
|
const FIELD_TYPE_NO_CHECKING = 'notinterpret'; |
|
21
|
|
|
const FIELD_TYPE_AUTHOR = 'author_field'; |
|
22
|
|
|
const FIELD_TYPE_CONTENT = 'content_field'; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var int |
|
26
|
|
|
*/ |
|
27
|
|
|
public static $enabledApiId = 0; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @inheritdoc |
|
31
|
|
|
*/ |
|
32
|
|
|
public static function tableName() |
|
33
|
|
|
{ |
|
34
|
|
|
return '{{%spam_checker}}'; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @inheritdoc |
|
39
|
|
|
*/ |
|
40
|
|
|
public function rules() |
|
41
|
|
|
{ |
|
42
|
|
|
return [ |
|
43
|
|
|
[['behavior'], 'required'], |
|
44
|
|
|
[['behavior'], 'string', 'max' => 255], |
|
45
|
|
|
[['name', 'author_field', 'content_field'], 'string', 'max' => 50], |
|
46
|
|
|
[['api_key'], 'string', 'max' => 90] |
|
47
|
|
|
]; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @inheritdoc |
|
52
|
|
|
*/ |
|
53
|
|
|
public function attributeLabels() |
|
54
|
|
|
{ |
|
55
|
|
|
return [ |
|
56
|
|
|
'id' => Yii::t('app', 'ID'), |
|
57
|
|
|
'behavior' => Yii::t('app', 'Behavior'), |
|
58
|
|
|
'api_key' => Yii::t('app', 'Api Key'), |
|
59
|
|
|
'name' => Yii::t('app', 'Name'), |
|
60
|
|
|
'author_field' => Yii::t('app', 'Author Field'), |
|
61
|
|
|
'content_field' => Yii::t('app', 'Content Field'), |
|
62
|
|
|
'enabledApiId' => Yii::t('app', 'Enabled Api Key'), |
|
63
|
|
|
]; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Search tasks |
|
68
|
|
|
* @param $params |
|
69
|
|
|
* @return ActiveDataProvider |
|
70
|
|
|
*/ |
|
71
|
|
|
public function search($params) |
|
72
|
|
|
{ |
|
73
|
|
|
/* @var $query \yii\db\ActiveQuery */ |
|
74
|
|
|
$query = self::find(); |
|
75
|
|
|
$dataProvider = new ActiveDataProvider( |
|
76
|
|
|
[ |
|
77
|
|
|
'query' => $query, |
|
78
|
|
|
'pagination' => [ |
|
79
|
|
|
'pageSize' => 10, |
|
80
|
|
|
], |
|
81
|
|
|
] |
|
82
|
|
|
); |
|
83
|
|
|
if (!($this->load($params))) { |
|
84
|
|
|
return $dataProvider; |
|
85
|
|
|
} |
|
86
|
|
|
$query->andFilterWhere(['id' => $this->id]); |
|
87
|
|
|
$query->andFilterWhere(['like', 'behavior', $this->behavior]); |
|
88
|
|
|
$query->andFilterWhere(['like', 'api_key', $this->api_key]); |
|
89
|
|
|
$query->andFilterWhere(['like', 'name', $this->name]); |
|
90
|
|
|
return $dataProvider; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Function return array map for drop down list |
|
95
|
|
|
* @return array |
|
96
|
|
|
*/ |
|
97
|
|
|
public static function getAvailableApis() |
|
98
|
|
|
{ |
|
99
|
|
|
static::getEnabledApiId(); |
|
100
|
|
|
$all = static::find()->all(); |
|
101
|
|
|
$map = ArrayHelper::map($all, 'id', 'name'); |
|
102
|
|
|
return ArrayHelper::merge([0 => Yii::t('app', 'Not selected')], $map); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public static function getEnabledApiId() |
|
106
|
|
|
{ |
|
107
|
|
|
$enabled = Yii::$app->getModule('core')->spamCheckerApiKey; |
|
108
|
|
|
static::$enabledApiId = static::getApiIdByClassName($enabled); |
|
109
|
|
|
return static::$enabledApiId; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public static function setEnabledApiId($id) |
|
113
|
|
|
{ |
|
114
|
|
|
$config = Yii::$app->getModule('core')->spamCheckerApiKey; |
|
115
|
|
|
$model = static::findOne($id); |
|
116
|
|
|
if ($model === null) { |
|
117
|
|
|
$config->value = ''; |
|
118
|
|
|
} else { |
|
119
|
|
|
$config->value = $model->behavior; |
|
120
|
|
|
} |
|
121
|
|
|
$config->save(); |
|
122
|
|
|
static::$enabledApiId = $id; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Function return id of SpanChecker by behavior class name |
|
127
|
|
|
* @param $className string |
|
128
|
|
|
* @return int |
|
129
|
|
|
*/ |
|
130
|
|
|
public static function getApiIdByClassName($className) |
|
131
|
|
|
{ |
|
132
|
|
|
$model = static::findOne(['behavior' => $className]); |
|
133
|
|
|
if ($model === null) { |
|
134
|
|
|
$id = 0; |
|
135
|
|
|
} else { |
|
136
|
|
|
$id = $model->id; |
|
137
|
|
|
} |
|
138
|
|
|
return $id; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
public static function getFieldTypesForForm() |
|
142
|
|
|
{ |
|
143
|
|
|
return [ |
|
144
|
|
|
self::FIELD_TYPE_NO_CHECKING => Yii::t('app', 'No'), |
|
145
|
|
|
self::FIELD_TYPE_AUTHOR => Yii::t('app', 'Username'), |
|
146
|
|
|
self::FIELD_TYPE_CONTENT => Yii::t('app', 'Content'), |
|
147
|
|
|
]; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @return SpamChecker |
|
|
|
|
|
|
152
|
|
|
*/ |
|
153
|
|
|
public static function getActive() |
|
154
|
|
|
{ |
|
155
|
|
|
return static::findOne(static::getEnabledApiId()); |
|
|
|
|
|
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.