<?php
class eventclass_purchaserequestdetail  extends TableEventsBase {
	
	function init() {
		$this->events = array(
	'ProcessValuesAdd' => true,
	'OnPageLoad' => true,
	'BeforeShowAdd' => 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 ProcessValuesAdd( &$values, $pageObject ) {
		// ======================================================
// PURCHASE REQUEST DETAIL - PROCESS RECORD VALUES
// Display logged-in user's Mother Facility
// ======================================================




$motherFacilityId = 0;

if (
    isset($_SESSION["mother_facility_id"]) &&
    $_SESSION["mother_facility_id"] !== null &&
    $_SESSION["mother_facility_id"] !== ""
) {
    $motherFacilityId = (int)$_SESSION["mother_facility_id"];
}

if ($motherFacilityId > 0) {
    $values["mother_facility_id"] = $motherFacilityId;
}
		;
		
	}

	function BeforeShowAdd( &$xt, &$templatefile, $pageObject ) {
		// ======================================================
// PURCHASE REQUEST DETAIL - ADD PAGE - BEFORE DISPLAY
// Get logged-in user's Mother Facility
// ======================================================

$userName = isset($_SESSION["UserID"])
    ? $_SESSION["UserID"]
    : "";

$motherFacilityId = 0;
$motherFacilityName = "";

if ($userName !== "") {

    $sqlUser = "
        SELECT
            u.mother_facility_id,
            mf.facility_name
        FROM users u
        LEFT JOIN mother_facility mf
            ON mf.id = u.mother_facility_id
        WHERE u.username = '" . db_addslashes($userName) . "'
        LIMIT 1
    ";

    $rsUser = DB::Query($sqlUser);
    $dataUser = $rsUser ? $rsUser->fetchAssoc() : null;

    if ($dataUser) {
        $motherFacilityId =
            (int)$dataUser["mother_facility_id"];

        $motherFacilityName =
            $dataUser["facility_name"];
    }
}

$pageObject->setProxyValue(
    "mother_facility_id",
    $motherFacilityId
);

$pageObject->setProxyValue(
    "mother_facility_name",
    $motherFacilityName
);
		;
		
	}
		

}


?>