0% found this document useful (0 votes)
17 views

0 DataHalteController - Php.rev

This document defines a DataHalteController class that implements CRUD actions for managing DataHalte models. The controller handles indexing, viewing, creating, updating, and deleting DataHalte records. It also defines methods to find related DataKib and Filelampiran models associated with each DataHalte. The controller behavior differs based on whether the current user role is 'admin' or 'user'.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

0 DataHalteController - Php.rev

This document defines a DataHalteController class that implements CRUD actions for managing DataHalte models. The controller handles indexing, viewing, creating, updating, and deleting DataHalte records. It also defines methods to find related DataKib and Filelampiran models associated with each DataHalte. The controller behavior differs based on whether the current user role is 'admin' or 'user'.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

<?

php

namespace app\controllers;

use Yii;
use app\models\DataHalte;
use app\models\DataKib;
use app\models\Filelampiran;
use app\models\search\DataHalteSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
* DataHalteController implements the CRUD actions for DataHalte model.
*/
if (@Yii::$app->user->identity->role == 'admin') {
class DataHalteController extends Controller
{
/**
* {@inheritdoc}
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}

/**
* Lists all DataHalte models.
* @return mixed
*/
public function actionIndex()
{
$searchModel = new DataHalteSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);

return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}

/**
* Displays a single DataHalte 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 DataHalte model.
* If creation is successful, the browser will be redirected to the 'view'
page.
* @return mixed
*/
public function actionCreate()
{
$model = new DataHalte();
$model2 = new DataKib();
$kode = DataHalte::getKodhalte();
$kodekib = DataKib::getKodekib();

if ($model->load(Yii::$app->request->post())) {
$model2->id_halte = $kode;
$model2->alamat = $model->lokasi_alamat;
$model2->id_kib = $kodekib;
$model2->guid = $_POST['DataKib']['guid'];
$model2->kolok = $_POST['DataKib']['kolok'];
$model2->kobar = $_POST['DataKib']['kobar'];
$model2->noreg = $_POST['DataKib']['noreg'];
$model2->harga = $_POST['DataKib']['harga'];
$model2->total = $_POST['DataKib']['total'];
$model2->status = $_POST['DataKib']['status'];
$model2->user = Yii::$app->user->identity->user;
$model->user = Yii::$app->user->identity->user;
// echo "<pre>";
// print_r($model->attributes);
// print_r($model2->attributes);
// print_r($_POST);
// exit;
if($model->save() && $model2->save()){
Yii::$app->session->setFlash('success', "Data Berhasil
disimpan");
return $this->redirect(['view', 'id' => $model->id_halte]);
}else{
Yii::$app->session->setFlash('danger', "Data Gagal disimpan");
}
}
return $this->render('create', [
'model' => $model,
'model2' => $model2,
'kode' => $kode,
'kodekib'=>$kodekib
]);
}
/**
* Updates an existing DataHalte 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);
$datakib = DataKib::find()->where(['id_halte'=>$id])->one();
$model2 = $this->findModel2($datakib['id_kib']);
$kode = $id;
$kodekib = $datakib['id_kib'];

if ($model->load(Yii::$app->request->post())) {
$model2->id_halte = $kode;
$model2->alamat = $model->lokasi_alamat;
$model2->id_kib = $kodekib;
$model2->guid = $_POST['DataKib']['guid'];
$model2->kolok = $_POST['DataKib']['kolok'];
$model2->kobar = $_POST['DataKib']['kobar'];
$model2->noreg = $_POST['DataKib']['noreg'];
$model2->harga = $_POST['DataKib']['harga'];
$model2->total = $_POST['DataKib']['total'];
$model2->status = $_POST['DataKib']['status'];
// echo "<pre>";
// print_r($model->attributes);
// print_r($model2->attributes);
// print_r($_POST);
// exit;
if($model->save()){
$model2->save();
Yii::$app->session->setFlash('success', "Data Berhasil
disimpan");
return $this->redirect(['view', 'id' => $model->id_halte]);
}else{
Yii::$app->session->setFlash('danger', "Data Gagal disimpan");
}
}
return $this->render('update', [
'model' => $model,
'model2' => $model2,
'kode' => $kode
]);
}

/**
* Deletes an existing DataHalte 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)
{
if($this->findModel($id)->delete()){
$datakib = DataKib::find()->where(['id_halte'=>$id])->one();
$this->findModel2($datakib['id_kib'])->delete();
$datafile = Filelampiran::find()->where(['id_asset'=>$id]);
if($datafile->count() >0 ){
$datafile = $datafile->all();
foreach($datafile as $data){
$this->findModel3($data['id_filelampiran'])->delete();
}
}
Yii::$app->session->setFlash('success', "Data Berhasil
Dihapuskan.");
return $this->redirect(['index']);
}
}

/**
* Finds the DataHalte model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return DataHalte the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = DataHalte::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested page does not exist.');
}

protected function findModel2($id)


{
if (($model = DataKib::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested page does not exist.');
}

protected function findModel3($id)


{
if (($model = Filelampiran::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested page does not exist.');
}

public function actionViewguest($id)


{
$this->layout = "main2";
return $this->render('view_guest', [
'model' => $this->findModel($id),
]);
}
}
}else if (@Yii::$app->user->identity->role == 'user') {
class DataHalteController extends Controller
{
/**
* {@inheritdoc}
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}

public function actionDelete($id)


{
if($this->findModel($id)->delete()){
$datakib = DataKib::find()->where(['id_halte'=>$id])->one();
$this->findModel2($datakib['id_kib'])->delete();
$datafile = Filelampiran::find()->where(['id_asset'=>$id]);
if($datafile->count() >0 ){
$datafile = $datafile->all();
foreach($datafile as $data){
$this->findModel3($data['id_filelampiran'])->delete();
}
}
Yii::$app->session->setFlash('success', "Data Berhasil
Dihapuskan.");
return $this->redirect(['index']);
}
}
protected function findModel($id)
{
if (($model = DataHalte::findOne(['id_halte'=>$id,'user'=>@Yii::$app-
>user->identity->user])) !== null) {
return $model;
}

throw new NotFoundHttpException('The requested page does not exist.');


}

public function actionIndex()


{
$this->layout = "main2";
$searchModel = new DataHalteSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);

return $this->render('index_guest', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
public function actionView($id)
{
$this->layout = "main2";
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
public function actionCreate()
{
$this->layout = "main2";
$model = new DataHalte();
$model2 = new DataKib();
$kode = DataHalte::getKodhalte();
$kodekib = DataKib::getKodekib();

if ($model->load(Yii::$app->request->post())) {
$model2->id_halte = $kode;
$model2->alamat = $model->lokasi_alamat;
$model2->id_kib = $kodekib;
$model2->guid = $_POST['DataKib']['guid'];
$model2->kolok = $_POST['DataKib']['kolok'];
$model2->kobar = $_POST['DataKib']['kobar'];
$model2->noreg = $_POST['DataKib']['noreg'];
$model2->harga = $_POST['DataKib']['harga'];
$model2->total = $_POST['DataKib']['total'];
$model2->status = $_POST['DataKib']['status'];
$model2->user = Yii::$app->user->identity->user;
$model->user = Yii::$app->user->identity->user;

if($model->save() && $model2->save()){


Yii::$app->session->setFlash('success', "Data Berhasil
disimpan");
return $this->redirect(['viewguest', 'id' => $model-
>id_halte]);
}else{
Yii::$app->session->setFlash('danger', "Data Gagal disimpan");
}
}
return $this->render('create_guest', [
'model' => $model,
'model2' => $model2,
'kode' => $kode,
'kodekib'=>$kodekib
]);
}

public function actionUpdate($id)


{
$this->layout = "main2";
$model = $this->findModel($id);
$datakib = DataKib::find()->where(['id_halte'=>$id])->one();
$model2 = $this->findModel2($datakib['id_kib']);
$kode = $id;
$kodekib = $datakib['id_kib'];

if ($model->load(Yii::$app->request->post())) {
$model2->id_halte = $kode;
$model2->alamat = $model->lokasi_alamat;
$model2->id_kib = $kodekib;
$model2->guid = $_POST['DataKib']['guid'];
$model2->kolok = $_POST['DataKib']['kolok'];
$model2->kobar = $_POST['DataKib']['kobar'];
$model2->noreg = $_POST['DataKib']['noreg'];
$model2->harga = $_POST['DataKib']['harga'];
$model2->total = $_POST['DataKib']['total'];
$model2->status = $_POST['DataKib']['status'];
// echo "<pre>";
// print_r($model->attributes);
// print_r($model2->attributes);
// print_r($_POST);
// exit;
if($model->save()){
$model2->save();
Yii::$app->session->setFlash('success', "Data Berhasil
disimpan");
return $this->redirect(['view', 'id' => $model->id_halte]);
}else{
Yii::$app->session->setFlash('danger', "Data Gagal disimpan");
}
}
return $this->render('updateguest', [
'model' => $model,
'model2' => $model2,
'kode' => $kode
]);
}

public function actionViewguest($id)


{
$this->layout = "main2";
return $this->render('view_guest', [
'model' => $this->findModel($id),
]);
}

protected function findModel2($id)


{
if (($model = DataKib::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested page does not exist.');
}

protected function findModel3($id)


{
if (($model = Filelampiran::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested page does not exist.');
}
}
} else{
header("Location: ../");
die();
}

You might also like