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.
 
 
 
 
 

92 lines
2.4 KiB

<?php
/**
* Created by Error202
* Date: 22.08.2017
*/
namespace frontend\controllers\account;
use yii\web\Controller;
use core\services\user\ProfileService;
use core\forms\user\ProfileEditForm;
use core\entities\user\User;
use yii\web\NotFoundHttpException;
use yii\filters\AccessControl;
use Yii;
class ProfileController extends Controller
{
public $layout = 'profile';
private $service;
public function __construct($id, $module, ProfileService $service, $config = [])
{
parent::__construct($id, $module, $config);
$this->service = $service;
}
public function behaviors(): array
{
return [
'access' => [
'class' => AccessControl::className(),
'only' => ['index'],
'rules' => [
[
'actions' => ['edit', 'social'],
'allow' => true,
'roles' => ['@'],
],
],
],
];
}
/**
* @return string|\yii\web\Response
* @throws NotFoundHttpException
*/
public function actionEdit()
{
$user = $this->findModel(Yii::$app->user->id);
$form = new ProfileEditForm($user);
if ($form->load(Yii::$app->request->post()) && $form->validate()) {
try {
$this->service->edit($user->id, $form);
Yii::$app->session->setFlash('success', Yii::t('user', 'Profile is saved.'));
return $this->redirect(['/account/profile/edit']);
} catch (\DomainException $e) {
Yii::$app->errorHandler->logException($e);
Yii::$app->session->setFlash('error', $e->getMessage());
}
}
return $this->render('edit', [
'model' => $form,
'user' => $user,
]);
}
public function actionSocial()
{
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.
* @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.');
}
}
}