-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathWidget.php
52 lines (43 loc) · 1.21 KB
/
Widget.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
<?php
namespace cleantalk\antispam;
use Yii;
use yii\base\Widget as BaseWidget;
use yii\helpers\Html;
class Widget extends BaseWidget
{
/** @var string CleanTalk application component ID */
public $apiComponentId = 'antispam';
/** @var string hidden field id */
protected $checkJsHtmlId;
/**
* @inheritdoc
*/
public function init()
{
parent::init();
$this->checkJsHtmlId = md5(rand(0, 1000));
}
/**
* @inheritdoc
*/
public function run()
{
$this->registerClientScript();
$this->getComponent()->startFormSubmitTime($this->checkJsHtmlId);
return Html::hiddenInput('ct_checkjs', -1, ['id' => $this->checkJsHtmlId]) .
Html::hiddenInput('ct_formid', $this->checkJsHtmlId);
}
protected function registerClientScript()
{
$js = 'setTimeout(function(){document.getElementById("' . $this->checkJsHtmlId . '").value="' . $this->getComponent()->getCheckJsCode() . '";}, 1000)';
$this->getView()
->registerJs($js);
}
/**
* @return \cleantalk\antispam\Component
*/
protected function getComponent()
{
return Yii::$app->get($this->apiComponentId);
}
}