/**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Suboutput::find();
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $this->load($params);
     if (!$this->validate()) {
         // uncomment the following line if you do not want to return any records when validation fails
         // $query->where('0=1');
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id, 'thang' => $this->thang, 'kdsatker' => $this->kdsatker, 'kddept' => $this->kddept, 'kdunit' => $this->kdunit, 'kdprogram' => $this->kdprogram, 'kdgiat' => $this->kdgiat, 'kdoutput' => $this->kdoutput, 'kdsoutput' => $this->kdsoutput, 'volsout' => $this->volsout]);
     $query->andFilterWhere(['like', 'ursoutput', $this->ursoutput]);
     return $dataProvider;
 }
 /**
  * Displays a single Output model.
  * @param integer $id
  * @return mixed
  */
 public function actionView($id)
 {
     $model = $this->findModel($id);
     /* List sub output */
     $dataProvider = new ActiveDataProvider(['query' => Suboutput::find()->where(['thang' => $model->thang, 'kdgiat' => $model->kdgiat, 'kdoutput' => $model->kdoutput, 'kdsatker' => $model->kdsatker])->orderBy('kdsoutput ASC'), 'pagination' => ['pageSize' => 20]]);
     /* Template for table */
     $template = [new Templatetable()];
     $model_form = new Formlaporan();
     /* Status button Form */
     $status_button_form = Formlaporan::findOne(['id_output' => $id]);
     if ($status_button_form) {
         $class_name_now = '\\kemdikbud\\to\\models\\' . $status_button_form->nama_class;
         $dataProviderLaporan = new ActiveDataProvider(['query' => $class_name_now::find(), 'pagination' => ['pageSize' => 10]]);
         $columns = json_decode($status_button_form->nama_kolom_json, true);
         $column_nama = [];
         foreach ($columns as $key => $value) {
             array_push($column_nama, $key);
         }
     }
     if (Yii::$app->request->post()) {
         if (!empty($_POST['Templatetable'])) {
             $template = Model::createMultiple(Templatetable::classname());
             Model::loadMultiple($template, Yii::$app->request->post());
             $string_sql = '';
             $nama_kolom = [];
             $rules = [];
             $rules_required = [];
             $rules_integer = [];
             $rules_string = [];
             $rules_required_item = [];
             $rules_integer_item = [];
             $rules_string_item = [];
             $string_integer = '';
             $string_string = '';
             $table_name = 'to' . $model->thang . strtolower($model->upt->nama_jenis) . $model->kdsatker . '_' . date('Ymdhis');
             $class_name = 'To' . $model->thang . strtolower($model->upt->nama_jenis) . $model->kdsatker . date('Ymdhis');
             $datenow = date('Y-m-d h:i:s');
             /* Format table_name = id_base_line_date_created */
             $string_sql .= '
                 CREATE TABLE ' . $table_name . ' (
                    id serial,
             ';
             foreach ($template as $tmp) {
                 $type = '';
                 if ($tmp->column_type == 0 || $tmp->column_type == 2 || $tmp->column_type == 3 || $tmp->column_type == 4 || $tmp->column_type == 5) {
                     $type = 'character varying';
                     $string_string .= strtolower(str_replace([' ', '/'], ['', ''], $tmp->column_name)) . ',';
                     array_push($rules_string_item, strtolower(str_replace([' ', '/'], ['', ''], $tmp->column_name)));
                 } elseif ($tmp->column_type == 1) {
                     $type = 'integer';
                     $string_integer .= strtolower(str_replace([' ', '/'], ['', ''], $tmp->column_name)) . ',';
                     array_push($rules_integer_item, strtolower(str_replace([' ', '/'], ['', ''], $tmp->column_name)));
                 }
                 if ($tmp->required == 0) {
                     array_push($rules_required_item, strtolower(str_replace([' ', '/'], ['', ''], $tmp->column_name)));
                 }
                 $nama_kolom[strtolower(str_replace([' ', '/'], ['', ''], $tmp->column_name))] = $tmp->column_name;
                 $string_sql .= strtolower(str_replace([' ', '/'], ['', ''], $tmp->column_name)) . ' ' . $type . ',';
             }
             array_push($rules_required, $rules_required_item);
             array_push($rules_integer, $rules_integer_item);
             array_push($rules_string, $rules_string_item);
             if ($rules_required_item) {
                 array_push($rules_required, 'required');
             }
             if ($rules_integer_item) {
                 array_push($rules_integer, 'integer');
             }
             if ($rules_string_item) {
                 array_push($rules_string, 'string');
             }
             array_push($rules, $rules_required);
             array_push($rules, $rules_integer);
             array_push($rules, $rules_string);
             $string_sql .= '
                    PRIMARY KEY( id )
                 );
             ';
             /* Template Table */
             /* Tempalte Model */
             $script_model = "<?php\n\nnamespace kemdikbud\\to\\models;\n\nuse Yii;\n\n/**\n * This is the model class for table \"" . $table_name . "\".\n */\nclass " . $class_name . " extends \\yii\\db\\ActiveRecord\n{\n    /**\n     * @inheritdoc\n     */\n    public static function tableName()\n    {\n        return '" . $table_name . "';\n    }\n\n    /**\n     * @inheritdoc\n     */\n    public function rules()\n    {\n        return " . str_replace(['"'], ['\''], json_encode($rules)) . ";\n    }\n\n    /**\n     * @inheritdoc\n     */\n    public function attributeLabels()\n    {\n        return " . str_replace(['{', ':', '}'], ['[', '=>', ']'], json_encode($nama_kolom)) . ";\n    }\n}";
             /* Save Model */
             $model_form->id_output = $id;
             $model_form->nama_tabel = $table_name;
             $model_form->nama_kolom_array = str_replace(['{', ':', '}'], ['[', '=>', ']'], json_encode($nama_kolom));
             $model_form->nama_kolom_json = json_encode($nama_kolom);
             $model_form->nama_class = $class_name;
             $model_form->date_created = $datenow;
             //$model->id_user_created     = $_POST['Outputbaseline']['id_base_line'];
             //$model->date_updated        = $_POST['Outputbaseline']['id_base_line'];
             //$model->id_user_updated     = $_POST['Outputbaseline']['id_base_line'];
             $model_form->approved = false;
             //$model->date_approved       = $_POST['Outputbaseline']['id_base_line'];
             /* Save Model */
             if ($model_form->save()) {
                 /* Create model file */
                 $fp = fopen(Yii::getAlias('@vendor/kemdikbud/yii2-target-output/models/') . $class_name . '.php', 'w');
                 fwrite($fp, $script_model);
                 fclose($fp);
                 /* Create model file */
                 /* Create table */
                 Yii::$app->db->createCommand($string_sql)->execute();
                 /* Create table */
                 /* kondisi ketika form laporan sudah jadi maka button berubah menjadi 'isi laporan' */
                 $status_button_form = Formlaporan::findOne(['id_output' => $id]);
                 /* ketika button sudah menjadi ii laporan maka munculkan tabel 'form laporan' dibawahnya */
                 if ($status_button_form) {
                     $class_name_now = '\\kemdikbud\\to\\models\\' . $status_button_form->nama_class;
                     $dataProviderLaporan = new ActiveDataProvider(['query' => $class_name_now::find(), 'pagination' => ['pageSize' => 10]]);
                     $columns = json_decode($status_button_form->nama_kolom_json, true);
                     $column_nama = [];
                     foreach ($columns as $key => $value) {
                         array_push($column_nama, $key);
                     }
                 }
                 return $this->render('view', ['model' => $this->findModel($id), 'dataProvider' => $dataProvider, 'template' => empty($template) ? [new Templatetable()] : $template, 'model_form' => $model_form, 'status_button_form' => $status_button_form, 'dataProviderLaporan' => $dataProviderLaporan, 'column_nama' => $column_nama]);
             } else {
                 if ($status_button_form) {
                     return $this->render('view', ['model' => $this->findModel($id), 'dataProvider' => $dataProvider, 'template' => empty($template) ? [new Templatetable()] : $template, 'model_form' => $model_form, 'status_button_form' => $status_button_form, 'dataProviderLaporan' => $dataProviderLaporan, 'column_nama' => $column_nama]);
                 } else {
                     return $this->render('view', ['model' => $this->findModel($id), 'dataProvider' => $dataProvider, 'template' => empty($template) ? [new Templatetable()] : $template, 'model_form' => $model_form, 'status_button_form' => $status_button_form]);
                 }
             }
         } else {
             /* Save isi laporan */
             $model_isi_laporan = new $class_name_now();
             if ($model_isi_laporan->load(Yii::$app->request->post()) && $model_isi_laporan->save()) {
                 return $this->render('view', ['model' => $this->findModel($id), 'dataProvider' => $dataProvider, 'template' => empty($template) ? [new Templatetable()] : $template, 'model_form' => $model_form, 'status_button_form' => $status_button_form, 'dataProviderLaporan' => $dataProviderLaporan, 'column_nama' => $column_nama]);
             } else {
                 return $this->render('view', ['model' => $this->findModel($id), 'dataProvider' => $dataProvider, 'template' => empty($template) ? [new Templatetable()] : $template, 'model_form' => $model_form, 'status_button_form' => $status_button_form, 'dataProviderLaporan' => $dataProviderLaporan, 'column_nama' => $column_nama]);
             }
         }
     } else {
         if ($status_button_form) {
             return $this->render('view', ['model' => $this->findModel($id), 'dataProvider' => $dataProvider, 'template' => empty($template) ? [new Templatetable()] : $template, 'model_form' => $model_form, 'status_button_form' => $status_button_form, 'dataProviderLaporan' => $dataProviderLaporan, 'column_nama' => $column_nama]);
         } else {
             return $this->render('view', ['model' => $this->findModel($id), 'dataProvider' => $dataProvider, 'template' => empty($template) ? [new Templatetable()] : $template, 'model_form' => $model_form, 'status_button_form' => $status_button_form]);
         }
     }
 }
 /**
  * Finds the Suboutput model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return Suboutput the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = Suboutput::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }