<?php
namespace Medienreaktor\Persons\Controller;

/***
 *
 * This file is part of the "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) 2018
 *
 ***/

/**
 * LocationController
 */
class LocationController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController
{
    /**
     * locationRepository
     *
     * @var \Medienreaktor\Persons\Domain\Repository\LocationRepository
     */
    protected $locationRepository = null;

    /**
     * companyRepository
     *
     * @var \Medienreaktor\Persons\Domain\Repository\CompanyRepository
     */
    protected $companyRepository = null;

    /**
     * organisationRepository
     *
     * @var \Medienreaktor\Persons\Domain\Repository\OrganisationRepository
     */
    protected $organisationRepository = null;
    public function __construct(\Medienreaktor\Persons\Domain\Repository\LocationRepository $locationRepository, \Medienreaktor\Persons\Domain\Repository\CompanyRepository $companyRepository, \Medienreaktor\Persons\Domain\Repository\OrganisationRepository $organisationRepository)
    {
        $this->locationRepository = $locationRepository;
        $this->companyRepository = $companyRepository;
        $this->organisationRepository = $organisationRepository;
    }


    /**
     * action list
     *
     * @return void
     */
    public function listAction(): \Psr\Http\Message\ResponseInterface
    {
        $locations = $this->locationRepository->findAll();
        $companys = $this->companyRepository->findAll();
        $organisations = $this->organisationRepository->findAll();

        $filters = array();
        $allCompanys = array();

        foreach ($companys as $company) {
            $allCompanys[$company->getUid()] = $company->getTitle();
        }

        if ($organisations) {
            foreach ($organisations as $keyOrganisation => $organisation) {
                if ($organisation->getCompanys()) {

                    $filters[$keyOrganisation]['title'] = $organisation->getTitle();

                    foreach ($organisation->getCompanys() as $company) {
                        $filters[$keyOrganisation]['companyIds'][] = $company->getUid();

                        unset($allCompanys[$company->getUid()]);
                    }
                }
            }

            foreach ($allCompanys as $keyCompany => $company) {
                $filter = array();
                $filter['title'] = $company;
                $filter['companyIds'][] = $keyCompany;
                $filters[] = $filter;
            }

        }

        $this->view->assign('filters', $filters);
        $this->view->assign('locations', $locations);
        $this->view->assign('companys', $companys);
        $this->view->assign('settings', $this->settings);
        return $this->htmlResponse();
    }

    /**
     * action show
     *
     * @param \Medienreaktor\Persons\Domain\Model\Location $location
     * @return void
     */
    public function showAction(\Medienreaktor\Persons\Domain\Model\Location $location): \Psr\Http\Message\ResponseInterface
    {
        $this->view->assign('location', $location);
        return $this->htmlResponse();
    }



}
