<?php
class eventclass_system_setting  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 ) {
		/* Normalize submitted values */
$values["setting_group"] = strtoupper(
    trim((string)($values["setting_group"] ?? "GENERAL"))
);

$values["setting_key"] = strtoupper(
    trim((string)($values["setting_key"] ?? ""))
);

$values["setting_value"] = trim(
    (string)($values["setting_value"] ?? "")
);

$values["description"] = trim(
    (string)($values["description"] ?? "")
);

$values["is_active"] =
    !empty($values["is_active"]) ? 1 : 0;


/* Required validation */
if ($values["setting_group"] === "") {
    $message = "Setting Group is required.";
    return false;
}

if ($values["setting_key"] === "") {
    $message = "Setting Key is required.";
    return false;
}

if ($values["setting_value"] === "") {
    $message = "Setting Value is required.";
    return false;
}


/* Setting key format validation */
if (
    !preg_match(
        '/^[A-Z][A-Z0-9_]*$/',
        $values["setting_key"]
    )
) {
    $message =
        "Setting Key must begin with a letter and may contain only " .
        "uppercase letters, numbers, and underscores.";

    return false;
}


/* Duplicate check */
$safeSettingKey =
    db_addslashes($values["setting_key"]);

$rsDuplicate = DB::Query("
    SELECT setting_id
    FROM system_setting
    WHERE setting_key = '" . $safeSettingKey . "'
    LIMIT 1
");

if (
    $rsDuplicate &&
    $rsDuplicate->fetchAssoc()
) {
    $message =
        "The setting key " .
        $values["setting_key"] .
        " already exists.";

    return false;
}

return true;
		;
		return true;
	}

	function BeforeEdit( &$values, &$sqlValues, $where, &$oldvalues, &$keys, &$message, $inline, $pageObject ) {
		/* Do not allow the setting key to be renamed */
$values["setting_key"] =
    strtoupper(
        trim(
            (string)($oldvalues["setting_key"] ?? "")
        )
    );


/* Normalize group */
$values["setting_group"] =
    strtoupper(
        trim(
            (string)($values["setting_group"] ?? "GENERAL")
        )
    );


/*
 * If Setting Value is left blank,
 * retain the existing value.
 */
$newSettingValue =
    trim(
        (string)($values["setting_value"] ?? "")
    );

if ($newSettingValue === "") {
    $values["setting_value"] =
        $oldvalues["setting_value"];
} else {
    $values["setting_value"] =
        $newSettingValue;
}


$values["description"] =
    trim(
        (string)($values["description"] ?? "")
    );

$values["is_active"] =
    !empty($values["is_active"]) ? 1 : 0;


/* Validation */
if ($values["setting_group"] === "") {
    $message = "Setting Group is required.";
    return false;
}

if (
    !isset($values["setting_value"]) ||
    trim((string)$values["setting_value"]) === ""
) {
    $message = "Setting Value cannot be empty.";
    return false;
}

return true;
		;
		return true;
	}
		

}


?>