Completed
Push — master ( 80fd4c...ece9a3 )
by Paweł
03:01
created

AccountController::actionSettings()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 2
nop 1
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace app\modules\admin\controllers;
4
5
use app\components\AccountManager;
6
use app\models\Tag;
7
use app\modules\admin\components\AccountStatsManager;
8
use app\modules\admin\controllers\actions\FavoriteAction;
9
use app\modules\admin\controllers\actions\MonitoringAction;
10
use app\modules\admin\models\Account;
11
use Yii;
12
use app\modules\admin\models\AccountSearch;
13
use yii\data\ActiveDataProvider;
14
use yii\db\Query;
15
use yii\helpers\Url;
16
use yii\web\Controller;
17
use yii\web\NotFoundHttpException;
18
use yii\filters\VerbFilter;
19
20
/**
21
 * AccountController implements the CRUD actions for Account model.
22
 */
23
class AccountController extends Controller
24
{
25
    /**
26
     * @inheritdoc
27
     */
28
    public function behaviors()
29
    {
30
        return [
31
            'verbs' => [
32
                'class' => VerbFilter::class,
33
                'actions' => [
34
                    'delete' => ['POST'],
35
                    'monitoring' => ['POST'],
36
                    'tags' => ['POST'],
37
                    'favorite' => ['POST'],
38
                ],
39
            ],
40
        ];
41
    }
42
43
    public function actions()
44
    {
45
        return [
46
            'favorite' => FavoriteAction::class,
47
            'monitoring' => MonitoringAction::class,
48
        ];
49
    }
50
51
    public function beforeAction($action)
52
    {
53
        if (parent::beforeAction($action)) {
54
            if (in_array($this->action->id, ['stats', 'media-tags', 'media-accounts'])) {
55
                Url::remember(Url::current());
56
            }
57
58
            return true;
59
        }
60
61
        return false;
62
    }
63
64
    public function actionSettings($id)
65
    {
66
        $model = $this->findModel($id);
67
        $model->setScenario(Account::SCENARIO_UPDATE);
68
69
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
70
            return $this->redirect(['account/dashboard', 'id' => $model->id]);
71
        }
72
73
        return $this->render('settings', [
74
            'model' => $model,
75
        ]);
76
    }
77
78
    public function actionTags($id)
79
    {
80
        $model = $this->findModel($id);
81
        $tags = Yii::$app->request->post('account_tags', []);
82
83
        $manager = Yii::createObject(AccountManager::class);
84
        $manager->updateTags($model, $tags);
85
86
        return $this->redirect(Url::previous());
87
    }
88
89
    /**
90
     * Lists all Account models.
91
     *
92
     * @return mixed
93
     */
94
    public function actionIndex()
95
    {
96
        $searchModel = new AccountSearch();
97
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
98
99
        return $this->render('index', [
100
            'searchModel' => $searchModel,
101
            'dataProvider' => $dataProvider,
102
        ]);
103
    }
104
105
    public function actionDashboard($id)
106
    {
107
        $model = $this->findModel($id);
108
109
        $manager = Yii::createObject([
110
            'class' => AccountStatsManager::class,
111
            'account' => $model,
112
        ]);
113
114
        return $this->render('dashboard', [
115
            'manager' => $manager,
116
            'model' => $model,
117
        ]);
118
    }
119
120
    public function actionStats($id)
121
    {
122
        $model = $this->findModel($id);
123
124
        $dataProvider = new ActiveDataProvider([
125
            'query' => $model->getAccountStats(),
126
            'sort' => [
127
                'defaultOrder' => ['created_at' => SORT_DESC],
128
            ],
129
        ]);
130
131
        return $this->render('stats', [
132
            'model' => $model,
133
            'dataProvider' => $dataProvider,
134
        ]);
135
    }
136
137
    public function actionMediaTags($id)
138
    {
139
        $model = $this->findModel($id);
140
141
        $dataProvider = new ActiveDataProvider([
142
            'query' => Tag::find()
143
                ->select([
144
                    'tag.*',
145
                    'count(tag.id) as occurs',
146
                ])
147
                ->innerJoinWith(['media' => function (Query $q) use ($model) {
148
                    $q->andWhere(['media.account_id' => $model->id]);
149
                }])
150
                ->groupBy('tag.id'),
151
        ]);
152
153
        $dataProvider->sort->attributes['occurs'] = [
154
            'asc' => ['occurs' => SORT_ASC],
155
            'desc' => ['occurs' => SORT_DESC],
156
        ];
157
        $dataProvider->sort->defaultOrder = [
158
            'occurs' => SORT_DESC,
159
            'name' => SORT_ASC,
160
        ];
161
162
163
        return $this->render('media-tags', [
164
            'model' => $model,
165
            'dataProvider' => $dataProvider,
166
        ]);
167
    }
168
169
    public function actionMediaAccounts($id)
170
    {
171
        $model = $this->findModel($id);
172
173
        $dataProvider = new ActiveDataProvider([
174
            'query' => Account::find()
175
                ->select([
176
                    'account.*',
177
                    'count(account.id) as occurs',
178
                ])
179
                ->innerJoinWith(['mediaAccounts.media' => function (Query $q) use ($model) {
180
                    $q->andWhere(['media.account_id' => $model->id]);
181
                }])
182
                ->groupBy('account.id'),
183
        ]);
184
185
        $dataProvider->sort->attributes['occurs'] = [
186
            'asc' => ['occurs' => SORT_ASC],
187
            'desc' => ['occurs' => SORT_DESC],
188
        ];
189
        $dataProvider->sort->defaultOrder = [
190
            'occurs' => SORT_DESC,
191
            'username' => SORT_ASC,
192
        ];
193
194
195
        return $this->render('media-accounts', [
196
            'model' => $model,
197
            'dataProvider' => $dataProvider,
198
        ]);
199
    }
200
201
    /**
202
     * Creates a new Account model.
203
     * If creation is successful, the browser will be redirected to the 'view' page.
204
     *
205
     * @return mixed
206
     */
207
    public function actionCreate()
208
    {
209
        $model = new Account();
210
211
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
212
            return $this->redirect(['account/dashboard', 'id' => $model->id]);
213
        } else {
214
            return $this->render('create', [
215
                'model' => $model,
216
            ]);
217
        }
218
    }
219
220
    /**
221
     * Updates an existing Account model.
222
     * If update is successful, the browser will be redirected to the 'view' page.
223
     *
224
     * @param integer $id
225
     * @return mixed
226
     */
227
    public function actionUpdate($id)
228
    {
229
        $model = $this->findModel($id);
230
231
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
232
            return $this->redirect(['account/dashboard', 'id' => $model->id]);
233
        } else {
234
            return $this->render('update', [
235
                'model' => $model,
236
            ]);
237
        }
238
    }
239
240
    /**
241
     * Deletes an existing Account model.
242
     * If deletion is successful, the browser will be redirected to the 'index' page.
243
     *
244
     * @param integer $id
245
     * @return mixed
246
     */
247
    public function actionDelete($id)
248
    {
249
        $this->findModel($id)->delete();
250
251
        return $this->redirect(['index']);
252
    }
253
254
    /**
255
     * Finds the Account model based on its primary key value.
256
     * If the model is not found, a 404 HTTP exception will be thrown.
257
     *
258
     * @param integer $id
259
     * @return Account the loaded model
260
     * @throws NotFoundHttpException if the model cannot be found
261
     */
262
    public function findModel($id)
263
    {
264
        if (($model = Account::findOne($id)) !== null) {
265
            return $model;
266
        } else {
267
            throw new NotFoundHttpException('The requested page does not exist.');
268
        }
269
    }
270
}
271