ValueColumn   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 4
dl 0
loc 53
ccs 0
cts 39
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 7 1
A renderDataCellContent() 0 10 1
A findPlanId() 0 4 1
A registerClientScript() 0 19 1
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\grid;
12
13
use hipanel\modules\finance\assets\PriceEstimator;
14
use Yii;
15
use yii\grid\Column;
16
use yii\helpers\Html;
17
use yii\helpers\Url;
18
19
class ValueColumn extends Column
20
{
21
    public $attribute = 'value';
22
23
    /**
24
     * @var integer
25
     */
26
    private $planId;
27
28
    public function init()
29
    {
30
        parent::init();
31
        $this->header = Yii::t('hipanel.finance.plan', 'Estimated value');
32
        $this->planId = $this->findPlanId();
33
        $this->registerClientScript();
34
    }
35
36
    protected function renderDataCellContent($model, $key, $index)
37
    {
38
        $fields = Html::hiddenInput("[{$model->id}]object_id", $model->object_id);
39
        $fields .= Html::hiddenInput("[{$model->id}]type", $model->type);
40
41
        return Html::tag('span', Html::tag('span', Html::tag('i', $fields, [
42
            'class' => 'fa fa-refresh fa-spin fa-fw',
43
            'data' => ['id' => $model->id],
44
        ]), ['class' => 'price-estimates']), ['class' => 'price-item', 'data' => ['id' => $model->id]]);
45
    }
46
47
    private function registerClientScript()
48
    {
49
        $calculateValueUrl = Url::toRoute(['@plan/calculate-values', 'planId' => $this->planId]);
50
        $view = Yii::$app->view;
51
        $view->registerAssetBundle(PriceEstimator::class);
52
        $view->registerJs(/** @lang ECMAScript 6 */
53
            "
54
        ;(function ($, window, document, undefined) {
55
            let Estimator = $('#bulk-plan').priceEstimator({
56
                url: '${calculateValueUrl}',
57
                estimatePlan: true,
58
                rowSelector: '.price-item',
59
                totalCellSelector: '#plan-monthly-value',
60
                totalPerObjectSelector: '.total-per-object',
61
            });
62
            Estimator.update();
63
        })(jQuery, window, document);
64
");
65
    }
66
67
    private function findPlanId()
68
    {
69
        return Yii::$app->request->get('id');
70
    }
71
}
72