frontend/modules/user/controllers/DefaultController.php
<?php
/*
* This file is part of the HomeFinanceV2 project.
*
* (c) Rafalsky.com <http://github.com/Rafalsky/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace frontend\modules\user\controllers;
use common\base\MultiModel;
use frontend\modules\user\models\AccountForm;
use Intervention\Image\ImageManagerStatic;
use trntv\filekit\actions\DeleteAction;
use trntv\filekit\actions\UploadAction;
use yii\filters\AccessControl;
use yii\web\Controller;
class DefaultController extends Controller
{
/**
* @return array
*/
public function actions()
{
return [
'avatar-upload' => [
'class' => UploadAction::className(),
'deleteRoute' => 'avatar-delete',
'on afterSave' => function($event) {
/* @var $file \League\Flysystem\File */
$file = $event->file;
$img = ImageManagerStatic::make($file->read())->fit(215, 215);
$file->put($img->encode());
}
],
'avatar-delete' => [
'class' => DeleteAction::className()
]
];
}
/**
* @return array
*/
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'allow' => true,
'roles' => ['@']
]
]
]
];
}
/**
* @return string|\yii\web\Response
* @throws \yii\base\InvalidParamException
*/
public function actionIndex()
{
$accountForm = new AccountForm();
$accountForm->setUser(\Yii::$app->user->identity);
$model = new MultiModel([
'models' => [
'account' => $accountForm,
'profile' => \Yii::$app->user->identity->userProfile
]
]);
if ($model->load(\Yii::$app->request->post()) && $model->save()) {
$locale = $model->getModel('profile')->locale;
\Yii::$app->session->setFlash('forceUpdateLocale');
\Yii::$app->session->setFlash('alert', [
'options' => ['class' => 'alert-success'],
'body' => \Yii::t('frontend', 'Your account has been successfully saved', [], $locale)
]);
return $this->refresh();
}
return $this->render('index', ['model' => $model]);
}
}