<?php

// TODO: Uncomment
include "access.php";
include_once "../includes/SystemConfiguration.class.php";
include "header.php";
global $systemConfiguration;
global $logger;
$errors = array();
$message = "";
$paymentGateway = new PaymentGateway();
if (isset($_POST['SBMT_REG'])) {
    $logger->LogInfo("Form has been submitted.");
    $paymentGateway = PaymentGateway::fetchFromParameters($_POST);
    if (!$paymentGateway->save()) {
        $logger->LogError("Error saving payment gateway.");
        foreach ($paymentGateway->errors as $error) {
            $logger->LogError($error);
            $errors[] = $error;
        }
    } else {
        $message = "Values were updated successfully!";
    }
} else {
    if (isset($_REQUEST['id']) && is_numeric($_REQUEST['id'])) {
        $logger->LogInfo("Page was called for edit of id: " . $_REQUEST['id']);
        $id = intval($_REQUEST['id']);
        $logger->LogDebug("Numeric id is: {$id}");
        $paymentGateway = PaymentGateway::fetchFromDb($id);
        if ($paymentGateway == null) {
            $logger->LogError("Invalid request. No payment gateway with id: {$id} exists.");
 public static function fetchFromDbForCode($paymentGatewayCode)
 {
     PaymentGateway::$staticErrors = array();
     $sql = "SELECT * FROM bsi_payment_gateway WHERE gateway_code = '" . strtolower(trim(mysql_escape_string($paymentGatewayCode))) . "'";
     $query = mysql_query($sql);
     if (!$query) {
         global $logger;
         $logger->LogFatal("Error executing query: {$sql}");
         $logger->LogFatal("Database error: " . mysql_errno() . ". Message: " . mysql_error());
         die("Database error: " . mysql_errno() . ". Message: " . mysql_error());
     }
     if ($row = mysql_fetch_assoc($query)) {
         $paymentGateway = PaymentGateway::fetchFromParameters($row);
         return $paymentGateway;
     } else {
         PaymentGateway::setStaticError("Payment gateway does not exist.");
         return null;
     }
 }