<?php
class eventclass_dromic_api_config  extends TableEventsBase {
	
	function init() {
		$this->events = array(
	'BeforeAdd' => true,
	'BeforeEdit' => true 
);
		$this->fieldValues = array(
	'filterLimit' => array(
		 
	),
	'mapIcon' => array(
		 
	),
	'viewCustom' => array(
		 
	),
	'lookupWhere' => array(
		 
	),
	'viewFileText' => array(
		 
	),
	'defaultValue' => array(
		 
	),
	'autoUpdateValue' => array(
		 
	),
	'uploadFolder' => array(
		 
	),
	'viewPluginInit' => array(
		 
	),
	'editPluginInit' => array(
		 
	) 
);
			}
		function BeforeAdd( &$values, &$sqlValues, &$message, $inline, $pageObject ) {
		$values["environment"] = trim($values["environment"]);
$values["base_url"] = trim($values["base_url"]);
$values["username"] = trim($values["username"]);
$values["password_value"] = trim($values["password_value"]);

if ($values["environment"] == "") {
    $message = "Environment is required.";
    return false;
}

if ($values["environment"] != "STAGING" && $values["environment"] != "PRODUCTION") {
    $message = "Environment must be STAGING or PRODUCTION.";
    return false;
}

if ($values["base_url"] == "") {
    $message = "Base URL is required.";
    return false;
}

if (!preg_match('/^https:\/\/.+/i', $values["base_url"])) {
    $message = "Base URL must start with https://";
    return false;
}

/*
    Remove trailing slash so API call will not become:
    https://domain.com//api/...
*/
$values["base_url"] = rtrim($values["base_url"], "/");

if ($values["username"] == "") {
    $message = "Username is required.";
    return false;
}

if ($values["password_value"] == "") {
    $message = "Password is required.";
    return false;
}

/*
    If this record is active, deactivate all other DROMIC API configs.
*/
if (isset($values["is_active"]) && $values["is_active"]) {
    DB::Exec("
        UPDATE dromic_api_config
        SET is_active = 0,
            updated_at = NOW()
    ");
}

/*
    Token should always be generated by the import button,
    not manually entered.
*/
$values["access_token"] = null;
$values["token_expires_at"] = null;

$values["created_at"] = date("Y-m-d H:i:s");
$values["updated_at"] = date("Y-m-d H:i:s");

return true;
		;
		return true;
	}

	function BeforeEdit( &$values, &$sqlValues, $where, &$oldvalues, &$keys, &$message, $inline, $pageObject ) {
		$values["environment"] = trim($values["environment"]);
$values["base_url"] = trim($values["base_url"]);
$values["username"] = trim($values["username"]);
$values["password_value"] = trim($values["password_value"]);

if ($values["environment"] == "") {
    $message = "Environment is required.";
    return false;
}

if ($values["environment"] != "STAGING" && $values["environment"] != "PRODUCTION") {
    $message = "Environment must be STAGING or PRODUCTION.";
    return false;
}

if ($values["base_url"] == "") {
    $message = "Base URL is required.";
    return false;
}

if (!preg_match('/^https:\/\/.+/i', $values["base_url"])) {
    $message = "Base URL must start with https://";
    return false;
}

$values["base_url"] = rtrim($values["base_url"], "/");

if ($values["username"] == "") {
    $message = "Username is required.";
    return false;
}

if ($values["password_value"] == "") {
    $message = "Password is required.";
    return false;
}

$currentConfigId = 0;

if (isset($keys["config_id"])) {
    $currentConfigId = (int)$keys["config_id"];
}

/*
    If this config is active, deactivate other configs.
*/
if (isset($values["is_active"]) && $values["is_active"]) {
    DB::Exec("
        UPDATE dromic_api_config
        SET is_active = 0,
            updated_at = NOW()
        WHERE config_id <> " . $currentConfigId
    );
}

/*
    If API credentials or URL changed, clear old token.
    The import button will request a fresh token.
*/
$credentialChanged = false;

if (isset($oldvalues["environment"]) && $oldvalues["environment"] != $values["environment"]) {
    $credentialChanged = true;
}

if (isset($oldvalues["base_url"]) && rtrim($oldvalues["base_url"], "/") != $values["base_url"]) {
    $credentialChanged = true;
}

if (isset($oldvalues["username"]) && $oldvalues["username"] != $values["username"]) {
    $credentialChanged = true;
}

if (isset($oldvalues["password_value"]) && $oldvalues["password_value"] != $values["password_value"]) {
    $credentialChanged = true;
}

if ($credentialChanged) {
    $values["access_token"] = null;
    $values["token_expires_at"] = null;
}

$values["updated_at"] = date("Y-m-d H:i:s");

return true;
		;
		return true;
	}
		

}


?>