<?php

namespace PHPMaker2021\rdigital21;

use Doctrine\DBAL\ParameterType;

/**
 * Page class
 */
class PesertaDidikAdd extends PesertaDidik
{
    use MessagesTrait;

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

    // Project ID
    public $ProjectID = PROJECT_ID;

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

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

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

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

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

        // Parent constuctor
        parent::__construct();

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

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

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

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

         // 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"))) {
            $content = $this->getContents();
            if ($ExportFileName == "") {
                $ExportFileName = $this->TableVar;
            }
            $class = PROJECT_NAMESPACE . Config("EXPORT_CLASSES." . $this->CustomExport);
            if (class_exists($class)) {
                $doc = new $class(Container("peserta_didik"));
                $doc->Text = @$content;
                if ($this->isExport("email")) {
                    echo $this->exportEmail($doc->Text);
                } else {
                    $doc->export();
                }
                DeleteTempImages(); // Delete temp images
                return;
            }
        }
        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 == "PesertaDidikView") {
                        $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['pd_id'];
        }
        return $key;
    }

    /**
     * Hide fields for add/edit
     *
     * @return void
     */
    protected function hideFieldsForAddEdit()
    {
        if ($this->isAdd() || $this->isCopy() || $this->isGridAdd()) {
            $this->pd_id->Visible = false;
        }
    }

    // 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->pd_id->Visible = false;
        $this->kode_tapel->setVisibility();
        $this->kelas->setVisibility();
        $this->nisn->setVisibility();
        $this->nis->setVisibility();
        $this->nama->setVisibility();
        $this->jk->setVisibility();
        $this->tmp_lahir->setVisibility();
        $this->tgl_lahir->setVisibility();
        $this->agama->setVisibility();
        $this->status->setVisibility();
        $this->anak_ke->setVisibility();
        $this->alamat_siswa->setVisibility();
        $this->no_hp->setVisibility();
        $this->sekolah_asal->setVisibility();
        $this->diterima_di_kls->setVisibility();
        $this->tanggal->setVisibility();
        $this->nama_ayah->setVisibility();
        $this->nama_ibu->setVisibility();
        $this->alamat->setVisibility();
        $this->telepon->setVisibility();
        $this->kerja_ayah->setVisibility();
        $this->kerja_ibu->setVisibility();
        $this->nama_wali->setVisibility();
        $this->no_telepon->setVisibility();
        $this->alamat_wali->setVisibility();
        $this->kerja_wali->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("pd_id") ?? Route("pd_id")) !== null) {
                $this->pd_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();

        // 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("PesertaDidikList"); // 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) == "PesertaDidikList") {
                        $returnUrl = $this->addMasterUrl($returnUrl); // List page, return to List page with correct master key if necessary
                    } elseif (GetPageName($returnUrl) == "PesertaDidikView") {
                        $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->pd_id->CurrentValue = null;
        $this->pd_id->OldValue = $this->pd_id->CurrentValue;
        $this->kode_tapel->CurrentValue = null;
        $this->kode_tapel->OldValue = $this->kode_tapel->CurrentValue;
        $this->kelas->CurrentValue = CurrentUserID();
        $this->nisn->CurrentValue = null;
        $this->nisn->OldValue = $this->nisn->CurrentValue;
        $this->nis->CurrentValue = null;
        $this->nis->OldValue = $this->nis->CurrentValue;
        $this->nama->CurrentValue = null;
        $this->nama->OldValue = $this->nama->CurrentValue;
        $this->jk->CurrentValue = null;
        $this->jk->OldValue = $this->jk->CurrentValue;
        $this->tmp_lahir->CurrentValue = null;
        $this->tmp_lahir->OldValue = $this->tmp_lahir->CurrentValue;
        $this->tgl_lahir->CurrentValue = null;
        $this->tgl_lahir->OldValue = $this->tgl_lahir->CurrentValue;
        $this->agama->CurrentValue = null;
        $this->agama->OldValue = $this->agama->CurrentValue;
        $this->status->CurrentValue = null;
        $this->status->OldValue = $this->status->CurrentValue;
        $this->anak_ke->CurrentValue = null;
        $this->anak_ke->OldValue = $this->anak_ke->CurrentValue;
        $this->alamat_siswa->CurrentValue = null;
        $this->alamat_siswa->OldValue = $this->alamat_siswa->CurrentValue;
        $this->no_hp->CurrentValue = null;
        $this->no_hp->OldValue = $this->no_hp->CurrentValue;
        $this->sekolah_asal->CurrentValue = null;
        $this->sekolah_asal->OldValue = $this->sekolah_asal->CurrentValue;
        $this->diterima_di_kls->CurrentValue = null;
        $this->diterima_di_kls->OldValue = $this->diterima_di_kls->CurrentValue;
        $this->tanggal->CurrentValue = null;
        $this->tanggal->OldValue = $this->tanggal->CurrentValue;
        $this->nama_ayah->CurrentValue = null;
        $this->nama_ayah->OldValue = $this->nama_ayah->CurrentValue;
        $this->nama_ibu->CurrentValue = null;
        $this->nama_ibu->OldValue = $this->nama_ibu->CurrentValue;
        $this->alamat->CurrentValue = null;
        $this->alamat->OldValue = $this->alamat->CurrentValue;
        $this->telepon->CurrentValue = null;
        $this->telepon->OldValue = $this->telepon->CurrentValue;
        $this->kerja_ayah->CurrentValue = null;
        $this->kerja_ayah->OldValue = $this->kerja_ayah->CurrentValue;
        $this->kerja_ibu->CurrentValue = null;
        $this->kerja_ibu->OldValue = $this->kerja_ibu->CurrentValue;
        $this->nama_wali->CurrentValue = null;
        $this->nama_wali->OldValue = $this->nama_wali->CurrentValue;
        $this->no_telepon->CurrentValue = null;
        $this->no_telepon->OldValue = $this->no_telepon->CurrentValue;
        $this->alamat_wali->CurrentValue = null;
        $this->alamat_wali->OldValue = $this->alamat_wali->CurrentValue;
        $this->kerja_wali->CurrentValue = null;
        $this->kerja_wali->OldValue = $this->kerja_wali->CurrentValue;
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    // Restore form values
    public function restoreFormValues()
    {
        global $CurrentForm;
        $this->kode_tapel->CurrentValue = $this->kode_tapel->FormValue;
        $this->kelas->CurrentValue = $this->kelas->FormValue;
        $this->nisn->CurrentValue = $this->nisn->FormValue;
        $this->nis->CurrentValue = $this->nis->FormValue;
        $this->nama->CurrentValue = $this->nama->FormValue;
        $this->jk->CurrentValue = $this->jk->FormValue;
        $this->tmp_lahir->CurrentValue = $this->tmp_lahir->FormValue;
        $this->tgl_lahir->CurrentValue = $this->tgl_lahir->FormValue;
        $this->agama->CurrentValue = $this->agama->FormValue;
        $this->status->CurrentValue = $this->status->FormValue;
        $this->anak_ke->CurrentValue = $this->anak_ke->FormValue;
        $this->alamat_siswa->CurrentValue = $this->alamat_siswa->FormValue;
        $this->no_hp->CurrentValue = $this->no_hp->FormValue;
        $this->sekolah_asal->CurrentValue = $this->sekolah_asal->FormValue;
        $this->diterima_di_kls->CurrentValue = $this->diterima_di_kls->FormValue;
        $this->tanggal->CurrentValue = $this->tanggal->FormValue;
        $this->nama_ayah->CurrentValue = $this->nama_ayah->FormValue;
        $this->nama_ibu->CurrentValue = $this->nama_ibu->FormValue;
        $this->alamat->CurrentValue = $this->alamat->FormValue;
        $this->telepon->CurrentValue = $this->telepon->FormValue;
        $this->kerja_ayah->CurrentValue = $this->kerja_ayah->FormValue;
        $this->kerja_ibu->CurrentValue = $this->kerja_ibu->FormValue;
        $this->nama_wali->CurrentValue = $this->nama_wali->FormValue;
        $this->no_telepon->CurrentValue = $this->no_telepon->FormValue;
        $this->alamat_wali->CurrentValue = $this->alamat_wali->FormValue;
        $this->kerja_wali->CurrentValue = $this->kerja_wali->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->pd_id->setDbValue($row['pd_id']);
        $this->kode_tapel->setDbValue($row['kode_tapel']);
        $this->kelas->setDbValue($row['kelas']);
        $this->nisn->setDbValue($row['nisn']);
        $this->nis->setDbValue($row['nis']);
        $this->nama->setDbValue($row['nama']);
        $this->jk->setDbValue($row['jk']);
        $this->tmp_lahir->setDbValue($row['tmp_lahir']);
        $this->tgl_lahir->setDbValue($row['tgl_lahir']);
        $this->agama->setDbValue($row['agama']);
        $this->status->setDbValue($row['status']);
        $this->anak_ke->setDbValue($row['anak_ke']);
        $this->alamat_siswa->setDbValue($row['alamat_siswa']);
        $this->no_hp->setDbValue($row['no_hp']);
        $this->sekolah_asal->setDbValue($row['sekolah_asal']);
        $this->diterima_di_kls->setDbValue($row['diterima_di_kls']);
        $this->tanggal->setDbValue($row['tanggal']);
        $this->nama_ayah->setDbValue($row['nama_ayah']);
        $this->nama_ibu->setDbValue($row['nama_ibu']);
        $this->alamat->setDbValue($row['alamat']);
        $this->telepon->setDbValue($row['telepon']);
        $this->kerja_ayah->setDbValue($row['kerja_ayah']);
        $this->kerja_ibu->setDbValue($row['kerja_ibu']);
        $this->nama_wali->setDbValue($row['nama_wali']);
        $this->no_telepon->setDbValue($row['no_telepon']);
        $this->alamat_wali->setDbValue($row['alamat_wali']);
        $this->kerja_wali->setDbValue($row['kerja_wali']);
    }

    // Return a row with default values
    protected function newRow()
    {
        $this->loadDefaultValues();
        $row = [];
        $row['pd_id'] = $this->pd_id->CurrentValue;
        $row['kode_tapel'] = $this->kode_tapel->CurrentValue;
        $row['kelas'] = $this->kelas->CurrentValue;
        $row['nisn'] = $this->nisn->CurrentValue;
        $row['nis'] = $this->nis->CurrentValue;
        $row['nama'] = $this->nama->CurrentValue;
        $row['jk'] = $this->jk->CurrentValue;
        $row['tmp_lahir'] = $this->tmp_lahir->CurrentValue;
        $row['tgl_lahir'] = $this->tgl_lahir->CurrentValue;
        $row['agama'] = $this->agama->CurrentValue;
        $row['status'] = $this->status->CurrentValue;
        $row['anak_ke'] = $this->anak_ke->CurrentValue;
        $row['alamat_siswa'] = $this->alamat_siswa->CurrentValue;
        $row['no_hp'] = $this->no_hp->CurrentValue;
        $row['sekolah_asal'] = $this->sekolah_asal->CurrentValue;
        $row['diterima_di_kls'] = $this->diterima_di_kls->CurrentValue;
        $row['tanggal'] = $this->tanggal->CurrentValue;
        $row['nama_ayah'] = $this->nama_ayah->CurrentValue;
        $row['nama_ibu'] = $this->nama_ibu->CurrentValue;
        $row['alamat'] = $this->alamat->CurrentValue;
        $row['telepon'] = $this->telepon->CurrentValue;
        $row['kerja_ayah'] = $this->kerja_ayah->CurrentValue;
        $row['kerja_ibu'] = $this->kerja_ibu->CurrentValue;
        $row['nama_wali'] = $this->nama_wali->CurrentValue;
        $row['no_telepon'] = $this->no_telepon->CurrentValue;
        $row['alamat_wali'] = $this->alamat_wali->CurrentValue;
        $row['kerja_wali'] = $this->kerja_wali->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

        // pd_id

        // kode_tapel

        // kelas

        // nisn

        // nis

        // nama

        // jk

        // tmp_lahir

        // tgl_lahir

        // agama

        // status

        // anak_ke

        // alamat_siswa

        // no_hp

        // sekolah_asal

        // diterima_di_kls

        // tanggal

        // nama_ayah

        // nama_ibu

        // alamat

        // telepon

        // kerja_ayah

        // kerja_ibu

        // nama_wali

        // no_telepon

        // alamat_wali

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

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

            // nisn
            $this->nisn->ViewValue = $this->nisn->CurrentValue;
            $this->nisn->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 = "";

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            // nisn
            $this->nisn->LinkCustomAttributes = "";
            $this->nisn->HrefValue = "";
            $this->nisn->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 = "";

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            // kerja_wali
            $this->kerja_wali->LinkCustomAttributes = "";
            $this->kerja_wali->HrefValue = "";
            $this->kerja_wali->TooltipValue = "";
        } elseif ($this->RowType == ROWTYPE_ADD) {
            // kode_tapel

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            // Add refer script

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            // kerja_wali
            $this->kerja_wali->LinkCustomAttributes = "";
            $this->kerja_wali->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();
        }
    }

    // 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->kelas->Required) {
            if (!$this->kelas->IsDetailKey && EmptyValue($this->kelas->FormValue)) {
                $this->kelas->addErrorMessage(str_replace("%s", $this->kelas->caption(), $this->kelas->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->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->tmp_lahir->Required) {
            if (!$this->tmp_lahir->IsDetailKey && EmptyValue($this->tmp_lahir->FormValue)) {
                $this->tmp_lahir->addErrorMessage(str_replace("%s", $this->tmp_lahir->caption(), $this->tmp_lahir->RequiredErrorMessage));
            }
        }
        if ($this->tgl_lahir->Required) {
            if (!$this->tgl_lahir->IsDetailKey && EmptyValue($this->tgl_lahir->FormValue)) {
                $this->tgl_lahir->addErrorMessage(str_replace("%s", $this->tgl_lahir->caption(), $this->tgl_lahir->RequiredErrorMessage));
            }
        }
        if ($this->agama->Required) {
            if (!$this->agama->IsDetailKey && EmptyValue($this->agama->FormValue)) {
                $this->agama->addErrorMessage(str_replace("%s", $this->agama->caption(), $this->agama->RequiredErrorMessage));
            }
        }
        if ($this->status->Required) {
            if (!$this->status->IsDetailKey && EmptyValue($this->status->FormValue)) {
                $this->status->addErrorMessage(str_replace("%s", $this->status->caption(), $this->status->RequiredErrorMessage));
            }
        }
        if ($this->anak_ke->Required) {
            if (!$this->anak_ke->IsDetailKey && EmptyValue($this->anak_ke->FormValue)) {
                $this->anak_ke->addErrorMessage(str_replace("%s", $this->anak_ke->caption(), $this->anak_ke->RequiredErrorMessage));
            }
        }
        if ($this->alamat_siswa->Required) {
            if (!$this->alamat_siswa->IsDetailKey && EmptyValue($this->alamat_siswa->FormValue)) {
                $this->alamat_siswa->addErrorMessage(str_replace("%s", $this->alamat_siswa->caption(), $this->alamat_siswa->RequiredErrorMessage));
            }
        }
        if ($this->no_hp->Required) {
            if (!$this->no_hp->IsDetailKey && EmptyValue($this->no_hp->FormValue)) {
                $this->no_hp->addErrorMessage(str_replace("%s", $this->no_hp->caption(), $this->no_hp->RequiredErrorMessage));
            }
        }
        if ($this->sekolah_asal->Required) {
            if (!$this->sekolah_asal->IsDetailKey && EmptyValue($this->sekolah_asal->FormValue)) {
                $this->sekolah_asal->addErrorMessage(str_replace("%s", $this->sekolah_asal->caption(), $this->sekolah_asal->RequiredErrorMessage));
            }
        }
        if ($this->diterima_di_kls->Required) {
            if (!$this->diterima_di_kls->IsDetailKey && EmptyValue($this->diterima_di_kls->FormValue)) {
                $this->diterima_di_kls->addErrorMessage(str_replace("%s", $this->diterima_di_kls->caption(), $this->diterima_di_kls->RequiredErrorMessage));
            }
        }
        if ($this->tanggal->Required) {
            if (!$this->tanggal->IsDetailKey && EmptyValue($this->tanggal->FormValue)) {
                $this->tanggal->addErrorMessage(str_replace("%s", $this->tanggal->caption(), $this->tanggal->RequiredErrorMessage));
            }
        }
        if ($this->nama_ayah->Required) {
            if (!$this->nama_ayah->IsDetailKey && EmptyValue($this->nama_ayah->FormValue)) {
                $this->nama_ayah->addErrorMessage(str_replace("%s", $this->nama_ayah->caption(), $this->nama_ayah->RequiredErrorMessage));
            }
        }
        if ($this->nama_ibu->Required) {
            if (!$this->nama_ibu->IsDetailKey && EmptyValue($this->nama_ibu->FormValue)) {
                $this->nama_ibu->addErrorMessage(str_replace("%s", $this->nama_ibu->caption(), $this->nama_ibu->RequiredErrorMessage));
            }
        }
        if ($this->alamat->Required) {
            if (!$this->alamat->IsDetailKey && EmptyValue($this->alamat->FormValue)) {
                $this->alamat->addErrorMessage(str_replace("%s", $this->alamat->caption(), $this->alamat->RequiredErrorMessage));
            }
        }
        if ($this->telepon->Required) {
            if (!$this->telepon->IsDetailKey && EmptyValue($this->telepon->FormValue)) {
                $this->telepon->addErrorMessage(str_replace("%s", $this->telepon->caption(), $this->telepon->RequiredErrorMessage));
            }
        }
        if ($this->kerja_ayah->Required) {
            if (!$this->kerja_ayah->IsDetailKey && EmptyValue($this->kerja_ayah->FormValue)) {
                $this->kerja_ayah->addErrorMessage(str_replace("%s", $this->kerja_ayah->caption(), $this->kerja_ayah->RequiredErrorMessage));
            }
        }
        if ($this->kerja_ibu->Required) {
            if (!$this->kerja_ibu->IsDetailKey && EmptyValue($this->kerja_ibu->FormValue)) {
                $this->kerja_ibu->addErrorMessage(str_replace("%s", $this->kerja_ibu->caption(), $this->kerja_ibu->RequiredErrorMessage));
            }
        }
        if ($this->nama_wali->Required) {
            if (!$this->nama_wali->IsDetailKey && EmptyValue($this->nama_wali->FormValue)) {
                $this->nama_wali->addErrorMessage(str_replace("%s", $this->nama_wali->caption(), $this->nama_wali->RequiredErrorMessage));
            }
        }
        if ($this->no_telepon->Required) {
            if (!$this->no_telepon->IsDetailKey && EmptyValue($this->no_telepon->FormValue)) {
                $this->no_telepon->addErrorMessage(str_replace("%s", $this->no_telepon->caption(), $this->no_telepon->RequiredErrorMessage));
            }
        }
        if ($this->alamat_wali->Required) {
            if (!$this->alamat_wali->IsDetailKey && EmptyValue($this->alamat_wali->FormValue)) {
                $this->alamat_wali->addErrorMessage(str_replace("%s", $this->alamat_wali->caption(), $this->alamat_wali->RequiredErrorMessage));
            }
        }
        if ($this->kerja_wali->Required) {
            if (!$this->kerja_wali->IsDetailKey && EmptyValue($this->kerja_wali->FormValue)) {
                $this->kerja_wali->addErrorMessage(str_replace("%s", $this->kerja_wali->caption(), $this->kerja_wali->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;
            }
        }
        $conn = $this->getConnection();

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

        // kode_tapel
        $this->kode_tapel->CurrentValue = taktif();
        $this->kode_tapel->setDbValueDef($rsnew, $this->kode_tapel->CurrentValue, "");

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        // kerja_wali
        $this->kerja_wali->setDbValueDef($rsnew, $this->kerja_wali->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 Breadcrumb
    protected function setupBreadcrumb()
    {
        global $Breadcrumb, $Language;
        $Breadcrumb = new Breadcrumb("index");
        $url = CurrentUrl();
        $Breadcrumb->add("list", $this->TableVar, $this->addMasterUrl("PesertaDidikList"), "", $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_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 = "your header";
    }

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

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