Completed
Push — master ( 07eda8...8e2f95 )
by Paweł
02:52
created

AccountController::actionDeleteAssociated()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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