yii2-test-1/backend/modules/profile/controllers/ProfileController.php

167 lines
4.6 KiB
PHP
Raw Permalink Normal View History

2023-05-06 20:40:02 +03:00
<?php
namespace app\modules\profile\controllers;
use app\modules\profile\models\ProfileSearch;
use common\models\Profile;
use common\services\UserService;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
/**
* ProfileController implements the CRUD actions for Profile model.
*/
class ProfileController extends Controller
{
/**
* @inheritDoc
*/
public function behaviors()
{
return array_merge(
parent::behaviors(),
[
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
'access' => [
'class' => AccessControl::class,
'rules' => [
[
'actions' => ['login', 'error'],
'allow' => true,
],
[
'allow' => true,
'roles' => ['@'],
],
],
],
]
);
}
/**
* Lists all Profile models.
*
* @return string
*/
public function actionIndex()
{
$searchModel = new ProfileSearch();
$dataProvider = $searchModel->search($this->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single Profile model.
* @param int $id ID
* @return string
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new Profile model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return string|\yii\web\Response
*/
public function actionCreate()
{
$model = new Profile();
$service = UserService::run();
$texts = $service->newTexts();
$users = $service->getAllUsers();
if ($this->request->isPost) {
if ($service->saveProfile($this->request->post(), $model)) {
$service->saveTexts($this->request->post('Text'), $model->id, $texts);
return $this->redirect(['view', 'id' => $model->id]);
}
}
return $this->render('create', [
'model' => $model,
'users' => $users,
'texts' => $texts
]);
}
/**
* Updates an existing Profile model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param int $id ID
* @return string|\yii\web\Response
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
$service = UserService::run();
$texts = $service->getTexts($id);
$users = $service->getAllUsers();
if ($this->request->isPost) {
if ($service->saveProfile($this->request->post(), $model)) {
$service->saveTexts($this->request->post('Text'), $model->id, $texts);
return $this->redirect(['view', 'id' => $model->id]);
}
}
return $this->render('update', [
'model' => $model,
'users' => $users,
'texts' => $texts
]);
}
/**
* Deletes an existing Profile model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param int $id ID
* @return \yii\web\Response
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionDelete($id)
{
$model = $this->findModel($id);
foreach ($model->texts as $text) {
$text->delete();
}
$model->delete();
return $this->redirect(['index']);
}
/**
* Finds the Profile model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param int $id ID
* @return Profile the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Profile::findOne(['id' => $id])) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested page does not exist.');
}
}