<?php
/*
 * This file is part of Phraseanet
 *
 * (c) 2005-2016 Alchemy
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace Alchemy\Phrasea\Controller\Root;

use Alchemy\Phrasea\Application\Helper\EntityManagerAware;
use Alchemy\Phrasea\Controller\Controller;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\Response;

class RootController extends Controller
{
    use EntityManagerAware;

    public function getRobots()
    {
        if ($this->getConf()->get(['registry', 'general', 'allow-indexation']) === true) {
            $buffer = "User-Agent: *\n" . "Allow: /\n";
        } else {
            $buffer = "User-Agent: *\n" . "Disallow: /\n";
        }

        return new Response($buffer, 200, ['Content-Type' => 'text/plain']);
    }

    public function getRoot()
    {
        return $this->app->redirectPath('homepage');
    }

    public function setLocale($locale)
    {
        $response = $this->app->redirectPath('root');

        $cookiePath = ini_get('session.cookie_path');
        $cookieDomain = ini_get('session.cookie_domain');
        $cookieHttpOnly = ini_get('session.cookie_httponly');
        $cookieSecure = ini_get('session.cookie_secure');

        $response->headers->setCookie(new Cookie(
            'locale',
            $locale,
            0,
            $cookiePath,
            empty($cookieDomain)? null : $cookieDomain,
            $cookieSecure ? true: false,
            $cookieHttpOnly ? true : false
        ));

        $authenticatedUser = $this->getAuthenticatedUser();

        // if connected, update user locale
        if ($authenticatedUser !== null) {
            try {
                $authenticatedUser->setLocale($locale);

                $this->getEntityManager()->flush();
            } catch (\Exception $e) {
                // invalid locale
            }
        }

        return $response;
    }

    public function getAvailableLanguages()
    {
        return $this->app->json($this->app['locales.available']);
    }
}
