HEX
Server: Apache
System: Linux server3230.server-vps.com 5.15.0-46-generic #49-Ubuntu SMP Thu Aug 4 18:03:25 UTC 2022 x86_64
User: lzgqnjwu (1002)
PHP: 7.4.33
Disabled: NONE
Upload Files
File: //proc/1126/cwd/backend/controllers/UserController.php
<?php

namespace backend\controllers;

use backend\models\search\UserSearch;
use backend\models\UserForm;
use common\models\Bot;
use common\models\BotContactBooks;
use common\models\BotTasks;
use common\models\SenderScedules;
use common\models\User;
use common\models\UserToken;
use Yii;
use yii\filters\VerbFilter;
use yii\helpers\ArrayHelper;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\data\ActiveDataProvider;

/**
 * UserController implements the CRUD actions for User model.
 */
class UserController extends Controller
{
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::class,
                'actions' => [
                    'delete' => ['post', 'get'],
                ],
            ],
        ];
    }

    /**
     * Lists all User models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new UserSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single User model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id)
        ]);
    }

    /**
     * @param $id
     * @return \yii\web\Response
     * @throws \yii\base\Exception
     * @throws NotFoundHttpException
     */
    public function actionLogin($id)
    {
        $model = $this->findModel($id);
        $tokenModel = UserToken::create(
            $model->getId(),
            UserToken::TYPE_LOGIN_PASS,
            60
        );

        return $this->redirect(
            Yii::$app->urlManagerFrontend->createAbsoluteUrl(['user/sign-in/login-by-pass', 'token' => $tokenModel->token])
        );
    }

    /**
     * Creates a new User model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new UserForm();
        $model->setScenario('create');
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['index']);
        }

        return $this->render('create', [
            'model' => $model,
            'roles' => ArrayHelper::map(Yii::$app->authManager->getRoles(), 'name', 'name'),
            'id' => null,
            'wallet' => null,
        ]);
    }

    /**
     * Updates an existing User model.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = new UserForm();
        $model->setModel($this->findModel($id));
        $bot_list = Bot::find()->where(['user_id' => $id])->orderBy(['id' => SORT_ASC]) ?? [];
        $bot_provider = new ActiveDataProvider([
            'query' => $bot_list,
            'pagination' => [
                'pageSize' => 100,
            ],
            'sort' => [
                'defaultOrder' => [
                    'id' => SORT_DESC, //сортировка по нужному id
                ]
            ],
        ]);

        $book_list = BotContactBooks::find()->where(['user_id' => $id])->orderBy(['id' => SORT_ASC]) ?? [];
        $book_provider = new ActiveDataProvider([
            'query' => $book_list,
            'pagination' => [
                'pageSize' => 100,
            ],
            'sort' => [
                'defaultOrder' => [
                    'id' => SORT_DESC, //сортировка по нужному id
                ]
            ],
        ]);

        $bot_tasks = BotTasks::find()->where(['user_id' => $id])->orderBy(['id' => SORT_ASC]) ?? [];
        $tasks_provider = new ActiveDataProvider([
            'query' => $bot_tasks,
            'pagination' => [
                'pageSize' => 100,
            ],
            'sort' => [
                'defaultOrder' => [
                    'id' => SORT_DESC, //сортировка по нужному id
                ]
            ],
        ]);

        $bot_scedule = SenderScedules::find()->where(['user_id' => $id])->orderBy(['id' => SORT_DESC])->limit(50) ?? [];
        $scedule_provider = new ActiveDataProvider([
            'query' => $bot_scedule,
            'pagination' => [
                'pageSize' => 100,
            ],
            'sort' => [
                'defaultOrder' => [
                    'id' => SORT_DESC, //сортировка по нужному id
                ]
            ],
        ]);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['index']);
        }



//        $model = new UserForm();
//        $model->setModel($this->findModel($id));
//
//        if(Yii::$app->request->isPost){
//            $request = Yii::$app->request->post('UserForm');
//
//            if($request){
//                $upd_model = User::findOne($id);
//                if($upd_model){
////                    $upd_model->username = (string)$request['username'];
////                    $upd_model->email = (string)$request['email'];
//                    $upd_model->status = (string)$request['status'];
//                    $upd_model->blocked = (string)$request['blocked'];
//                    $upd_model->save();
//                    return $this->redirect('index');
//                }
//            }
//        }

        return $this->render('update', [
            'model' => $model,
            'roles' => ArrayHelper::map(Yii::$app->authManager->getRoles(), 'name', 'name'),
            'id' => $id,
            'user_id' => $id,
            'wallet' => null,
            'bot_provider' => $bot_provider,
            'scedule_provider' => $scedule_provider,
            'tasks_provider' => $tasks_provider,
            'book_provider' => $book_provider,
        ]);
    }

    /**
     * Deletes an existing User model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        Yii::$app->authManager->revokeAll($id);
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the User model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return User the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = User::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }

    public function actionCreatebot($user = null)
    {
        if($user){
            $bot = new Bot();
            $bot->user_id = (string)$user;
            $bot->bot_type = (string)1;
            $bot->name = (string)'New Bot';
            $bot->token = (string)'111222333';
            $bot->visible = (string)0;
            $bot->save();

            return $this->redirect(['user/update', 'id' => $user]);
        }
    }

    public function actionCreatecontactsbook($user = null)
    {
        $bot_id = 1;
        if($user){
            $default_bot_data = Bot::find()->where(['user_id' => $user])->orderBy(['id' => SORT_DESC])->one();
            if($default_bot_data){
                $bot_id = $default_bot_data->id;
            }
            $bot = new BotContactBooks();
            $bot->user_id = (string)$user;
            $bot->bot_id = (string)$bot_id;
            $bot->name = (string)'New Book';
            $bot->save();

            return $this->redirect(['user/update', 'id' => $user]);
        }
    }

    public function actionCreatetask($user = null)
    {
        $bot_id = 1;
        $book_id = 1;
        if($user){
            $default_bot_data = Bot::find()->where(['user_id' => $user])->orderBy(['id' => SORT_DESC])->one();
            if($default_bot_data){
                $bot_id = $default_bot_data->id;
            }
            $default_book_data = BotContactBooks::find()->where(['user_id' => $user])->orderBy(['id' => SORT_DESC])->one();
            if($default_book_data){
                $book_id = $default_book_data->id;
            }
            $bot = new BotTasks();
            $bot->user_id = (string)$user;
            $bot->bot_id = (string)$bot_id;
            $bot->task_name = (string)'New Task';
            $bot->bot_type = (string)1;
            $bot->bot_contacts_book_id = (string)$book_id;
            $bot->message = (string)'Test message';
            $bot->status = (string)0;
            $bot->save();

            return $this->redirect(['user/update', 'id' => $user]);
        }
    }

    public function actionUserbotupdate($id = null)
    {
        if($id){
            return $this->redirect(['/bot/update', 'id' => $id]);
        }
    }

    public function actionUserbotdelete($id = null)
    {
        if($id){
            return $this->redirect(['/bot/delete', 'id' => $id]);
        }
    }


    public function actionUserbookupdate($id = null)
    {
        if($id){
            return $this->redirect(['/botcontactbooks/update', 'id' => $id]);
        }
    }

    public function actionUserbookdelete($id = null)
    {
        if($id){
            return $this->redirect(['/botcontactbooks/delete', 'id' => $id]);
        }
    }

    public function actionUsertaskupdate($id = null)
    {
        if($id){
            return $this->redirect(['/bottasks/update', 'id' => $id]);
        }
    }

    public function actionUsertaskdelete($id = null)
    {
        if($id){
            return $this->redirect(['/bottasks/delete', 'id' => $id]);
        }
    }
}