Completed
Push — master ( 958dbc...2d3a59 )
by Dmitry
05:33
created

PriceGridView   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
lcom 1
cbo 10
dl 0
loc 101
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B columns() 0 87 2
1
<?php
2
3
namespace hipanel\modules\finance\grid;
4
5
use hipanel\grid\RefColumn;
6
use hipanel\modules\finance\grid\presenters\price\PricePresenterFactory;
7
use hipanel\modules\finance\menus\PriceActionsMenu;
8
use hipanel\modules\finance\models\Price;
9
use hipanel\modules\finance\widgets\LinkToObjectResolver;
10
use hipanel\modules\finance\widgets\PriceType;
11
use hiqdev\yii2\menus\grid\MenuColumn;
12
use Yii;
13
use yii\bootstrap\Html;
14
15
/**
16
 * Class PriceGridView
17
 *
18
 * @author Dmytro Naumenko <[email protected]>
19
 */
20
class PriceGridView extends \hipanel\grid\BoxedGridView
21
{
22
    /**
23
     * @var \hipanel\modules\finance\grid\presenters\price\PricePresenterFactory
24
     */
25
    private $presenterFactory;
26
27
    public function __construct(PricePresenterFactory $presenterFactory, array $config = [])
28
    {
29
        parent::__construct($config);
30
        $this->presenterFactory = $presenterFactory;
31
    }
32
33
    public function columns()
34
    {
35
        return array_merge(parent::columns(), [
36
            'plan' => [
37
                'format' => 'raw',
38
                'filterAttribute' => 'plan_name_ilike',
39
                'filterOptions' => ['class' => 'narrow-filter'],
40
                'value' => function (Price $model) {
41
                    return Html::a($model->plan->name, ['@plan/view', 'id' => $model->plan->id]);
42
                },
43
            ],
44
            'price' => [
45
                'label' => Yii::t('hipanel.finance.price', 'Price'),
46
                'format' => 'raw',
47
                'value' => function (Price $model) {
48
                    return $this->presenterFactory->build(get_class($model))->renderPrice($model);
49
                }
50
            ],
51
            'object->name' => [
52
                'label' => Yii::t('hipanel', 'Object'),
53
                'format' => 'html',
54
                'value' => function (Price $model) {
55
                    $link = LinkToObjectResolver::widget([
56
                        'model' => $model->object,
57
                        'labelAttribute' => 'name',
58
                    ]);
59
60
                    return $link ?: Yii::t('hipanel.finance.price', 'Any');
61
                }
62
            ],
63
            'object->name-any' => [
64
                'label' => Yii::t('hipanel', 'Object'),
65
                'value' => function (Price $model) {
0 ignored issues
show
Unused Code introduced by
The parameter $model is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
66
                    return Yii::t('hipanel.finance.price', 'Any');
67
                }
68
            ],
69
            'object->label' => [
70
                'label' => Yii::t('hipanel', 'Details'),
71
                'value' => function (Price $model) {
72
                    return $model->object->label;
73
                }
74
            ],
75
            'type' => [
76
                'class' => RefColumn::class,
77
                'label' => Yii::t('hipanel', 'Type'),
78
                'attribute' => 'type',
79
                'filterAttribute' => 'type',
80
                'filterOptions' => ['class' => 'narrow-filter'],
81
                'format' => 'raw',
82
                'gtype' => 'type,price',
83
                'findOptions' => [
84
                    'select' => 'name_label',
85
                    'pnames' => 'monthly,overuse',
86
                    'with_recursive' => 1,
87
                    'mapOptions' => ['from' => 'oname'],
88
                ],
89
                'value' => function ($model) {
90
                    return PriceType::widget(['model' => $model]);
91
                }
92
            ],
93
            'unit' => [
94
                'class' => RefColumn::class,
95
                'attribute' => 'unit',
96
                'filterAttribute' => 'unit',
97
                'filterOptions' => ['class' => 'narrow-filter'],
98
                'format' => 'raw',
99
                'gtype' => 'type,unit',
100
                'findOptions' => [
101
                    'with_recursive' => 1,
102
                    'select' => 'name_label',
103
                    'mapOptions' => ['from' => 'name'],
104
                ],
105
            ],
106
            'currency' => [
107
                'class' => RefColumn::class,
108
                'attribute' => 'currency',
109
                'filterAttribute' => 'currency',
110
                'filterOptions' => ['class' => 'narrow-filter'],
111
                'format' => 'raw',
112
                'gtype' => 'type,currency',
113
            ],
114
            'actions' => [
115
                'class' => MenuColumn::class,
116
                'menuClass' => PriceActionsMenu::class,
117
            ],
118
        ]);
119
    }
120
}
121