<?php

class Inbox_GroupController extends Inbox_Controller_Base
{

    public function init()
    {
        parent::init();
		
	    $this->_table = 'AbookGroupNames';

		$this->_columns = array(
			'GroupName' => '',
			'Account' => '',
			'id' => ''
		);
    }

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

            if (null === $account)
            {
                $sql = "SELECT * from {$this->_table}";
                $groups = $this->_dbAdapter->fetchAll($sql);
                $this->renderJson($groups);
            }
            else
            {
                $sql = "SELECT * from {$this->_table} where `account`=:account";
                $stmt = $this->_dbAdapter->prepare($sql);
                $stmt->bindParam('account', $account);
                $stmt->execute();
                $result = $stmt->fetchAll();
                $this->renderJson($result);
            }
        }
        catch (Exception $e)
        {
            $this->renderJsonError( $e->getMessage() );
        }
    }

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

        try
        {
            $stmt = $this->_dbAdapter->prepare($sql);
            $stmt->bindParam('id', $id);
            $stmt->execute();
            $result = $stmt->fetchObject();

            if ($result)
            {
                $this->renderJson($result);
            }
            else
            {
                $this->renderJson($this->_noResults);
            }
        }
        catch (Exception $e)
        {
           $this->renderJsonError( $e->getMessage() );
        }
    }

    public function postAction()
    {
        try
        {
            $this->populateParameters();

            $sql = $this->getInsertStatement();

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

            foreach($this->_columns as $key => $val)
            {
                if( $key != 'id' ){
                $stmt->bindParam($key, $this->_columns[$key]);
                }
            }

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

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

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

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

        try
        {
            $this->populateParameters();
            $sql = $this->getUpdateStatement();

            $stmt = $this->_dbAdapter->prepare($sql);
            
            $stmt->bindParam('id', $id);

            foreach($this->_restParameters as $key => $val)
            {
                $stmt->bindParam($key, $this->_restParameters[$key]);
            }

            $success = $stmt->execute();

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

            $this->renderJson($this->_dbAdapter->fetchRow($select));
        }
        catch (Exception $e)
        {
            $this->renderJsonError( $e->getMessage() );
            /*
            $this->renderJson(array(
                'error' => array(
                    'text' => $e->getMessage(),
                    'sql' => $sql,
                    'rest' => $this->_restParameters,
                    'json' => $this->_inputJsonArray
                )
            )); */
        }
    }

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

        try
        {
            $sql = "DELETE FROM {$this->_table} WHERE `id`=:id";

            $stmt = $this->_dbAdapter->prepare($sql);
            $stmt->bindParam('id', $id);
            $success = $stmt->execute();

            $sql = "DELETE FROM AbookGroup WHERE `GroupID`=:GroupID";
            
            $stmt = $this->_dbAdapter->prepare($sql);
            $stmt->bindParam('GroupID', $id);
            $success = $stmt->execute();

            $this->renderJson($this->_statusOK);
        }
        catch (Exception $e)
        {
           $this->renderJsonError( $e );
        }
    }

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

