Commit 66d011d4 authored by Shakarim Sapa's avatar Shakarim Sapa

- Добавлен контроллер для подписок на кейсы;

parent 278ea0f7
...@@ -2,13 +2,25 @@ ...@@ -2,13 +2,25 @@
namespace common\modules\cases\controllers; namespace common\modules\cases\controllers;
use yii\web\Controller; use Yii;
use common\components\BaseController;
use common\modules\cases\models\CasesBids;
use yii\web\NotFoundHttpException;
use yii\web\Response;
use yii\widgets\ActiveForm;
/** /**
* Default controller for the `Cases` module * Default controller for the `Cases` module
*/ */
class DefaultController extends Controller class DefaultController extends BaseController
{ {
public static function actionsTitles()
{
return [
'Add' => 'Subscribe to blog'
];
}
/** /**
* Renders the index view for the module * Renders the index view for the module
* @return string * @return string
...@@ -17,4 +29,44 @@ class DefaultController extends Controller ...@@ -17,4 +29,44 @@ class DefaultController extends Controller
{ {
return $this->render('index'); return $this->render('index');
} }
/**
* @return array
* @throws NotFoundHttpException
* @throws \Exception
*/
public function actionAdd(){
Yii::$app->response->format = Response::FORMAT_JSON;
// Получили дату
$date = new \DateTime();
// Создали модель
$model = new CasesBids();
// Получили и переопределили данные из формы
if(Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
// Переопределили дату
$model->date = $date->format('Y-m-d H:i:s');
// Открыли транзакцию
$transaction = Yii::$app->db->beginTransaction();
// Пытаемся сохранить запись в базе
try {
// Если запись успешна
if($model->save()) {
// Коммитим транзакцию
$transaction->commit();
// Возвращаем результат
return ['success' => true];
} else {
// В противном случае возвращаем форму с ошибками
return ActiveForm::validate($model);
}
} catch (\Exception $e) {
// Обрабатываем исключение
$transaction->rollBack();
throw $e;
}
} else {
// Если это не ajax запрос, возвращаем ошибку
throw new NotFoundHttpException('The requested page does not exist.');
}
}
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment