Your IP : 216.73.216.79


Current Path : /var/www/html/ekursus/backend/controllers/
Upload File :
Current File : /var/www/html/ekursus/backend/controllers/NewssController.php

<?php

namespace backend\controllers;

use Yii;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use app\models\CsvForm;
use yii\web\UploadedFile;
use backend\models\FileSurvey;
//use backend\models\IntegrationSurveyBank;
use backend\models\NewssSearch;

/**
 * RefController implements the CRUD actions for Ref model.
 */
class NewssController extends Controller {

    /**
     * @inheritdoc
     */
    public function behaviors() {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    public function actionIndex() {
        $searchModel = new NewssSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
                    'searchModel' => $searchModel,
                    'dataProvider' => $dataProvider,
        ]);
    }
    
    
    public function actionList() {
        $searchModel = new NewssSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('list',
        [
                    'searchModel' => $searchModel,
                    'dataProvider' => $dataProvider,
        ]
                );
    }
    
    public function actionTypeNewss() {
        $searchModel = new NewssSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

//        $moduleArr = [];
//        $module = Menu::find()->where("module_ind = '1'")->all();
//        foreach ($module as $param) {
//            $moduleArr[$param->id] = $param->menu_txt;
//        }

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

    public function actionUpload() {
        $model = new CsvForm;

        if ($model->load(Yii::$app->request->post())) {
            $file = UploadedFile::getInstance($model, 'file');
            $filename = 'Data.' . $file->extension;
            $upload = $file->saveAs('uploads/' . $filename);
            if ($upload) {
                define('CSV_PATH', 'uploads/');
                $csv_file = CSV_PATH . $filename;
                $filecsv = file($csv_file);
                print_r($filecsv);
                foreach ($filecsv as $data) {
                    $modelnew = new IntegrationSurveyBank();
                    $hasil = explode(",", $data);
                    $id = $hasil[0];
                    $newss_id = $hasil[1];
                    $ref_no = $hasil[2];
                    $ejob_id = $hasil[3];
                    $industri_msic_2008 = $hasil[4];
                    $industri_msic_2001 = $hasil[5];
                    $reg_name = $hasil[6];
                    $operation_office = $hasil[7];
                    $reg_by = $hasil[8];
                    $person_in_charge = $hasil[9];
                    $created_at = $hasil[10];
                    $modelnew->id = $id;
                    $modelnew->newss_id = $newss_id;
                    $modelnew->ref_no = $ref_no;
                    $modelnew->ejob_id = $ejob_id;
                    $modelnew->industri_msic_2008 = $industri_msic_2008;
                    $modelnew->industri_msic_2001 = $industri_msic_2001;
                    $modelnew->reg_name = $reg_name;
                    $modelnew->operation_office = $operation_office;
                    $modelnew->reg_by = $reg_by;
                    $modelnew->person_in_charge = $person_in_charge;
                    $modelnew->created_at = $created_at;

                    $modelnew->save();
                }
                unlink('uploads/' . $filename);
                return $this->redirect(['newss/index']);
            }
        } else {
            return $this->render('upload', ['model' => $model]);
        }
    }

    public function actionExport() {
        $model = IntegrationSurveyBank::find()->All();
        $filename = 'Data-' . Date('YmdGis') . '-Data.xls';
        header("Content-type: application/vnd-ms-excel");
        header("Content-Disposition: attachment; filename=" . $filename);
        echo '<table border="1" width="100%">
        <thead>
            <tr>
                <th>ID</th>
                <th>Newss_ID</th>
                <th>Reference_No</th>
                <th>eJob_ID</th>
                <th>Industri_MSIC_2008</th>
                <th>Industri_MSIC_2001</th>
                <th>Registration_Name</th>
                <th>Operation_Office</th>
                <th>Register_By</th>
                <th>Person_In_Charge</th>
                <th>Created_At</th>
                <th>Created_By</th>
                <th>Updated_At</th>
                <th>Updated_By</th>
                
            </tr>
        </thead>';
        foreach ($model as $data) {
            echo '
                <tr>
                    <td>' . $data['id'] . '</td>
                    <td>' . $data['newss_id'] . '</td>
                    <td>' . $data['ref_no'] . '</td>
                    <td>' . $data['ejob_id'] . '</td>
                    <td>' . $data['industri_msic_2008'] . '</td>
                    <td>' . $data['industri_msic_2001'] . '</td>
                    <td>' . $data['reg_name'] . '</td>
                    <td>' . $data['operation_office'] . '</td>
                    <td>' . $data['reg_by'] . '</td>
                    <td>' . $data['person_in_charge'] . '</td>
                    <td>' . $data['created_at'] . '</td>
                    <td>' . $data['created_by'] . '</td>
                    <td>' . $data['updated_at'] . '</td>
                    <td>' . $data['updated_by'] . '</td>
                </tr>
            ';
        }
        echo '</table>';
    }
    
    public function actionDelete($id) {
        $this->findModel($id)->delete();
        $log = new \backend\models\Log();
        $log->module = Yii::$app->controller->id;
        $log->details = Yii::$app->controller->action->id . ":" . $id;

        $log->save();
        return $this->redirect(['index']);
    }
    
    protected function findModel($id) {
        if (($model = FileSurvey::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }

}