File: D:/HostingSpaces/SBogers10/bomacon.komma.pro/app/WebsiteConfig/WebsiteConfigController.php
<?php
namespace App\WebsiteConfig;
use App\Helpers\KommaHelpers;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\MessageBag;
use Illuminate\View\View;
use Komma\KMS\Core\Attributes\Attribute;
use Komma\KMS\Core\Attributes\Models\Traits\HasThumbnailInterface;
use Komma\KMS\Core\Entities\DisplayNameInterface;
use Komma\KMS\Core\SectionController;
use App\WebsiteConfig\Model\WebsiteConfig;
class WebsiteConfigController extends SectionController
{
protected $slug = "websiteconfig";
protected $classModelName = WebsiteConfig::class;
/**
* Constructor
* @param $section
*/
public function __construct()
{
$section = new WebsiteConfigSection($this->slug);
parent::__construct($section);
$this->modelService = new WebsiteConfigModelService();
}
/**
* Returns a rendered view
*
* @param Model $model
* @return View
*/
protected function render(Model $model = null)
{
$this->section->generateAttributesAndAddThemToTabs($this->forModelInstance);
$byValueFrom = $this->section->getAttributes()->mapToGroups(function($attribute, $index) {
/** @var Attribute $attribute */
return [$attribute->getsValueFrom() => $attribute];
});
$this->load($model ?? new WebsiteConfig(), $byValueFrom);
return $this->makeView();
}
/**
* @param Model $model
* @return \Illuminate\Http\RedirectResponse|mixed
*/
public function show($model)
{
return Redirect::action('\\'.get_class($this) . '@index');
}
/**
* Makes the view and returns it
*
* @return \Illuminate\View\View
*/
protected function makeView(): View
{
$modelId = ($this->forModelInstance) ? $this->forModelInstance->id : null;
$successes = (Session::has('successes')) ? Session::get('successes') : new MessageBag();
$thumbnail = '';
if(is_a($this->forModelInstance, HasThumbnailInterface::class)) {
$thumbnail = $this->forModelInstance->getThumbnail();
};
if(is_a($this->forModelInstance, DisplayNameInterface::class)) {
$displayName = $this->forModelInstance->getDisplayName();
} else {
$displayName = $this->section->getSectionNewModel();
}
$saveRoute = $this->modelService->getSaveRoute($this->slug, $modelId);
$siteSlug = !$this->siteService->getCurrentSite()->exists ? null : $this->siteService->getCurrentSite()->slug;
return \View::make('kms/section.website_config', [
'sectionTitle' => $this->section->getSectionTitle(),
'sectionTabs' => $this->section->getTabs(),
'slug' => $this->slug,
'siteSlug' => $siteSlug,
'successes' => $successes,
'models' => $this->modelService->getModelsForSideBar(),
'maxUploadSize' => \Komma\KMS\Helpers\KommaHelpers::fileUploadMaxSize(),
'maxPostSize' => KommaHelpers::maxPostSize(),
'modelClassName' => $this->classModelName,
'saveRoute' => $saveRoute,
'sortable' => $this->sortable,
'showEntity' => $this->showEntity,
'thumbnail' => $thumbnail,
'displayName' => $displayName,
'currentModel' => $this->forModelInstance,
'submitButtonLabel' => $this->section->getSubmitButtonLabel(),
'preventNavigationTranslation' => json_encode(__('KMS::kms/prevent-navigation'))
]);
}
/**
* A function that MUST delegate to services to accomplish loading.
* It MUST NOT act like a service.
*
* @param Model $model
* @param Collection $attributesByValueFrom Keys must be ValueFrom integers. Values must be Attributes that have the ValueFrom integers
* @return Collection The same collection as you've passed in. Only "filled".
*/
protected function load(Model $model, Collection $attributesByValueFrom = null): Collection
{
if($attributesByValueFrom === null) return new Collection();
/** @var Attribute $attribute */
$this->modelService->load($model, $attributesByValueFrom->collapse());
return $attributesByValueFrom;
}
protected function save(Model $model, Collection $attributesByValueFrom = null): Model
{
if($attributesByValueFrom == null) return $model;
/** @var Attribute $attribute */
$model = $this->modelService->save($model, $attributesByValueFrom->collapse());
return $model;
}
public function store()
{
parent::store();
$sessionData = [
'tabslug' => request('tabslug'),
'success' => __('KMS::kms/global.saved')
];
return redirect()->action('\\'.get_class($this) . '@index')->with($sessionData);
}
}