File: D:/HostingSpaces/SBogers10/cafe-de-bout.komma.pro/app/Components/ComponentApiController.php
<?php
namespace App\Components;
use App\Components\Component\ComponentSaveState;
use App\Components\ComponentArea\ComponentAreaService;
use App\Components\ComponentType\ComponentTypeFactory;
use App\Components\ComponentType\Types\AbstractComponentType;
use Illuminate\Http\Request;
/**
* Class ComponentApiController
*
* Used by javascript to communicate with the DynamicGroupApiBackend
*
* @package App\Components
*/
class ComponentApiController
{
/**
* Returns an array containing all available group names
*
* @return array
*/
public function getAllAvailableComponentTypeNames()
{
return array_values(__('KMS::kms/attributes/components.types'));
}
/**
* Resolves a dynamic group name to HTML
*
* @param Request $request
* @param null $componentAreaAttributeKey
* @param null $componentSaveState
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function resolveComponentTypeToView(Request $request, $componentAreaAttributeKey = null, $componentSaveState = null)
{
if($componentAreaAttributeKey == null || $componentSaveState == null) {
$componentAreaAttributeKey = $request->input('componentAreaAttributeKey');
$componentSaveState = $request->input('componentSaveState');
$componentSaveState = json_decode($componentSaveState, true);
}
if(!$componentSaveState) throw new \RuntimeException('ComponentApiController:resolveComponentTypeToView. Encountered an invalid savestate string: '.$request->input('componentSaveState'));
$componentSaveState = ComponentSaveState::fromArray($componentSaveState);
/** @var AbstractComponentType $componentType */
try {
$componentType = ComponentTypeFactory::make($componentSaveState->getComponentTypeId());
} catch (\Exception $e) {
abort(400, 'Could not resolve ComponentType" '.$componentSaveState->getComponentTypeId().'"');
}
$componentType = ComponentAreaService::generateComponentAttributeKeysForComponent($componentType, $componentAreaAttributeKey, $componentSaveState->getId());
$componentData = [
'component' => $componentType,
'id' => $componentSaveState->getId(), //An id of 0 represents a new component
];
return view('kms.partials.entity.component', $componentData);
}
/**
* Resolves a set of dynamic group name to HTML
*
* @param Request $request
* @return array
* @throws \Throwable
*/
public function resolveComponentAreaToView(Request $request)
{
$componentAreaAttributeKey = $request->input('componentAreaAttributeKey');
$componentSaveStates = json_decode($request->input('componentSaveStates'), true);
$html = [];
foreach ($componentSaveStates as $componentSaveState) {
array_push($html, ComponentApiController::resolveComponentTypeToView($request, $componentAreaAttributeKey, $componentSaveState )->render());
}
return $html;
}
}