-
Notifications
You must be signed in to change notification settings - Fork 1
/
Widget.php
65 lines (56 loc) · 1.97 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
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
/**
* Created by Ostashev Dmitriy <[email protected]>
* Date: 10.10.14 14:45
* ------------------------------------------------------------
*/
namespace ostashevdv\cackle;
use yii\web\View;
use yii\helpers\Json;
class Widget extends \yii\base\Widget
{
/**
* @var array $widget массив настроек виджета
* подробнее на http://cackle.ru/help/widget-api
*/
public $widget = [];
/** @var string $jsInitWidget инициализация JavaScript переменной Cackle */
protected $jsInitWidget = <<<'JS'
cackle_widget = window.cackle_widget || [];
JS;
/** @var string $jsLoadWidget загрузка js скрипта */
protected $jsLoadWidget = <<<'JS'
(function() {
var mc = document.createElement('script');
mc.type = 'text/javascript';
mc.async = true;
mc.src = ('https:' == document.location.protocol ? 'https' : 'http') + '://cackle.me/widget.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(mc, s.nextSibling);
})();
JS;
public function init()
{
parent::init();
if (!is_array($this->widget)) {
throw new InvalidConfigException("\$options - must be array() [[widget=>'', id=>1, ...],[],[]]");
} else {
foreach($this->widget as $widget) {
if (!array_key_exists('widget',$widget)) {
throw new InvalidConfigException("\$options - must be array() [[widget=>'', id=>1, ...],[],[]]");
}
}
}
return true;
}
public function run()
{
parent::run();
foreach ($this->widget as $widget) {
$js[] = 'cackle_widget.push('.Json::encode($widget).');';
}
$js = implode(PHP_EOL, $js);
$this->getView()->registerJs($js);
$this->getView()->registerJs($this->jsInitWidget, View::POS_HEAD, 'cackle_init');
$this->getView()->registerJs($this->jsLoadWidget, View::POS_END, 'cackle_load');
}
}