<?php

class Callback {

	public function getMemberById($db, $id) {
		$result = array();
		$db_id = (int)$id;
		$q = "SELECT * FROM members WHERE id=${db_id}";
		$r = pg_query($db, $q);
		if ($r && pg_num_rows($r) && $f=pg_fetch_assoc($r)) {
			$result = $f;
		}
		return $result;
	}

	public function getMemberByEmail($db, $email) {
		$result = array();
		$db_email = pg_escape_string(strtolower($email));
		$q = "SELECT * FROM members WHERE lower(email)='${db_email}' OR lower(username)='${db_email}'";
		$r = pg_query($db, $q);
		if ($r && pg_num_rows($r) && $f=pg_fetch_assoc($r)) {
			$result = $f;
		}
		return $result;
	}

	public function updateMember($db, $member_id, $last_acct, $count_acct) {
		$result = array();
		$id = (int)$member_id;
		$db_last_acct = date("Y-m-d H:i:s",strtotime($last_acct));
		$db_count_acct = (int)$count_acct;
		$q = "UPDATE members SET last_acct='${db_last_acct}',count_acct=${db_count_acct} WHERE id=${id}";
		$r = pg_query($db, $q);
		if ($r && pg_affected_rows($r)) {
			return Callback::getMemberById($db, $id);
		}
		return $result;
	}
}

// vi:ts=2

