Passed
Push — master ( fe727e...818769 )
by Paweł
02:40
created

MonitoringController::actionCreateAccount()   C

Complexity

Conditions 8
Paths 2

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 18
nc 2
nop 0
dl 0
loc 27
rs 5.3846
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created for IG Monitoring.
4
 * User: jakim <[email protected]>
5
 * Date: 19.01.2018
6
 */
7
8
namespace app\modules\admin\controllers;
9
10
11
use app\components\AccountManager;
12
use app\models\Favorite;
13
use app\models\Proxy;
14
use app\models\ProxyTag;
15
use app\modules\admin\models\Account;
16
use app\modules\admin\models\AccountMonitoringForm;
17
use app\modules\admin\models\AccountSearch;
18
use app\modules\admin\models\Tag;
19
use app\modules\admin\models\TagSearch;
20
use yii\filters\VerbFilter;
21
use yii\helpers\Inflector;
22
use yii\helpers\StringHelper;
23
use yii\web\Controller;
24
25
class MonitoringController extends Controller
26
{
27
    public function behaviors()
28
    {
29
        return [
30
            'verbs' => [
31
                'class' => VerbFilter::class,
32
                'actions' => [
33
                    'create-account' => ['POST'],
34
                ],
35
            ],
36
        ];
37
    }
38
39
    public function actionCreateAccount()
40
    {
41
        $form = new AccountMonitoringForm();
42
43
        if ($form->load(\Yii::$app->request->post()) && $form->validate()) {
44
            $usernames = StringHelper::explode($form->usernames, ',', true, true);
45
            foreach ($usernames as $username) {
46
                $account = Account::findOne(['username' => $username]);
47
                if ($account === null) {
48
                    $account = new Account(['username' => $username]);
49
                }
50
                $account->proxy_id = $form->proxy_id ?: null;
51
                $account->proxy_tag_id = $form->proxy_tag_id ?: null;
52
                $account->monitoring = 1;
53
                if ($account->save()) {
54
                    \Yii::$app->session->setFlash('success', 'OK!');
55
                    $accountManager = \Yii::createObject(AccountManager::class);
56
                    $accountManager->updateTags($account, (array)$form->tags);
57
                } else {
58
                    \Yii::error('Validation error: ' . json_encode($account->errors), __METHOD__);
59
                    \Yii::$app->session->setFlash('error', 'ERR!');
60
                }
61
62
            }
63
        }
64
65
        return $this->redirect(['monitoring/accounts', 'sort' => '-created_at']);
66
    }
67
68
    public function actionTags()
69
    {
70
        $searchModel = new TagSearch(['monitoring' => 1]);
71
        $dataProvider = $searchModel->search(\Yii::$app->request->queryParams);
72
73
        return $this->render('tags', [
74
            'searchModel' => $searchModel,
75
            'dataProvider' => $dataProvider,
76
        ]);
77
    }
78
79
    public function actionAccounts()
80
    {
81
82
        $label = \Yii::$app->request->post('label');
83
        $url = \Yii::$app->request->post('url');
84
        if ($label && $url) {
85
            (new Favorite([
86
                'url' => $url,
87
                'label' => "<span class='fa fa-search'></span> $label",
88
            ]))->insert(false);
89
        }
90
91
        $searchModel = new AccountSearch();
92
        $dataProvider = $searchModel->search(\Yii::$app->request->queryParams);
93
        $dataProvider->query->andWhere(['account.monitoring' => 1]);
94
95
        return $this->render('accounts', [
96
            'searchModel' => $searchModel,
97
            'dataProvider' => $dataProvider,
98
        ]);
99
    }
100
}