Test Failed
Push — master ( 4a086f...4834ec )
by vistart
05:02
created

OrganizationController::actionSetUpOrganization()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 21
rs 8.7624
c 1
b 0
f 1
cc 5
eloc 15
nc 8
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\web\ServerErrorHttpException;
19
use yii\web\Controller;
20
21
/**
22
 * Organization Controller, designed for user module.
23
 *
24
 * @version 1.0
25
 * @author vistart <[email protected]>
26
 */
27
class OrganizationController extends Controller
28
{
29
    const RESULT_SUCCESS = 'success';
30
    const RESULT_FAILED = 'failed';
31
    const SESSION_KEY_MESSAGE = 'session_key_message';
32
    const SESSION_KEY_RESULT = 'session_key_result';
33
    public $organizationSetUpSuccessMessage;
34
    public $organizationSetUpFailedMessage;
35
    public $departmentSetUpSuccessMessage;
36
    public $departmentSetUpFailedMessage;
37
38
    protected $viewBasePath = '@rhosocial/organization/web/user/views/organization/';
39
40
    protected function initMessages()
41
    {
42
        if (!is_string($this->organizationSetUpSuccessMessage)) {
43
            $this->organizationSetUpSuccessMessage = Yii::t('organization' ,'Organization Set Up.');
44
        }
45
        if (!is_string($this->organizationSetUpFailedMessage)) {
46
            $this->organizationSetUpFailedMessage = Yii::t('organization', 'Organization Set Up Failed.');
47
        }
48
        if (!is_string($this->departmentSetUpSuccessMessage)) {
49
            $this->departmentSetUpSuccessMessage = Yii::t('organization' ,'Department Set Up.');
50
        }
51
        if (!is_string($this->departmentSetUpFailedMessage)) {
52
            $this->departmentSetUpFailedMessage = Yii::t('organization', 'Department Set Up Failed.');
53
        }
54
    }
55
56
    public function init()
57
    {
58
        $this->initMessages();
59
        parent::init();
60
    }
61
62
    public function actionIndex()
63
    {
64
        return $this->render('index');
65
    }
66
67
    /**
68
     * List all organization(s) and department(s) which current user has joined in.
69
     * @return string the rendering result.
70
     */
71
    public function actionList()
72
    {
73
        $identity = Yii::$app->user->identity;
74
        if (!$identity) {
75
            throw new ServerErrorHttpException('User Not Found.');
76
        }
77
        $dataProvider = new ActiveDataProvider([
78
            'query' => $identity->getAtOrganizations(),
79
            'pagination' => [
80
                'pageParam' => 'oganization-page',
81
                'pageSize' => 20,
82
            ],
83
            'sort' => [
84
                'sortParam' => 'organization-sort',
85
            ],
86
        ]);
87
        return $this->render($this->viewBasePath . 'list', ['dataProvider' => $dataProvider]);
88
    }
89
90
    /**
91
     * @return string the rendering result.
92
     */
93
    public function actionSetUpOrganization()
94
    {
95
        $model = new SetUpForm(['user' => Yii::$app->user->identity]);
96
        if ($model->load(Yii::$app->request->post())) {
97
            try {
98
                if (($result = $model->setUpOrganization()) === true) {
99
                    Yii::$app->session->setFlash(self::SESSION_KEY_RESULT, self::RESULT_SUCCESS);
100
                    Yii::$app->session->setFlash(self::SESSION_KEY_MESSAGE, $this->organizationSetUpSuccessMessage);
101
                    return $this->redirect(['index']);
102
                }
103
                if ($result instanceof \Exception) {
104
                    throw $result;
105
                }
106
            } catch (\Exception $ex) {
107
                Yii::error($ex->getMessage(), __METHOD__);
108
                Yii::$app->session->setFlash(self::SESSION_KEY_RESULT, self::RESULT_FAILED);
109
                Yii::$app->session->setFlash(self::SESSION_KEY_MESSAGE, $this->organizationSetUpFailedMessage);
110
            }
111
        }
112
        return $this->render('set-up-organization', ['model' => $model]);
113
    }
114
115
    /**
116
     * Set up department.
117
     * @param string $parent Parent organization or department ID.
118
     * @return string the rendering result.
119
     */
120
    public function actionSetUpDepartment($parent)
121
    {
122
        $model = new SetUpForm(['user' => Yii::$app->user->identity, 'parent' => $parent]);
123
        if ($model->load(Yii::$app->request->post())) {
124
            try {
125
                if (($result = $model->setUpDepartment()) === true) {
126
                    Yii::$app->session->setFlash(self::SESSION_KEY_RESULT, self::RESULT_SUCCESS);
127
                    Yii::$app->session->setFlash(self::SESSION_KEY_MESSAGE, $this->departmentSetUpSuccessMessage);
128
                    return $this->redirect(['index']);
129
                }
130
                if ($result instanceof \Exception) {
131
                    throw $result;
132
                }
133
            } catch (\Exception $ex) {
134
                Yii::error($ex->getMessage(), __METHOD__);
135
                Yii::$app->session->setFlash(self::SESSION_KEY_RESULT, self::RESULT_FAILED);
136
                Yii::$app->session->setFlash(self::SESSION_KEY_MESSAGE, $this->departmentSetUpFailedMessage);
137
            }
138
        }
139
        return $this->render('set-up-organization', ['model' => $model]);
140
    }
141
}
142