<?

class TLegacyDjSeat {

	private $db;
	private $localpath;

	function __construct($db, $localpath) {
		$this->db = $db;
		$this->localpath = $localpath;
	}
	
	public function Extend_DJSeat($cpID, $trace = false) {
		//Each quarter, extend all DJ Seat licenses for another 3 months
		require_once($this->localpath . "/class.product.php");
		$product = new TProduct($this->db, $this->localpath);

		//Output details to screen
		if ($trace)
			echo "==================== ClientProductID: $cpID ===========================<br>\n";

		$this->db->openEx("SELECT * FROM client_product WHERE (ID = :cpID) ", $cpID);
		if ($this->db->num_rows() > 0) {
			$client_product = $this->db->row();
			//1. Generate ney key
			$keyinfo = $product->product_regkey($client_product["clientID"], $client_product["productID"], false);
			$regkey = $keyinfo["key"];
			$date_expire = $keyinfo["date_expire"];
			
			//2. Update registration key
			$this->db->openEx("UPDATE client_product SET regkey=:regKey, date_expire=:date_expire WHERE (ID = :cpID) ", $regkey, $date_expire, $cpID);
			
			$this->db->openEx("SELECT * FROM client_product WHERE (ID = :cpID) ", $cpID);
			if ($this->db->num_rows() > 0) {
				$updatedDjProduct = $this->db->row();
				$date_expire = $updatedDjProduct["date_expire"];
			}

			//Save details for email
			$client_product["name"] = "DJ Seat license (SAM Broadcaster)";
			if ($client_product["productID"] == 15)
				$client_product["version"] = "v3.x";
			else
				$client_product["version"] = "v4.x";

			$client_product["regkey"] = $regkey;
			$client_product["date_expire"] = $date_expire;

			//Output details to screen
			if ($trace)
				echo "client_productID: $cpID, RegKey: $regkey, Notes: $client_product[notes] <br>\n";
			return $client_product;
		}//if
	}
}