HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/base.komma.pro/vendor/komma/kms/src/Transfer/TransferController.php
<?php

namespace Komma\KMS\Transfer;

use Komma\KMS\Helpers\KommaHelpers;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\MessageBag;

/**
 * Class TransferController
 *
 * Imports and exports stuff
 *
 * @package App\Kms\Transfer
 */
class TransferController extends Controller
{
    protected $slug = "transfer";

    /** @var string The name of the file input field */
    public static $fileInputFieldName = 'File-file';

    /** @var AbstractCsvImportService */
    protected $csvImportService;

    /** @var AbstractCsvExportService */
    protected $csvExportService;

    /** @var AbstractFolderImporterService */
    protected $documentImporterService;

    /** @var TransferSection */
    protected $section;

    public function __construct()
    {
        if(app()->runningInConsole()) return;
        $this->section = new TransferSection();
        $this->section->setSectionTitle(ucfirst(__('KMS::transfer.transfer')));
        $this->section->setSubmitButtonLabel(__('KMS::transfer.transfer'));

        //Make the services. Remember, if you need to know which concrete one's are used, check the KmsServiceProvider
        $this->csvImportService = app(AbstractCsvImportService::class);
        $this->csvExportService = app(AbstractCsvExportService::class);
//        $this->documentImporterService = app(AbstractFolderImporterService::class);
    }

    public function index() {
        // Build section tabs
        $this->section->getTabs()->buildTabs();
        return $this->makeView();
    }

    /**
     * Handles an import request using a file input.
     *
     * @param Request $request
     * @return $this|RedirectResponse
     */
    public function Import(Request $request)
    {
        $response = Redirect::back();

        //Check if input field did contain a file. If not return a response with an error.
        if(!$request->hasFile(self::$fileInputFieldName)) {
            $errorMessagesForInput[TransferController::$fileInputFieldName][] = __('KMS::transfer.not_imported_no_file');
            $response = $response->withErrors($errorMessagesForInput);
            return $response;
        }

        //Temporary store the file, import from it. Then delete it.
        $file = $request->file(self::$fileInputFieldName)->store('importexport');
        $path = storage_path('app'.DIRECTORY_SEPARATOR.$file);
        $messageBag = $this->csvImportService->importFromFile($path);
        unlink($path);

        //Fill the response with error and success messages.
        $errorMessagesForInput = [];
        //Add errors for the input if any
        if (isset($messageBag->getMessages()['errors'])) {
            foreach ($messageBag->getMessages()['errors'] as $message) {
                $errorMessagesForInput[TransferController::$fileInputFieldName][] = $message;
            }
            $response = $response->withErrors($errorMessagesForInput);
        }

        //Add success messages for the input if any
        if (isset($messageBag->getMessages()['successes'])) {
            $successMessageBag = new MessageBag();
            foreach ($messageBag->getMessages()['successes'] as $message) {
                $successMessageBag->add(TransferController::$fileInputFieldName, $message);
            }

            $response = $response->with('successes', $successMessageBag);
        }

        //Return the response
        return $response;
    }

    /**
     * Export to a csv file.
     *
     * @param Request $request
     * @return mixed
     */
    public function export(Request $request)
    {
        $path = 'importexport/importexport.csv';
        Storage::delete($path); //Delete old file if it exists.
        $csvData = $this->csvExportService->exportToCsvFileString();
        Storage::put($path, $csvData);
        return Storage::download($path);
    }

    /**
     * Import documents from an ftp folder and link them to models
     */
    public function importDocuments()
    {
        $messageBag = $this->documentImporterService->importDocuments();

        $response = $this->index();

        if(isset($messageBag->getMessages()['errors'])) {
            $errorMessagesForInput = [];
            foreach ($messageBag->getMessages()['errors'] as $message) {
                $errorMessagesForInput['document_import'][] = $message;
            }
            $response = $response->withErrors($errorMessagesForInput);
        }

        //Add success messages for the input if any
        if(isset($messageBag->getMessages()['successes'])) {
            $successMessageBag = new MessageBag();
            foreach ($messageBag->getMessages()['successes'] as $message) {
                $successMessageBag->add('document_import', $message);
            }

            $response = $response->with('successes', $successMessageBag);
        }

        return $response;
    }

    /**
     * Makes the view and returns it
     *
     * @return View
     */
    protected function makeView(): View
    {
        $successes = (Session::has('successes')) ? Session::get('successes') : new MessageBag();

        return view('KMS::section.transfer', [
            'section' => $this,
            'importRoute' => [
                'route' => route('transfer.import'),
                'method' => 'POST'
            ],
            'exportRoute' => [
                'route' => route('transfer.export'),
                'method' => 'POST'
            ],
            'importDocumentsRoute' => [
                'route' => route('transfer.import_documents'),
                'method' => 'POST'
            ],
            'slug' => $this->slug,
            'successes' => $successes,
            'maxUploadSize' => KommaHelpers::fileUploadMaxSize()
        ]);
    }
}