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/veltech.komma.pro/app/Komma/Kms/Transfer/TransferController.php
<?php

namespace App\Komma\Kms\Transfer;

/**
 * @author      Komma <info@komma.pro>
 * @copyright   (c) 2012-2016, Komma
 */

use App\Komma\Kms\Core\SectionController;
use App\Komma\Pages\Models\Page;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\MessageBag;

class TransferController extends SectionController
{
    protected $slug = "transfer";
    protected $forModelName = Page::class; //We need to set a model altough we don't even use it. This is because of the core of the KMS demands it

    /** @var TransferService $transferService */
    protected $transferService;

    public function __construct(TransferSection $section)
    {
        parent::__construct($section);

        $this->transferService = $service = $this->section->getSectionService();
        if(!is_a($service, TransferService::class)) throw new \RuntimeException('The section service must be "'.TransferService::class.'". Got "'.get_class($service).'"');
    }

    public function index() {
        // Build section tabs
        $this->section->getSectionTabDirector()->buildTabs();

        return $this->section->render();
    }

    public function Import(Request $request)
    {
        TransferService::$fileInputFieldName = 'File-file'; //Set the name of the input field that is used for imports. This input also must have the

        $messageBagOrNull = $this->transferService->import($request);

        /** @var RedirectResponse $response */
        $response = Redirect::back();

        $errorMessagesForInput = [];
        if($messageBagOrNull) {
            //Add errors for the input if any
            if (isset($messageBagOrNull->getMessages()['errors'])) {
                foreach ($messageBagOrNull->getMessages()['errors'] as $message) {
                    $errorMessagesForInput[TransferService::$fileInputFieldName][] = $message;
                }
                $response = $response->withErrors($errorMessagesForInput);
            }

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

                $response = $response->with('successes', $successMessageBag);
            }
        } else {
            $errorMessagesForInput[TransferService::$fileInputFieldName][] = __('kms/transfer.not_imported_no_file');
            $response = $response->withErrors($errorMessagesForInput);
        }

        return $response;
    }

    public function export(Request $request)
    {
        return $this->transferService->export();
    }

    /**
     * Import documents from ftp and link them to models
     */
    public function importDocuments()
    {
        /** @var TransferService $service */
        $service = $this->section->getSectionService();
        if(!is_a($service, TransferService::class)) throw new \RuntimeException('The section service must be "'.TransferService::class.'". Got "'.get_class($service).'"');

        $messageBag = $service->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;
    }
}