kwork/controllers/AccountThresholdController.php

91 lines
2.8 KiB
PHP

<?php
namespace app\controllers;
use Yii;
use app\models\Account;
use app\models\AccountThreshold;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\web\Response;
use yii\widgets\ActiveForm;
use yii\helpers\Url;
class AccountThresholdController extends Controller
{
public function actionIndex($account_id)
{
$account = $this->findAccount($account_id);
return $this->render('index', [
'account' => $account,
]);
}
public function actionCreate($account_id)
{
$account = $this->findAccount($account_id);
$model = new AccountThreshold(['account_id' => $account->id]);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
$account->refresh();
$account->checkAndSendNotifications();
Yii::$app->session->setFlash('success', 'Порог добавлен');
return $this->redirect(['index', 'account_id' => $account->id]);
}
return $this->render('create', ['model' => $model]);
}
public function actionUpdate($account_id, $id)
{
$account = $this->findAccount($account_id);
$threshold = $this->findThreshold($id);
if ($threshold->account_id != $account->id) {
throw new NotFoundHttpException('Данный порог не принадлежит аккаунту');
}
if ($threshold->load(Yii::$app->request->post()) && $threshold->save()) {
$account->refresh();
$account->checkAndSendNotifications();
Yii::$app->session->setFlash('success', 'Порог обновлен');
return $this->redirect(['index', 'account_id' => $account->id]);
}
return $this->render('update', ['model' => $threshold]);
}
public function actionDelete($account_id, $id)
{
$account = $this->findAccount($account_id);
$threshold = $this->findThreshold($id);
if ($threshold->account_id != $account->id) {
throw new NotFoundHttpException('Данный порог не принадлежит аккаунту');
}
$threshold->delete();
$account->refresh();
$account->checkAndSendNotifications();
Yii::$app->session->setFlash('success', 'Порог удален');
return $this->redirect(['index', 'account_id' => $account->id]);
}
// Вспомогательные методы
protected function findAccount($id)
{
if (($model = Account::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException('Аккаунт не найден');
}
protected function findThreshold($id)
{
if (($model = AccountThreshold::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException('Порог не найден');
}
}