KoolReport's Forum

Official Support Area, Q&As, Discussions, Suggestions and Bug reports.
Forum's Guidelines

Deleting a file #2624

Closed Peter Wendel opened this topic on on Mar 31, 2022 - 11 comments

Peter Wendel commented on Mar 31, 2022

Hello it's me again :)

I built a dashboard looking like this:

STD-Dashboard

Below of "STD-Datei löschen" I want to build a form with a select-field to select a file for deleting it. What I have done is buuilding an action:


namespace App\KV\Dashboard\Actions;

use App\KV\Dashboard\Elements\StdSelect;
use Illuminate\Support\Facades\File;
use koolreport\dashboard\notifications\Note;

class HmStdDelete extends \koolreport\dashboard\admin\actions\Action
{
    

    protected function form(){
        return [
            'filename' => StdSelect::create(),
        ];
    }

    protected function handle($form,$models){
        File::delete(public_path(env('HM_STD_PATH').$form->input('filename')->value()));

        return Note::success('Datei '.$form->input('filename')->value().' wurde gelöscht');
    }
}


I thought, when I add this to my existing dashboard, the task will be done, but the dashboard shows "Oops".

So I think I'm doing something wrong... How can I sove this?

Below the code of the dashboard:

<?php

namespace App\KV\Dashboard\Dashboards;

use App\KV\Dashboard\Tables\StdFileList;
use koolreport\dashboard\containers\Html;
use koolreport\dashboard\containers\Panel;
use koolreport\dashboard\containers\Row;
use koolreport\dashboard\inputs\FileUploader;

class StdBoard extends \koolreport\dashboard\Dashboard
{
    protected function widgets()
    {
       return [
           Html::h2('Heilmittel: STD-Datei-Verarbeitung'),
           Html::hr(),
           Row::create()->sub([
              StdFileList::create(),
              Panel::create()->header('STD-Verwaltung')->type('primary')->sub([
                  Html::h5('STD-Datei hochladen'),
                  FileUploader::create('std_uploader')
                    ->type('primary')
                    ->accept(["std"])
                    ->saveToFolder(public_path(env('HM_STD_PATH')))
                    ->fileName(function($name,$ext){
                      return strtoupper($name).".".$ext;
                  })
                  ->resolveValue(function($file){
                      return $file["name"];
                  })
                  ->fileNotAllowedError("Dateityp nicht erlaubt.")
                  ->fileSizeLmitError("Datei ist zu groß!")
                  ->unknownError("So mysterious")
                  ->noFileError("Keine Datei ausgewählt!")
                  ->noFileSelectedText("Keine Datei ausgewählt!")
                  ->selectFileText("STD-Datei auswählen.."),
                  Html::hr(),
                  Html::p('Die Berechnung kann nur mit einer gültigen STD-Datei erfolgen.'),
                  Html::p('Links die Liste der breits vorhandenen Dateien.'),
                  Html::hr(),
                  Html::h5('STD-Datei löschen'),
                  HmStdDelete::create(), // This is the row making the board oopsing...

                  ]),

           ]),
         
       ];
    }
}

And for being complete the "StdSelect" used in the form:

<?php

namespace App\KV\Dashboard\Elements;

use Illuminate\Support\Facades\File;
use koolreport\dashboard\fields\Number;
use koolreport\dashboard\fields\Text;
use function public_path;

class StdSelect extends \koolreport\dashboard\inputs\Select2
{
    protected function onInit()
    {
        $this
            ->placeHolder('Bitte auswählen!')
            ->cssStyle("margin-left:10px")
            ->cssClass("form-control")
            ->size("lg");
    }

    protected function dataSource()
    {
        $files = File::files(public_path(env('HM_STD_PATH')));
        $array =[];
        foreach($files as $file):
            array_push($array,['id'=>$file->getFilename() ,'filename' => $file->getFilename()]);
        endforeach;
        return $array;
    }

    protected function fields()
    {
        return
        [
        Number::create('id'),
        Text::create('filename'),
        ];
    }

}

KoolReport commented on Mar 31, 2022

I guess the StdFileList is a table, you can add a button field to the table, so that each row (representing a file) has a delete button. On click button, you perform delete the file on that row and refresh table. How do you think of this idea?

Peter Wendel commented on Mar 31, 2022

Okay, that sounds easier... so where do I have to add this? This is the StdFileList, generated from a directory-listing:

<?php

namespace App\KV\Dashboard\Tables;

use Illuminate\Support\Facades\File;
use koolreport\dashboard\fields\Text;

class StdFileList extends \koolreport\dashboard\widgets\Table
{
    protected function dataSource()
    {
        $files = File::files(public_path(env('HM_STD_PATH')));
        $array =[];
        foreach($files as $file):
            array_push($array,['filename' => $file->getFilename()]);
            endforeach;
        return $array;
    }

    protected function fields()
    {
        Text::create('filename');
    }
}

Peter Wendel commented on Mar 31, 2022

So I did this in StdFileList to test the Button, but nothing happens: (no button in dashboard)

protected function fields()
    {
        return [
            Text::create('filename'),
            Button::create()
                ->onClick(function($row){
                    return "alert('".$row["filename"]."')";
                })
            ];
    }

I'm getting "oops" .

KoolReport commented on Mar 31, 2022

Let try this code:

<?php

namespace App\KV\Dashboard\Tables;

use Illuminate\Support\Facades\File;
use koolreport\dashboard\Client;
use koolreport\dashboard\fields\Button;
use koolreport\dashboard\fields\Text;

class StdFileList extends \koolreport\dashboard\widgets\Table
{
    protected function dataSource()
    {
        $files = File::files(public_path(env('HM_STD_PATH')));
        $array =[];
        foreach($files as $file):
            array_push($array,['filename' => $file->getFilename()]);
            endforeach;
        return $array;
    }

    protected function actionDelete($request, $response)
    {
        $filename = $request->params("filename");

        File::delete(public_path(env('HM_STD_PATH').$filename));
        return Note::success('Datei '.$filename.' wurde gelöscht');
    }

    protected function fields()
    {
        return [
            Text::create('filename'),
            Button::create("filename")
                ->text("Delete")
                ->onClick(function($value,$row){
                    return Client::widget("StdFileList")->action("delete",["filename"=>$value]);
                })
        ];
    }
}
Peter Wendel commented on Mar 31, 2022

Okay, this works :) The only thing to be done that's left is reloading the table.

This must be done in actionDelete I think?

KoolReport commented on Mar 31, 2022

You do this:

    protected function actionDelete($request, $response)
    {
        $filename = $request->params("filename");

        File::delete(public_path(env('HM_STD_PATH').$filename));

        $this->update();// Refresh table

        return Note::success('Datei '.$filename.' wurde gelöscht');
    }
Peter Wendel commented on Mar 31, 2022

Okay, that's easy :)

So this works and thanks for helping me clearing some of the connections between the things in the dashboard-framework.

Have a nice day!

Regards, Peter

Peter Wendel commented on Mar 31, 2022

A last question just comes up:

I build this to replace "FileUploader" in the above Dashboard and refreshing "StdFileList" and getting a Note about that:

<?php

namespace App\KV\Dashboard\Inputs;

use koolreport\dashboard\Client;
use koolreport\dashboard\notifications\Note;

class HmStdUploader extends \koolreport\dashboard\inputs\FileUploader
{

    protected function actionUpload($request, $response)
    {
        Client::widget("StdFileList")->action("change");
        return Note::success('Datei hochgeladen');
    }
}

I also added to "StdFileList"

 protected function actionChange($request,$response){
        $this->update();
        return Note::success('Tabelle aktualisiert');
    }

But now the file doesn't upload and the Upload field stuck in action. Which detail is missing?

Thanks again, Peter

KoolReport commented on Mar 31, 2022

You should do this:


namespace App\KV\Dashboard\Inputs;

use koolreport\dashboard\Client;
use koolreport\dashboard\notifications\Note;

class HmStdUploader extends \koolreport\dashboard\inputs\FileUploader
{

    protected function actionUpload($request, $response)
    {
        parent::actionUpload($request, $response);
        if($this->errorMessage()===null) {
            $this->sibling("StdFileList")->update();
            return Note::success('Datei hochgeladen and Tabelle aktualisiert');
        } else {
            return Note::danger($this->errorMessage());
        }
    }
}

It is because your previous code overwrites our default actionUpload. Also the Client is the class to generate javascript action which is only used at client-side, not server-side. At server-side, you refer to another widget using sibling() method.

You may remove the actionChange method in StdFileList. It is not necessary.

Let me know if it works.

Peter Wendel commented on Apr 1, 2022

Good Morning!

Aah... okay. The code works :)
It works better if you eliminate the "!", because errorMessage() must be null, when everything is okay. ;-)

Thanks for your and have a nice day!

Peter

KoolReport commented on Apr 1, 2022

Ah yes, you are right :)

Build Your Excellent Data Report

Let KoolReport help you to make great reports. It's free & open-source released under MIT license.

Download KoolReport View demo
help needed
solved

Dashboard