File: D:/HostingSpaces/SBogers10/honger.komma.pro/app/KommaApp/Kms/Transfer/TransferController.php
<?php
namespace App\KommaApp\Kms\Transfer;
/**
* @author Komma <info@komma.pro>
* @copyright (c) 2012-2016, Komma
*/
use App\KommaApp\Kms\Core\SectionController;
use App\KommaApp\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
protected $treeService;
public function __construct(TransferSection $section)
{
parent::__construct($section);
}
public function index() {
// Build section tabs
$this->section->getSectionTabDirector()->buildTabs();
return $this->section->render();
}
/**
* Triggered when the import export button is clicked
*
* @param Request $request
* @return RedirectResponse|MessageBag
*/
public function importExport(Request $request)
{
/** @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).'"');
TransferService::$fileInputFieldName = 'File-file'; //Set the name of the input field that is used for imports. This input also must have the
$messageBagOrDownload = $service->importExport($request);
if(!is_a($messageBagOrDownload, MessageBag::class)) return $messageBagOrDownload;
/** @var RedirectResponse $response */
$response = Redirect::back();
//Add errors for the input if any
if(isset($messageBagOrDownload->getMessages()['errors'])) {
$errorMessagesForInput = [];
foreach ($messageBagOrDownload->getMessages()['errors'] as $message) {
$errorMessagesForInput[TransferService::$fileInputFieldName][] = $message;
}
$response = $response->withErrors($errorMessagesForInput);
}
//Add success messages for the input if any
if(isset($messageBagOrDownload->getMessages()['successes'])) {
$successMessageBag = new MessageBag();
foreach ($messageBagOrDownload->getMessages()['successes'] as $message) {
$successMessageBag->add(TransferService::$fileInputFieldName, $message);
}
$response = $response->with('successes', $successMessageBag);
}
return $response;
}
/**
* 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;
}
}