<?php

class Inbox_CountsController extends Inbox_Controller_Base
{
    public function init()
    {
        parent::init();

        $this->_table = 'ui_account';
        $this->_columns = array();
    }

    public function getAction()
    {
        $id = $this->_getParam('id', 0);

        if($id == 0)
        {
            $this->preconditionFailed();
            return;
        }

        try
        {
            $sql = "SELECT accountName as name, unreadMail as unseen_count from ui_account where userId=:id";
            $stmt = $this->_dbAdapter->prepare($sql);
            $stmt->bindParam("id", $id);
            $stmt->execute();
            $result = $stmt->fetchAll();
            if (empty($result))
            {
                $this->renderJson($this->_noResults);
            }
            else
            {
                $this->renderJson(array('status' => 'OK', 'inbox' => $result));
            }
        }
        catch (Exception $e)
        {
            $this->renderJson(array('error' => array('text' => $e->getMessage())));
        }
    }

    public function indexAction()
    {
        try
        {
            $account = $this->_getParam('account');

            if (null === $account)
            {
                $this->methodNotAllowed();
                return;
            }
            else
            {
                $sql = 'SELECT a.accountName as name, a.unreadMail as unseen_count FROM ui_account a join Users u on (a.userId=u.id) WHERE u.Account = :account';
                $stmt = $this->_dbAdapter->prepare($sql);
                $stmt->bindParam('account', $account);
                $stmt->execute();
                $result = $stmt->fetchAll();
                if (empty($result))
                {
                    $this->renderJson($this->_noResults);
                }
                else
                {
                    $this->renderJson(array('status' => 'OK', 'inbox' => $result));
                }
            }
        }
        catch (Exception $e)
        {
            $this->renderJson(array('error' => array('text' => $e->getMessage())));
        }
    }

    public function postAction()
    {
        $this->methodNotAllowed();
    }

    public function putAction()
    {
        $this->methodNotAllowed();
    }

    public function deleteAction()
    {
        $this->methodNotAllowed();
    }
}
