<?php
namespace Medienreaktor\Products\Controller;

use Psr\Http\Message\ResponseInterface;
use TYPO3\CMS\Extbase\Http\ForwardResponse;

/***
 *
 * This file is part of the "Products" Extension for TYPO3 CMS.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 *
 *  (c) 2017 Daniel Kestler &lt;daniel.kestler@medienreaktor&gt;, medienreaktor GmbH
 *
 ***/

/**
 * LocationController
 */
class LocationController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController
{

    /**
     * locationRepository
     *
     * @var \Medienreaktor\Products\Domain\Repository\LocationRepository
     */
    protected $locationRepository = null;
    public function __construct(\Medienreaktor\Products\Domain\Repository\LocationRepository $locationRepository)
    {
        $this->locationRepository = $locationRepository;
    }

    /**
     * action index
     *
     * @return void
     */
    public function indexAction(): \Psr\Http\Message\ResponseInterface
    {
        $locations = $this->locationRepository->findAll();
        $countries = $this->locationRepository->findAllCountries();

        $this->view->assign('locations', $locations);
        $this->view->assign('countries', $countries);
        return $this->htmlResponse();
    }
    /**
     * switchable Controller Migration
     *
     * @return ForwardResponse|ResponseInterface
     */
    public function switchableAction()
    {
        // Wert für 'switchableControllerActions' aus flexForm holen
        $cObjData = \nn\t3::Tsfe()->cObjData( $this->request );
        $ffData = \nn\t3::FlexForm()->parse($cObjData['pi_flexform']);

        // ... und an passende Action weiterleiten
        $action = preg_replace('/[^>]*>(.*)/', '\1', $ffData['switchableControllerActions']);
        $methodName = "{$action}Action";

        if (!method_exists($this, $methodName)) {
            return $this->htmlResponse("PayloadController->{$methodName}() exisitert nicht.");
        }

        return new ForwardResponse( $action );
    }

}
