<?php

/**
 * Database transaction
 *
 * @author: http://phpdao.com
 * @date: 27.11.2007
 */
class Transaction {

    private static $transactions;
    private $connection;

    public function Transaction() {
        $this->connection = new Connection();
        if (!Transaction::$transactions) {
            Transaction::$transactions = new ArrayList();
        }
        Transaction::$transactions->add($this);

        $myfile = fopen("backup/" . date("Y-m-d") . ".txt", "a+") or die("Unable to open file!");
        fwrite($myfile, 'BEGIN' . ";\r\n");
        fclose($myfile);

        $this->connection->executeQuery('BEGIN');
    }

    /**
     * Zakonczenie transakcji i zapisanie zmian
     */
    public function commit() {
        $myfile = fopen("backup/" . date("Y-m-d") . ".txt", "a+") or die("Unable to open file!");
        fwrite($myfile, 'COMMIT' . ";\r\n");
        fclose($myfile);

        $this->connection->executeQuery('COMMIT');
        $this->connection->close();
        Transaction::$transactions->removeLast();
    }

    /**
     * Zakonczenie transakcji i wycofanie zmian
     */
    public function rollback($exception = null) {
        // Log rollback to backup file
        $myfile = fopen("backup/" . date("Y-m-d") . ".txt", "a+") or die("Unable to open file!");
        fwrite($myfile, 'ROLLBACK' . ";\r\n");
        fclose($myfile);
        // Log exception details if provided
        if ($exception instanceof Exception) {
            $this->logException($exception);
        }

        $this->connection->executeQuery('ROLLBACK');
        $this->connection->close();
        Transaction::$transactions->removeLast();
    }

    /**
     * Log exception details with SQL and mysqli_error
     */
    private function logException($exception) {//for rollback logging

        $exceptionDetails = "/* ===== ROLLBACK =====\n";
        $exceptionDetails .= "Date: " . date("Y-m-d H:i:s") . "\n";
        $exceptionDetails .= "EXCEPTION Error Message And SQL Command: " . $exception->getMessage() . "\n";
        $exceptionDetails .= "MySQL Error: " . mysqli_error($this->connection->getConnection()) . "\n";
        $exceptionDetails .= "File: " . $exception->getFile() . "\n";
        $exceptionDetails .= "Line: " . $exception->getLine() . "\n";
        
        //$exceptionDetails .= "SQL: " . (isset($_SESSION['last_query']) ? $_SESSION['last_query'] : "N/A") . "\n";
        $exceptionDetails .= "Trace: " . $exception->getTraceAsString() . "\n";
        $exceptionDetails .= "===================== */\n";

        liveBackupCommentglobal($exceptionDetails);
    }

    /**
     * Pobranie polaczenia dla obencej transakcji
     *
     * @return polazenie do bazy
     */
    public function getConnection() {
        return $this->connection;
    }

    /**
     * Zwraca obecna transakcje
     *
     * @return transkacja
     */
    public static function getCurrentTransaction() {
        if (Transaction::$transactions) {
            $tran = Transaction::$transactions->getLast();
            return $tran;
        }
        return;
    }

}

?>