<?php
class SablePush
{
	var $title, $msg, $repeatCount, $repeatInterval, $userCode, $url; 
	public function __construct(){}
	
	public function listNotification()
	{
		global $sableDB;
		return $sableDB->getRows('select * from push_notification');
	}
	
	public function listNotificationUser($userCode)
	{
		global $sableDB;
		$sqlQuery = "select * from push_notification WHERE user_code = ".$userCode.
					" and push_notification_loop > 0";// and push_notification_time <= '".date('Y-m-d H:i:s')."'";
		$dt = $sableDB->getRows($sqlQuery);
		return $dt;
	}
	
	public function listUsers()
	{
		global $sableDB;
		return $sableDB->getRows("select * from push_notification");
	}
	
	public function loginUsers($username, $password)
	{
		$sqlQuery = "SELECT id as userid, username, password FROM ".$this->userTable." WHERE username='$username' AND password='$password'";
		return $this->getData($sqlQuery);
	}
	
	public function saveNotification()
	{
		/*
		1	push_notification_id
		2	push_notification_title	
		3	push_notification_message
		4	push_url
		4	push_notification_time
		6	push_notification_repeat
		7	push_notification_loop
		8	push_notification_published
		9	user_code
		*/
		if (SABLE_PUSH_ENABLED)
		{
			global $sableDB;
			$sqlQuery = "insert into push_notification 
						(push_notification_message, push_notification_title, push_notification_time, push_notification_loop, push_notification_repeat, user_code, push_url)
						 VALUES('".addslashes($this->msg)."', '".$this->title."', '".date('Y-m-d H:i:s')."', '".$this->repeatCount."', '".$this->repeatInterval."', '".$this->userCode."', '".$this->url."')";
			$result = $sableDB->updateQuery($sqlQuery);
			if (!$result)
			{
				SableSystem::ajaxResponse(false, "", "Failed to save the notification.", 5, "edtNotification", false, "edtNotification");
			} 
			else 
			{
				return $result;
			}
		} else return false;
	}
	
	public function updateNotification($id, $nextTime)
	{
		global $sableDB;
		$sqlUpdate = "update push_notification 
						set push_notification_time = '".$nextTime."', push_notification_published = CURRENT_TIMESTAMP(),
						push_notification_loop = (push_notification_loop - 1) 
						WHERE push_notification_id = ".$id;
		$res = $sableDB->updateQuery($sqlUpdate);
	}
}
?>