<?php
namespace Medienreaktor\Contact\Controller;

use TYPO3\CMS\Core\Utility\GeneralUtility;

/***
 *
 * This file is part of the "Contact Persons" 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) 2019 Daniel Kestler <info@medienreaktor.de>, medienreaktor GmbH
 *
 ***/
/**
 * PersonController
 */
class PersonController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController
{

    /**
     * personRepository
     *
     * @var \Medienreaktor\Contact\Domain\Repository\PersonRepository
     */
    protected $personRepository = null;
    public function __construct(\Medienreaktor\Contact\Domain\Repository\PersonRepository $personRepository)
    {
        $this->personRepository = $personRepository;
    }

    /**
     * action list
     *
     * @return void
     */
    public function listAction(): \Psr\Http\Message\ResponseInterface
    {
        $persons = [];
        if (!empty($this->settings['showSelection'])) {
            foreach (GeneralUtility::trimExplode(",", $this->settings['showSelection'], true) as $personUid) {
                $person = $this->personRepository->findByUid($personUid);
                if ($person) {
                    $persons[] = $person;
                }
            }
        } else {
            if (!empty($this->settings['showByCategories'])) {
                $categories = GeneralUtility::trimExplode(",", $this->settings["showByCategories"], true);
                $persons = $this->personRepository->findByCategories($categories);
            } else {
                $persons = $this->personRepository->findAll();
            }
        }
        $this->view->assign('persons', $persons);
        return $this->htmlResponse();
    }

}
