<?php

namespace PHPMaker2021\rdigital21;

use Doctrine\DBAL\ParameterType;

/**
 * Page class
 */
class NilaiFormatifEdit extends NilaiFormatif
{
    use MessagesTrait;

    // Page ID
    public $PageID = "edit";

    // Project ID
    public $ProjectID = PROJECT_ID;

    // Table name
    public $TableName = 'nilai_formatif';

    // Page object name
    public $PageObjName = "NilaiFormatifEdit";

    // Rendering View
    public $RenderingView = false;

    // Page headings
    public $Heading = "";
    public $Subheading = "";
    public $PageHeader;
    public $PageFooter;

    // Page terminated
    private $terminated = false;

    // Page heading
    public function pageHeading()
    {
        global $Language;
        if ($this->Heading != "") {
            return $this->Heading;
        }
        if (method_exists($this, "tableCaption")) {
            return $this->tableCaption();
        }
        return "";
    }

    // Page subheading
    public function pageSubheading()
    {
        global $Language;
        if ($this->Subheading != "") {
            return $this->Subheading;
        }
        if ($this->TableName) {
            return $Language->phrase($this->PageID);
        }
        return "";
    }

    // Page name
    public function pageName()
    {
        return CurrentPageName();
    }

    // Page URL
    public function pageUrl()
    {
        $url = ScriptName() . "?";
        if ($this->UseTokenInUrl) {
            $url .= "t=" . $this->TableVar . "&"; // Add page token
        }
        return $url;
    }

    // Show Page Header
    public function showPageHeader()
    {
        $header = $this->PageHeader;
        $this->pageDataRendering($header);
        if ($header != "") { // Header exists, display
            echo '<p id="ew-page-header">' . $header . '</p>';
        }
    }

    // Show Page Footer
    public function showPageFooter()
    {
        $footer = $this->PageFooter;
        $this->pageDataRendered($footer);
        if ($footer != "") { // Footer exists, display
            echo '<p id="ew-page-footer">' . $footer . '</p>';
        }
    }

    // Validate page request
    protected function isPageRequest()
    {
        global $CurrentForm;
        if ($this->UseTokenInUrl) {
            if ($CurrentForm) {
                return ($this->TableVar == $CurrentForm->getValue("t"));
            }
            if (Get("t") !== null) {
                return ($this->TableVar == Get("t"));
            }
        }
        return true;
    }

    // Constructor
    public function __construct()
    {
        global $Language, $DashboardReport, $DebugTimer;
        global $UserTable;

        // Custom template
        $this->UseCustomTemplate = true;

        // Initialize
        $GLOBALS["Page"] = &$this;

        // Language object
        $Language = Container("language");

        // Parent constuctor
        parent::__construct();

        // Table object (nilai_formatif)
        if (!isset($GLOBALS["nilai_formatif"]) || get_class($GLOBALS["nilai_formatif"]) == PROJECT_NAMESPACE . "nilai_formatif") {
            $GLOBALS["nilai_formatif"] = &$this;
        }

        // Page URL
        $pageUrl = $this->pageUrl();

        // Table name (for backward compatibility only)
        if (!defined(PROJECT_NAMESPACE . "TABLE_NAME")) {
            define(PROJECT_NAMESPACE . "TABLE_NAME", 'nilai_formatif');
        }

        // Start timer
        $DebugTimer = Container("timer");

        // Debug message
        LoadDebugMessage();

        // Open connection
        $GLOBALS["Conn"] = $GLOBALS["Conn"] ?? $this->getConnection();

        // User table object
        $UserTable = Container("usertable");
    }

    // Get content from stream
    public function getContents($stream = null): string
    {
        global $Response;
        return is_object($Response) ? $Response->getBody() : ob_get_clean();
    }

    // Is lookup
    public function isLookup()
    {
        return SameText(Route(0), Config("API_LOOKUP_ACTION"));
    }

    // Is AutoFill
    public function isAutoFill()
    {
        return $this->isLookup() && SameText(Post("ajax"), "autofill");
    }

    // Is AutoSuggest
    public function isAutoSuggest()
    {
        return $this->isLookup() && SameText(Post("ajax"), "autosuggest");
    }

    // Is modal lookup
    public function isModalLookup()
    {
        return $this->isLookup() && SameText(Post("ajax"), "modal");
    }

    // Is terminated
    public function isTerminated()
    {
        return $this->terminated;
    }

    /**
     * Terminate page
     *
     * @param string $url URL for direction
     * @return void
     */
    public function terminate($url = "")
    {
        if ($this->terminated) {
            return;
        }
        global $ExportFileName, $TempImages, $DashboardReport, $Response;

        // Page is terminated
        $this->terminated = true;
        global $OldSkipHeaderFooter, $SkipHeaderFooter;
        $SkipHeaderFooter = $OldSkipHeaderFooter;
        if (Post("customexport") === null) {
             // Page Unload event
            if (method_exists($this, "pageUnload")) {
                $this->pageUnload();
            }

            // Global Page Unloaded event (in userfn*.php)
            Page_Unloaded();
        }

        // Export
        if ($this->CustomExport && $this->CustomExport == $this->Export && array_key_exists($this->CustomExport, Config("EXPORT_CLASSES"))) {
            if (is_array(Session(SESSION_TEMP_IMAGES))) { // Restore temp images
                $TempImages = Session(SESSION_TEMP_IMAGES);
            }
            if (Post("data") !== null) {
                $content = Post("data");
            }
            $ExportFileName = Post("filename", "");
            if ($ExportFileName == "") {
                $ExportFileName = $this->TableVar;
            }
            $class = PROJECT_NAMESPACE . Config("EXPORT_CLASSES." . $this->CustomExport);
            if (class_exists($class)) {
                $doc = new $class(Container("nilai_formatif"));
                $doc->Text = @$content;
                if ($this->isExport("email")) {
                    echo $this->exportEmail($doc->Text);
                } else {
                    $doc->export();
                }
                DeleteTempImages(); // Delete temp images
                return;
            }
        }
        if ($this->CustomExport) { // Save temp images array for custom export
            if (is_array($TempImages)) {
                $_SESSION[SESSION_TEMP_IMAGES] = $TempImages;
            }
        }
        if (!IsApi() && method_exists($this, "pageRedirecting")) {
            $this->pageRedirecting($url);
        }

        // Close connection
        CloseConnections();

        // Return for API
        if (IsApi()) {
            $res = $url === true;
            if (!$res) { // Show error
                WriteJson(array_merge(["success" => false], $this->getMessages()));
            }
            return;
        } else { // Check if response is JSON
            if (StartsString("application/json", $Response->getHeaderLine("Content-type")) && $Response->getBody()->getSize()) { // With JSON response
                $this->clearMessages();
                return;
            }
        }

        // Go to URL if specified
        if ($url != "") {
            if (!Config("DEBUG") && ob_get_length()) {
                ob_end_clean();
            }

            // Handle modal response
            if ($this->IsModal) { // Show as modal
                $row = ["url" => GetUrl($url), "modal" => "1"];
                $pageName = GetPageName($url);
                if ($pageName != $this->getListUrl()) { // Not List page
                    $row["caption"] = $this->getModalCaption($pageName);
                    if ($pageName == "NilaiFormatifView") {
                        $row["view"] = "1";
                    }
                } else { // List page should not be shown as modal => error
                    $row["error"] = $this->getFailureMessage();
                    $this->clearFailureMessage();
                }
                WriteJson($row);
            } else {
                SaveDebugMessage();
                Redirect(GetUrl($url));
            }
        }
        return; // Return to controller
    }

    // Get records from recordset
    protected function getRecordsFromRecordset($rs, $current = false)
    {
        $rows = [];
        if (is_object($rs)) { // Recordset
            while ($rs && !$rs->EOF) {
                $this->loadRowValues($rs); // Set up DbValue/CurrentValue
                $row = $this->getRecordFromArray($rs->fields);
                if ($current) {
                    return $row;
                } else {
                    $rows[] = $row;
                }
                $rs->moveNext();
            }
        } elseif (is_array($rs)) {
            foreach ($rs as $ar) {
                $row = $this->getRecordFromArray($ar);
                if ($current) {
                    return $row;
                } else {
                    $rows[] = $row;
                }
            }
        }
        return $rows;
    }

    // Get record from array
    protected function getRecordFromArray($ar)
    {
        $row = [];
        if (is_array($ar)) {
            foreach ($ar as $fldname => $val) {
                if (array_key_exists($fldname, $this->Fields) && ($this->Fields[$fldname]->Visible || $this->Fields[$fldname]->IsPrimaryKey)) { // Primary key or Visible
                    $fld = &$this->Fields[$fldname];
                    if ($fld->HtmlTag == "FILE") { // Upload field
                        if (EmptyValue($val)) {
                            $row[$fldname] = null;
                        } else {
                            if ($fld->DataType == DATATYPE_BLOB) {
                                $url = FullUrl(GetApiUrl(Config("API_FILE_ACTION") .
                                    "/" . $fld->TableVar . "/" . $fld->Param . "/" . rawurlencode($this->getRecordKeyValue($ar))));
                                $row[$fldname] = ["type" => ContentType($val), "url" => $url, "name" => $fld->Param . ContentExtension($val)];
                            } elseif (!$fld->UploadMultiple || !ContainsString($val, Config("MULTIPLE_UPLOAD_SEPARATOR"))) { // Single file
                                $url = FullUrl(GetApiUrl(Config("API_FILE_ACTION") .
                                    "/" . $fld->TableVar . "/" . Encrypt($fld->physicalUploadPath() . $val)));
                                $row[$fldname] = ["type" => MimeContentType($val), "url" => $url, "name" => $val];
                            } else { // Multiple files
                                $files = explode(Config("MULTIPLE_UPLOAD_SEPARATOR"), $val);
                                $ar = [];
                                foreach ($files as $file) {
                                    $url = FullUrl(GetApiUrl(Config("API_FILE_ACTION") .
                                        "/" . $fld->TableVar . "/" . Encrypt($fld->physicalUploadPath() . $file)));
                                    if (!EmptyValue($file)) {
                                        $ar[] = ["type" => MimeContentType($file), "url" => $url, "name" => $file];
                                    }
                                }
                                $row[$fldname] = $ar;
                            }
                        }
                    } else {
                        $row[$fldname] = $val;
                    }
                }
            }
        }
        return $row;
    }

    // Get record key value from array
    protected function getRecordKeyValue($ar)
    {
        $key = "";
        if (is_array($ar)) {
            $key .= @$ar['nilai_f_id'];
        }
        return $key;
    }

    /**
     * Hide fields for add/edit
     *
     * @return void
     */
    protected function hideFieldsForAddEdit()
    {
    }

    // Lookup data
    public function lookup()
    {
        global $Language, $Security;

        // Get lookup object
        $fieldName = Post("field");
        $lookup = $this->Fields[$fieldName]->Lookup;

        // Get lookup parameters
        $lookupType = Post("ajax", "unknown");
        $pageSize = -1;
        $offset = -1;
        $searchValue = "";
        if (SameText($lookupType, "modal")) {
            $searchValue = Post("sv", "");
            $pageSize = Post("recperpage", 10);
            $offset = Post("start", 0);
        } elseif (SameText($lookupType, "autosuggest")) {
            $searchValue = Param("q", "");
            $pageSize = Param("n", -1);
            $pageSize = is_numeric($pageSize) ? (int)$pageSize : -1;
            if ($pageSize <= 0) {
                $pageSize = Config("AUTO_SUGGEST_MAX_ENTRIES");
            }
            $start = Param("start", -1);
            $start = is_numeric($start) ? (int)$start : -1;
            $page = Param("page", -1);
            $page = is_numeric($page) ? (int)$page : -1;
            $offset = $start >= 0 ? $start : ($page > 0 && $pageSize > 0 ? ($page - 1) * $pageSize : 0);
        }
        $userSelect = Decrypt(Post("s", ""));
        $userFilter = Decrypt(Post("f", ""));
        $userOrderBy = Decrypt(Post("o", ""));
        $keys = Post("keys");
        $lookup->LookupType = $lookupType; // Lookup type
        if ($keys !== null) { // Selected records from modal
            if (is_array($keys)) {
                $keys = implode(Config("MULTIPLE_OPTION_SEPARATOR"), $keys);
            }
            $lookup->FilterFields = []; // Skip parent fields if any
            $lookup->FilterValues[] = $keys; // Lookup values
            $pageSize = -1; // Show all records
        } else { // Lookup values
            $lookup->FilterValues[] = Post("v0", Post("lookupValue", ""));
        }
        $cnt = is_array($lookup->FilterFields) ? count($lookup->FilterFields) : 0;
        for ($i = 1; $i <= $cnt; $i++) {
            $lookup->FilterValues[] = Post("v" . $i, "");
        }
        $lookup->SearchValue = $searchValue;
        $lookup->PageSize = $pageSize;
        $lookup->Offset = $offset;
        if ($userSelect != "") {
            $lookup->UserSelect = $userSelect;
        }
        if ($userFilter != "") {
            $lookup->UserFilter = $userFilter;
        }
        if ($userOrderBy != "") {
            $lookup->UserOrderBy = $userOrderBy;
        }
        $lookup->toJson($this); // Use settings from current page
    }
    public $FormClassName = "ew-horizontal ew-form ew-edit-form";
    public $IsModal = false;
    public $IsMobileOrModal = false;
    public $DbMasterFilter;
    public $DbDetailFilter;
    public $HashValue; // Hash Value
    public $DisplayRecords = 1;
    public $StartRecord;
    public $StopRecord;
    public $TotalRecords = 0;
    public $RecordRange = 10;
    public $RecordCount;

    /**
     * Page run
     *
     * @return void
     */
    public function run()
    {
        global $ExportType, $CustomExportType, $ExportFileName, $UserProfile, $Language, $Security, $CurrentForm,
            $SkipHeaderFooter;

        // Is modal
        $this->IsModal = Param("modal") == "1";

        // Create form object
        $CurrentForm = new HttpForm();
        $this->CurrentAction = Param("action"); // Set up current action
        global $OldSkipHeaderFooter, $SkipHeaderFooter;
        $OldSkipHeaderFooter = $SkipHeaderFooter;
        $SkipHeaderFooter = true;
        $this->nilai_f_id->setVisibility();
        $this->kode_tapel->setVisibility();
        $this->penilaian_id->setVisibility();
        $this->guru_id->setVisibility();
        $this->kode_mapel->setVisibility();
        $this->kelas->setVisibility();
        $this->nis->setVisibility();
        $this->nama->setVisibility();
        $this->jk->setVisibility();
        $this->f1->setVisibility();
        $this->f2->setVisibility();
        $this->f3->setVisibility();
        $this->f4->setVisibility();
        $this->f5->setVisibility();
        $this->f6->setVisibility();
        $this->f7->setVisibility();
        $this->f8->setVisibility();
        $this->f9->setVisibility();
        $this->f10->setVisibility();
        $this->f11->setVisibility();
        $this->f12->setVisibility();
        $this->f13->setVisibility();
        $this->f14->setVisibility();
        $this->f15->setVisibility();
        $this->naf->setVisibility();
        $this->hideFieldsForAddEdit();

        // Do not use lookup cache
        $this->setUseLookupCache(false);

        // Global Page Loading event (in userfn*.php)
        Page_Loading();

        // Page Load event
        if (method_exists($this, "pageLoad")) {
            $this->pageLoad();
        }

        // Set up lookup cache

        // Check modal
        if ($this->IsModal) {
            $SkipHeaderFooter = true;
        }
        $this->IsMobileOrModal = IsMobile() || $this->IsModal;
        $this->FormClassName = "ew-form ew-edit-form ew-horizontal";
        $loaded = false;
        $postBack = false;

        // Set up current action and primary key
        if (IsApi()) {
            // Load key values
            $loaded = true;
            if (($keyValue = Get("nilai_f_id") ?? Key(0) ?? Route(2)) !== null) {
                $this->nilai_f_id->setQueryStringValue($keyValue);
                $this->nilai_f_id->setOldValue($this->nilai_f_id->QueryStringValue);
            } elseif (Post("nilai_f_id") !== null) {
                $this->nilai_f_id->setFormValue(Post("nilai_f_id"));
                $this->nilai_f_id->setOldValue($this->nilai_f_id->FormValue);
            } else {
                $loaded = false; // Unable to load key
            }

            // Load record
            if ($loaded) {
                $loaded = $this->loadRow();
            }
            if (!$loaded) {
                $this->setFailureMessage($Language->phrase("NoRecord")); // Set no record message
                $this->terminate();
                return;
            }
            $this->CurrentAction = "update"; // Update record directly
            $this->OldKey = $this->getKey(true); // Get from CurrentValue
            $postBack = true;
        } else {
            if (Post("action") !== null) {
                $this->CurrentAction = Post("action"); // Get action code
                if (!$this->isShow()) { // Not reload record, handle as postback
                    $postBack = true;
                }

                // Get key from Form
                $this->setKey(Post($this->OldKeyName), $this->isShow());
            } else {
                $this->CurrentAction = "show"; // Default action is display

                // Load key from QueryString
                $loadByQuery = false;
                if (($keyValue = Get("nilai_f_id") ?? Route("nilai_f_id")) !== null) {
                    $this->nilai_f_id->setQueryStringValue($keyValue);
                    $loadByQuery = true;
                } else {
                    $this->nilai_f_id->CurrentValue = null;
                }
            }

            // Set up master detail parameters
            $this->setupMasterParms();

            // Load recordset
            if ($this->isShow()) {
                // Load current record
                $loaded = $this->loadRow();
                $this->OldKey = $loaded ? $this->getKey(true) : ""; // Get from CurrentValue
            }
        }

        // Process form if post back
        if ($postBack) {
            $this->loadFormValues(); // Get form values
        }

        // Validate form if post back
        if ($postBack) {
            if (!$this->validateForm()) {
                $this->EventCancelled = true; // Event cancelled
                $this->restoreFormValues();
                if (IsApi()) {
                    $this->terminate();
                    return;
                } else {
                    $this->CurrentAction = ""; // Form error, reset action
                }
            }
        }

        // Perform current action
        switch ($this->CurrentAction) {
            case "show": // Get a record to display
                if (!$loaded) { // Load record based on key
                    if ($this->getFailureMessage() == "") {
                        $this->setFailureMessage($Language->phrase("NoRecord")); // No record found
                    }
                    $this->terminate("NilaiFormatifList"); // No matching record, return to list
                    return;
                }
                break;
            case "update": // Update
                $returnUrl = $this->getReturnUrl();
                if (GetPageName($returnUrl) == "NilaiFormatifList") {
                    $returnUrl = $this->addMasterUrl($returnUrl); // List page, return to List page with correct master key if necessary
                }
                $this->SendEmail = true; // Send email on update success
                if ($this->editRow()) { // Update record based on key
                    if ($this->getSuccessMessage() == "") {
                        $this->setSuccessMessage($Language->phrase("UpdateSuccess")); // Update success
                    }
                    if (IsApi()) {
                        $this->terminate(true);
                        return;
                    } else {
                        $this->terminate($returnUrl); // Return to caller
                        return;
                    }
                } elseif (IsApi()) { // API request, return
                    $this->terminate();
                    return;
                } elseif ($this->getFailureMessage() == $Language->phrase("NoRecord")) {
                    $this->terminate($returnUrl); // Return to caller
                    return;
                } else {
                    $this->EventCancelled = true; // Event cancelled
                    $this->restoreFormValues(); // Restore form values if update failed
                }
        }

        // Set up Breadcrumb
        $this->setupBreadcrumb();

        // Render the record
        $this->RowType = ROWTYPE_EDIT; // Render as Edit
        $this->resetAttributes();
        $this->renderRow();

        // Set LoginStatus / Page_Rendering / Page_Render
        if (!IsApi() && !$this->isTerminated()) {
            // Pass table and field properties to client side
            $this->toClientVar(["tableCaption"], ["caption", "Visible", "Required", "IsInvalid", "Raw"]);

            // Setup login status
            SetupLoginStatus();

            // Pass login status to client side
            SetClientVar("login", LoginStatus());

            // Global Page Rendering event (in userfn*.php)
            Page_Rendering();

            // Page Render event
            if (method_exists($this, "pageRender")) {
                $this->pageRender();
            }
        }
    }

    // Get upload files
    protected function getUploadFiles()
    {
        global $CurrentForm, $Language;
    }

    // Load form values
    protected function loadFormValues()
    {
        // Load from form
        global $CurrentForm;

        // Check field name 'nilai_f_id' first before field var 'x_nilai_f_id'
        $val = $CurrentForm->hasValue("nilai_f_id") ? $CurrentForm->getValue("nilai_f_id") : $CurrentForm->getValue("x_nilai_f_id");
        if (!$this->nilai_f_id->IsDetailKey) {
            $this->nilai_f_id->setFormValue($val);
        }

        // Check field name 'kode_tapel' first before field var 'x_kode_tapel'
        $val = $CurrentForm->hasValue("kode_tapel") ? $CurrentForm->getValue("kode_tapel") : $CurrentForm->getValue("x_kode_tapel");
        if (!$this->kode_tapel->IsDetailKey) {
            if (IsApi() && $val === null) {
                $this->kode_tapel->Visible = false; // Disable update for API request
            } else {
                $this->kode_tapel->setFormValue($val);
            }
        }

        // Check field name 'penilaian_id' first before field var 'x_penilaian_id'
        $val = $CurrentForm->hasValue("penilaian_id") ? $CurrentForm->getValue("penilaian_id") : $CurrentForm->getValue("x_penilaian_id");
        if (!$this->penilaian_id->IsDetailKey) {
            if (IsApi() && $val === null) {
                $this->penilaian_id->Visible = false; // Disable update for API request
            } else {
                $this->penilaian_id->setFormValue($val);
            }
        }

        // Check field name 'guru_id' first before field var 'x_guru_id'
        $val = $CurrentForm->hasValue("guru_id") ? $CurrentForm->getValue("guru_id") : $CurrentForm->getValue("x_guru_id");
        if (!$this->guru_id->IsDetailKey) {
            if (IsApi() && $val === null) {
                $this->guru_id->Visible = false; // Disable update for API request
            } else {
                $this->guru_id->setFormValue($val);
            }
        }

        // Check field name 'kode_mapel' first before field var 'x_kode_mapel'
        $val = $CurrentForm->hasValue("kode_mapel") ? $CurrentForm->getValue("kode_mapel") : $CurrentForm->getValue("x_kode_mapel");
        if (!$this->kode_mapel->IsDetailKey) {
            if (IsApi() && $val === null) {
                $this->kode_mapel->Visible = false; // Disable update for API request
            } else {
                $this->kode_mapel->setFormValue($val);
            }
        }

        // Check field name 'kelas' first before field var 'x_kelas'
        $val = $CurrentForm->hasValue("kelas") ? $CurrentForm->getValue("kelas") : $CurrentForm->getValue("x_kelas");
        if (!$this->kelas->IsDetailKey) {
            if (IsApi() && $val === null) {
                $this->kelas->Visible = false; // Disable update for API request
            } else {
                $this->kelas->setFormValue($val);
            }
        }

        // Check field name 'nis' first before field var 'x_nis'
        $val = $CurrentForm->hasValue("nis") ? $CurrentForm->getValue("nis") : $CurrentForm->getValue("x_nis");
        if (!$this->nis->IsDetailKey) {
            if (IsApi() && $val === null) {
                $this->nis->Visible = false; // Disable update for API request
            } else {
                $this->nis->setFormValue($val);
            }
        }

        // Check field name 'nama' first before field var 'x_nama'
        $val = $CurrentForm->hasValue("nama") ? $CurrentForm->getValue("nama") : $CurrentForm->getValue("x_nama");
        if (!$this->nama->IsDetailKey) {
            if (IsApi() && $val === null) {
                $this->nama->Visible = false; // Disable update for API request
            } else {
                $this->nama->setFormValue($val);
            }
        }

        // Check field name 'jk' first before field var 'x_jk'
        $val = $CurrentForm->hasValue("jk") ? $CurrentForm->getValue("jk") : $CurrentForm->getValue("x_jk");
        if (!$this->jk->IsDetailKey) {
            if (IsApi() && $val === null) {
                $this->jk->Visible = false; // Disable update for API request
            } else {
                $this->jk->setFormValue($val);
            }
        }

        // Check field name 'f1' first before field var 'x_f1'
        $val = $CurrentForm->hasValue("f1") ? $CurrentForm->getValue("f1") : $CurrentForm->getValue("x_f1");
        if (!$this->f1->IsDetailKey) {
            if (IsApi() && $val === null) {
                $this->f1->Visible = false; // Disable update for API request
            } else {
                $this->f1->setFormValue($val);
            }
        }

        // Check field name 'f2' first before field var 'x_f2'
        $val = $CurrentForm->hasValue("f2") ? $CurrentForm->getValue("f2") : $CurrentForm->getValue("x_f2");
        if (!$this->f2->IsDetailKey) {
            if (IsApi() && $val === null) {
                $this->f2->Visible = false; // Disable update for API request
            } else {
                $this->f2->setFormValue($val);
            }
        }

        // Check field name 'f3' first before field var 'x_f3'
        $val = $CurrentForm->hasValue("f3") ? $CurrentForm->getValue("f3") : $CurrentForm->getValue("x_f3");
        if (!$this->f3->IsDetailKey) {
            if (IsApi() && $val === null) {
                $this->f3->Visible = false; // Disable update for API request
            } else {
                $this->f3->setFormValue($val);
            }
        }

        // Check field name 'f4' first before field var 'x_f4'
        $val = $CurrentForm->hasValue("f4") ? $CurrentForm->getValue("f4") : $CurrentForm->getValue("x_f4");
        if (!$this->f4->IsDetailKey) {
            if (IsApi() && $val === null) {
                $this->f4->Visible = false; // Disable update for API request
            } else {
                $this->f4->setFormValue($val);
            }
        }

        // Check field name 'f5' first before field var 'x_f5'
        $val = $CurrentForm->hasValue("f5") ? $CurrentForm->getValue("f5") : $CurrentForm->getValue("x_f5");
        if (!$this->f5->IsDetailKey) {
            if (IsApi() && $val === null) {
                $this->f5->Visible = false; // Disable update for API request
            } else {
                $this->f5->setFormValue($val);
            }
        }

        // Check field name 'f6' first before field var 'x_f6'
        $val = $CurrentForm->hasValue("f6") ? $CurrentForm->getValue("f6") : $CurrentForm->getValue("x_f6");
        if (!$this->f6->IsDetailKey) {
            if (IsApi() && $val === null) {
                $this->f6->Visible = false; // Disable update for API request
            } else {
                $this->f6->setFormValue($val);
            }
        }

        // Check field name 'f7' first before field var 'x_f7'
        $val = $CurrentForm->hasValue("f7") ? $CurrentForm->getValue("f7") : $CurrentForm->getValue("x_f7");
        if (!$this->f7->IsDetailKey) {
            if (IsApi() && $val === null) {
                $this->f7->Visible = false; // Disable update for API request
            } else {
                $this->f7->setFormValue($val);
            }
        }

        // Check field name 'f8' first before field var 'x_f8'
        $val = $CurrentForm->hasValue("f8") ? $CurrentForm->getValue("f8") : $CurrentForm->getValue("x_f8");
        if (!$this->f8->IsDetailKey) {
            if (IsApi() && $val === null) {
                $this->f8->Visible = false; // Disable update for API request
            } else {
                $this->f8->setFormValue($val);
            }
        }

        // Check field name 'f9' first before field var 'x_f9'
        $val = $CurrentForm->hasValue("f9") ? $CurrentForm->getValue("f9") : $CurrentForm->getValue("x_f9");
        if (!$this->f9->IsDetailKey) {
            if (IsApi() && $val === null) {
                $this->f9->Visible = false; // Disable update for API request
            } else {
                $this->f9->setFormValue($val);
            }
        }

        // Check field name 'f10' first before field var 'x_f10'
        $val = $CurrentForm->hasValue("f10") ? $CurrentForm->getValue("f10") : $CurrentForm->getValue("x_f10");
        if (!$this->f10->IsDetailKey) {
            if (IsApi() && $val === null) {
                $this->f10->Visible = false; // Disable update for API request
            } else {
                $this->f10->setFormValue($val);
            }
        }

        // Check field name 'f11' first before field var 'x_f11'
        $val = $CurrentForm->hasValue("f11") ? $CurrentForm->getValue("f11") : $CurrentForm->getValue("x_f11");
        if (!$this->f11->IsDetailKey) {
            if (IsApi() && $val === null) {
                $this->f11->Visible = false; // Disable update for API request
            } else {
                $this->f11->setFormValue($val);
            }
        }

        // Check field name 'f12' first before field var 'x_f12'
        $val = $CurrentForm->hasValue("f12") ? $CurrentForm->getValue("f12") : $CurrentForm->getValue("x_f12");
        if (!$this->f12->IsDetailKey) {
            if (IsApi() && $val === null) {
                $this->f12->Visible = false; // Disable update for API request
            } else {
                $this->f12->setFormValue($val);
            }
        }

        // Check field name 'f13' first before field var 'x_f13'
        $val = $CurrentForm->hasValue("f13") ? $CurrentForm->getValue("f13") : $CurrentForm->getValue("x_f13");
        if (!$this->f13->IsDetailKey) {
            if (IsApi() && $val === null) {
                $this->f13->Visible = false; // Disable update for API request
            } else {
                $this->f13->setFormValue($val);
            }
        }

        // Check field name 'f14' first before field var 'x_f14'
        $val = $CurrentForm->hasValue("f14") ? $CurrentForm->getValue("f14") : $CurrentForm->getValue("x_f14");
        if (!$this->f14->IsDetailKey) {
            if (IsApi() && $val === null) {
                $this->f14->Visible = false; // Disable update for API request
            } else {
                $this->f14->setFormValue($val);
            }
        }

        // Check field name 'f15' first before field var 'x_f15'
        $val = $CurrentForm->hasValue("f15") ? $CurrentForm->getValue("f15") : $CurrentForm->getValue("x_f15");
        if (!$this->f15->IsDetailKey) {
            if (IsApi() && $val === null) {
                $this->f15->Visible = false; // Disable update for API request
            } else {
                $this->f15->setFormValue($val);
            }
        }

        // Check field name 'naf' first before field var 'x_naf'
        $val = $CurrentForm->hasValue("naf") ? $CurrentForm->getValue("naf") : $CurrentForm->getValue("x_naf");
        if (!$this->naf->IsDetailKey) {
            if (IsApi() && $val === null) {
                $this->naf->Visible = false; // Disable update for API request
            } else {
                $this->naf->setFormValue($val);
            }
        }
    }

    // Restore form values
    public function restoreFormValues()
    {
        global $CurrentForm;
        $this->nilai_f_id->CurrentValue = $this->nilai_f_id->FormValue;
        $this->kode_tapel->CurrentValue = $this->kode_tapel->FormValue;
        $this->penilaian_id->CurrentValue = $this->penilaian_id->FormValue;
        $this->guru_id->CurrentValue = $this->guru_id->FormValue;
        $this->kode_mapel->CurrentValue = $this->kode_mapel->FormValue;
        $this->kelas->CurrentValue = $this->kelas->FormValue;
        $this->nis->CurrentValue = $this->nis->FormValue;
        $this->nama->CurrentValue = $this->nama->FormValue;
        $this->jk->CurrentValue = $this->jk->FormValue;
        $this->f1->CurrentValue = $this->f1->FormValue;
        $this->f2->CurrentValue = $this->f2->FormValue;
        $this->f3->CurrentValue = $this->f3->FormValue;
        $this->f4->CurrentValue = $this->f4->FormValue;
        $this->f5->CurrentValue = $this->f5->FormValue;
        $this->f6->CurrentValue = $this->f6->FormValue;
        $this->f7->CurrentValue = $this->f7->FormValue;
        $this->f8->CurrentValue = $this->f8->FormValue;
        $this->f9->CurrentValue = $this->f9->FormValue;
        $this->f10->CurrentValue = $this->f10->FormValue;
        $this->f11->CurrentValue = $this->f11->FormValue;
        $this->f12->CurrentValue = $this->f12->FormValue;
        $this->f13->CurrentValue = $this->f13->FormValue;
        $this->f14->CurrentValue = $this->f14->FormValue;
        $this->f15->CurrentValue = $this->f15->FormValue;
        $this->naf->CurrentValue = $this->naf->FormValue;
    }

    /**
     * Load row based on key values
     *
     * @return void
     */
    public function loadRow()
    {
        global $Security, $Language;
        $filter = $this->getRecordFilter();

        // Call Row Selecting event
        $this->rowSelecting($filter);

        // Load SQL based on filter
        $this->CurrentFilter = $filter;
        $sql = $this->getCurrentSql();
        $conn = $this->getConnection();
        $res = false;
        $row = $conn->fetchAssoc($sql);
        if ($row) {
            $res = true;
            $this->loadRowValues($row); // Load row values
        }
        return $res;
    }

    /**
     * Load row values from recordset or record
     *
     * @param Recordset|array $rs Record
     * @return void
     */
    public function loadRowValues($rs = null)
    {
        if (is_array($rs)) {
            $row = $rs;
        } elseif ($rs && property_exists($rs, "fields")) { // Recordset
            $row = $rs->fields;
        } else {
            $row = $this->newRow();
        }

        // Call Row Selected event
        $this->rowSelected($row);
        if (!$rs) {
            return;
        }
        $this->nilai_f_id->setDbValue($row['nilai_f_id']);
        $this->kode_tapel->setDbValue($row['kode_tapel']);
        $this->penilaian_id->setDbValue($row['penilaian_id']);
        $this->guru_id->setDbValue($row['guru_id']);
        $this->kode_mapel->setDbValue($row['kode_mapel']);
        $this->kelas->setDbValue($row['kelas']);
        $this->nis->setDbValue($row['nis']);
        $this->nama->setDbValue($row['nama']);
        $this->jk->setDbValue($row['jk']);
        $this->f1->setDbValue($row['f1']);
        $this->f2->setDbValue($row['f2']);
        $this->f3->setDbValue($row['f3']);
        $this->f4->setDbValue($row['f4']);
        $this->f5->setDbValue($row['f5']);
        $this->f6->setDbValue($row['f6']);
        $this->f7->setDbValue($row['f7']);
        $this->f8->setDbValue($row['f8']);
        $this->f9->setDbValue($row['f9']);
        $this->f10->setDbValue($row['f10']);
        $this->f11->setDbValue($row['f11']);
        $this->f12->setDbValue($row['f12']);
        $this->f13->setDbValue($row['f13']);
        $this->f14->setDbValue($row['f14']);
        $this->f15->setDbValue($row['f15']);
        $this->naf->setDbValue($row['naf']);
    }

    // Return a row with default values
    protected function newRow()
    {
        $row = [];
        $row['nilai_f_id'] = null;
        $row['kode_tapel'] = null;
        $row['penilaian_id'] = null;
        $row['guru_id'] = null;
        $row['kode_mapel'] = null;
        $row['kelas'] = null;
        $row['nis'] = null;
        $row['nama'] = null;
        $row['jk'] = null;
        $row['f1'] = null;
        $row['f2'] = null;
        $row['f3'] = null;
        $row['f4'] = null;
        $row['f5'] = null;
        $row['f6'] = null;
        $row['f7'] = null;
        $row['f8'] = null;
        $row['f9'] = null;
        $row['f10'] = null;
        $row['f11'] = null;
        $row['f12'] = null;
        $row['f13'] = null;
        $row['f14'] = null;
        $row['f15'] = null;
        $row['naf'] = null;
        return $row;
    }

    // Load old record
    protected function loadOldRecord()
    {
        // Load old record
        $this->OldRecordset = null;
        $validKey = $this->OldKey != "";
        if ($validKey) {
            $this->CurrentFilter = $this->getRecordFilter();
            $sql = $this->getCurrentSql();
            $conn = $this->getConnection();
            $this->OldRecordset = LoadRecordset($sql, $conn);
        }
        $this->loadRowValues($this->OldRecordset); // Load row values
        return $validKey;
    }

    // Render row values based on field settings
    public function renderRow()
    {
        global $Security, $Language, $CurrentLanguage;

        // Initialize URLs

        // Call Row_Rendering event
        $this->rowRendering();

        // Common render codes for all row types

        // nilai_f_id

        // kode_tapel

        // penilaian_id

        // guru_id

        // kode_mapel

        // kelas

        // nis

        // nama

        // jk

        // f1

        // f2

        // f3

        // f4

        // f5

        // f6

        // f7

        // f8

        // f9

        // f10

        // f11

        // f12

        // f13

        // f14

        // f15

        // naf
        if ($this->RowType == ROWTYPE_VIEW) {
            // nilai_f_id
            $this->nilai_f_id->ViewValue = $this->nilai_f_id->CurrentValue;
            $this->nilai_f_id->ViewCustomAttributes = "";

            // kode_tapel
            $this->kode_tapel->ViewValue = $this->kode_tapel->CurrentValue;
            $this->kode_tapel->ViewCustomAttributes = "";

            // penilaian_id
            $this->penilaian_id->ViewValue = $this->penilaian_id->CurrentValue;
            $this->penilaian_id->ViewCustomAttributes = "";

            // guru_id
            $this->guru_id->ViewValue = $this->guru_id->CurrentValue;
            $this->guru_id->ViewCustomAttributes = "";

            // kode_mapel
            $this->kode_mapel->ViewValue = $this->kode_mapel->CurrentValue;
            $this->kode_mapel->ViewCustomAttributes = "";

            // kelas
            $this->kelas->ViewValue = $this->kelas->CurrentValue;
            $this->kelas->ViewCustomAttributes = "";

            // nis
            $this->nis->ViewValue = $this->nis->CurrentValue;
            $this->nis->ViewCustomAttributes = "";

            // nama
            $this->nama->ViewValue = $this->nama->CurrentValue;
            $this->nama->ViewCustomAttributes = "";

            // jk
            $this->jk->ViewValue = $this->jk->CurrentValue;
            $this->jk->ViewCustomAttributes = "";

            // f1
            $this->f1->ViewValue = $this->f1->CurrentValue;
            $this->f1->ViewCustomAttributes = "";

            // f2
            $this->f2->ViewValue = $this->f2->CurrentValue;
            $this->f2->ViewCustomAttributes = "";

            // f3
            $this->f3->ViewValue = $this->f3->CurrentValue;
            $this->f3->ViewCustomAttributes = "";

            // f4
            $this->f4->ViewValue = $this->f4->CurrentValue;
            $this->f4->ViewCustomAttributes = "";

            // f5
            $this->f5->ViewValue = $this->f5->CurrentValue;
            $this->f5->ViewCustomAttributes = "";

            // f6
            $this->f6->ViewValue = $this->f6->CurrentValue;
            $this->f6->ViewCustomAttributes = "";

            // f7
            $this->f7->ViewValue = $this->f7->CurrentValue;
            $this->f7->ViewCustomAttributes = "";

            // f8
            $this->f8->ViewValue = $this->f8->CurrentValue;
            $this->f8->ViewCustomAttributes = "";

            // f9
            $this->f9->ViewValue = $this->f9->CurrentValue;
            $this->f9->ViewCustomAttributes = "";

            // f10
            $this->f10->ViewValue = $this->f10->CurrentValue;
            $this->f10->ViewCustomAttributes = "";

            // f11
            $this->f11->ViewValue = $this->f11->CurrentValue;
            $this->f11->ViewCustomAttributes = "";

            // f12
            $this->f12->ViewValue = $this->f12->CurrentValue;
            $this->f12->ViewCustomAttributes = "";

            // f13
            $this->f13->ViewValue = $this->f13->CurrentValue;
            $this->f13->ViewCustomAttributes = "";

            // f14
            $this->f14->ViewValue = $this->f14->CurrentValue;
            $this->f14->ViewCustomAttributes = "";

            // f15
            $this->f15->ViewValue = $this->f15->CurrentValue;
            $this->f15->ViewCustomAttributes = "";

            // naf
            $this->naf->ViewValue = $this->naf->CurrentValue;
            $this->naf->CssClass = "font-weight-bold font-italic";
            $this->naf->ViewCustomAttributes = "";

            // nilai_f_id
            $this->nilai_f_id->LinkCustomAttributes = "";
            $this->nilai_f_id->HrefValue = "";
            $this->nilai_f_id->TooltipValue = "";

            // kode_tapel
            $this->kode_tapel->LinkCustomAttributes = "";
            $this->kode_tapel->HrefValue = "";
            $this->kode_tapel->TooltipValue = "";

            // penilaian_id
            $this->penilaian_id->LinkCustomAttributes = "";
            $this->penilaian_id->HrefValue = "";
            $this->penilaian_id->TooltipValue = "";

            // guru_id
            $this->guru_id->LinkCustomAttributes = "";
            $this->guru_id->HrefValue = "";
            $this->guru_id->TooltipValue = "";

            // kode_mapel
            $this->kode_mapel->LinkCustomAttributes = "";
            $this->kode_mapel->HrefValue = "";
            $this->kode_mapel->TooltipValue = "";

            // kelas
            $this->kelas->LinkCustomAttributes = "";
            $this->kelas->HrefValue = "";
            $this->kelas->TooltipValue = "";

            // nis
            $this->nis->LinkCustomAttributes = "";
            $this->nis->HrefValue = "";
            $this->nis->TooltipValue = "";

            // nama
            $this->nama->LinkCustomAttributes = "";
            $this->nama->HrefValue = "";
            $this->nama->TooltipValue = "";

            // jk
            $this->jk->LinkCustomAttributes = "";
            $this->jk->HrefValue = "";
            $this->jk->TooltipValue = "";

            // f1
            $this->f1->LinkCustomAttributes = "";
            $this->f1->HrefValue = "";
            $this->f1->TooltipValue = "";

            // f2
            $this->f2->LinkCustomAttributes = "";
            $this->f2->HrefValue = "";
            $this->f2->TooltipValue = "";

            // f3
            $this->f3->LinkCustomAttributes = "";
            $this->f3->HrefValue = "";
            $this->f3->TooltipValue = "";

            // f4
            $this->f4->LinkCustomAttributes = "";
            $this->f4->HrefValue = "";
            $this->f4->TooltipValue = "";

            // f5
            $this->f5->LinkCustomAttributes = "";
            $this->f5->HrefValue = "";
            $this->f5->TooltipValue = "";

            // f6
            $this->f6->LinkCustomAttributes = "";
            $this->f6->HrefValue = "";
            $this->f6->TooltipValue = "";

            // f7
            $this->f7->LinkCustomAttributes = "";
            $this->f7->HrefValue = "";
            $this->f7->TooltipValue = "";

            // f8
            $this->f8->LinkCustomAttributes = "";
            $this->f8->HrefValue = "";
            $this->f8->TooltipValue = "";

            // f9
            $this->f9->LinkCustomAttributes = "";
            $this->f9->HrefValue = "";
            $this->f9->TooltipValue = "";

            // f10
            $this->f10->LinkCustomAttributes = "";
            $this->f10->HrefValue = "";
            $this->f10->TooltipValue = "";

            // f11
            $this->f11->LinkCustomAttributes = "";
            $this->f11->HrefValue = "";
            $this->f11->TooltipValue = "";

            // f12
            $this->f12->LinkCustomAttributes = "";
            $this->f12->HrefValue = "";
            $this->f12->TooltipValue = "";

            // f13
            $this->f13->LinkCustomAttributes = "";
            $this->f13->HrefValue = "";
            $this->f13->TooltipValue = "";

            // f14
            $this->f14->LinkCustomAttributes = "";
            $this->f14->HrefValue = "";
            $this->f14->TooltipValue = "";

            // f15
            $this->f15->LinkCustomAttributes = "";
            $this->f15->HrefValue = "";
            $this->f15->TooltipValue = "";

            // naf
            $this->naf->LinkCustomAttributes = "";
            $this->naf->HrefValue = "";
            $this->naf->TooltipValue = "";
        } elseif ($this->RowType == ROWTYPE_EDIT) {
            // nilai_f_id
            $this->nilai_f_id->EditAttrs["class"] = "form-control";
            $this->nilai_f_id->EditCustomAttributes = "";
            $this->nilai_f_id->EditValue = $this->nilai_f_id->CurrentValue;
            $this->nilai_f_id->ViewCustomAttributes = "";

            // kode_tapel
            $this->kode_tapel->EditAttrs["class"] = "form-control";
            $this->kode_tapel->EditCustomAttributes = "";
            if (!$this->kode_tapel->Raw) {
                $this->kode_tapel->CurrentValue = HtmlDecode($this->kode_tapel->CurrentValue);
            }
            $this->kode_tapel->EditValue = HtmlEncode($this->kode_tapel->CurrentValue);

            // penilaian_id
            $this->penilaian_id->EditAttrs["class"] = "form-control";
            $this->penilaian_id->EditCustomAttributes = "";
            if ($this->penilaian_id->getSessionValue() != "") {
                $this->penilaian_id->CurrentValue = GetForeignKeyValue($this->penilaian_id->getSessionValue());
                $this->penilaian_id->ViewValue = $this->penilaian_id->CurrentValue;
                $this->penilaian_id->ViewCustomAttributes = "";
            } else {
                if (!$this->penilaian_id->Raw) {
                    $this->penilaian_id->CurrentValue = HtmlDecode($this->penilaian_id->CurrentValue);
                }
                $this->penilaian_id->EditValue = HtmlEncode($this->penilaian_id->CurrentValue);
            }

            // guru_id
            $this->guru_id->EditAttrs["class"] = "form-control";
            $this->guru_id->EditCustomAttributes = "";
            if (!$this->guru_id->Raw) {
                $this->guru_id->CurrentValue = HtmlDecode($this->guru_id->CurrentValue);
            }
            $this->guru_id->EditValue = HtmlEncode($this->guru_id->CurrentValue);

            // kode_mapel
            $this->kode_mapel->EditAttrs["class"] = "form-control";
            $this->kode_mapel->EditCustomAttributes = "";
            $curVal = trim(strval($this->kode_mapel->CurrentValue));
            if ($curVal != "") {
                $this->kode_mapel->ViewValue = $this->kode_mapel->lookupCacheOption($curVal);
            } else {
                $this->kode_mapel->ViewValue = $this->kode_mapel->Lookup !== null && is_array($this->kode_mapel->Lookup->Options) ? $curVal : null;
            }
            if ($this->kode_mapel->ViewValue !== null) { // Load from cache
                $this->kode_mapel->EditValue = array_values($this->kode_mapel->Lookup->Options);
            } else { // Lookup from database
                if ($curVal == "") {
                    $filterWrk = "0=1";
                } else {
                    $filterWrk = "\"kode_mapel\"" . SearchString("=", $this->kode_mapel->CurrentValue, DATATYPE_MEMO, "");
                }
                $sqlWrk = $this->kode_mapel->Lookup->getSql(true, $filterWrk, '', $this, false, true);
                $rswrk = Conn()->executeQuery($sqlWrk)->fetchAll(\PDO::FETCH_BOTH);
                $ari = count($rswrk);
                $arwrk = $rswrk;
                $this->kode_mapel->EditValue = $arwrk;
            }

            // kelas
            $this->kelas->EditAttrs["class"] = "form-control";
            $this->kelas->EditCustomAttributes = "";
            $curVal = trim(strval($this->kelas->CurrentValue));
            if ($curVal != "") {
                $this->kelas->ViewValue = $this->kelas->lookupCacheOption($curVal);
            } else {
                $this->kelas->ViewValue = $this->kelas->Lookup !== null && is_array($this->kelas->Lookup->Options) ? $curVal : null;
            }
            if ($this->kelas->ViewValue !== null) { // Load from cache
                $this->kelas->EditValue = array_values($this->kelas->Lookup->Options);
            } else { // Lookup from database
                if ($curVal == "") {
                    $filterWrk = "0=1";
                } else {
                    $filterWrk = "\"kelas\"" . SearchString("=", $this->kelas->CurrentValue, DATATYPE_MEMO, "");
                }
                $lookupFilter = function() {
                    return "`kode_tapel`='".kode_tapel()."'";
                };
                $lookupFilter = $lookupFilter->bindTo($this);
                $sqlWrk = $this->kelas->Lookup->getSql(true, $filterWrk, $lookupFilter, $this, false, true);
                $rswrk = Conn()->executeQuery($sqlWrk)->fetchAll(\PDO::FETCH_BOTH);
                $ari = count($rswrk);
                $arwrk = $rswrk;
                $this->kelas->EditValue = $arwrk;
            }

            // nis
            $this->nis->EditAttrs["class"] = "form-control";
            $this->nis->EditCustomAttributes = "";
            if (!$this->nis->Raw) {
                $this->nis->CurrentValue = HtmlDecode($this->nis->CurrentValue);
            }
            $this->nis->EditValue = HtmlEncode($this->nis->CurrentValue);

            // nama
            $this->nama->EditAttrs["class"] = "form-control";
            $this->nama->EditCustomAttributes = "";
            if (!$this->nama->Raw) {
                $this->nama->CurrentValue = HtmlDecode($this->nama->CurrentValue);
            }
            $this->nama->EditValue = HtmlEncode($this->nama->CurrentValue);

            // jk
            $this->jk->EditAttrs["class"] = "form-control";
            $this->jk->EditCustomAttributes = "";
            if (!$this->jk->Raw) {
                $this->jk->CurrentValue = HtmlDecode($this->jk->CurrentValue);
            }
            $this->jk->EditValue = HtmlEncode($this->jk->CurrentValue);

            // f1
            $this->f1->EditAttrs["class"] = "form-control";
            $this->f1->EditCustomAttributes = "";
            if (!$this->f1->Raw) {
                $this->f1->CurrentValue = HtmlDecode($this->f1->CurrentValue);
            }
            $this->f1->EditValue = HtmlEncode($this->f1->CurrentValue);

            // f2
            $this->f2->EditAttrs["class"] = "form-control";
            $this->f2->EditCustomAttributes = "";
            if (!$this->f2->Raw) {
                $this->f2->CurrentValue = HtmlDecode($this->f2->CurrentValue);
            }
            $this->f2->EditValue = HtmlEncode($this->f2->CurrentValue);

            // f3
            $this->f3->EditAttrs["class"] = "form-control";
            $this->f3->EditCustomAttributes = "";
            if (!$this->f3->Raw) {
                $this->f3->CurrentValue = HtmlDecode($this->f3->CurrentValue);
            }
            $this->f3->EditValue = HtmlEncode($this->f3->CurrentValue);

            // f4
            $this->f4->EditAttrs["class"] = "form-control";
            $this->f4->EditCustomAttributes = "";
            if (!$this->f4->Raw) {
                $this->f4->CurrentValue = HtmlDecode($this->f4->CurrentValue);
            }
            $this->f4->EditValue = HtmlEncode($this->f4->CurrentValue);

            // f5
            $this->f5->EditAttrs["class"] = "form-control";
            $this->f5->EditCustomAttributes = "";
            if (!$this->f5->Raw) {
                $this->f5->CurrentValue = HtmlDecode($this->f5->CurrentValue);
            }
            $this->f5->EditValue = HtmlEncode($this->f5->CurrentValue);

            // f6
            $this->f6->EditAttrs["class"] = "form-control";
            $this->f6->EditCustomAttributes = "";
            if (!$this->f6->Raw) {
                $this->f6->CurrentValue = HtmlDecode($this->f6->CurrentValue);
            }
            $this->f6->EditValue = HtmlEncode($this->f6->CurrentValue);

            // f7
            $this->f7->EditAttrs["class"] = "form-control";
            $this->f7->EditCustomAttributes = "";
            if (!$this->f7->Raw) {
                $this->f7->CurrentValue = HtmlDecode($this->f7->CurrentValue);
            }
            $this->f7->EditValue = HtmlEncode($this->f7->CurrentValue);

            // f8
            $this->f8->EditAttrs["class"] = "form-control";
            $this->f8->EditCustomAttributes = "";
            if (!$this->f8->Raw) {
                $this->f8->CurrentValue = HtmlDecode($this->f8->CurrentValue);
            }
            $this->f8->EditValue = HtmlEncode($this->f8->CurrentValue);

            // f9
            $this->f9->EditAttrs["class"] = "form-control";
            $this->f9->EditCustomAttributes = "";
            if (!$this->f9->Raw) {
                $this->f9->CurrentValue = HtmlDecode($this->f9->CurrentValue);
            }
            $this->f9->EditValue = HtmlEncode($this->f9->CurrentValue);

            // f10
            $this->f10->EditAttrs["class"] = "form-control";
            $this->f10->EditCustomAttributes = "";
            if (!$this->f10->Raw) {
                $this->f10->CurrentValue = HtmlDecode($this->f10->CurrentValue);
            }
            $this->f10->EditValue = HtmlEncode($this->f10->CurrentValue);

            // f11
            $this->f11->EditAttrs["class"] = "form-control";
            $this->f11->EditCustomAttributes = "";
            if (!$this->f11->Raw) {
                $this->f11->CurrentValue = HtmlDecode($this->f11->CurrentValue);
            }
            $this->f11->EditValue = HtmlEncode($this->f11->CurrentValue);

            // f12
            $this->f12->EditAttrs["class"] = "form-control";
            $this->f12->EditCustomAttributes = "";
            if (!$this->f12->Raw) {
                $this->f12->CurrentValue = HtmlDecode($this->f12->CurrentValue);
            }
            $this->f12->EditValue = HtmlEncode($this->f12->CurrentValue);

            // f13
            $this->f13->EditAttrs["class"] = "form-control";
            $this->f13->EditCustomAttributes = "";
            if (!$this->f13->Raw) {
                $this->f13->CurrentValue = HtmlDecode($this->f13->CurrentValue);
            }
            $this->f13->EditValue = HtmlEncode($this->f13->CurrentValue);

            // f14
            $this->f14->EditAttrs["class"] = "form-control";
            $this->f14->EditCustomAttributes = "";
            if (!$this->f14->Raw) {
                $this->f14->CurrentValue = HtmlDecode($this->f14->CurrentValue);
            }
            $this->f14->EditValue = HtmlEncode($this->f14->CurrentValue);

            // f15
            $this->f15->EditAttrs["class"] = "form-control";
            $this->f15->EditCustomAttributes = "";
            if (!$this->f15->Raw) {
                $this->f15->CurrentValue = HtmlDecode($this->f15->CurrentValue);
            }
            $this->f15->EditValue = HtmlEncode($this->f15->CurrentValue);

            // naf
            $this->naf->EditAttrs["class"] = "form-control";
            $this->naf->EditCustomAttributes = "";
            if (!$this->naf->Raw) {
                $this->naf->CurrentValue = HtmlDecode($this->naf->CurrentValue);
            }
            $this->naf->EditValue = HtmlEncode($this->naf->CurrentValue);

            // Edit refer script

            // nilai_f_id
            $this->nilai_f_id->LinkCustomAttributes = "";
            $this->nilai_f_id->HrefValue = "";

            // kode_tapel
            $this->kode_tapel->LinkCustomAttributes = "";
            $this->kode_tapel->HrefValue = "";

            // penilaian_id
            $this->penilaian_id->LinkCustomAttributes = "";
            $this->penilaian_id->HrefValue = "";

            // guru_id
            $this->guru_id->LinkCustomAttributes = "";
            $this->guru_id->HrefValue = "";

            // kode_mapel
            $this->kode_mapel->LinkCustomAttributes = "";
            $this->kode_mapel->HrefValue = "";

            // kelas
            $this->kelas->LinkCustomAttributes = "";
            $this->kelas->HrefValue = "";

            // nis
            $this->nis->LinkCustomAttributes = "";
            $this->nis->HrefValue = "";

            // nama
            $this->nama->LinkCustomAttributes = "";
            $this->nama->HrefValue = "";

            // jk
            $this->jk->LinkCustomAttributes = "";
            $this->jk->HrefValue = "";

            // f1
            $this->f1->LinkCustomAttributes = "";
            $this->f1->HrefValue = "";

            // f2
            $this->f2->LinkCustomAttributes = "";
            $this->f2->HrefValue = "";

            // f3
            $this->f3->LinkCustomAttributes = "";
            $this->f3->HrefValue = "";

            // f4
            $this->f4->LinkCustomAttributes = "";
            $this->f4->HrefValue = "";

            // f5
            $this->f5->LinkCustomAttributes = "";
            $this->f5->HrefValue = "";

            // f6
            $this->f6->LinkCustomAttributes = "";
            $this->f6->HrefValue = "";

            // f7
            $this->f7->LinkCustomAttributes = "";
            $this->f7->HrefValue = "";

            // f8
            $this->f8->LinkCustomAttributes = "";
            $this->f8->HrefValue = "";

            // f9
            $this->f9->LinkCustomAttributes = "";
            $this->f9->HrefValue = "";

            // f10
            $this->f10->LinkCustomAttributes = "";
            $this->f10->HrefValue = "";

            // f11
            $this->f11->LinkCustomAttributes = "";
            $this->f11->HrefValue = "";

            // f12
            $this->f12->LinkCustomAttributes = "";
            $this->f12->HrefValue = "";

            // f13
            $this->f13->LinkCustomAttributes = "";
            $this->f13->HrefValue = "";

            // f14
            $this->f14->LinkCustomAttributes = "";
            $this->f14->HrefValue = "";

            // f15
            $this->f15->LinkCustomAttributes = "";
            $this->f15->HrefValue = "";

            // naf
            $this->naf->LinkCustomAttributes = "";
            $this->naf->HrefValue = "";
        }
        if ($this->RowType == ROWTYPE_ADD || $this->RowType == ROWTYPE_EDIT || $this->RowType == ROWTYPE_SEARCH) { // Add/Edit/Search row
            $this->setupFieldTitles();
        }

        // Call Row Rendered event
        if ($this->RowType != ROWTYPE_AGGREGATEINIT) {
            $this->rowRendered();
        }

        // Save data for Custom Template
        if ($this->RowType == ROWTYPE_VIEW || $this->RowType == ROWTYPE_EDIT || $this->RowType == ROWTYPE_ADD) {
            $this->Rows[] = $this->customTemplateFieldValues();
        }
    }

    // Validate form
    protected function validateForm()
    {
        global $Language;

        // Check if validation required
        if (!Config("SERVER_VALIDATE")) {
            return true;
        }
        if ($this->nilai_f_id->Required) {
            if (!$this->nilai_f_id->IsDetailKey && EmptyValue($this->nilai_f_id->FormValue)) {
                $this->nilai_f_id->addErrorMessage(str_replace("%s", $this->nilai_f_id->caption(), $this->nilai_f_id->RequiredErrorMessage));
            }
        }
        if ($this->kode_tapel->Required) {
            if (!$this->kode_tapel->IsDetailKey && EmptyValue($this->kode_tapel->FormValue)) {
                $this->kode_tapel->addErrorMessage(str_replace("%s", $this->kode_tapel->caption(), $this->kode_tapel->RequiredErrorMessage));
            }
        }
        if ($this->penilaian_id->Required) {
            if (!$this->penilaian_id->IsDetailKey && EmptyValue($this->penilaian_id->FormValue)) {
                $this->penilaian_id->addErrorMessage(str_replace("%s", $this->penilaian_id->caption(), $this->penilaian_id->RequiredErrorMessage));
            }
        }
        if ($this->guru_id->Required) {
            if (!$this->guru_id->IsDetailKey && EmptyValue($this->guru_id->FormValue)) {
                $this->guru_id->addErrorMessage(str_replace("%s", $this->guru_id->caption(), $this->guru_id->RequiredErrorMessage));
            }
        }
        if ($this->kode_mapel->Required) {
            if (!$this->kode_mapel->IsDetailKey && EmptyValue($this->kode_mapel->FormValue)) {
                $this->kode_mapel->addErrorMessage(str_replace("%s", $this->kode_mapel->caption(), $this->kode_mapel->RequiredErrorMessage));
            }
        }
        if ($this->kelas->Required) {
            if (!$this->kelas->IsDetailKey && EmptyValue($this->kelas->FormValue)) {
                $this->kelas->addErrorMessage(str_replace("%s", $this->kelas->caption(), $this->kelas->RequiredErrorMessage));
            }
        }
        if ($this->nis->Required) {
            if (!$this->nis->IsDetailKey && EmptyValue($this->nis->FormValue)) {
                $this->nis->addErrorMessage(str_replace("%s", $this->nis->caption(), $this->nis->RequiredErrorMessage));
            }
        }
        if ($this->nama->Required) {
            if (!$this->nama->IsDetailKey && EmptyValue($this->nama->FormValue)) {
                $this->nama->addErrorMessage(str_replace("%s", $this->nama->caption(), $this->nama->RequiredErrorMessage));
            }
        }
        if ($this->jk->Required) {
            if (!$this->jk->IsDetailKey && EmptyValue($this->jk->FormValue)) {
                $this->jk->addErrorMessage(str_replace("%s", $this->jk->caption(), $this->jk->RequiredErrorMessage));
            }
        }
        if ($this->f1->Required) {
            if (!$this->f1->IsDetailKey && EmptyValue($this->f1->FormValue)) {
                $this->f1->addErrorMessage(str_replace("%s", $this->f1->caption(), $this->f1->RequiredErrorMessage));
            }
        }
        if ($this->f2->Required) {
            if (!$this->f2->IsDetailKey && EmptyValue($this->f2->FormValue)) {
                $this->f2->addErrorMessage(str_replace("%s", $this->f2->caption(), $this->f2->RequiredErrorMessage));
            }
        }
        if ($this->f3->Required) {
            if (!$this->f3->IsDetailKey && EmptyValue($this->f3->FormValue)) {
                $this->f3->addErrorMessage(str_replace("%s", $this->f3->caption(), $this->f3->RequiredErrorMessage));
            }
        }
        if ($this->f4->Required) {
            if (!$this->f4->IsDetailKey && EmptyValue($this->f4->FormValue)) {
                $this->f4->addErrorMessage(str_replace("%s", $this->f4->caption(), $this->f4->RequiredErrorMessage));
            }
        }
        if ($this->f5->Required) {
            if (!$this->f5->IsDetailKey && EmptyValue($this->f5->FormValue)) {
                $this->f5->addErrorMessage(str_replace("%s", $this->f5->caption(), $this->f5->RequiredErrorMessage));
            }
        }
        if ($this->f6->Required) {
            if (!$this->f6->IsDetailKey && EmptyValue($this->f6->FormValue)) {
                $this->f6->addErrorMessage(str_replace("%s", $this->f6->caption(), $this->f6->RequiredErrorMessage));
            }
        }
        if ($this->f7->Required) {
            if (!$this->f7->IsDetailKey && EmptyValue($this->f7->FormValue)) {
                $this->f7->addErrorMessage(str_replace("%s", $this->f7->caption(), $this->f7->RequiredErrorMessage));
            }
        }
        if ($this->f8->Required) {
            if (!$this->f8->IsDetailKey && EmptyValue($this->f8->FormValue)) {
                $this->f8->addErrorMessage(str_replace("%s", $this->f8->caption(), $this->f8->RequiredErrorMessage));
            }
        }
        if ($this->f9->Required) {
            if (!$this->f9->IsDetailKey && EmptyValue($this->f9->FormValue)) {
                $this->f9->addErrorMessage(str_replace("%s", $this->f9->caption(), $this->f9->RequiredErrorMessage));
            }
        }
        if ($this->f10->Required) {
            if (!$this->f10->IsDetailKey && EmptyValue($this->f10->FormValue)) {
                $this->f10->addErrorMessage(str_replace("%s", $this->f10->caption(), $this->f10->RequiredErrorMessage));
            }
        }
        if ($this->f11->Required) {
            if (!$this->f11->IsDetailKey && EmptyValue($this->f11->FormValue)) {
                $this->f11->addErrorMessage(str_replace("%s", $this->f11->caption(), $this->f11->RequiredErrorMessage));
            }
        }
        if ($this->f12->Required) {
            if (!$this->f12->IsDetailKey && EmptyValue($this->f12->FormValue)) {
                $this->f12->addErrorMessage(str_replace("%s", $this->f12->caption(), $this->f12->RequiredErrorMessage));
            }
        }
        if ($this->f13->Required) {
            if (!$this->f13->IsDetailKey && EmptyValue($this->f13->FormValue)) {
                $this->f13->addErrorMessage(str_replace("%s", $this->f13->caption(), $this->f13->RequiredErrorMessage));
            }
        }
        if ($this->f14->Required) {
            if (!$this->f14->IsDetailKey && EmptyValue($this->f14->FormValue)) {
                $this->f14->addErrorMessage(str_replace("%s", $this->f14->caption(), $this->f14->RequiredErrorMessage));
            }
        }
        if ($this->f15->Required) {
            if (!$this->f15->IsDetailKey && EmptyValue($this->f15->FormValue)) {
                $this->f15->addErrorMessage(str_replace("%s", $this->f15->caption(), $this->f15->RequiredErrorMessage));
            }
        }
        if ($this->naf->Required) {
            if (!$this->naf->IsDetailKey && EmptyValue($this->naf->FormValue)) {
                $this->naf->addErrorMessage(str_replace("%s", $this->naf->caption(), $this->naf->RequiredErrorMessage));
            }
        }

        // Return validate result
        $validateForm = !$this->hasInvalidFields();

        // Call Form_CustomValidate event
        $formCustomError = "";
        $validateForm = $validateForm && $this->formCustomValidate($formCustomError);
        if ($formCustomError != "") {
            $this->setFailureMessage($formCustomError);
        }
        return $validateForm;
    }

    // Update record based on key values
    protected function editRow()
    {
        global $Security, $Language;
        $oldKeyFilter = $this->getRecordFilter();
        $filter = $this->applyUserIDFilters($oldKeyFilter);
        $conn = $this->getConnection();
        $this->CurrentFilter = $filter;
        $sql = $this->getCurrentSql();
        $rsold = $conn->fetchAssoc($sql);
        $editRow = false;
        if (!$rsold) {
            $this->setFailureMessage($Language->phrase("NoRecord")); // Set no record message
            $editRow = false; // Update Failed
        } else {
            // Save old values
            $this->loadDbValues($rsold);
            $rsnew = [];

            // kode_tapel
            $this->kode_tapel->setDbValueDef($rsnew, $this->kode_tapel->CurrentValue, null, $this->kode_tapel->ReadOnly);

            // penilaian_id
            if ($this->penilaian_id->getSessionValue() != "") {
                $this->penilaian_id->ReadOnly = true;
            }
            $this->penilaian_id->setDbValueDef($rsnew, $this->penilaian_id->CurrentValue, null, $this->penilaian_id->ReadOnly);

            // guru_id
            $this->guru_id->setDbValueDef($rsnew, $this->guru_id->CurrentValue, null, $this->guru_id->ReadOnly);

            // kode_mapel
            $this->kode_mapel->setDbValueDef($rsnew, $this->kode_mapel->CurrentValue, null, $this->kode_mapel->ReadOnly);

            // kelas
            $this->kelas->setDbValueDef($rsnew, $this->kelas->CurrentValue, null, $this->kelas->ReadOnly);

            // nis
            $this->nis->setDbValueDef($rsnew, $this->nis->CurrentValue, null, $this->nis->ReadOnly);

            // nama
            $this->nama->setDbValueDef($rsnew, $this->nama->CurrentValue, null, $this->nama->ReadOnly);

            // jk
            $this->jk->setDbValueDef($rsnew, $this->jk->CurrentValue, null, $this->jk->ReadOnly);

            // f1
            $this->f1->setDbValueDef($rsnew, $this->f1->CurrentValue, null, $this->f1->ReadOnly);

            // f2
            $this->f2->setDbValueDef($rsnew, $this->f2->CurrentValue, null, $this->f2->ReadOnly);

            // f3
            $this->f3->setDbValueDef($rsnew, $this->f3->CurrentValue, null, $this->f3->ReadOnly);

            // f4
            $this->f4->setDbValueDef($rsnew, $this->f4->CurrentValue, null, $this->f4->ReadOnly);

            // f5
            $this->f5->setDbValueDef($rsnew, $this->f5->CurrentValue, null, $this->f5->ReadOnly);

            // f6
            $this->f6->setDbValueDef($rsnew, $this->f6->CurrentValue, null, $this->f6->ReadOnly);

            // f7
            $this->f7->setDbValueDef($rsnew, $this->f7->CurrentValue, null, $this->f7->ReadOnly);

            // f8
            $this->f8->setDbValueDef($rsnew, $this->f8->CurrentValue, null, $this->f8->ReadOnly);

            // f9
            $this->f9->setDbValueDef($rsnew, $this->f9->CurrentValue, null, $this->f9->ReadOnly);

            // f10
            $this->f10->setDbValueDef($rsnew, $this->f10->CurrentValue, null, $this->f10->ReadOnly);

            // f11
            $this->f11->setDbValueDef($rsnew, $this->f11->CurrentValue, null, $this->f11->ReadOnly);

            // f12
            $this->f12->setDbValueDef($rsnew, $this->f12->CurrentValue, null, $this->f12->ReadOnly);

            // f13
            $this->f13->setDbValueDef($rsnew, $this->f13->CurrentValue, null, $this->f13->ReadOnly);

            // f14
            $this->f14->setDbValueDef($rsnew, $this->f14->CurrentValue, null, $this->f14->ReadOnly);

            // f15
            $this->f15->setDbValueDef($rsnew, $this->f15->CurrentValue, null, $this->f15->ReadOnly);

            // naf
            $this->naf->setDbValueDef($rsnew, $this->naf->CurrentValue, null, $this->naf->ReadOnly);

            // Call Row Updating event
            $updateRow = $this->rowUpdating($rsold, $rsnew);
            if ($updateRow) {
                if (count($rsnew) > 0) {
                    try {
                        $editRow = $this->update($rsnew, "", $rsold);
                    } catch (\Exception $e) {
                        $this->setFailureMessage($e->getMessage());
                    }
                } else {
                    $editRow = true; // No field to update
                }
                if ($editRow) {
                }
            } else {
                if ($this->getSuccessMessage() != "" || $this->getFailureMessage() != "") {
                    // Use the message, do nothing
                } elseif ($this->CancelMessage != "") {
                    $this->setFailureMessage($this->CancelMessage);
                    $this->CancelMessage = "";
                } else {
                    $this->setFailureMessage($Language->phrase("UpdateCancelled"));
                }
                $editRow = false;
            }
        }

        // Call Row_Updated event
        if ($editRow) {
            $this->rowUpdated($rsold, $rsnew);
        }

        // Clean upload path if any
        if ($editRow) {
        }

        // Write JSON for API request
        if (IsApi() && $editRow) {
            $row = $this->getRecordsFromRecordset([$rsnew], true);
            WriteJson(["success" => true, $this->TableVar => $row]);
        }
        return $editRow;
    }

    // Set up master/detail based on QueryString
    protected function setupMasterParms()
    {
        $validMaster = false;
        // Get the keys for master table
        if (($master = Get(Config("TABLE_SHOW_MASTER"), Get(Config("TABLE_MASTER")))) !== null) {
            $masterTblVar = $master;
            if ($masterTblVar == "") {
                $validMaster = true;
                $this->DbMasterFilter = "";
                $this->DbDetailFilter = "";
            }
            if ($masterTblVar == "penilaian") {
                $validMaster = true;
                $masterTbl = Container("penilaian");
                if (($parm = Get("fk_penilaian_id", Get("penilaian_id"))) !== null) {
                    $masterTbl->penilaian_id->setQueryStringValue($parm);
                    $this->penilaian_id->setQueryStringValue($masterTbl->penilaian_id->QueryStringValue);
                    $this->penilaian_id->setSessionValue($this->penilaian_id->QueryStringValue);
                    if (!is_numeric($masterTbl->penilaian_id->QueryStringValue)) {
                        $validMaster = false;
                    }
                } else {
                    $validMaster = false;
                }
            }
        } elseif (($master = Post(Config("TABLE_SHOW_MASTER"), Post(Config("TABLE_MASTER")))) !== null) {
            $masterTblVar = $master;
            if ($masterTblVar == "") {
                    $validMaster = true;
                    $this->DbMasterFilter = "";
                    $this->DbDetailFilter = "";
            }
            if ($masterTblVar == "penilaian") {
                $validMaster = true;
                $masterTbl = Container("penilaian");
                if (($parm = Post("fk_penilaian_id", Post("penilaian_id"))) !== null) {
                    $masterTbl->penilaian_id->setFormValue($parm);
                    $this->penilaian_id->setFormValue($masterTbl->penilaian_id->FormValue);
                    $this->penilaian_id->setSessionValue($this->penilaian_id->FormValue);
                    if (!is_numeric($masterTbl->penilaian_id->FormValue)) {
                        $validMaster = false;
                    }
                } else {
                    $validMaster = false;
                }
            }
        }
        if ($validMaster) {
            // Save current master table
            $this->setCurrentMasterTable($masterTblVar);
            $this->setSessionWhere($this->getDetailFilter());

            // Reset start record counter (new master key)
            if (!$this->isAddOrEdit()) {
                $this->StartRecord = 1;
                $this->setStartRecordNumber($this->StartRecord);
            }

            // Clear previous master key from Session
            if ($masterTblVar != "penilaian") {
                if ($this->penilaian_id->CurrentValue == "") {
                    $this->penilaian_id->setSessionValue("");
                }
            }
        }
        $this->DbMasterFilter = $this->getMasterFilter(); // Get master filter
        $this->DbDetailFilter = $this->getDetailFilter(); // Get detail filter
    }

    // Set up Breadcrumb
    protected function setupBreadcrumb()
    {
        global $Breadcrumb, $Language;
        $Breadcrumb = new Breadcrumb("index");
        $url = CurrentUrl();
        $Breadcrumb->add("list", $this->TableVar, $this->addMasterUrl("NilaiFormatifList"), "", $this->TableVar, true);
        $pageId = "edit";
        $Breadcrumb->add("edit", $pageId, $url);
    }

    // Setup lookup options
    public function setupLookupOptions($fld)
    {
        if ($fld->Lookup !== null && $fld->Lookup->Options === null) {
            // Get default connection and filter
            $conn = $this->getConnection();
            $lookupFilter = "";

            // No need to check any more
            $fld->Lookup->Options = [];

            // Set up lookup SQL and connection
            switch ($fld->FieldVar) {
                case "x_kode_mapel":
                    break;
                case "x_kelas":
                    $lookupFilter = function () {
                        return "`kode_tapel`='".kode_tapel()."'";
                    };
                    $lookupFilter = $lookupFilter->bindTo($this);
                    break;
                default:
                    $lookupFilter = "";
                    break;
            }

            // Always call to Lookup->getSql so that user can setup Lookup->Options in Lookup_Selecting server event
            $sql = $fld->Lookup->getSql(false, "", $lookupFilter, $this);

            // Set up lookup cache
            if ($fld->UseLookupCache && $sql != "" && count($fld->Lookup->Options) == 0) {
                $totalCnt = $this->getRecordCount($sql, $conn);
                if ($totalCnt > $fld->LookupCacheCount) { // Total count > cache count, do not cache
                    return;
                }
                $rows = $conn->executeQuery($sql)->fetchAll(\PDO::FETCH_BOTH);
                $ar = [];
                foreach ($rows as $row) {
                    $row = $fld->Lookup->renderViewRow($row);
                    $ar[strval($row[0])] = $row;
                }
                $fld->Lookup->Options = $ar;
            }
        }
    }

    // Set up starting record parameters
    public function setupStartRecord()
    {
        if ($this->DisplayRecords == 0) {
            return;
        }
        if ($this->isPageRequest()) { // Validate request
            $startRec = Get(Config("TABLE_START_REC"));
            $pageNo = Get(Config("TABLE_PAGE_NO"));
            if ($pageNo !== null) { // Check for "pageno" parameter first
                if (is_numeric($pageNo)) {
                    $this->StartRecord = ($pageNo - 1) * $this->DisplayRecords + 1;
                    if ($this->StartRecord <= 0) {
                        $this->StartRecord = 1;
                    } elseif ($this->StartRecord >= (int)(($this->TotalRecords - 1) / $this->DisplayRecords) * $this->DisplayRecords + 1) {
                        $this->StartRecord = (int)(($this->TotalRecords - 1) / $this->DisplayRecords) * $this->DisplayRecords + 1;
                    }
                    $this->setStartRecordNumber($this->StartRecord);
                }
            } elseif ($startRec !== null) { // Check for "start" parameter
                $this->StartRecord = $startRec;
                $this->setStartRecordNumber($this->StartRecord);
            }
        }
        $this->StartRecord = $this->getStartRecordNumber();

        // Check if correct start record counter
        if (!is_numeric($this->StartRecord) || $this->StartRecord == "") { // Avoid invalid start record counter
            $this->StartRecord = 1; // Reset start record counter
            $this->setStartRecordNumber($this->StartRecord);
        } elseif ($this->StartRecord > $this->TotalRecords) { // Avoid starting record > total records
            $this->StartRecord = (int)(($this->TotalRecords - 1) / $this->DisplayRecords) * $this->DisplayRecords + 1; // Point to last page first record
            $this->setStartRecordNumber($this->StartRecord);
        } elseif (($this->StartRecord - 1) % $this->DisplayRecords != 0) {
            $this->StartRecord = (int)(($this->StartRecord - 1) / $this->DisplayRecords) * $this->DisplayRecords + 1; // Point to page boundary
            $this->setStartRecordNumber($this->StartRecord);
        }
    }

    // Page Load event
    public function pageLoad()
    {
        //Log("Page Load");
    }

    // Page Unload event
    public function pageUnload()
    {
        //Log("Page Unload");
    }

    // Page Redirecting event
    public function pageRedirecting(&$url)
    {
        // Example:
        //$url = "your URL";
    }

    // Message Showing event
    // $type = ''|'success'|'failure'|'warning'
    public function messageShowing(&$msg, $type)
    {
        if ($type == 'success') {
            //$msg = "your success message";
        } elseif ($type == 'failure') {
            //$msg = "your failure message";
        } elseif ($type == 'warning') {
            //$msg = "your warning message";
        } else {
            //$msg = "your message";
        }
    }

    // Page Render event
    public function pageRender()
    {
        //Log("Page Render");
    }

    // Page Data Rendering event
    public function pageDataRendering(&$header)
    {
        // Example:
        $header = '<div class="card card-outline card-primary card-body"';
    }

    // Page Data Rendered event
    public function pageDataRendered(&$footer)
    {
        // Example:
        $footer = "</div>";
    }

    // Form Custom Validate event
    public function formCustomValidate(&$customError)
    {
        // Return error message in CustomError
        return true;
    }
}
