jamespakorn / yii2_dohdashboard_chiangmai

Yii2 DOH Dashboard ChiangMai
0 stars 0 forks source link

Day1 #1

Open jamespakorn opened 7 years ago

jamespakorn commented 7 years ago

DOH Dashboard ChiangMai Installation

  1. XAMPP(apache(web server)+php+mysql) >> https://www.apachefriends.org/index.html

  2. composer >> https://getcomposer.org/

  3. git >> https://git-scm.com/

  4. Visual Studio Code >> https://code.visualstudio.com/

  5. Java 8 sdk >> http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

  6. Netbeans 8.2 Full >> https://netbeans.org/downloads/

  7. Sign up Account Github >> https://github.com/

  8. Download zip Yii2 Basic Template >> https://github.com/yiisoft/yii2/releases/download/2.0.12/yii-basic-app-2.0.12.tgz

  9. Download zip Yii2 Advanced Template >> https://github.com/yiisoft/yii2/releases/download/2.0.12/yii-advanced-app-2.0.12.tgz

  10. Navicat Premium 11 >> Private

jamespakorn commented 7 years ago

yii2 CreateCommand


 $conn = \Yii::$app->db;

  $sql = 'คำสั่ง sql';

   $cmd = $conn->createCommand($sql);
    // $cmd->bindValue(':startdate', $startdate);
     // $cmd->bindValue(':enddate', $enddate);
    $data = $cmd->queryAll();
jamespakorn commented 7 years ago

composer require miloschuman/yii2-highcharts-widget

jamespakorn commented 7 years ago

composer.json

    "miloschuman/yii2-highcharts-widget": "dev-master",
jamespakorn commented 7 years ago

สำหรับเครื่องที่ ใช้งาน yii2 ใหม่ ก่อนใช้งาน composer อื่นใน yii2 composer global require "fxp/composer-asset-plugin:^1.2.0"

jamespakorn commented 7 years ago
use miloschuman\highcharts\Highcharts;

echo Highcharts::widget([
   'options' => [
      'title' => ['text' => 'Fruit Consumption'],
      'xAxis' => [
         'categories' => ['Apples', 'Bananas', 'Oranges']
      ],
      'yAxis' => [
         'title' => ['text' => 'Fruit eaten']
      ],
      'series' => [
         ['name' => 'Jane', 'data' => [1, 0, 4]],
         ['name' => 'John', 'data' => [5, 7, 3]]
      ]
   ]
]);
jamespakorn commented 7 years ago
use miloschuman\highcharts\Highcharts;

    echo Highcharts::widget([
       'options' => [
        'chart'=> [
            type=> 'column'
        ],
          'title' => ['text' => 'Fruit Consumption'],
          'xAxis' => [
             'categories' => ['เขต1', 'เขต2', 'เขต3']
          ],
          'yAxis' => [
             'title' => ['text' => 'Fruit eaten']
          ],
          'series' => [
             ['name' => 'ข้อมูล', 'data' => [1, 2, 4]],
          ]
       ]
    ]);
jamespakorn commented 7 years ago

<?php echo Highcharts::widget([
            'scripts' => [
                'modules/exporting',
                'themes/grid-light',
            ],
            'options' => [
                'chart'=> [
                    'type'=> 'column'
                ],
                'title'=> [
                    'text'=> 'ตัวชีั้วัด'
                ],
                'subtitle'=> [
                    //'text'=> 'ปีงบประมาณ'
                ],
                'xAxis'=>[
                    'type' => 'category',
                    'labels' => [
                        'rotation' => -45,
                        'style' => [
                            'fontSize'=>'13px',
                            'fontFamily'=>'Verdana, sans-serif'
                        ]
                    ]
                ],
                'yAxis' => [
                    'min' => 0,
                    'title' => [
                    // 'text' => 'ดัชนีมวลกาย'
                    ],
                    'plotLines' => [[
                        // 'value' => (int) $kpiTemplateToYear->target_value,
                        'value' => 5,
                        'color' => 'red',
                        'width' => 2,
                        'label' => [
                            //'text' => 'Theoretical mean => 932',
                            'align' => 'center',
                            'style' => [
                                'color' => 'gray'
                            ]
                        ]
                    ]]
                ],
                'legend' => [
                    'enabled' => false
                ],
                'tooltip' => [
                    'pointFormat' => ''
                ],
                'series' => [
                    [
                        'name' => 'Population',
                        'colorByPoint' => false,
                        // 'color'=> '#62cb31',
                        'data' => [["เขต 1",70.17],["เขต 2",65.86],["เขต 3",63.42],["เขต 4",47.93],["เขต 5",63.98],["เขต 6",50.12],["เขต 7",66.1],["เขต 8",75.95],["เขต 9",63.13],["เขต 10",73.2],["เขต 11",68.18],["เขต 12",75.26]],
                        'dataLabels'=> [
                            'enabled'=> true,
                            'rotation'=> -90,
                            'color'=> 'gray',
                            'align'=> 'right',
                            'format'=> '{point.y:.2f}',
                            'y' => 10,
                            'style' => [
                                'fontSize' => '10px',
                                'fontFamily' => 'Verdana, sans-serif'
                            ]
                        ]
                    ]
                ]
            ]
        ])?>
jamespakorn commented 7 years ago

gii in mudule

jamespakorn commented 7 years ago
<table id="datatables" class="table table-striped table-hover table-bordered" cellspacing="0" width="100%">
    <thead>
      <tr class="warning">
        <th>ลำดับ</th>
        <th>รหัสหน่วย</th>
        <th>กลุ่มหน่วยงาน</th>
      </tr>
    </thead>
    <tbody>

     <?php

       foreach ($data as $k => $v) {
        echo '<tr>';
        echo '<td>'.($k+1).'</td>';
        echo '<td>'.$v['hostypecode'].'</td>';
        echo '<td>'.$v['hostypename'].'</td>';
        echo '</tr>';
       }

     ?>

    </tbody>
  </table>
jamespakorn commented 7 years ago
<div class="content animate-panel">
<div class="row">
            <div class="col-lg-12">
                <div class="hpanel">
                    <div class="panel-body h-200">

                          <!-- Code -->

                    </div>
                    <!-- <div class="panel-footer"></div> -->
                </div>
            </div>

</div>
</div>
jamespakorn commented 7 years ago

 public $css = [
       ....,
        //datatables
        'datatables/css/datatables.bootstrap.min.css',
        'datatables/extensions/buttons/css/buttons.bootstrap.min.css',
]
jamespakorn commented 7 years ago

public $js=[
        .......,
        //datatables
        'js/datatables.js',
        'datatables/js/jquery.datatables.min.js',
        'datatables/js/datatables.bootstrap.min.js',
        'datatables/extensions/buttons/js/datatables.buttons.min.js',
        'datatables/extensions/buttons/js/buttons.bootstrap.min.js',
        'datatables/js/pdfmake.min.js',
        'datatables/js/vfs_fonts.js',
        'datatables/js/jszip.min.js',
        'datatables/extensions/buttons/js/buttons.html5.min.js',
        'datatables/extensions/buttons/js/buttons.print.min.js',
        'datatables/extensions/buttons/js/buttons.colVis.min.js',
]
jamespakorn commented 7 years ago

<?php
$script = <<< JS

        //Datatables
        getDatatable('#datatables');
        // getDatatableTotal('#table_total',2);

JS;
$this->registerJs($script);

?>
jamespakorn commented 7 years ago

                            <?php

                            foreach ($data as $k => $v) {
                                echo '<tr>';
                                echo '<td>'.($k+1).'</td>';
                                echo '<td>'.$v['areaname'].'</td>';
                                echo '<td>'.number_format($v['target']).'</td>';
                                echo '<td>'.number_format($v['result']).'</td>';
                                echo '</tr>';
                            }

                            ?>
jamespakorn commented 7 years ago

 use miloschuman\highcharts\Highcharts;

<?php 
    echo Highcharts::widget([
            'scripts' => [
                'modules/exporting',
                'themes/grid-light',
            ],
            'options' => [
                'chart'=> [
                    'type'=> 'column'
                ],
                'title'=> [
                    'text'=> 'ตัวชีั้วัด'
                ],
                'subtitle'=> [
                    //'text'=> 'ปีงบประมาณ'
                ],
                'xAxis'=>[
                    'type' => 'category',
                    'labels' => [
                        'rotation' => -45,
                        'style' => [
                            'fontSize'=>'13px',
                            'fontFamily'=>'Verdana, sans-serif'
                        ]
                    ]
                ],
                'yAxis' => [
                    'min' => 0,
                    'title' => [
                    // 'text' => 'ดัชนีมวลกาย'
                    ],
                    'plotLines' => [[
                        // 'value' => (int) $kpiTemplateToYear->target_value,
                        'value' => 50,
                        'color' => 'red',
                        'width' => 2,
                        'label' => [
                            //'text' => 'Theoretical mean => 932',
                            'align' => 'center',
                            'style' => [
                                'color' => 'gray'
                            ]
                        ]
                    ]]
                ],
                'legend' => [
                    'enabled' => false
                ],
                'tooltip' => [
                    'pointFormat' => ''
                ],
                'series' => [
                    [
                        'name' => 'Population',
                        'colorByPoint' => false,
                        // 'color'=> '#62cb31',
                        'data' => [["เขต 1",70.17],["เขต 2",65.86],["เขต 3",63.42],["เขต 4",47.93],["เขต 5",63.98],["เขต 6",50.12],["เขต 7",66.1],["เขต 8",75.95],["เขต 9",63.13],["เขต 10",73.2],["เขต 11",68.18],["เขต 12",75.26]],
                        'dataLabels'=> [
                            'enabled'=> true,
                            'rotation'=> -90,
                            'color'=> 'gray',
                            'align'=> 'right',
                            'format'=> '{point.y:.2f}',
                            'y' => 10,
                            'style' => [
                                'fontSize' => '10px',
                                'fontFamily' => 'Verdana, sans-serif'
                            ]
                        ]
                    ]
                ]
            ]
        ]);
        ?>
jamespakorn commented 7 years ago

        foreach ($data as $k => $v) {
           $chartData[]=[$v['areaname'],(float) $v['total_ratio']];
        }
jamespakorn commented 7 years ago

<?php

  echo Highcharts::widget([
    'id'=>'xxx',
    'scripts' => [
        'highcharts-more',  
        'modules/exporting',
        'themes/grid-light',
    ],
    'options' => [
        'chart'=> [
            'type' => 'gauge',
            'plotBackgroundColor' => null,
            'plotBackgroundImage' => null,
            'plotBorderWidth' => 0,
            'plotShadow' => false
        ],
        'title' => 'ร้อยละ',

        'pane' => [
          'startAngle' => -150,
          'endAngle' => 150,
          'background' => [[
            'backgroundColor' => [
            'linearGradient' => [ x1 => 0, y1 => 0, x2 => 0, y2 => 1 ],
            'stops' => [
              [0, '#FFF'],
              [1, '#333']
            ]
          ],
          'borderWidth' => 0,
          'outerRadius' => '109%'
          ], [
          'backgroundColor' => [
          'linearGradient' => [ x1 => 0, y1 => 0, x2 => 0, y2 => 1 ],
            'stops' => [
              [0, '#333'],
              [1, '#FFF']
            ]
          ],
            'borderWidth' => 1,
            'outerRadius' => '107%'
          ], [
          // default background
          ], [
          'backgroundColor' => '#DDD',
          'borderWidth' => 0,
          'outerRadius' => '105%',
          'innerRadius' => '103%'
          ]]
        ],
        // the value axis
        'yAxis'=>[
            'min' => 0,
            'max' => 100,

            'minorTickInterval' => 'auto',
            'minorTickWidth' => 1,
            'minorTickLength' => 10,
            'minorTickPosition' => 'inside',
            'minorTickColor' => '#666',

            'tickPixelInterval' => 30,
            'tickWidth' => 2,
            'tickPosition' => 'inside',
            'tickLength' => 10,
            'tickColor' => '#666',
            'labels' => [
                'step' => 2,
                'rotation' => 'auto'
            ],
            'title' => [
                'text' => 'ร้อยละ'
            ],
            // 'plotBands' => $plotBands
            'plotBands'=> [[
                'from'=> 0,
                'to'=> 50,
                'color'=> '#55BF3B' // green
            ], [
                'from'=> 51,
                'to'=> 80,
                'color'=> '#DDDF0D' // yellow
            ], [
                'from'=> 81,
                'to'=> 100,
                'color'=> '#DF5353' // red
            ]],
        ],
        'tooltip' => [
            'pointFormat' => 'ร้อยละ {point.y:.2f}',
            'style' => [
                'fontSize' => '18px',
                'fontFamily' => 'Verdana, sans-serif'
            ]
        ],
        'series' => [[
            'name' => 'ร้อยละ',
            // 'data' => [(int)$chartHdc->percentage],
            'data' => [$gaugeData],
            'tooltip'=> [
                    'valueSuffix' => ' '
            ],
            'dataLabels'=> [
                'enabled'=> true,
                'color'=> 'blue',
                'format'=> '{point.y:.2f}',
                'style' => [
                    'fontSize' => '10px',
                    'fontFamily' => 'Verdana, sans-serif'
                ]
            ]
        ]]
    ]
])?>
jamespakorn commented 7 years ago

dashboard.zip

jamespakorn commented 7 years ago

Yii2 CreateCommand http://www.yiiframework.com/doc-2.0/guide-db-dao.html

jamespakorn commented 7 years ago

        //Create Connection
        $conn = \Yii::$app->db;

        //kpi data
        $sql = 'select * from kpi_hdc k
        left join kpi_template_to_year y on y.kpi_template_id=k.kpi_template_id
        where k.kpi_template_id=:kpi_template_id and y.year=:year';

        $cmd = $conn->createCommand($sql);
        $cmd->bindValue(':kpi_template_id', $kpi_template_id);
        $cmd->bindValue(':year', $year);
        $q = $cmd->queryOne();
jamespakorn commented 7 years ago

Yii2 Request http://www.yiiframework.com/doc-2.0/guide-runtime-requests.html

jamespakorn commented 7 years ago

select * from kpi_hdc k
left join kpi i on i.id=k.kpi_template_id
left join kpi_template_to_year y on y.kpi_template_id=k.kpi_template_id
where k.kpi_template_id=6 and y.year="2017"
jamespakorn commented 7 years ago

      $request = \Yii::$app->request;
        // echo $year = $request->get('year');
        // echo $kid=$request->get('kid');

        $year=$request->get('year');
        $kpi_template_id=$request->get('kid');
        $bYear=intval($year)+543;
jamespakorn commented 7 years ago

http://localhost/dohdashboard/frontend/web/dashboard/anc5/index?year=2017&kid=2

jamespakorn commented 7 years ago

-- Q1-03. ร้อยละหญิงตั้งครรภ์ที่ได้รับการดูแลก่อนคลอด 4 ครั้ง ตามเกณฑ์คุณภาพ - Trend 
SELECT K.b_year,ROUND( SUM(IFNULL(K.result,0))/SUM(IFNULL(K.target,0))*100,2 ) as total_ratio
FROM s_anc4 K 
INNER JOIN cchangwat C ON C.changwatcode=LEFT(K.areacode,2) 
INNER JOIN cregion Z ON Z.region_code=C.regioncode  
WHERE K.id = '0748a96f259f329a83206d40c47df150' 
    AND K.b_year BETWEEN :queryYearPrev and :queryYear
group by K.b_year
jamespakorn commented 7 years ago

Module dashboard 8/11/2560

dashboard-8112560.zip

jsondune commented 7 years ago

HDC SERVICE Data Dictionary (ตาราง S) DOPHDashboardDataDict-20170910.pdf

jamespakorn commented 7 years ago

  //trend
        if($kpi['sql_query_trend']){

                        $cmd = $conn->createCommand($kpi['sql_query_trend']);
                        $cmd->bindValue(':queryYearPrev',$bYearPrev);
                        $cmd->bindValue(':queryYear',$bYear);
                        $trends = $cmd->queryAll();

                        //จัดรูปแบบ trend chart 
                        foreach ($trends as  $t) {
                            $trendData[]=[$t['b_year'],(float) $t['total_ratio']];

                        }

        }
panulove commented 7 years ago

//แสดงวัน เดือน ปี แบบไทย <?php
$_month_name = array("01"=>"มกราคม", "02"=>"กุมภาพันธ์", "03"=>"มีนาคม",
"04"=>"เมษายน", "05"=>"พฤษภาคม", "06"=>"มิถุนายน",
"07"=>"กรกฎาคม", "08"=>"สิงหาคม", "09"=>"กันยายน",
"10"=>"ตุลาคม", "11"=>"พฤศจิกายน", "12"=>"ธันวาคม");

$vardate=date('Y-m-d'); $yy=date('Y'); $mm =date('m');$dd=date('d'); if ($dd<10){ $dd=substr($dd,1,2); } $date=$dd ." ".$_month_name[$mm]." ".$yy+= 543; echo 'วันที่ประมวลผล :: '.$date; ?>

jamespakorn commented 7 years ago

<?php 
$this->title = 'ระบบบันทึกข้อมูลตัวชี้วัดตัวชีัวัดสิ่งแวดล้อม';
?>
<?=$this->render('//_header')?>

<div class="content animate-panel">
<div class="row">
            <div class="col-lg-12">
                <div class="hpanel">
                    <div class="panel-body h-200">

                          <!-- Code -->

                    </div>
                    <!-- <div class="panel-footer"></div> -->
                </div>
            </div>

</div>
</div>
jamespakorn commented 7 years ago

kpi_env_envriskmng.zip

jamespakorn commented 7 years ago

 public static function GetList() {
        return yii\helpers\ArrayHelper::map(self::find()->all(), 'changwatcode', 'changwatname');
    }
jamespakorn commented 7 years ago

use kartik\select2\Select2;
<?php
echo $form->field($model, 'year_id')->widget(Select2::classname(), [
    'data' => KpiFiscalYear::GetList(),
    'options' => ['placeholder' => 'กรุณาเลือก...'],
    'pluginOptions' => [
        'allowClear' => true,
    ],
]);
?>
jamespakorn commented 7 years ago

ออกแบบระบบติดตาม KPI จังหวัด ปรับ 30 ตค.60.pdf

jamespakorn commented 7 years ago

http://www.yiiframework.com/doc-2.0/guide-db-active-record.html

jamespakorn commented 7 years ago
  "2amigos/yii2-ckeditor-widget": "~1.0"
jamespakorn commented 7 years ago

Code Editer On Web APP

https://github.com/trntv/yii2-aceeditor

https://ace.c9.io/

jamespakorn commented 7 years ago

ก่อนใช้งาน Composer ใน Yii2


composer global require "fxp/composer-asset-plugin:^1.2.0"
jamespakorn commented 7 years ago

On running composer update on my Yii2 projects, Composer now reports The "extra.asset-installer-paths" option is deprecated, use the "config.fxp-asset.installer-paths" option.

So should the default composer.json be updated to reflect this?

This modification produces correct results for me...



  "config": {
    "fxp-asset": {
      "installer-paths": {
        "npm-asset-library": "vendor/npm",
        "bower-asset-library": "vendor/bower"
      }
    },
    "process-timeout": 1800
  },
  "extra": {
  },
jamespakorn commented 7 years ago

<?php
echo $form->field($model, 'comment')->widget(\dosamigos\ckeditor\CKEditor::className(), [
    'clientOptions' => ['height' => 200],
    'preset' => 'advance',
])
?>
jamespakorn commented 7 years ago

<table id="datatables" class="table table-striped table-hover table-bordered"  width="100%">
                    <thead>
                        <tr class="info">
                            <th></th>
                            <th>ลำดับ</th>
                            <th>จังหวัด</th>
                            <th>ปี</th>
                            <th>ไตรมาส</th>
                            <th>เป้าหมาย(แห่ง)</th>
                            <th>ประเด็นมีการพัฒนาระบบ
            ฐานข้อมูล สถานการณ์
            และการเฝ้าระวังด้าน
            สิ่งแวดล้อมและสุขภาพ</th>
                            <th>ประเด็นมีกลไกการจัดการ
            ปัจจัยเสี่ยงจากมลพิษ
            สิ่งแวดล้อมอย่างบูรณาการ
            โดยขับเคลื่อน
            การดำเนินงานผ่านกลไก
            คณะกรรมการสาธารณสุข
            จังหวัด (คสจ.)</th>
                            <th>ประเด็นมีระบบและกลไก
            สนับสนุนการจัดการมูล
            ฝอยติดเชื้อของ
            โรงพยาบาล รพศ./รพท./
            รพช. สังกัดกระทรวง
            สาธารณสุขให้ถูกต้อง
            ตามกฎหมาย</th>
                            <th>ประเด็นมีการส่งเสริมให้
            ท้องถิ่นมีการจัดบริการ
            อนามัยสิ่งแวดล้อมที่ได้
            มาตรฐาน (EHA)</th>
                            <th>ประเด็นมีการดำเนินงาน
            เพื่อส่งเสริมสนับสนุน
            ให้เกิดตำบลที่มีชุมชน
            เข้มแข็ง ด้านอนามัย
            สิ่งแวดล้อม</th>
                            <th>ประเด็นมีการจัดระบบ
            เฝ้าระวังสุขภาพจากการ
            ประกอบอาชีพและมลพิษ
            สิ่งแวดล้อม</th>
                            <th>สรุปผลการดำาเนินงาน</th>
                            <th>ไฟล์แนบ</th>

                         </tr>
                    </thead>
        <!--            <tfoot>
                        <tr>
                            <th></th>
                            <th>รวมทั้งหมด</th>
                            <th></th>
                        </tr>
                    </tfoot>-->
                    <tbody>

                        <?php
foreach ($data as $key => $item) {

    echo '<tr>';
    echo '<td>';
    echo Html::a('<i class="fa fa-eye fa-lg text-info"></i>', ['kpienvenvriskmng/view', 'id' => $item['id']]) . ' ';
    echo Html::a('<i class="fa fa-pencil-square-o fa-lg text-primary"></i>', ['kpienvenvriskmng/update', 'id' => $item['id']]) . ' ';
    echo Html::a('<i class="fa fa-trash fa-lg text-danger"></i>', ['kpienvenvriskmng/delete', 'id' => $item['id']], ['data-method' => "post", 'data-confirm' => "ยืนยันการลบ?"]);
    echo '</td>';

    echo '<td>' . ($key + 1) . '</td>';
    echo '<td>' . $item['changwatname'] . '</td>';
    echo '<td>' . $item['year_buddhism'] . '</td>';
    echo '<td>' . $item['quater_id'] . '</td>';
    echo '<td>' . $item['target'] . '</td>';
    echo '<td>' . $item['result1'] . '</td>';
    echo '<td>' . $item['result2'] . '</td>';
    echo '<td>' . $item['result3'] . '</td>';
    echo '<td>' . $item['result4'] . '</td>';
    echo '<td>' . $item['result5'] . '</td>';
    echo '<td>' . $item['result6'] . '</td>';
    echo '<td>' . $item['conclude_result'] . '</td>';
    if ($item['file_attachment']) {
        echo '<td>' . Html::a('<i class="fa fa-file-text-o text-warning" aria-hidden="true"></i> ', Yii::getAlias('@web') . '/uploads/' . $item['file_attachment']) . '</td>';
    } else {
        echo '<td></td>';
    }

    // echo '<td>' . Yii::$app->thaidate->ShortDate($item['book_date']) . '</td>';
    echo '</tr>';
}
?>

                    </tbody>
                </table>
jamespakorn commented 7 years ago

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

        $conn = Yii::$app->db;
        $sql = 'select  e.*,c.changwatname,f.`year`,f.year_buddhism
        from kpi_env_envriskmng e
        inner join cchangwat c on c.changwatcode=e.areacode
        left join kpi_fiscal_year f on f.`year`=e.year_id';

        $cmd = $conn->createCommand($sql);
        $data =$cmd->queryAll();

        return $this->render('index', [
            'data'=>$data
            // 'searchModel' => $searchModel,
            // 'dataProvider' => $dataProvider,
        ]);
    }
jamespakorn commented 7 years ago


<?php
$script = <<< JS

        //Datatables
         getDatatable('#datatables');
        // getDatatableSum('#datatables',2,3);

JS;
$this->registerJs($script);

?>
jamespakorn commented 7 years ago

UploadForm.zip

jamespakorn commented 7 years ago

view form


use kartik\widgets\FileInput;

<?php
echo $form->field($modelfile, 'file')->widget(FileInput::classname(), [
    'options' => [
        //'multiple' => true
    ],
    'pluginOptions' => [
        'initialPreview' => [],
        // 'allowedFileExtensions' => ['pdf','doc','docx','xls','xlsx','zip','rar'],
        'showPreview' => true,
        'showCaption' => true,
        'showRemove' => true,
        'showUpload' => false,
    ],
]);
?>
jamespakorn commented 7 years ago

use yii\web\UploadedFile;

            //upload file
            $modelfile->file = UploadedFile::getInstance($modelfile, 'file');

            if ($modelfile->file) {
                if ($modelfile->file->saveAs('uploads/files_' . $model->id . '.' . $modelfile->file->extension)) {
                    $model->file_attachment = 'files_' . $model->id . '.' . $modelfile->file->extension;
                    $model->save();

                }
            }
jamespakorn commented 7 years ago

## view form
<div class="form-group">
<?php
echo $model->file_attachment != null ? Html::a('<i class="fa fa-file-text-o fa-lg text-warning" aria-hidden="true"> ไฟล์แนบ</i> ', Yii::getAlias('@web') . '/uploads/' . $model->file_attachment) : '';
?>
<hr>
</div>
jamespakorn commented 7 years ago

env.zip

jamespakorn commented 7 years ago

Controller/Delete


    public function actionDelete($id)
    {

        $model = $this->findModel($id);

        // $filepath =  Yii::getAlias('@web') .'/uploads/' . $model->file_attachment;
        $filepath ='uploads/' . $model->file_attachment;

        if (file_exists($filepath)) {
            unlink($filepath);
            $this->findModel($id)->delete();
        }

       return $this->redirect(['index']);
    }
jamespakorn commented 7 years ago

View/ Index


<?php

if (Yii::$app->session->hasFlash('Save')) {

    echo kartik\widgets\Alert::widget([
        'type' => Alert::TYPE_SUCCESS,
        'icon' => 'glyphicon glyphicon-info-sign',
        'title' => 'สถานะการบันทึกข้อมูล!',
        'titleOptions' => ['icon' => 'info-sign'],
        'body' => Yii::$app->session->getFlash('Save'),
        'showSeparator' => true,
        'delay' => 3000,
    ]);
}
?>