update Schedule full CRUD
This commit is contained in:
157
controllers/ScheduleController.php
Normal file
157
controllers/ScheduleController.php
Normal file
@@ -0,0 +1,157 @@
|
||||
<?php
|
||||
|
||||
namespace app\controllers;
|
||||
|
||||
use Yii;
|
||||
use app\models\Schedule;
|
||||
use app\models\ScheduleSearch;
|
||||
use yii\web\Controller;
|
||||
use yii\web\NotFoundHttpException;
|
||||
use yii\filters\VerbFilter;
|
||||
use yii\helpers\Html;
|
||||
use yii\helpers\Url;
|
||||
|
||||
/**
|
||||
* ScheduleController implements the CRUD actions for Schedule model.
|
||||
*/
|
||||
class ScheduleController extends Controller {
|
||||
|
||||
public function init() {
|
||||
parent::init();
|
||||
if (Yii::$app->user->isGuest)
|
||||
return $this->redirect(['/site/login']);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function behaviors() {
|
||||
return [
|
||||
'verbs' => [
|
||||
'class' => VerbFilter::className(),
|
||||
'actions' => [
|
||||
'delete' => ['POST'],
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public function actionIndex() {
|
||||
$this->view->title = 'Lịch trình';
|
||||
$searchModel = new ScheduleSearch();
|
||||
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
|
||||
|
||||
return $this->render('index', [
|
||||
'searchModel' => $searchModel,
|
||||
'dataProvider' => $dataProvider,
|
||||
]);
|
||||
}
|
||||
|
||||
public function actionCreate() {
|
||||
$model = new Schedule();
|
||||
Yii::$app->response->format = "json";
|
||||
if (Yii::$app->request->post()) {
|
||||
$post = Yii::$app->request->post();
|
||||
$data = [
|
||||
"Name" => $post["Name"],
|
||||
"Description" => $post["Description"],
|
||||
"SunTime1" => Schedule::combineTime($post["FromSun1"], $post["ToSun1"]),
|
||||
"SunTime2" => Schedule::combineTime($post["FromSun2"], $post["ToSun2"]),
|
||||
"SunTime3" => Schedule::combineTime($post["FromSun3"], $post["ToSun3"]),
|
||||
"MonTime1" => Schedule::combineTime($post["FromMon1"], $post["ToMon1"]),
|
||||
"MonTime2" => Schedule::combineTime($post["FromMon2"], $post["ToMon2"]),
|
||||
"MonTime3" => Schedule::combineTime($post["FromMon3"], $post["ToMon3"]),
|
||||
"TueTime1" => Schedule::combineTime($post["FromTue1"], $post["ToTue1"]),
|
||||
"TueTime2" => Schedule::combineTime($post["FromTue2"], $post["ToTue2"]),
|
||||
"TueTime3" => Schedule::combineTime($post["FromTue3"], $post["ToTue3"]),
|
||||
"WedTime1" => Schedule::combineTime($post["FromWed1"], $post["ToWed1"]),
|
||||
"WedTime2" => Schedule::combineTime($post["FromWed2"], $post["ToWed2"]),
|
||||
"WedTime3" => Schedule::combineTime($post["FromWed3"], $post["ToWed3"]),
|
||||
"ThuTime1" => Schedule::combineTime($post["FromThu1"], $post["ToThu1"]),
|
||||
"ThuTime2" => Schedule::combineTime($post["FromThu2"], $post["ToThu2"]),
|
||||
"ThuTime3" => Schedule::combineTime($post["FromThu3"], $post["ToThu3"]),
|
||||
"FriTime1" => Schedule::combineTime($post["FromFri1"], $post["ToFri1"]),
|
||||
"FriTime2" => Schedule::combineTime($post["FromFri2"], $post["ToFri2"]),
|
||||
"FriTime3" => Schedule::combineTime($post["FromFri3"], $post["ToFri3"]),
|
||||
"SatTime1" => Schedule::combineTime($post["FromSat1"], $post["ToSat1"]),
|
||||
"SatTime2" => Schedule::combineTime($post["FromSat2"], $post["ToSat2"]),
|
||||
"SatTime3" => Schedule::combineTime($post["FromSat3"], $post["ToSat3"])
|
||||
];
|
||||
if ($model->create($data)) {
|
||||
return ["status" => true];
|
||||
} else
|
||||
return ["status" => false, "type" => "error"];
|
||||
} else {
|
||||
return [
|
||||
"title" => Html::tag("i", "", ["class" => "fa fa-plus-square"]) . " Thêm",
|
||||
"form" => $this->renderPartial("form", [
|
||||
"model" => $model,
|
||||
"url" => Url::to(["create"]),
|
||||
"dayOfWeek" => Schedule::$dayOfWeek
|
||||
])
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
public function actionUpdate($id) {
|
||||
$model = $this->findModel($id);
|
||||
Yii::$app->response->format = "json";
|
||||
if (Yii::$app->request->post()) {
|
||||
$post = Yii::$app->request->post();
|
||||
$model->name = $post["Name"];
|
||||
$model->description = $post["Description"];
|
||||
$model->SunTime1 = Schedule::combineTime($post["FromSun1"], $post["ToSun1"]);
|
||||
$model->SunTime2 = Schedule::combineTime($post["FromSun2"], $post["ToSun2"]);
|
||||
$model->SunTime3 = Schedule::combineTime($post["FromSun3"], $post["ToSun3"]);
|
||||
$model->MonTime1 = Schedule::combineTime($post["FromMon1"], $post["ToMon1"]);
|
||||
$model->MonTime2 = Schedule::combineTime($post["FromMon2"], $post["ToMon2"]);
|
||||
$model->MonTime3 = Schedule::combineTime($post["FromMon3"], $post["ToMon3"]);
|
||||
$model->TueTime1 = Schedule::combineTime($post["FromTue1"], $post["ToTue1"]);
|
||||
$model->TueTime2 = Schedule::combineTime($post["FromTue2"], $post["ToTue2"]);
|
||||
$model->TueTime3 = Schedule::combineTime($post["FromTue3"], $post["ToTue3"]);
|
||||
$model->WedTime1 = Schedule::combineTime($post["FromWed1"], $post["ToWed1"]);
|
||||
$model->WedTime2 = Schedule::combineTime($post["FromWed2"], $post["ToWed2"]);
|
||||
$model->WedTime3 = Schedule::combineTime($post["FromWed3"], $post["ToWed3"]);
|
||||
$model->ThuTime1 = Schedule::combineTime($post["FromThu1"], $post["ToThu1"]);
|
||||
$model->ThuTime2 = Schedule::combineTime($post["FromThu2"], $post["ToThu2"]);
|
||||
$model->ThuTime3 = Schedule::combineTime($post["FromThu3"], $post["ToThu3"]);
|
||||
$model->FriTime1 = Schedule::combineTime($post["FromFri1"], $post["ToFri1"]);
|
||||
$model->FriTime2 = Schedule::combineTime($post["FromFri2"], $post["ToFri2"]);
|
||||
$model->FriTime3 = Schedule::combineTime($post["FromFri3"], $post["ToFri3"]);
|
||||
$model->SatTime1 = Schedule::combineTime($post["FromSat1"], $post["ToSat1"]);
|
||||
$model->SatTime2 = Schedule::combineTime($post["FromSat2"], $post["ToSat2"]);
|
||||
$model->SatTime3 = Schedule::combineTime($post["FromSat3"], $post["ToSat3"]);
|
||||
$model->modified_at = time();
|
||||
if ($model->save()) {
|
||||
return ["status" => true];
|
||||
} else
|
||||
return ["status" => false, "type" => "error"];
|
||||
} else {
|
||||
return [
|
||||
"title" => Html::tag("i", "", ["class" => "fa fa-edit"]) . " Tùy chỉnh",
|
||||
"form" => $this->renderPartial("form", [
|
||||
"model" => $model,
|
||||
"url" => Url::to(["update", "id" => $id]),
|
||||
"dayOfWeek" => Schedule::$dayOfWeek
|
||||
])
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
public function actionDelete() {
|
||||
if (Yii::$app->request->post()) {
|
||||
$lists = Yii::$app->request->post("lists");
|
||||
Schedule::deleteAll(["IN", "id", $lists]);
|
||||
\app\models\Staff::updateAll(["schedule_id" => 0, ["IN", "id", $lists]]);
|
||||
}
|
||||
}
|
||||
|
||||
protected function findModel($id) {
|
||||
if (($model = Schedule::findOne($id)) !== null) {
|
||||
return $model;
|
||||
}
|
||||
|
||||
throw new NotFoundHttpException('The requested page does not exist.');
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user