QuantityCell::init()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
/*
4
 * Cart module for Yii2
5
 *
6
 * @link      https://github.com/hiqdev/yii2-cart
7
 * @package   yii2-cart
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hiqdev\yii2\cart\widgets;
13
14
class QuantityCell extends \yii\base\Widget
15
{
16
    const MODE_SELECT = 'select';
17
18
    const MODE_NUMBER = 'number';
19
20
    public $model;
21
22
    public $type;
23
24
    public function init()
25
    {
26
        parent::init();
27
        $this->registerClientScript();
28
    }
29
30
    public function run()
31
    {
32
        return $this->render('QuantityCell_view', [
33
            'view'   => $this->getView(),
34
            'model'  => $this->model,
35
            'widget' => $this,
36
        ]);
37
    }
38
39
    /**
40
     * Select is default mode.
41
     */
42
    public function isSelectMode()
43
    {
44
        return $this->type !== self::MODE_NUMBER && method_exists($this->model, 'getQuantityOptions');
45
    }
46
47
    public function registerClientScript()
48
    {
49
        $this->getView()->registerJs(<<<JS
50
            jQuery(document).on('change', '.quantity-field', function(evnet) {
51
                var form = jQuery(this).parents('form').get(0);
52
                $(form).submit();
53
            });
54
JS
55
        );
56
    }
57
}
58