Completed
Push — master ( e6186f...9e6012 )
by vistart
03:57
created

UserListWidget   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 0
loc 56
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B init() 0 28 6
A run() 0 7 1
1
<?php
2
3
/**
4
 *  _   __ __ _____ _____ ___  ____  _____
5
 * | | / // // ___//_  _//   ||  __||_   _|
6
 * | |/ // /(__  )  / / / /| || |     | |
7
 * |___//_//____/  /_/ /_/ |_||_|     |_|
8
 * @link https://vistart.me/
9
 * @copyright Copyright (c) 2016 - 2017 vistart
10
 * @license https://vistart.me/license/
11
 */
12
13
namespace rhosocial\user\widgets;
14
15
use Yii;
16
use yii\base\Widget;
17
use yii\data\ActiveDataProvider;
18
use yii\grid\ActionColumn;
19
use yii\helpers\Url;
20
use yii\web\ServerErrorHttpException;
21
22
class UserListWidget extends Widget
23
{
24
    /**
25
     * @var ActiveDataProvider
26
     */
27
    public $dataProvider;
28
    /**
29
     * @var array|null|string ActionColumn class configuration. Null if you do not need it.
30
     * 'default' if you want to use the default configuration.
31
     * Note: If you want to use your own ActionColumn class configuration, please do not
32
     * forget to attach the 'class' key.
33
     */
34
    public $actionColumn;
35
36
    const ACTION_COLUMN_DEFAULT = 'default';
37
38
    /**
39
     * Initialize attributes.
40
     */
41
    public function init()
42
    {
43
        if (empty($this->dataProvider)) {
44
            throw new ServerErrorHttpException('Invalid User Provider.');
45
        }
46
        if (strtolower($this->actionColumn) == self::ACTION_COLUMN_DEFAULT) {
47
            $this->actionColumn = [
48
                'class' => ActionColumn::class,
49
                'header' => Yii::t('user', 'Action'),
50
                'urlCreator' => function ($action, $model, $key, $index, ActionColumn $column) {
0 ignored issues
show
Unused Code introduced by
The parameter $key 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...
Unused Code introduced by
The parameter $index 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...
Unused Code introduced by
The parameter $column 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...
51
                    /* @var $model User */
52
                    if ($action == 'view') {
53
                        return Url::to(['view', 'id' => $model->id]);
54
                    } elseif ($action == 'update') {
55
                        return Url::to(['update', 'id' => $model->id]);
56
                    } elseif ($action == 'delete') {
57
                        return Url::to(['deregister', 'id' => $model->id]);
58
                    }
59
                    return '#';
60
                },
61
                'visibleButtons' => [
62
                    'view' => Yii::$app->user->can('viewUser'),
63
                    'update' => Yii::$app->user->can('updateUser'),
64
                    'delete' => Yii::$app->user->can('deleteUser'),
65
                ],
66
            ];
67
        }
68
    }
69
70
    public function run()
71
    {
72
        return $this->render('user-list', [
73
            'dataProvider' => $this->dataProvider,
74
            'actionColumn' => $this->actionColumn
75
        ]);
76
    }
77
}