<?php

class Inbox_Controller_Base extends Atmail_Controller_Base
{
    protected $_dbTables;
    protected $_dbAdapter;

    // HTTP response headers will be one of the following:
    const STATUS_CODE_CONTINUE               = 100; // 100 Continue
    const STATUS_CODE_OK                     = 200; // 200 OK
    const STATUS_CODE_CREATED                = 201; // 201 Created
    const STATUS_CODE_ACCEPTED               = 202; // 202 Accepted
    const STATUS_CODE_NOT_MODIFIED           = 304; // 304 Not Modified
    const STATUS_CODE_BAD_REQUEST            = 400; // 400 Bad Request
    const STATUS_CODE_AUTHORIZATION_REQUIRED = 401; // 401 Authorization required
    const STATUS_CODE_FORBIDDEN              = 403; // 403 Forbidden
    const STATUS_CODE_NOT_FOUND              = 404; // 404 Not Found
    const STATUS_CODE_METHOD_NOT_ALLOWED     = 405; // 405 Method Not Allowed
    const STATUS_CODE_PRECONDITION_FAILED    = 412; // 412 Precondition Failed
    const STATUS_CODE_INTERNAL_SERVER_ERROR  = 500; // 500 Internal Server Error
    const STATUS_CODE_NOT_IMPLEMENTED        = 501; // 501 Not Implemented

    public function init()
    {
        parent::init();

        // Load the system settings
        $this->_globalConfig = Zend_Registry::get('config')->global;

        $this->_dbTables = new dbTables();
        $this->_dbAdapter = Zend_Registry::get('dbAdapter');

        $this->_noResults = array('status' => 'NO_RESULTS');
        $this->_zeroResults = array('status' => 'ZERO_RESULTS');
        $this->_methodNotAllowed = array('status' => 'METHOD_NOT_ALLOWED');
        $this->_preconditionFailed = array('status' => 'PRECONDITION_FAILED');

        $this->_statusOK = array('status' => 'OK');
    }

    protected function methodNotAllowed()
    {
        $this->getResponse()->setHttpResponseCode(self::STATUS_CODE_METHOD_NOT_ALLOWED);
        $this->renderJson($this->_methodNotAllowed);
    }

    protected function preconditionFailed()
    {
        $this->getResponse()->setHttpResponseCode(self::STATUS_CODE_PRECONDITION_FAILED);
        $this->renderJson($this->_preconditionFailed);
    }

    protected function isJson()
    {
        $contentType = $this->getRequest()->getHeader('Content-Type');
        if (!empty($contentType) && strcasecmp($contentType, 'application/json') == 0)
        {
            return true;
        }

        return false;
    }

    protected function getJsonParam($jsonArray, $key, $default = null)
    {
        $key = (string) $key;
        if (isset($jsonArray[$key])) {
            return $jsonArray[$key];
        }

        return $default;
    }
}
