Menu

#19 Add Download Button to _list.php buttons

open
nobody
None
2018-10-30
2018-10-30
Anonymous
No

Originally created by: DPineault

I needed to add a download button to the _list.php

In the DefaultController.php controller I added

    /* Added by CARDA Consultants Inc., Daniel Pineault */
    public function actionDownload()
    {
        $file=Yii::$app->request->get('file');
        $path=Yii::$app->request->get('path');
        if (file_exists($path.$file)) {
            return Yii::$app->response->sendFile($path.$file);
        } else {
            throw new \yii\web\NotFoundHttpException("{$file} is not found!");
        }
    }

Then I modified the _list.php view files as follows

<?php
use yii\helpers\Html;
use yii\grid\GridView;
use yii\helpers\Url;

echo GridView::widget ( [ 
        'id' => 'install-grid',
        'dataProvider' => $dataProvider,
        'columns' => array (
                'name',
                'size:shortSize',
                'create_time:datetime',
                array (
                        'header' => 'Actions',
                        'class' => 'yii\grid\ActionColumn',
                        'template' => '{restore} {download} {delete}', /* Modified by CARDA Consultants Inc., Daniel Pineault   */
                        'buttons' => [ 
                                'delete' => function ($url, $model) {
                                    return Html::a ( '<span class="glyphicon glyphicon-remove"></span>', $url, [ 
                                            'title' => Yii::t ( 'app', 'Delete this backup' ) ,
                                            'data' => [
                                                'confirm' => 'Are you sure you want to delete this backup?',  /* Added by CARDA Consultants Inc., Daniel Pineault   */
                                                'method' => 'post',
                                            ],
                                    ] );
                                },

                                'restore' => function ($url, $model) {
                                    return Html::a ( '<span class="glyphicon glyphicon-save"></span>', $url, [ 
                                            'title' => Yii::t ( 'app', 'Restore this backup' ) ,
                                            'data' => [
                                                'confirm' => 'Are you sure you want to restore this version of your database and permanently crush any existing data?',  /* Added by CARDA Consultants Inc., Daniel Pineault    */
                                                'method' => 'post',
                                            ],
                                    ] );
                                },

                                /* Added by CARDA Consultants Inc., Daniel Pineault */
                                'download' => function ($url, $model) {
                                    return Html::a ( '<span class="glyphicon glyphicon-cloud-download"></span>', $url, [ 
                                            'title' => Yii::t ( 'app', 'Download this backup' ),
                                            'data-method'=>'post',
                                    ] );
                                } 
                        ],
                        'urlCreator' => function ($action, $model, $key, $index) {

                                $url = Url::toRoute ( [ 
                                        'default/' .$action,
                                        'file' => $model ['name'] ,
                                        'path'=>\Yii::getAlias('@backend').'/db/' /*Added by CARDA Consultants Inc., Daniel Pineault */
                                ] );
                                return $url;

                        } 
                )

        ) 
] );
?>

Hopefully this can help someone else, and might be a feature worth adding.

Also, I'd recommend changing the header' => 'Delete DB', to perhaps 'header' => 'Actions' as 'Delete DB' isn't accurate since it has multiple buttons.

Discussion


Log in to post a comment.