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

    private $platforms = ['ios' => 1,'android' => 2, 'web' => 3];
    private $is_active = ['active' => 1, 'in_active' => 0];


    // Check if record exists for the user, device, platform
    public function checkActiveToken($userId, $deviceId, $platform) {
        return $this->find('first', array(
            'conditions' => array(
                'user_id'   => $userId,
                'device_id' => $deviceId,
                'platform'  => $platform
            )
        ));
    }

    // get tokens
    public function getToken($params = array()) {

        $conditions = array();

        // Support single ID or multiple IDs
        if (!empty($params['user_id'])) {
            if (is_array($params['user_id'])) {
                $conditions['PushToken.user_id'] = $params['user_id'];
            } else {
                $conditions['PushToken.user_id'] = $params['user_id'];
            }
        }

        if (!empty($params['device_id'])) {
            $conditions['PushToken.device_id'] = $params['device_id'];
        }

        if (!empty($params['platform'])) {
            $conditions['PushToken.platform'] = $params['platform'];
        }

        if (isset($params['status'])) {
            $conditions['PushToken.is_active'] = $params['status'];
        }
        if (isset($params['push_token'])) {
            $conditions['PushToken.push_token'] = $params['push_token'];
        }

        return $this->find('all', array(
            'conditions' => $conditions
        ));
    }

    public function deleteByCondition($deleteConditions)
    {
        return $this->deleteAll(
            array($deleteConditions)
        );
    }
	

	 
}