You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
2.6 KiB

7 years ago
<?php
/**
* Created by Error202
* Date: 22.08.2017
*/
namespace frontend\controllers\account;
3 years ago
use DomainException;
use frontend\components\FrontendController;
7 years ago
use core\services\user\ProfileService;
use core\forms\user\ProfileEditForm;
use core\entities\user\User;
3 years ago
use yii\base\Exception;
7 years ago
use yii\web\NotFoundHttpException;
use yii\filters\AccessControl;
use Yii;
3 years ago
use yii\web\Response;
7 years ago
class ProfileController extends FrontendController
7 years ago
{
public $layout = 'profile';
7 years ago
3 years ago
private ProfileService $service;
7 years ago
public function __construct($id, $module, ProfileService $service, $config = [])
{
parent::__construct($id, $module, $config);
3 years ago
$this->service = $service;
7 years ago
}
public function behaviors(): array
{
return [
'access' => [
'class' => AccessControl::class,
'only' => ['index'],
7 years ago
'rules' => [
[
'actions' => ['edit', 'social'],
'allow' => true,
'roles' => ['@'],
7 years ago
],
],
],
];
}
/**
3 years ago
* @return string|Response
* @throws NotFoundHttpException|Exception
*/
3 years ago
public function actionEdit(): Response|string
7 years ago
{
$user = $this->findModel(Yii::$app->user->id);
$form = new ProfileEditForm($user);
if ($form->load(Yii::$app->request->post()) && $form->validate()) {
try {
3 years ago
$this->service->edit($user->id, $form);
7 years ago
Yii::$app->session->setFlash('success', Yii::t('user', 'Profile is saved.'));
7 years ago
return $this->redirect(['/account/profile/edit']);
3 years ago
} catch (DomainException $e) {
7 years ago
Yii::$app->errorHandler->logException($e);
Yii::$app->session->setFlash('error', $e->getMessage());
}
}
7 years ago
return $this->render('edit', [
'model' => $form,
'user' => $user,
7 years ago
]);
}
3 years ago
public function actionSocial(): string
7 years ago
{
return $this->render('social');
}
/**
* Finds the User model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
*
7 years ago
* @param integer $id
*
7 years ago
* @return User the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
3 years ago
protected function findModel(int $id): User
7 years ago
{
if (($model = User::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}