<?php

namespace PHPMaker2021\rdigital21;

use Doctrine\DBAL\ParameterType;

/**
 * Page class
 */
class NilaiSumatifAdd extends NilaiSumatif
{
    use MessagesTrait;

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

    // Project ID
    public $ProjectID = PROJECT_ID;

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

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

    // 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_sumatif)
        if (!isset($GLOBALS["nilai_sumatif"]) || get_class($GLOBALS["nilai_sumatif"]) == PROJECT_NAMESPACE . "nilai_sumatif") {
            $GLOBALS["nilai_sumatif"] = &$this;
        }

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

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

        // 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_sumatif"));
                $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 == "NilaiSumatifView") {
                        $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_s_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-add-form";
    public $IsModal = false;
    public $IsMobileOrModal = false;
    public $DbMasterFilter = "";
    public $DbDetailFilter = "";
    public $StartRecord;
    public $Priv = 0;
    public $OldRecordset;
    public $CopyRecord;

    /**
     * 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_s_id->Visible = false;
        $this->kode_tapel->setVisibility();
        $this->penilaian_id->setVisibility();
        $this->guru_id->setVisibility();
        $this->kode_mapel->setVisibility();
        $this->kelas->setVisibility();
        $this->nis->setVisibility();
        $this->nisn->setVisibility();
        $this->nama->setVisibility();
        $this->jk->setVisibility();
        $this->s1->setVisibility();
        $this->s2->setVisibility();
        $this->s3->setVisibility();
        $this->s4->setVisibility();
        $this->s5->setVisibility();
        $this->s6->setVisibility();
        $this->s7->setVisibility();
        $this->s8->setVisibility();
        $this->nas->setVisibility();
        $this->sts->setVisibility();
        $this->sas->setVisibility();
        $this->nr1->setVisibility();
        $this->nr2->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-add-form ew-horizontal";
        $postBack = false;

        // Set up current action
        if (IsApi()) {
            $this->CurrentAction = "insert"; // Add record directly
            $postBack = true;
        } elseif (Post("action") !== null) {
            $this->CurrentAction = Post("action"); // Get form action
            $this->setKey(Post($this->OldKeyName));
            $postBack = true;
        } else {
            // Load key values from QueryString
            if (($keyValue = Get("nilai_s_id") ?? Route("nilai_s_id")) !== null) {
                $this->nilai_s_id->setQueryStringValue($keyValue);
            }
            $this->OldKey = $this->getKey(true); // Get from CurrentValue
            $this->CopyRecord = !EmptyValue($this->OldKey);
            if ($this->CopyRecord) {
                $this->CurrentAction = "copy"; // Copy record
            } else {
                $this->CurrentAction = "show"; // Display blank record
            }
        }

        // Load old record / default values
        $loaded = $this->loadOldRecord();

        // Set up master/detail parameters
        // NOTE: must be after loadOldRecord to prevent master key values overwritten
        $this->setupMasterParms();

        // Load form values
        if ($postBack) {
            $this->loadFormValues(); // Load form values
        }

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

        // Perform current action
        switch ($this->CurrentAction) {
            case "copy": // Copy an existing record
                if (!$loaded) { // Record not loaded
                    if ($this->getFailureMessage() == "") {
                        $this->setFailureMessage($Language->phrase("NoRecord")); // No record found
                    }
                    $this->terminate("NilaiSumatifList"); // No matching record, return to list
                    return;
                }
                break;
            case "insert": // Add new record
                $this->SendEmail = true; // Send email on add success
                if ($this->addRow($this->OldRecordset)) { // Add successful
                    if ($this->getSuccessMessage() == "" && Post("addopt") != "1") { // Skip success message for addopt (done in JavaScript)
                        $this->setSuccessMessage($Language->phrase("AddSuccess")); // Set up success message
                    }
                    $returnUrl = $this->getReturnUrl();
                    if (GetPageName($returnUrl) == "NilaiSumatifList") {
                        $returnUrl = $this->addMasterUrl($returnUrl); // List page, return to List page with correct master key if necessary
                    } elseif (GetPageName($returnUrl) == "NilaiSumatifView") {
                        $returnUrl = $this->getViewUrl(); // View page, return to View page with keyurl directly
                    }
                    if (IsApi()) { // Return to caller
                        $this->terminate(true);
                        return;
                    } else {
                        $this->terminate($returnUrl);
                        return;
                    }
                } elseif (IsApi()) { // API request, return
                    $this->terminate();
                    return;
                } else {
                    $this->EventCancelled = true; // Event cancelled
                    $this->restoreFormValues(); // Add failed, restore form values
                }
        }

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

        // Render row based on row type
        $this->RowType = ROWTYPE_ADD; // Render add type

        // Render row
        $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 default values
    protected function loadDefaultValues()
    {
        $this->nilai_s_id->CurrentValue = null;
        $this->nilai_s_id->OldValue = $this->nilai_s_id->CurrentValue;
        $this->kode_tapel->CurrentValue = null;
        $this->kode_tapel->OldValue = $this->kode_tapel->CurrentValue;
        $this->penilaian_id->CurrentValue = null;
        $this->penilaian_id->OldValue = $this->penilaian_id->CurrentValue;
        $this->guru_id->CurrentValue = null;
        $this->guru_id->OldValue = $this->guru_id->CurrentValue;
        $this->kode_mapel->CurrentValue = null;
        $this->kode_mapel->OldValue = $this->kode_mapel->CurrentValue;
        $this->kelas->CurrentValue = CurrentUserID();
        $this->nis->CurrentValue = null;
        $this->nis->OldValue = $this->nis->CurrentValue;
        $this->nisn->CurrentValue = null;
        $this->nisn->OldValue = $this->nisn->CurrentValue;
        $this->nama->CurrentValue = null;
        $this->nama->OldValue = $this->nama->CurrentValue;
        $this->jk->CurrentValue = null;
        $this->jk->OldValue = $this->jk->CurrentValue;
        $this->s1->CurrentValue = 0;
        $this->s2->CurrentValue = 0;
        $this->s3->CurrentValue = 0;
        $this->s4->CurrentValue = 0;
        $this->s5->CurrentValue = 0;
        $this->s6->CurrentValue = 0;
        $this->s7->CurrentValue = 0;
        $this->s8->CurrentValue = 0;
        $this->nas->CurrentValue = 0;
        $this->sts->CurrentValue = 0;
        $this->sas->CurrentValue = 0;
        $this->nr1->CurrentValue = 0;
        $this->nr2->CurrentValue = 0;
    }

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

        // 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 'nisn' first before field var 'x_nisn'
        $val = $CurrentForm->hasValue("nisn") ? $CurrentForm->getValue("nisn") : $CurrentForm->getValue("x_nisn");
        if (!$this->nisn->IsDetailKey) {
            if (IsApi() && $val === null) {
                $this->nisn->Visible = false; // Disable update for API request
            } else {
                $this->nisn->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 's1' first before field var 'x_s1'
        $val = $CurrentForm->hasValue("s1") ? $CurrentForm->getValue("s1") : $CurrentForm->getValue("x_s1");
        if (!$this->s1->IsDetailKey) {
            if (IsApi() && $val === null) {
                $this->s1->Visible = false; // Disable update for API request
            } else {
                $this->s1->setFormValue($val);
            }
        }

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

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

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

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

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

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

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

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

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

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

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

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

        // Check field name 'nilai_s_id' first before field var 'x_nilai_s_id'
        $val = $CurrentForm->hasValue("nilai_s_id") ? $CurrentForm->getValue("nilai_s_id") : $CurrentForm->getValue("x_nilai_s_id");
    }

    // Restore form values
    public function restoreFormValues()
    {
        global $CurrentForm;
        $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->nisn->CurrentValue = $this->nisn->FormValue;
        $this->nama->CurrentValue = $this->nama->FormValue;
        $this->jk->CurrentValue = $this->jk->FormValue;
        $this->s1->CurrentValue = $this->s1->FormValue;
        $this->s2->CurrentValue = $this->s2->FormValue;
        $this->s3->CurrentValue = $this->s3->FormValue;
        $this->s4->CurrentValue = $this->s4->FormValue;
        $this->s5->CurrentValue = $this->s5->FormValue;
        $this->s6->CurrentValue = $this->s6->FormValue;
        $this->s7->CurrentValue = $this->s7->FormValue;
        $this->s8->CurrentValue = $this->s8->FormValue;
        $this->nas->CurrentValue = $this->nas->FormValue;
        $this->sts->CurrentValue = $this->sts->FormValue;
        $this->sas->CurrentValue = $this->sas->FormValue;
        $this->nr1->CurrentValue = $this->nr1->FormValue;
        $this->nr2->CurrentValue = $this->nr2->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
        }

        // Check if valid User ID
        if ($res) {
            $res = $this->showOptionLink("add");
            if (!$res) {
                $userIdMsg = DeniedMessage();
                $this->setFailureMessage($userIdMsg);
            }
        }
        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_s_id->setDbValue($row['nilai_s_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->nisn->setDbValue($row['nisn']);
        $this->nama->setDbValue($row['nama']);
        $this->jk->setDbValue($row['jk']);
        $this->s1->setDbValue($row['s1']);
        $this->s2->setDbValue($row['s2']);
        $this->s3->setDbValue($row['s3']);
        $this->s4->setDbValue($row['s4']);
        $this->s5->setDbValue($row['s5']);
        $this->s6->setDbValue($row['s6']);
        $this->s7->setDbValue($row['s7']);
        $this->s8->setDbValue($row['s8']);
        $this->nas->setDbValue($row['nas']);
        $this->sts->setDbValue($row['sts']);
        $this->sas->setDbValue($row['sas']);
        $this->nr1->setDbValue($row['nr1']);
        $this->nr2->setDbValue($row['nr2']);
    }

    // Return a row with default values
    protected function newRow()
    {
        $this->loadDefaultValues();
        $row = [];
        $row['nilai_s_id'] = $this->nilai_s_id->CurrentValue;
        $row['kode_tapel'] = $this->kode_tapel->CurrentValue;
        $row['penilaian_id'] = $this->penilaian_id->CurrentValue;
        $row['guru_id'] = $this->guru_id->CurrentValue;
        $row['kode_mapel'] = $this->kode_mapel->CurrentValue;
        $row['kelas'] = $this->kelas->CurrentValue;
        $row['nis'] = $this->nis->CurrentValue;
        $row['nisn'] = $this->nisn->CurrentValue;
        $row['nama'] = $this->nama->CurrentValue;
        $row['jk'] = $this->jk->CurrentValue;
        $row['s1'] = $this->s1->CurrentValue;
        $row['s2'] = $this->s2->CurrentValue;
        $row['s3'] = $this->s3->CurrentValue;
        $row['s4'] = $this->s4->CurrentValue;
        $row['s5'] = $this->s5->CurrentValue;
        $row['s6'] = $this->s6->CurrentValue;
        $row['s7'] = $this->s7->CurrentValue;
        $row['s8'] = $this->s8->CurrentValue;
        $row['nas'] = $this->nas->CurrentValue;
        $row['sts'] = $this->sts->CurrentValue;
        $row['sas'] = $this->sas->CurrentValue;
        $row['nr1'] = $this->nr1->CurrentValue;
        $row['nr2'] = $this->nr2->CurrentValue;
        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_s_id

        // kode_tapel

        // penilaian_id

        // guru_id

        // kode_mapel

        // kelas

        // nis

        // nisn

        // nama

        // jk

        // s1

        // s2

        // s3

        // s4

        // s5

        // s6

        // s7

        // s8

        // nas

        // sts

        // sas

        // nr1

        // nr2
        if ($this->RowType == ROWTYPE_VIEW) {
            // nilai_s_id
            $this->nilai_s_id->ViewValue = $this->nilai_s_id->CurrentValue;
            $this->nilai_s_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 = "";

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            // 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 = "";

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            // nr2
            $this->nr2->LinkCustomAttributes = "";
            $this->nr2->HrefValue = "";
            $this->nr2->TooltipValue = "";
        } elseif ($this->RowType == ROWTYPE_ADD) {
            // 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 = "";
            if (!$Security->isAdmin() && $Security->isLoggedIn() && !$this->userIDAllow("add")) { // Non system admin
                $this->kelas->CurrentValue = CurrentUserID();
                $this->kelas->EditValue = $this->kelas->CurrentValue;
                $this->kelas->ViewCustomAttributes = "";
            } else {
                $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);

            // nisn
            $this->nisn->EditAttrs["class"] = "form-control";
            $this->nisn->EditCustomAttributes = "";
            if (!$this->nisn->Raw) {
                $this->nisn->CurrentValue = HtmlDecode($this->nisn->CurrentValue);
            }
            $this->nisn->EditValue = HtmlEncode($this->nisn->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);

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

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

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

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

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

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

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

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

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

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

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

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

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

            // Add refer script

            // 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 = "";

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            // nr2
            $this->nr2->LinkCustomAttributes = "";
            $this->nr2->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->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->nisn->Required) {
            if (!$this->nisn->IsDetailKey && EmptyValue($this->nisn->FormValue)) {
                $this->nisn->addErrorMessage(str_replace("%s", $this->nisn->caption(), $this->nisn->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->s1->Required) {
            if (!$this->s1->IsDetailKey && EmptyValue($this->s1->FormValue)) {
                $this->s1->addErrorMessage(str_replace("%s", $this->s1->caption(), $this->s1->RequiredErrorMessage));
            }
        }
        if ($this->s2->Required) {
            if (!$this->s2->IsDetailKey && EmptyValue($this->s2->FormValue)) {
                $this->s2->addErrorMessage(str_replace("%s", $this->s2->caption(), $this->s2->RequiredErrorMessage));
            }
        }
        if ($this->s3->Required) {
            if (!$this->s3->IsDetailKey && EmptyValue($this->s3->FormValue)) {
                $this->s3->addErrorMessage(str_replace("%s", $this->s3->caption(), $this->s3->RequiredErrorMessage));
            }
        }
        if ($this->s4->Required) {
            if (!$this->s4->IsDetailKey && EmptyValue($this->s4->FormValue)) {
                $this->s4->addErrorMessage(str_replace("%s", $this->s4->caption(), $this->s4->RequiredErrorMessage));
            }
        }
        if ($this->s5->Required) {
            if (!$this->s5->IsDetailKey && EmptyValue($this->s5->FormValue)) {
                $this->s5->addErrorMessage(str_replace("%s", $this->s5->caption(), $this->s5->RequiredErrorMessage));
            }
        }
        if ($this->s6->Required) {
            if (!$this->s6->IsDetailKey && EmptyValue($this->s6->FormValue)) {
                $this->s6->addErrorMessage(str_replace("%s", $this->s6->caption(), $this->s6->RequiredErrorMessage));
            }
        }
        if ($this->s7->Required) {
            if (!$this->s7->IsDetailKey && EmptyValue($this->s7->FormValue)) {
                $this->s7->addErrorMessage(str_replace("%s", $this->s7->caption(), $this->s7->RequiredErrorMessage));
            }
        }
        if ($this->s8->Required) {
            if (!$this->s8->IsDetailKey && EmptyValue($this->s8->FormValue)) {
                $this->s8->addErrorMessage(str_replace("%s", $this->s8->caption(), $this->s8->RequiredErrorMessage));
            }
        }
        if ($this->nas->Required) {
            if (!$this->nas->IsDetailKey && EmptyValue($this->nas->FormValue)) {
                $this->nas->addErrorMessage(str_replace("%s", $this->nas->caption(), $this->nas->RequiredErrorMessage));
            }
        }
        if ($this->sts->Required) {
            if (!$this->sts->IsDetailKey && EmptyValue($this->sts->FormValue)) {
                $this->sts->addErrorMessage(str_replace("%s", $this->sts->caption(), $this->sts->RequiredErrorMessage));
            }
        }
        if ($this->sas->Required) {
            if (!$this->sas->IsDetailKey && EmptyValue($this->sas->FormValue)) {
                $this->sas->addErrorMessage(str_replace("%s", $this->sas->caption(), $this->sas->RequiredErrorMessage));
            }
        }
        if ($this->nr1->Required) {
            if (!$this->nr1->IsDetailKey && EmptyValue($this->nr1->FormValue)) {
                $this->nr1->addErrorMessage(str_replace("%s", $this->nr1->caption(), $this->nr1->RequiredErrorMessage));
            }
        }
        if ($this->nr2->Required) {
            if (!$this->nr2->IsDetailKey && EmptyValue($this->nr2->FormValue)) {
                $this->nr2->addErrorMessage(str_replace("%s", $this->nr2->caption(), $this->nr2->RequiredErrorMessage));
            }
        }

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

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

    // Add record
    protected function addRow($rsold = null)
    {
        global $Language, $Security;

        // Check if valid User ID
        $validUser = false;
        if ($Security->currentUserID() != "" && !EmptyValue($this->kelas->CurrentValue) && !$Security->isAdmin()) { // Non system admin
            $validUser = $Security->isValidUserID($this->kelas->CurrentValue);
            if (!$validUser) {
                $userIdMsg = str_replace("%c", CurrentUserID(), $Language->phrase("UnAuthorizedUserID"));
                $userIdMsg = str_replace("%u", $this->kelas->CurrentValue, $userIdMsg);
                $this->setFailureMessage($userIdMsg);
                return false;
            }
        }

        // Check if valid key values for master user
        if ($Security->currentUserID() != "" && !$Security->isAdmin()) { // Non system admin
            $masterFilter = $this->sqlMasterFilter_penilaian();
            if (strval($this->penilaian_id->CurrentValue) != "") {
                $masterFilter = str_replace("@penilaian_id@", AdjustSql($this->penilaian_id->CurrentValue, "DB"), $masterFilter);
            } else {
                $masterFilter = "";
            }
            if ($masterFilter != "") {
                $rsmaster = Container("penilaian")->loadRs($masterFilter)->fetch(\PDO::FETCH_ASSOC);
                $masterRecordExists = $rsmaster !== false;
                $validMasterKey = true;
                if ($masterRecordExists) {
                    $validMasterKey = $Security->isValidUserID($rsmaster['kelas']);
                } elseif ($this->getCurrentMasterTable() == "penilaian") {
                    $validMasterKey = false;
                }
                if (!$validMasterKey) {
                    $masterUserIdMsg = str_replace("%c", CurrentUserID(), $Language->phrase("UnAuthorizedMasterUserID"));
                    $masterUserIdMsg = str_replace("%f", $masterFilter, $masterUserIdMsg);
                    $this->setFailureMessage($masterUserIdMsg);
                    return false;
                }
            }
        }

        // Check referential integrity for master table 'nilai_sumatif'
        $validMasterRecord = true;
        $masterFilter = $this->sqlMasterFilter_penilaian();
        if (strval($this->penilaian_id->CurrentValue) != "") {
            $masterFilter = str_replace("@penilaian_id@", AdjustSql($this->penilaian_id->CurrentValue, "DB"), $masterFilter);
        } else {
            $validMasterRecord = false;
        }
        if ($validMasterRecord) {
            $rsmaster = Container("penilaian")->loadRs($masterFilter)->fetch();
            $validMasterRecord = $rsmaster !== false;
        }
        if (!$validMasterRecord) {
            $relatedRecordMsg = str_replace("%t", "penilaian", $Language->phrase("RelatedRecordRequired"));
            $this->setFailureMessage($relatedRecordMsg);
            return false;
        }
        $conn = $this->getConnection();

        // Load db values from rsold
        $this->loadDbValues($rsold);
        if ($rsold) {
        }
        $rsnew = [];

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

        // penilaian_id
        $this->penilaian_id->setDbValueDef($rsnew, $this->penilaian_id->CurrentValue, null, false);

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

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

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

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

        // nisn
        $this->nisn->setDbValueDef($rsnew, $this->nisn->CurrentValue, null, false);

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

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

        // s1
        $this->s1->setDbValueDef($rsnew, $this->s1->CurrentValue, null, false);

        // s2
        $this->s2->setDbValueDef($rsnew, $this->s2->CurrentValue, null, false);

        // s3
        $this->s3->setDbValueDef($rsnew, $this->s3->CurrentValue, null, false);

        // s4
        $this->s4->setDbValueDef($rsnew, $this->s4->CurrentValue, null, false);

        // s5
        $this->s5->setDbValueDef($rsnew, $this->s5->CurrentValue, null, false);

        // s6
        $this->s6->setDbValueDef($rsnew, $this->s6->CurrentValue, null, false);

        // s7
        $this->s7->setDbValueDef($rsnew, $this->s7->CurrentValue, null, false);

        // s8
        $this->s8->setDbValueDef($rsnew, $this->s8->CurrentValue, null, false);

        // nas
        $this->nas->setDbValueDef($rsnew, $this->nas->CurrentValue, null, false);

        // sts
        $this->sts->setDbValueDef($rsnew, $this->sts->CurrentValue, null, false);

        // sas
        $this->sas->setDbValueDef($rsnew, $this->sas->CurrentValue, null, false);

        // nr1
        $this->nr1->setDbValueDef($rsnew, $this->nr1->CurrentValue, null, false);

        // nr2
        $this->nr2->setDbValueDef($rsnew, $this->nr2->CurrentValue, null, false);

        // Call Row Inserting event
        $insertRow = $this->rowInserting($rsold, $rsnew);
        $addRow = false;
        if ($insertRow) {
            try {
                $addRow = $this->insert($rsnew);
            } catch (\Exception $e) {
                $this->setFailureMessage($e->getMessage());
            }
            if ($addRow) {
            }
        } 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("InsertCancelled"));
            }
            $addRow = false;
        }
        if ($addRow) {
            // Call Row Inserted event
            $this->rowInserted($rsold, $rsnew);
        }

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

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

    // Show link optionally based on User ID
    protected function showOptionLink($id = "")
    {
        global $Security;
        if ($Security->isLoggedIn() && !$Security->isAdmin() && !$this->userIDAllow($id)) {
            return $Security->isValidUserID($this->kelas->CurrentValue);
        }
        return true;
    }

    // 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);

            // 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("NilaiSumatifList"), "", $this->TableVar, true);
        $pageId = ($this->isCopy()) ? "Copy" : "Add";
        $Breadcrumb->add("add", $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;
            }
        }
    }

    // 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;
    }
}
