Advertisement
Masyar_Syauqi

KaryawanController

Dec 23rd, 2015
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.27 KB | None | 0 0
  1. <?php
  2.  
  3. namespace backend\controllers;
  4.  
  5. use Yii;
  6. use app\models\Karyawan;
  7. use app\models\Keluarga;
  8. use backend\models\Model;
  9. use backend\models\Pendidikan;
  10. use backend\models\KaryawanSearch;
  11. use yii\web\Controller;
  12. use yii\web\NotFoundHttpException;
  13. use yii\filters\VerbFilter;
  14. use yii\helpers\ArrayHelper;
  15.  
  16. /**
  17.  * KaryawanController implements the CRUD actions for Karyawan model.
  18.  */
  19. class KaryawanController extends Controller
  20. {
  21.     public function behaviors()
  22.     {
  23.         return [
  24.             'verbs' => [
  25.                 'class' => VerbFilter::className(),
  26.                 'actions' => [
  27.                     'delete' => ['post'],
  28.                 ],
  29.             ],
  30.         ];
  31.     }
  32.  
  33.     /**
  34.      * Lists all Karyawan models.
  35.      * @return mixed
  36.      */
  37.     public function actionIndex()
  38.     {
  39.         $searchModel = new KaryawanSearch();
  40.         $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
  41.  
  42.         return $this->render('index', [
  43.             'searchModel' => $searchModel,
  44.             'dataProvider' => $dataProvider,
  45.         ]);
  46.     }
  47.  
  48.     /**
  49.      * Displays a single Karyawan model.
  50.      * @param integer $id
  51.      * @return mixed
  52.      */
  53.     public function actionView($id)
  54.     {
  55.         return $this->render('view', [
  56.             'model' => $this->findModel($id),
  57.         ]);
  58.     }
  59.  
  60.     /**
  61.      * Creates a new Karyawan model.
  62.      * If creation is successful, the browser will be redirected to the 'view' page.
  63.      * @return mixed
  64.      */
  65.     /**public function actionCreate()
  66.     {
  67.         $model = new Karyawan();
  68.         $modelsKeluarga = [new Keluarga];
  69.  
  70.         if ($model->load(Yii::$app->request->post()) && $model->save()) {
  71.             return $this->redirect(['view', 'id' => $model->id_karyawan]);
  72.         } else {
  73.             return $this->render('create', [
  74.                 'model' => $model,
  75.  
  76.             ]);
  77.         }
  78.     }**/
  79.  
  80.     public function actionCreate()
  81.     {
  82.         $model = new Karyawan;
  83.         $modelsKeluarga = [new Keluarga];
  84.         $modelsPendidikan = [new Pendidikan];
  85.         if ($model->load(Yii::$app->request->post())) {
  86.  
  87.             $modelsKeluarga = Model::createMultiple(Keluarga::classname());
  88.             Model::loadMultiple($modelsKeluarga, Yii::$app->request->post());
  89.             $modelsPendidikan = Model::createMultiple(Pendidikan::classname());
  90.             Model::loadMultiple($modelsPendidikan, Yii::$app->request->post());
  91.  
  92.             // ajax validation
  93.           /*  if (Yii::$app->request->isAjax) {
  94.                 Yii::$app->response->format = Response::FORMAT_JSON;
  95.                 return ArrayHelper::merge(
  96.                     ActiveForm::validateMultiple($modelsKeluarga),
  97.                     ActiveForm::validate($model)
  98.                 );
  99.             }*/
  100.  
  101.             // validate all models
  102.             $valid = $model->validate();
  103.             $valid = Model::validateMultiple($modelsKeluarga) && $valid;
  104.             $valid = Model::validateMultiple($modelsPendidikan) && $valid;
  105.  
  106.             if ($valid) {
  107.                 $transaction = \Yii::$app->db->beginTransaction();
  108.                 try {
  109.                     if ($flag = $model->save(false)) {
  110.                         foreach ($modelsKeluarga as $modelKeluarga) {
  111.                             $modelKeluarga->nik = $model->nik;
  112.                             if (! ($flag = $modelKeluarga->save(false))) {
  113.                                 $transaction->rollBack();
  114.                                 break;
  115.                             }
  116.                         }
  117.                         foreach ($modelsPendidikan as $modelPendidikan) {
  118.                             $modelPendidikan->nik = $model->nik;
  119.                             if (! ($flag = $modelPendidikan->save(false))) {
  120.                                 $transaction->rollBack();
  121.                                 break;
  122.                             }
  123.                         }
  124.                     }
  125.                     if ($flag) {
  126.                         $transaction->commit();
  127.                         return $this->redirect(['view', 'id' => $model->id_karyawan]);
  128.                     }
  129.                 } catch (Exception $e) {
  130.                     $transaction->rollBack();
  131.                 }
  132.             }
  133.         }
  134.  
  135.         return $this->render('create', [
  136.             'model' => $model,
  137.             'modelsKeluarga' => (empty($modelsKeluarga)) ? [new Keluarga] : $modelsKeluarga,
  138.             'modelsPendidikan' => (empty($modelsPendidikan)) ? [new Pendidikan] : $modelsPendidikan
  139.              
  140.         ]);
  141.     }
  142.  
  143.     /**
  144.      * Updates an existing Karyawan model.
  145.      * If update is successful, the browser will be redirected to the 'view' page.
  146.      * @param integer $id
  147.      * @return mixed
  148.      */
  149.     /*public function actionUpdate($id)
  150.     {
  151.         $model = $this->findModel($id);
  152.  
  153.         if ($model->load(Yii::$app->request->post()) && $model->save()) {
  154.             return $this->redirect(['view', 'id' => $model->id_karyawan]);
  155.         } else {
  156.             return $this->render('update', [
  157.                 'model' => $model,
  158.             ]);
  159.         }
  160.     }*/
  161.  
  162.     public function actionUpdate($id)
  163.     {
  164.         $model = $this->findModel($id);
  165.         //$modelsKeluarga = $model->nik;
  166.         $modelsKeluarga = $model->nik0;
  167.  
  168.         if ($model->load(Yii::$app->request->post())) {
  169.  
  170.             $oldIDs = ArrayHelper::map($modelsKeluarga, 'nik', 'nik');
  171.             $modelsKeluarga = Model::createMultiple(Keluarga::classname(), $modelsKeluarga);
  172.             Model::loadMultiple($modelsKeluarga, Yii::$app->request->post());
  173.             $deletedIDs = array_diff($oldIDs, array_filter(ArrayHelper::map($modelsKeluarga, 'nik', 'nik')));
  174.  
  175.             /*// ajax validation
  176.             if (Yii::$app->request->isAjax) {
  177.                 Yii::$app->response->format = Response::FORMAT_JSON;
  178.                 return ArrayHelper::merge(
  179.                     ActiveForm::validateMultiple($modelsKeluarga),
  180.                     ActiveForm::validate($model)
  181.                 );
  182.             }*/
  183.  
  184.             // validate all models
  185.             $valid = $model->validate();
  186.             $valid = Model::validateMultiple($modelsKeluarga) && $valid;
  187.  
  188.             if ($valid) {
  189.                 $transaction = \Yii::$app->db->beginTransaction();
  190.                 try {
  191.                     if ($flag = $model->save(false)) {
  192.                         if (! empty($deletedIDs)) {
  193.                             Keluarga::deleteAll(['nik' => $deletedIDs]);
  194.                         }
  195.                         foreach ($modelsKeluarga as $modelKeluarga) {
  196.                             $modelKeluarga->nik = $model->nik;
  197.                             if (! ($flag = $modelKeluarga->save(false))) {
  198.                                 $transaction->rollBack();
  199.                                 break;
  200.                             }
  201.                         }
  202.                     }
  203.                     if ($flag) {
  204.                         $transaction->commit();
  205.                         return $this->redirect(['view', 'id' => $model->id_karyawan]);
  206.                     }
  207.                 } catch (Exception $e) {
  208.                     $transaction->rollBack();
  209.                 }
  210.             }
  211.         }
  212.  
  213.         return $this->render('update', [
  214.             'model' => $model,
  215.             'modelsKeluarga' => (empty($modelsKeluarga)) ? [new Keluarga] : $modelsKeluarga
  216.         ]);
  217.  
  218.     }
  219.  
  220.     /**
  221.      * Deletes an existing Karyawan model.
  222.      * If deletion is successful, the browser will be redirected to the 'index' page.
  223.      * @param integer $id
  224.      * @return mixed
  225.      */
  226.     public function actionDelete($id)
  227.     {
  228.         $this->findModel($id)->delete();
  229.  
  230.         return $this->redirect(['index']);
  231.     }
  232.  
  233.     /**
  234.      * Finds the Karyawan model based on its primary key value.
  235.      * If the model is not found, a 404 HTTP exception will be thrown.
  236.      * @param integer $id
  237.      * @return Karyawan the loaded model
  238.      * @throws NotFoundHttpException if the model cannot be found
  239.      */
  240.     protected function findModel($id)
  241.     {
  242.         if (($model = Karyawan::findOne($id)) !== null) {
  243.             return $model;
  244.         } else {
  245.             throw new NotFoundHttpException('The requested page does not exist.');
  246.         }
  247.     }
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement