<?php
include_once '../objects/db_table_object.php';

class TriggerEvent extends DBTableObject
{
    // database connection and table name
    //private $conn;
    protected $table_name = TRIGGEREVENTS_TABLE_NAME;
    protected $array_key = "triggerevent";
	
	// object properties
    public int $indexCount = -1;
    public int $srcUserId = -1;
    public int $sessionId = -1;
    public int $type = -1;
    public $successful = 0;
    public float $timeStamp = 0.0;
	
    
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	public function toArray () : array
	{
		return array (
			"indexCount" => (int)$this->indexCount,
			"sessionId" => (int)$this->sessionId,
			"srcUserId" => (int)$this->srcUserId,
			"typeAsInt" => (int)$this->type,
			"timeStamp" => (float)$this->timeStamp,
			"successful" => $this->successful == 1 ? true : false
		);
	}
	
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	function sanitize ()
	{
        $this->indexCount=htmlspecialchars(strip_tags($this->indexCount));
        $this->sessionId=htmlspecialchars(strip_tags($this->sessionId));
        $this->srcUserId=htmlspecialchars(strip_tags($this->srcUserId));
        $this->timeStamp=htmlspecialchars(strip_tags($this->timeStamp));
        $this->successful=htmlspecialchars(strip_tags($this->successful));
        $this->type=htmlspecialchars(strip_tags($this->type));
    }

	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	function record ()
	{
        // query to insert record of new user signup
        $query = "INSERT INTO " . $this->table_name . " 
        SET sessionId=:sessionId, indexCount=:indexCount, srcUserId=:srcUserId, type=:type, timeStamp=:timeStamp, successful=:successful
        ON DUPLICATE KEY UPDATE srcUserId=:srcUserId, type=:type, timeStamp=:timeStamp, successful=:successful";
    
        // prepare query
        $stmt = $this->conn->prepare($query);
    
        // sanitize
        $this->sanitize();
    
        // bind values
        $stmt->bindParam(":indexCount", $this->indexCount);
        $stmt->bindParam(":timeStamp", $this->timeStamp);
        $stmt->bindParam(":successful", $this->successful);
        $stmt->bindParam(":sessionId", $this->sessionId);
        $stmt->bindParam(":srcUserId", $this->srcUserId);
        $stmt->bindParam(":type", $this->type);
    
        // execute query
        return $stmt->execute();
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	function recordFromReact ()
	{
        // query to insert record of new user signup
        $query = "INSERT INTO " . $this->table_name . " 
        SET sessionId=:sessionId, indexCount=:indexCount, srcUserId=:srcUserId, type=:type, timeStamp=:timeStamp, successful=:successful
        ON DUPLICATE KEY UPDATE indexCount=:indexCount";
    
        // prepare query
        $stmt = $this->conn->prepare($query);
    
        // sanitize
        $this->sanitize();
    
        // bind values
        $stmt->bindParam(":indexCount", $this->indexCount);
        $stmt->bindParam(":timeStamp", $this->timeStamp);
        $stmt->bindParam(":successful", $this->successful);
        $stmt->bindParam(":sessionId", $this->sessionId);
        $stmt->bindParam(":srcUserId", $this->srcUserId);
        $stmt->bindParam(":type", $this->type);
    
        // execute query
        return $stmt->execute();
    }
}
?>