/** * 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]); } } }
/** * Creates a new Outputbaseline model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Outputbaseline(); $template = [new Templatetable()]; if (Yii::$app->request->post()) { /* Template Table */ $template = Model::createMultiple(Templatetable::classname()); Model::loadMultiple($template, Yii::$app->request->post()); $string_sql = ''; $nama_kolom = []; $string_integer = ''; $string_string = ''; /* Format table_name = id_base_line_date_created */ $string_sql .= ' CREATE TABLE to_' . $_POST['Outputbaseline']['id_base_line'] . '_' . date('Y_m_d_h_i_s') . ' ( id serial, '; foreach ($template as $tmp) { $type = ''; if ($tmp->column_type == 0) { $type = 'character varying'; $string_string .= strtolower(str_replace([' '], [''], $tmp->column_name)) . ','; } elseif ($tmp->column_type == 1) { $type = 'integer'; $string_integer .= 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 . ','; } $string_sql .= ' PRIMARY KEY( id ) ); '; /* Template Table */ /* Tempalte Model */ //$script_model = ''; $table_name = 'To' . $_POST['Outputbaseline']['id_base_line'] . '' . date('Ymdhis'); $script_model = "<?php\n\nnamespace kemdikbud\\to\\models;\n\nuse Yii;\n\n/**\n * This is the model class for table \"to_2_2015_07_13_11_06_05\".\n *\n */\nclass " . $table_name . " extends \\yii\\db\\ActiveRecord\n{\n /**\n * @inheritdoc\n */\n public static function tableName()\n {\n return 'to_" . $_POST['Outputbaseline']['id_base_line'] . '_' . date('Y_m_d_h_i_s') . "';\n }\n\n /**\n * @inheritdoc\n */\n public function rules()\n {\n return [\n [['no'], 'integer'],\n [['uraian', 'keterangan'], 'string']\n ];\n }\n\n /**\n * @inheritdoc\n */\n public function attributeLabels()\n {\n return " . str_replace(['{', ':', '}'], ['[', '=>', ']'], json_encode($nama_kolom)) . ";\n }\n}\n "; $fp = fopen(Yii::getAlias('@vendor/kemdikbud/yii2-target-output/models/') . $table_name . '.php', 'w'); fwrite($fp, $script_model); fclose($fp); /* Template Model */ /* Save Model */ $datenow = date('Y-m-d h:i:s'); $model->id_base_line = $_POST['Outputbaseline']['id_base_line']; $model->nama_tabel = 'to_' . $_POST['Outputbaseline']['id_base_line'] . '_' . date('Y_m_d_h_i_s'); $model->nama_kolom_array = str_replace(['{', ':', '}'], ['[', '=>', ']'], json_encode($nama_kolom)); $model->nama_kolom_json = json_encode($nama_kolom); $model->nama_class = $table_name; $model->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->approved = false; //$model->date_approved = $_POST['Outputbaseline']['id_base_line']; /* Save Model */ if ($model->save()) { Yii::$app->db->createCommand($string_sql)->execute(); return $this->redirect(['viewoutput', 'id' => $model->id_base_line]); } else { return $this->render('create', ['model' => $model]); } } else { return $this->render('create', ['model' => $model, 'template' => empty($template) ? [new Templatetable()] : $template]); } }