| Current Path : /var/www/html/akept/backend/modules/speaker/controllers/ |
| Current File : /var/www/html/akept/backend/modules/speaker/controllers/SpeakerController.php |
<?php
namespace backend\modules\speaker\controllers;
use Yii;
use backend\modules\speaker\models\Speaker;
use backend\modules\speaker\models\SpeakerSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\UploadedFile;
use yii\helpers\Url;
/**
* SpeakerController implements the CRUD actions for Speaker model.
*/
class SpeakerController extends Controller {
/**
* {@inheritdoc}
*/
public function behaviors() {
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}
/**
* Lists all Speaker models.
* @return mixed
*/
public function actionIndex() {
$searchModel = new SpeakerSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single Speaker model.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionView($id) {
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new Speaker model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate() {
$model = new Speaker();
$model->created_at = date("Y-m-d H:i:s");
$model->created_by = Yii::$app->user->identity->id;
$model->updated_at = date("Y-m-d H:i:s");
$model->updated_by = Yii::$app->user->identity->id;
if ($model->load(Yii::$app->request->post())) {
$model->speaker_img = UploadedFile::getInstance($model, 'speaker_img');
if (!empty($model->speaker_img)) {
$speaker_img = 'uploads/' . date("YmdHis") . '.' . $model->speaker_img->extension;
if ($model->speaker_img->saveAs('../' . $speaker_img))
$model->speaker_img = $speaker_img;
}
if ($model->save()) {
$this->getView()->registerJs("swal.fire({
title:'Berjaya',
icon: 'success',
text: 'Data telah disimpan',
}).then(function(result){
window.location = '" . Url::toRoute(['index']) . "';
});");
}
}
return $this->render('create', ['model' => $model,]);
}
/**
* Updates an existing Speaker model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionUpdate($id) {
$model = $this->findModel($id);
$model->updated_at = date("Y-m-d H:i:s");
$model->updated_by = Yii::$app->user->identity->id;
$original_speaker_img = $model->speaker_img;
if ($model->load(Yii::$app->request->post())) {
$model->speaker_img = UploadedFile::getInstance($model, 'speaker_img');
if (!empty($model->speaker_img)) {
$speaker_img = 'uploads/' . date("YmdHis") . '.' . $model->speaker_img->extension;
if ($model->speaker_img->saveAs('../' . $speaker_img))
$model->speaker_img = $speaker_img;
}else
$model->speaker_img = $original_speaker_img;
if ($model->save()) {
$this->getView()->registerJs("swal.fire({
title:'Berjaya',
icon: 'success',
text: 'Data telah disimpan',
}).then(function(result){
window.location = '" . Url::toRoute(['index']) . "';
});");
}
}
return $this->render('update', ['model' => $model,]);
}
/**
* Deletes an existing Speaker model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionDelete() {
$id = $_POST['id'];
$status = 'failed';
if ($this->findModel($id)->delete())
$status = 'success';
return $status;
}
/**
* Finds the Speaker model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return Speaker the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id) {
if (($model = Speaker::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested page does not exist.');
}
}