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/pietvanmierlo/stempelbv.nl/app/Komma/Components/ComponentApiController.php
<?php


namespace App\Komma\Components;

use App\Komma\Components\Component\ComponentSaveState;
use App\Komma\Components\ComponentArea\ComponentAreaService;
use App\Komma\Components\ComponentType\ComponentTypeFactory;
use App\Komma\Components\ComponentType\Types\AbstractComponentType;
use Illuminate\Http\Request;

/**
 * Class ComponentApiController
 *
 * Used by javascript to communicate with the DynamicGroupApiBackend
 *
 * @package App\Komma\Components
 */
class ComponentApiController
{

    /**
     * Returns an array containing all available group names
     *
     * @return array
     */
    public function getAllAvailableComponentTypeNames()
    {
        return array_values(__('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;
    }
}