Test Failed
Push — master ( 4834ec...9ac98c )
by vistart
18:06
created

OrganizationController::actionRevoke()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 1
nc 1
nop 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\organization\web\user\controllers;
14
15
use rhosocial\organization\forms\SetUpForm;
16
use Yii;
17
use yii\data\ActiveDataProvider;
18
use yii\filters\AccessControl;
19
use yii\web\BadRequestHttpException;
20
use yii\web\Controller;
21
use yii\web\ServerErrorHttpException;
22
23
/**
24
 * Organization Controller, designed for user module.
25
 *
26
 * @version 1.0
27
 * @author vistart <[email protected]>
28
 */
29
class OrganizationController extends Controller
30
{
31
    public $layout = '@rhosocial/organization/web/user/views/layouts/organization';
32
    const RESULT_SUCCESS = 'success';
33
    const RESULT_FAILED = 'failed';
34
    const SESSION_KEY_MESSAGE = 'session_key_message';
35
    const SESSION_KEY_RESULT = 'session_key_result';
36
    public $organizationSetUpSuccessMessage;
37
    public $organizationSetUpFailedMessage;
38
    public $departmentSetUpSuccessMessage;
39
    public $departmentSetUpFailedMessage;
40
41
    protected $viewBasePath = '@rhosocial/organization/web/user/views/organization/';
42
43
    protected function initMessages()
44
    {
45
        if (!is_string($this->organizationSetUpSuccessMessage)) {
46
            $this->organizationSetUpSuccessMessage = Yii::t('organization' ,'Organization Set Up.');
47
        }
48
        if (!is_string($this->organizationSetUpFailedMessage)) {
49
            $this->organizationSetUpFailedMessage = Yii::t('organization', 'Organization Set Up Failed.');
50
        }
51
        if (!is_string($this->departmentSetUpSuccessMessage)) {
52
            $this->departmentSetUpSuccessMessage = Yii::t('organization' ,'Department Set Up.');
53
        }
54
        if (!is_string($this->departmentSetUpFailedMessage)) {
55
            $this->departmentSetUpFailedMessage = Yii::t('organization', 'Department Set Up Failed.');
56
        }
57
    }
58
59
    public function init()
60
    {
61
        $this->initMessages();
62
        parent::init();
63
    }
64
65
    /**
66
     * @inheritdoc
67
     */
68
    public function behaviors()
69
    {
70
        return [
71
            'access' => [
72
                'class' => AccessControl::class,
73
                'rules' => [
74
                    [
75
                        'allow' => false,
76
                        'roles' => ['?'],
77
                    ],
78
                    [
79
                        'actions' => ['update'],
80
                        'allow' => true,
81
                        'roles' => ['manageProfile'],
82
                    ],
83
                    [
84
                        'actions' => ['revoke'],
85
                        'allow' => true,
86
                        'roles' => ['revokeOrganization'],
87
                    ],
88
                ],
89
            ],
90
        ];
91
    }
92
93
    public function actionIndex()
94
    {
95
        return $this->render($this->viewBasePath . 'index');
96
    }
97
98
    /**
99
     * List all organization(s) and department(s) which current user has joined in.
100
     * @return string the rendering result.
101
     */
102
    public function actionList()
103
    {
104
        $identity = Yii::$app->user->identity;
105
        if (!$identity) {
106
            throw new ServerErrorHttpException('User Not Found.');
107
        }
108
        $dataProvider = new ActiveDataProvider([
109
            'query' => $identity->getAtOrganizations(),
110
            'pagination' => [
111
                'pageParam' => 'oganization-page',
112
                'pageSize' => 20,
113
            ],
114
            'sort' => [
115
                'sortParam' => 'organization-sort',
116
            ],
117
        ]);
118
        return $this->render($this->viewBasePath . 'list', ['dataProvider' => $dataProvider]);
119
    }
120
121
    /**
122
     * @return string the rendering result.
123
     */
124
    public function actionSetUpOrganization()
125
    {
126
        $model = new SetUpForm(['user' => Yii::$app->user->identity]);
127
        if ($model->load(Yii::$app->request->post())) {
128
            try {
129
                if (($result = $model->setUpOrganization()) === true) {
130
                    Yii::$app->session->setFlash(self::SESSION_KEY_RESULT, self::RESULT_SUCCESS);
131
                    Yii::$app->session->setFlash(self::SESSION_KEY_MESSAGE, $this->organizationSetUpSuccessMessage);
132
                    return $this->redirect(['list']);
133
                }
134
                if ($result instanceof \Exception) {
135
                    throw $result;
136
                }
137
            } catch (\Exception $ex) {
138
                Yii::error($ex->getMessage(), __METHOD__);
139
                Yii::$app->session->setFlash(self::SESSION_KEY_RESULT, self::RESULT_FAILED);
140
                Yii::$app->session->setFlash(self::SESSION_KEY_MESSAGE, $this->organizationSetUpFailedMessage);
141
            }
142
        }
143
        return $this->render($this->viewBasePath . 'set-up-organization', ['model' => $model]);
144
    }
145
146
    /**
147
     * Set up department.
148
     * @param string $parent Parent organization or department ID.
149
     * @return string the rendering result.
150
     */
151
    public function actionSetUpDepartment($parent)
152
    {
153
        $model = new SetUpForm(['user' => Yii::$app->user->identity, 'parent' => $parent]);
154
        if (!$model->getParent()) {
155
            throw new BadRequestHttpException(Yii::t('organization', 'Parent Organization/Department Not Exist.'));
156
        }
157
        if ($model->load(Yii::$app->request->post())) {
158
            try {
159
                if (($result = $model->setUpDepartment()) === true) {
160
                    Yii::$app->session->setFlash(self::SESSION_KEY_RESULT, self::RESULT_SUCCESS);
161
                    Yii::$app->session->setFlash(self::SESSION_KEY_MESSAGE, $this->departmentSetUpSuccessMessage);
162
                    return $this->redirect(['list']);
163
                }
164
                if ($result instanceof \Exception) {
165
                    throw $result;
166
                }
167
            } catch (\Exception $ex) {
168
                Yii::error($ex->getMessage(), __METHOD__);
169
                Yii::$app->session->setFlash(self::SESSION_KEY_RESULT, self::RESULT_FAILED);
170
                Yii::$app->session->setFlash(self::SESSION_KEY_MESSAGE, $this->departmentSetUpFailedMessage);
171
            }
172
        }
173
        return $this->render($this->viewBasePath . 'set-up-organization', ['model' => $model]);
174
    }
175
176
    public function actionView($id)
0 ignored issues
show
Unused Code introduced by
The parameter $id 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...
177
    {
178
        return $this->render($this->viewBasePath . 'view');
179
    }
180
181
    public function actionUpdate($id)
0 ignored issues
show
Unused Code introduced by
The parameter $id 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...
182
    {
183
        return $this->render($this->viewBasePath . 'update');
184
    }
185
186
    public function actionRevoke($id)
0 ignored issues
show
Unused Code introduced by
The parameter $id 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...
187
    {
188
        
189
    }
190
}
191