<?php
class AccessKey extends AppModel
{
    var $name='AccessKey';

    public function saveAccessKey($formData){

        $error = "";

        $form["device_id"] = trim($formData["device_id"]);
        $form["secret_key"] = trim($formData["secret_key"]);

        $form["created_on"] = trim($formData["created_on"]);

        $fileds = array('device_id', 'secret_key', 'created_on');

        $data["AccessKey"] = $form;
        if(!empty($form["device_id"]) && !empty($form["secret_key"])){
            if(!$this->save($data,false,$fileds)){
                $error = "22";
            }
        }else{
            $error = "23";
        }

        return $error;
    }

    public function getAccessToken($device_id)
    {
        $error = "";
        $access_key = $this->find('first', array(
            'conditions' => array('device_id' => $device_id),
            'order'=> array('created_on'=>'desc')
        )	);
        if(empty($access_key)) {
            $error = "23";
            return $error;
        } else {
            return $access_key['AccessKey']['secret_key'];
        }
    }

    public function deleteAccessKey($device_id)
    {
        $access_key = $this->find('first', array(
            'conditions' => array('device_id' => $device_id),
            'order'=> array('created_on'=>'desc')
        )	);
        $this->delete($access_key['AccessKey']['id']);
    }
}