<?php
/**
 * A class for redirecting to another url.
 */
class controller_logic_redirect extends controller_logic
{
    ////////////////////////////////////////////////////////////////////////////
    // Construct and destruct functions.

    ////////////////////////////////////////////////////////////////////////////
    // Overload functions.

    ////////////////////////////////////////////////////////////////////////////
    // Interface functions.

    /**
     * Sets 30* header to the response.
     *
     * @param string $to The redirect address.
     * @param string (optional) $code The http redirect code.
     * @return controller A fluent interface.
     */
    public function execute($to, $code = 302)
    {
        if (!preg_match('~^((https?)|(ftp))://~i', $to))
        {
            $to = $this->request->base() . $to;
        }
        
        $this->response->clear()
                       ->set_header('Location: ' . $to, true, $code)
                       ->set_header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
        
        return $this;
    }

    ////////////////////////////////////////////////////////////////////////////
    // Internal functions.
}
