<?php

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

    // http://atmail.local/index.php/api/inbox2/view/id/1/GroupID/1
    public function init()
    {
        //TODO: extend authentication to support multiple user levels/roles/groups
        // require_once 'application/models/api.php';
        // $this->api = new api();

        // $this->view->baseUrl = $this->_request->getBaseUrl();
        // $this->view->appBaseUrl = $this->_request->getBaseUrl() . (strpos($this->_request->getBaseUrl(),'index.php')?'':'/index.php');
        // $this->view->siteBaseUrl = Zend_Registry::get('siteBaseUrl');
        // $this->view->moduleBaseUrl = $this->view->appBaseUrl . ($this->_request->module == 'default'?'':'/' . $this->_request->module);

        // $this->_helper->viewRenderer->setNoRender();

        // Load all the params
        // $this->requestParams = $this->getRequest()->getParams();

        // Use Cookie auth
        // if ( !Atmail_FormAuth::authenticated() )
        // {
        //    return $this->_forward('timeout', 'mail', 'api', $this->requestParams);
        // }

        //$this->userData = Zend_Auth::getInstance()->getStorage()->read();
        //$this->userData = Atmail_Password::processUser($this->userData);

        // Load the session
        // $this->session = new Zend_Session_Namespace(ATMAIL_NAMESPACE);

        // Load the contacts class
        // $this->_contacts = new contacts( array('Account' => $this->userData['Account']) );
        // $this->_contacts = new contacts( array('admin' => true) );

        // Find the users settings ( used for theme )
        // $this->view->UserSettings = Zend_Registry::get('UserSettings');

        // Used to determine the contact in the global addressbook
        // $this->view->Account = $this->userData['Account'];

        // 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');
    }

// =======================================================================================================
    public function indexAction()
    {
        $q = "SELECT * from ui_account";
        $accounts = $this->_dbAdapter->fetchAll($q);
        $this->renderJson($accounts);


        //$this->renderJson($this->_noResults);

        // $params = $this->getRequest()->getParams();
        // $this->renderJson( array('method' => 'index', 'params' => $params) );
        // $this->restserver->handle( array('method' => 'index') );
    }

    public function getAction()
    {
        $id = $this->_getParam('id', 0);
        $params = $this->getRequest()->getParams();
        $this->renderJson( array('method' => 'get', 'id' => $id, 'params' => $params) );

        // $params = $this->getRequest()->getParams();
        // $this->renderJson( array('method' => 'get', 'params' => $params) );
    }

    public function postAction()
    {
        $id = $this->_getParam('id', 0);
        $params = $this->getRequest()->getParams();

        $this->renderJson( array('method' => 'post', 'id' => $id, 'params' => $params) );
    }

    public function putAction()
    {
        $id = $this->_getParam('id', 0);
        $params = $this->getRequest()->getParams();

        $this->renderJson( array('method' => 'put', 'id' => $id, 'params' => $params) );
    }

    public function deleteAction()
    {
        $id = $this->_getParam('id', 0);
        $params = $this->getRequest()->getParams();

        $this->renderJson( array('method' => 'delete', 'id' => $id, 'params' => $params) );
    }
}
