<?php
namespace Engine\Mvc\Response;

/**
 * Standard HTML redirect. Echoes a html page to the user in case the automatic redirect from the server fails
 * @author Lucian Sirbu
 */
class HtmlRedirect extends Html
{
	/**
	 * (non-PHPdoc)
	 * @see \Engine\Mvc\Response\GeneralAbstract::initialize()
	 */
	protected function initialize($url, array $headersArr)
	{
		$html = <<<HTML
<!DOCTYPE HTML>
<html lang="en-US">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="refresh" content="1;url={%URL}">
        <script type="text/javascript">
			setTimeout(function() {
            	window.location.href = "{%URL}"
			}, 2000);
        </script>
        <title>Redirect to {%URL}</title>
    </head>
    <body>
        If you are not redirected automatically, follow <a href='{%URL}'>{%URL}</a>
    </body>
</html>
HTML;
		$replacements = [
			'{%URL}' => $url,
		];
		parent::initialize(str_replace(array_keys($replacements), array_values($replacements), $html), []);

		$this->headers->append("Location", $url);

		$this->headers->append("Cache-Control", "must-revalidate, no-store, no-cache");
	}
}
