<?php

/**
 * ErrorController - The default error controller class
 * 
 * @author
 * @version 
 */

require_once 'Zend/Controller/Action.php';

class ErrorController extends Zend_Controller_Action
{

    /**
     * This action handles  
     *    - Application errors
     *    - Errors in the controller chain arising from missing 
     *      controller classes and/or action methods
     */
    public function errorAction()
    {
        $errors = $this->_getParam('error_handler');
        switch ($errors->type) {
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
                // 404 error -- controller or action not found                
                $this->getResponse()->setRawHeader('HTTP/1.1 404 Not Found');
                $this->view->title = 'HTTP/1.1 404 Not Found';
                break;
            default:
                // application error; display error page, but don't change                
                // status code
                $this->view->title = 'Application Error';
                break;
        }
        
        $this->view->message = $errors->exception;
        
        
        $table = new Errors();
        
        if (isset($_SERVER['HTTP_REFERER'])) {
        	$referer = $_SERVER['HTTP_REFERER'];
        } else {
        	$referer = '';
        }
        
        if (isset($errors->request)) {
        	$params = $errors->request->getParams();
        
	        $paramkeys = array_keys($params);
	        $values = implode('-', $params);
	        $keys   = implode('-', $paramkeys);
        } else {
        	$keys = '';
        	$values = '';
        }
        
        if (isset($errors->exception)) {
        	$message = $errors->exception->getMessage();
			$stack = $errors->exception->getTraceAsString();
        	
        } else {
        	$message = '';
        	$stack = '';
        }
        
        $data = array(
        			'datetime' => new Zend_Db_Expr('NOW()'),
        			'uri'	   => $_SERVER['REQUEST_URI'],
        			'referer'  => $referer,
        			'message'  => $message,
        			'stack_trace' => $stack,
        			'params'    => $keys . ' ### ' . $values
        			);
        			
        $table->insert($data);
    }  
    
}
