<?php

class Push {

	public function getMemberPlayerId($db, $member_id, $player_id, $access_token) {
		$db_player_id = pg_escape_string($player_id);
		$db_access_token = pg_escape_string(substr($access_token,0,36));
		$q = "SELECT * FROM members_devices WHERE member_id=".((int)$member_id)." AND player_id='${db_player_id}' AND (player_id<>'' OR model_id=1)";
		error_log($q);
		$r = pg_query($db, $q);
		if ($r && pg_num_rows($r) && $f=pg_fetch_assoc($r)) {
			return $f;
		}
		$q = "SELECT * FROM members_devices WHERE member_id=".((int)$member_id)." AND player_id='' AND access_token='${db_access_token}'";
		error_log($q);
		$r = pg_query($db, $q);
		if ($r && pg_num_rows($r) && $f=pg_fetch_assoc($r)) {
			return $f;
		}
		return NULL;
	}

	public function saveMemberPlayerId($db, $data) {
		$db_player_id = pg_escape_string(substr($data['player_id'],0,36));
		$db_cordova = pg_escape_string(substr($data['cordova'],0,10));
		$model = Push::getModelByName($db, $data['model']);
		$model_id = $model==NULL?0:$model['id'];
		$platform = Push::getPlatformByName($db, $data['platform']);
		$platform_id = $platform==NULL?0:$platform['id'];
		$db_uuid = pg_escape_string(substr($data['uuid'],0,36));
		$db_version = pg_escape_string(substr($data['version'],0,10));
		$manufacturer = Push::getManufacturerByName($db, $data['manufacturer']);
		$manufacturer_id = $manufacturer==NULL?0:$manufacturer['id'];
		$is_virtual = (!$data['is_virtual'] || $data['is_virtual']=='') ? 'f' : 't';
		$db_country = pg_escape_string($data['country']);
		$db_access_token = pg_escape_string($data['access_token']);
		$db_refresh_token = pg_escape_string($data['refresh_token']);
		
		$device = Push::getMemberPlayerId($db, $data['member_id'], $data['player_id'], $data['access_token']);
		if ($device && isset($device["id"]) && $device["id"]>0) {
			$q = "UPDATE members_devices SET ".(($device["player_id"]=="" || $device["player_id"]!=$data['player_id'])?"player_id='${db_player_id}',":"");
			$q.= "cordova='${db_cordova}',model_id=".((int)$model_id).",platform_id=".((int)$platform_id).",uuid='${db_uuid}',version='${db_version}',";
			$q.= "manufacturer_id=".((int)$manufacturer_id).",is_virtual='${is_virtual}', updated=now(), status=1, ";
			$q.= "country='${db_country}'";
			$q.= " WHERE id=".$device['id']." RETURNING *";
			error_log($q);
			$r = pg_query($db, $q);
			if ($r && pg_num_rows($r) && $f=pg_fetch_assoc($r)) {
				return $f; // Updated
			}
			return $device; // No update
		} else {
			$q = "INSERT INTO members_devices (member_id,player_id,cordova,model_id,platform_id,uuid,version,manufacturer_id,is_virtual,access_token,refresh_token,status,country) VALUES (";
			$q.= ((int)$data['member_id']).",'${db_player_id}','${db_cordova}',".((int)$model_id).",".((int)$platform_id).",'${db_uuid}','${db_version}',".((int)$manufacturer_id).",";
			$q.= "'${is_virtual}','${db_access_token}','${db_refresh_token}',1,'${db_country}') RETURNING *";
			error_log($q);
			$r = pg_query($db, $q);
			if ($r && pg_num_rows($r) && $f=pg_fetch_assoc($r)) {
				return $f; // New record
			}
		}
		return NULL;
	}

	public function getModelByName($db, $model) {
		$db_model = pg_escape_string(substr($model,0,100));
		$q = "SELECT * FROM members_device_models WHERE lower(model)=lower('${db_model}')";
		$r = pg_query($db, $q);
		if ($r && pg_num_rows($r) && $f=pg_fetch_assoc($r)) {
			return $f;
		}
		$q = "INSERT INTO members_device_models (model) VALUES ('${db_model}') RETURNING *";
		$r = pg_query($db, $q);
		if ($r && pg_num_rows($r) && $f=pg_fetch_assoc($r)) {
			return $f;
		}
		return NULL;
	}

	public function getPlatformByName($db, $platform) {
		$db_platform = pg_escape_string(substr($platform,0,50));
		$q = "SELECT * FROM members_device_platforms WHERE lower(platform)=lower('${db_platform}')";
		$r = pg_query($db, $q);
		if ($r && pg_num_rows($r) && $f=pg_fetch_assoc($r)) {
			return $f;
		}
		$q = "INSERT INTO members_device_platforms (platform) VALUES ('${db_platform}') RETURNING *";
		$r = pg_query($db, $q);
		if ($r && pg_num_rows($r) && $f=pg_fetch_assoc($r)) {
			return $f;
		}
		return NULL;
	}

	public function getManufacturerByName($db, $manufacturer) {
		$db_manufacturer = pg_escape_string(substr($manufacturer,0,50));
		$q = "SELECT * FROM members_device_manufacturers WHERE lower(manufacturer)=lower('${db_manufacturer}')";
		$r = pg_query($db, $q);
		if ($r && pg_num_rows($r) && $f=pg_fetch_assoc($r)) {
			return $f;
		}
		$q = "INSERT INTO members_device_manufacturers (manufacturer) VALUES ('${db_manufacturer}') RETURNING *";
		$r = pg_query($db, $q);
		if ($r && pg_num_rows($r) && $f=pg_fetch_assoc($r)) {
			return $f;
		}
		return NULL;
	}
}

// vi:ts=2

