Test Failed
Push — master ( c65d32...957d91 )
by vistart
34:16 queued 14:11
created

OrganizationController::behaviors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 1
eloc 7
nc 1
nop 0
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' => true,
76
                        'roles' => ['@'],
77
                    ],
78
                ],
79
            ],
80
        ];
81
    }
82
83
    public function actionIndex()
84
    {
85
        return $this->render($this->viewBasePath . 'index');
86
    }
87
88
    /**
89
     * List all organization(s) and department(s) which current user has joined in.
90
     * @return string the rendering result.
91
     */
92
    public function actionList()
93
    {
94
        $identity = Yii::$app->user->identity;
95
        if (!$identity) {
96
            throw new ServerErrorHttpException('User Not Found.');
97
        }
98
        $dataProvider = new ActiveDataProvider([
99
            'query' => $identity->getAtOrganizations(),
100
            'pagination' => [
101
                'pageParam' => 'oganization-page',
102
                'pageSize' => 20,
103
            ],
104
            'sort' => [
105
                'sortParam' => 'organization-sort',
106
            ],
107
        ]);
108
        return $this->render($this->viewBasePath . 'list', ['dataProvider' => $dataProvider]);
109
    }
110
111
    /**
112
     * @return string the rendering result.
113
     */
114
    public function actionSetUpOrganization()
115
    {
116
        $model = new SetUpForm(['user' => Yii::$app->user->identity]);
117
        if ($model->load(Yii::$app->request->post())) {
118
            try {
119
                if (($result = $model->setUpOrganization()) === true) {
120
                    Yii::$app->session->setFlash(self::SESSION_KEY_RESULT, self::RESULT_SUCCESS);
121
                    Yii::$app->session->setFlash(self::SESSION_KEY_MESSAGE, $this->organizationSetUpSuccessMessage);
122
                    return $this->redirect(['list']);
123
                }
124
                if ($result instanceof \Exception) {
125
                    throw $result;
126
                }
127
            } catch (\Exception $ex) {
128
                Yii::error($ex->getMessage(), __METHOD__);
129
                Yii::$app->session->setFlash(self::SESSION_KEY_RESULT, self::RESULT_FAILED);
130
                Yii::$app->session->setFlash(self::SESSION_KEY_MESSAGE, $this->organizationSetUpFailedMessage);
131
            }
132
        }
133
        return $this->render($this->viewBasePath . 'set-up-organization', ['model' => $model]);
134
    }
135
136
    /**
137
     * Set up department.
138
     * @param string $parent Parent organization or department ID.
139
     * @return string the rendering result.
140
     */
141
    public function actionSetUpDepartment($parent)
142
    {
143
        $model = new SetUpForm(['user' => Yii::$app->user->identity, 'parent' => $parent]);
144
        if (!$model->getParent()) {
145
            throw new BadRequestHttpException(Yii::t('organization', 'Parent Organization/Department Not Exist.'));
146
        }
147
        if ($model->load(Yii::$app->request->post())) {
148
            try {
149
                if (($result = $model->setUpDepartment()) === true) {
150
                    Yii::$app->session->setFlash(self::SESSION_KEY_RESULT, self::RESULT_SUCCESS);
151
                    Yii::$app->session->setFlash(self::SESSION_KEY_MESSAGE, $this->departmentSetUpSuccessMessage);
152
                    return $this->redirect(['list']);
153
                }
154
                if ($result instanceof \Exception) {
155
                    throw $result;
156
                }
157
            } catch (\Exception $ex) {
158
                Yii::error($ex->getMessage(), __METHOD__);
159
                Yii::$app->session->setFlash(self::SESSION_KEY_RESULT, self::RESULT_FAILED);
160
                Yii::$app->session->setFlash(self::SESSION_KEY_MESSAGE, $this->departmentSetUpFailedMessage);
161
            }
162
        }
163
        return $this->render($this->viewBasePath . 'set-up-organization', ['model' => $model]);
164
    }
165
166
    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...
167
    {
168
        return $this->render($this->viewBasePath . 'view');
169
    }
170
171
    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...
172
    {
173
        return $this->render($this->viewBasePath . 'update');
174
    }
175
176
    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...
177
    {
178
        
179
    }
180
}
181