StatisticTableGenerator::initClientScript()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 0
cts 27
cp 0
rs 9.472
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * Finance module for HiPanel
4
 *
5
 * @link      https://github.com/hiqdev/hipanel-module-finance
6
 * @package   hipanel-module-finance
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\finance\widgets;
12
13
use yii\base\InvalidConfigException;
14
use yii\base\Widget;
15
use yii\helpers\Url;
16
use yii\web\View;
17
18
class StatisticTableGenerator extends Widget
19
{
20
    /**
21
     * @var array
22
     */
23
    public $statistic = [];
24
25
    /**
26
     * @var string
27
     */
28
    public $type;
29
30
    public function init()
31
    {
32
        if (!$this->type) {
33
            throw new InvalidConfigException('Attribute `type` must be set.');
34
        }
35
        if (!empty($this->statistic)) {
36
            $this->sortStatistic();
37
        }
38
        $this->initClientScript();
39
    }
40
41
    public function run()
42
    {
43
        return $this->render('statisticTableGenerator', ['id' => $this->getId(), 'statistic' => $this->statistic]);
44
    }
45
46
    protected function sortStatistic()
47
    {
48
    }
49
50
    protected function initClientScript()
51
    {
52
        $id = $this->getId();
53
        $type = $this->type;
54
        $url = Url::to(['@purse/generate-all']);
55
56
        $this->view->registerJs(<<<"JS"
57
        (function() {
58
            function updateTable() {
59
                var table = $('#{$id}');
60
                var loading = table.parents('.box').find('.loading');
61
                $.ajax({
62
                    url: '{$url}',
63
                    method: 'POST',
64
                    data: {type: '{$type}'},
65
                    dataType: 'html',
66
                    beforeSend: function( xhr ) {
67
                        loading.show();
68
                    }
69
                }).done(function (data) {
70
                    loading.hide();
71
                });
72
            }
73
            setInterval(updateTable, 10000);
74
        })();
75
JS
76
            , View::POS_END);
77
    }
78
}
79