Completed
Push — master ( 691d80...8f3098 )
by Dmitry
13:51
created

PriceHistoryWidget::run()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 0
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 hipanel\helpers\ArrayHelper;
14
use hipanel\modules\finance\models\Plan;
15
use hipanel\modules\finance\models\PriceHistory;
16
use yii\base\Widget;
17
use yii\helpers\Url;
18
19
/**
20
 * Class PriceHistoryWidget
21
 * @package hipanel\modules\finance\widgets
22
 */
23
class PriceHistoryWidget extends Widget
24
{
25
    /**
26
     * @var Plan
27
     */
28
    public $model;
29
30
    /**
31
     * @inheritDoc
32
     */
33
    public function run()
34
    {
35
        $this->registerJsScript();
36
37
        return $this->render('PriceHistoryWidget', [
38
            'collapseItems' => $this->renderCollapseItems(),
39
            'widget' => $this,
40
        ]);
41
    }
42
43
    /**
44
     * @return string[]
45
     */
46
    private function getSortedTimesArray(): array
47
    {
48
        $dates = array_unique(ArrayHelper::getColumn($this->model->priceHistory, function (PriceHistory $el): string {
49
            return $el->time;
50
        }));
51
        rsort($dates);
52
53
        return $dates;
54
    }
55
56
    /**
57
     * @return string[]
58
     */
59
    private function renderCollapseItems(): array
60
    {
61
        $res = [];
62
        foreach ($this->getSortedTimesArray() as $date) {
63
            $res[] = [
64
                'label' => $date,
65
                'content' => '',
66
                'options' => [
67
                    'id' => $date,
68
                ],
69
            ];
70
        }
71
72
        return array_filter([
73
            'items' => $res,
74
        ]);
75
    }
76
77
    private function registerJsScript(): void
78
    {
79
        $calculateValueUrl = Url::toRoute(['@plan/get-plan-history', 'plan_id' => $this->model->id]);
80
81
        $this->view->registerJs(<<<JS
82
(function ($, window, document, undefined) {
83
    $('.collapse-toggle').on('click', function() {
84
        $.ajax({
85
            method: 'post',
86
            url: `$calculateValueUrl&date=\${\$(this).text()}`,
87
            success: (res) => {
88
                const collapsBody = $(this).attr('href') + ' .panel-body';
89
                $(collapsBody).html(res);
90
91
                //Initialize ArraySpoiler onclick
92
                $('a[id*=w][class*=badge]').popover({"placement":"bottom","html":true}).on('show.bs.popover', function(e) {
93
                    $('[data-popover-group="main"]').not(e.target).popover('hide');
94
                });
95
            },
96
            error: function (xhr) {
97
                hipanel.notify.error(xhr.statusText);
98
            }
99
        });
100
    });
101
})(jQuery, window, document);
102
JS
103
        );
104
    }
105
}
106