<?php

class Inbox_SettingsController extends Inbox_Controller_Base
{
    protected $userId;
    protected $displayEmailImages;
    protected $autoSaveDraft;
    protected $notifications;
    protected $messagePreview;
    protected $enableExtraRecipientFields;
    protected $language;

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

    protected function populateParameters()
    {
        if ($this->isJson())
        {
            $inputJson = file_get_contents('php://input');
            $input = json_decode( $inputJson, TRUE );

            $this->userId =                     $this->getJsonParam($input, 'userId', '');
            $this->displayEmailImages =         $this->getJsonParam($input, 'displayEmailImages', '');
            $this->autoSaveDraft =              $this->getJsonParam($input, 'autoSaveDraft', '');
            $this->notifications =              $this->getJsonParam($input, 'notifications', '');
            $this->messagePreview =             $this->getJsonParam($input, 'messagePreview', '');
            $this->enableExtraRecipientFields = $this->getJsonParam($input, 'enableExtraRecipientFields', '');
            $this->language =                   $this->getJsonParam($input, 'language', '');
        }
        else
        {
            $this->userId =                     $this->_getParam('userId', '');
            $this->displayEmailImages =         $this->_getParam('displayEmailImages', '');
            $this->autoSaveDraft =              $this->_getParam('autoSaveDraft', '');
            $this->notifications =              $this->_getParam('notifications', '');
            $this->messagePreview =             $this->_getParam('messagePreview', '');
            $this->enableExtraRecipientFields = $this->_getParam('enableExtraRecipientFields', '');
            $this->language =                   $this->_getParam('language', '');
        }
    }

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

            if (null === $userId)
            {
                $sql = 'SELECT * from ui_settings';
                $accounts = $this->_dbAdapter->fetchAll($sql);
                $this->renderJson($accounts);
            }
            else
            {
                $sql = 'SELECT * from ui_settings where userId=:userId';
                $stmt = $this->_dbAdapter->prepare($sql);
                $stmt->bindParam('userId', $userId);
                $stmt->execute();
                $result = $stmt->fetchAll();
                $this->renderJson($result);
            }
        }
        catch (Exception $e)
        {
            $this->renderJson(array('error' => array('text' => $e->getMessage())));
        }
    }

    public function getAction()
    {
        $id = $this->_getParam('id', 0);
        $sql = 'SELECT * from ui_settings where id=:id';

        try
        {
            $stmt = $this->_dbAdapter->prepare($sql);
            $stmt->bindParam('id', $id);
            $stmt->execute();
            $result = $stmt->fetchObject();
            $this->renderJson($result);
        }
        catch (Exception $e)
        {
            $this->renderJson(array('error' => array('text' => $e->getMessage())));
        }
    }

    public function postAction()
    {
        try
        {
            $sql = <<<EOD
INSERT INTO ui_settings (
`userId`,
`displayEmailImages`,
`autoSaveDraft`,
`notifications`,
`messagePreview`,
`enableExtraRecipientFields`,
`language`
) VALUES (
:userId,
:displayEmailImages,
:autoSaveDraft,
:notifications,
:messagePreview,
:enableExtraRecipientFields,
:language
);
EOD;

            $this->populateParameters();

            $stmt = $this->_dbAdapter->prepare($sql);

            $stmt->bindParam('userId',                     $this->userId);
            $stmt->bindParam('displayEmailImages',         $this->displayEmailImages);
            $stmt->bindParam('autoSaveDraft',              $this->autoSaveDraft);
            $stmt->bindParam('notifications',              $this->notifications);
            $stmt->bindParam('messagePreview',             $this->messagePreview);
            $stmt->bindParam('enableExtraRecipientFields', $this->enableExtraRecipientFields);
            $stmt->bindParam('language',                   $this->language);

            $success = $stmt->execute();
            $id = $this->_dbAdapter->lastInsertId();

            $select = $this->_dbAdapter
                ->select()
                ->from('ui_settings')
                ->where('id = ' . $this->_dbAdapter->quote($id, 'INTEGER'));

            $this->renderJson($this->_dbAdapter->fetchRow($select));
        }
        catch (Exception $e)
        {
            $this->renderJson(array('error' => array('text' => $e->getMessage())));
        }
    }

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

        try
        {
            $sql = <<<EOD
UPDATE ui_settings SET
`id`                     = :id,
`userId`                     = :userId,
`displayEmailImages`         = :displayEmailImages,
`autoSaveDraft`              = :autoSaveDraft,
`notifications`              = :notifications,
`messagePreview`             = :messagePreview,
`enableExtraRecipientFields` = :enableExtraRecipientFields,
`language`                   = :language
WHERE id=:id
EOD;

            $this->populateParameters();

            $stmt = $this->_dbAdapter->prepare($sql);

            $stmt->bindParam('userId',                     $this->userId);
            $stmt->bindParam('displayEmailImages',         $this->displayEmailImages);
            $stmt->bindParam('autoSaveDraft',              $this->autoSaveDraft);
            $stmt->bindParam('notifications',              $this->notifications);
            $stmt->bindParam('messagePreview',             $this->messagePreview);
            $stmt->bindParam('enableExtraRecipientFields', $this->enableExtraRecipientFields);
            $stmt->bindParam('language',                   $this->language);

            $stmt->bindParam('id',                         $id);

            $success = $stmt->execute();

            $select = $this->_dbAdapter
                ->select()
                ->from('ui_settings')
                ->where('id = ' . $this->_dbAdapter->quote($id, 'INTEGER'));

            $this->renderJson($this->_dbAdapter->fetchRow($select));
        }
        catch (Exception $e)
        {
            $this->renderJson(array('error' => array('text' => $e->getMessage())));
        }
    }

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

        try
        {
            $sql = 'DELETE FROM ui_settings WHERE id=:id';

            $stmt = $this->_dbAdapter->prepare($sql);
            $stmt->bindParam('id', $id);
            $success = $stmt->execute();
            $this->renderJson($this->_statusOK);
        }
        catch (Exception $e)
        {
            $this->renderJson(array('error' => array('text' => $e->getMessage())));
        }
    }
}
