Location: PHPKode > projects > Science Lab Inventory & Order Management > sciencelabinv/roomsedit.php
<?php
session_start(); // Initialize Session data
ob_start(); // Turn on output buffering
?>
<?php include "ewcfg7.php" ?>
<?php include "ewmysql7.php" ?>
<?php include "phpfn7.php" ?>
<?php include "roomsinfo.php" ?>
<?php include "useraccountsinfo.php" ?>
<?php include "userfn7.php" ?>
<?php

// Create page object
$rooms_edit = new crooms_edit();
$Page =& $rooms_edit;

// Page init
$rooms_edit->Page_Init();

// Page main
$rooms_edit->Page_Main();
?>
<?php include "header.php" ?>
<script type="text/javascript">
<!--

// Create page object
var rooms_edit = new ew_Page("rooms_edit");

// page properties
rooms_edit.PageID = "edit"; // page ID
rooms_edit.FormID = "froomsedit"; // form ID
var EW_PAGE_ID = rooms_edit.PageID; // for backward compatibility

// extend page with ValidateForm function
rooms_edit.ValidateForm = function(fobj) {
	ew_PostAutoSuggest(fobj);
	if (!this.ValidateRequired)
		return true; // ignore validation
	if (fobj.a_confirm && fobj.a_confirm.value == "F")
		return true;
	var i, elm, aelm, infix;
	var rowcnt = (fobj.key_count) ? Number(fobj.key_count.value) : 1;
	for (i=0; i<rowcnt; i++) {
		infix = (fobj.key_count) ? String(i+1) : "";
		elm = fobj.elements["x" + infix + "_id"];
		if (elm && !ew_HasValue(elm))
			return ew_OnError(this, elm, ewLanguage.Phrase("EnterRequiredField") + " - <?php echo ew_JsEncode2($rooms->id->FldCaption()) ?>");

		// Call Form Custom Validate event
		if (!this.Form_CustomValidate(fobj)) return false;
	}
	return true;
}

// extend page with Form_CustomValidate function
rooms_edit.Form_CustomValidate =  
 function(fobj) { // DO NOT CHANGE THIS LINE!

 	// Your custom validation code here, return false if invalid. 
 	return true;
 }
rooms_edit.SelectAllKey = function(elem) {
	ew_SelectAll(elem);
	ew_ClickAll(elem);
}
<?php if (EW_CLIENT_VALIDATE) { ?>
rooms_edit.ValidateRequired = true; // uses JavaScript validation
<?php } else { ?>
rooms_edit.ValidateRequired = false; // no JavaScript validation
<?php } ?>

//-->
</script>
<script type="text/javascript">
<!--
var ew_DHTMLEditors = [];

//-->
</script>
<script language="JavaScript" type="text/javascript">
<!--

// Write your client script here, no need to add script tags.
// To include another .js script, use:
// ew_ClientScriptInclude("my_javascript.js"); 
//-->

</script>
<p><span class="phpmaker"><?php echo $Language->Phrase("Edit") ?>&nbsp;<?php echo $Language->Phrase("TblTypeTABLE") ?><?php echo $rooms->TableCaption() ?><br><br>
<a href="<?php echo $rooms->getReturnUrl() ?>"><?php echo $Language->Phrase("GoBack") ?></a></span></p>
<?php
if (EW_DEBUG_ENABLED)
	echo ew_DebugMsg();
$rooms_edit->ShowMessage();
?>
<form name="froomsedit" id="froomsedit" action="<?php echo ew_CurrentPage() ?>" method="post" onsubmit="return rooms_edit.ValidateForm(this);">
<p>
<input type="hidden" name="a_table" id="a_table" value="rooms">
<input type="hidden" name="a_edit" id="a_edit" value="U">
<table cellspacing="0" class="ewGrid"><tr><td class="ewGridContent">
<div class="ewGridMiddlePanel">
<table cellspacing="0" class="ewTable">
<?php if ($rooms->id->Visible) { // id ?>
	<tr<?php echo $rooms->id->RowAttributes ?>>
		<td class="ewTableHeader"><?php echo $rooms->id->FldCaption() ?><?php echo $Language->Phrase("FieldRequiredIndicator") ?></td>
		<td<?php echo $rooms->id->CellAttributes() ?>><span id="el_id">
<div<?php echo $rooms->id->ViewAttributes() ?>><?php echo $rooms->id->EditValue ?></div><input type="hidden" name="x_id" id="x_id" value="<?php echo ew_HtmlEncode($rooms->id->CurrentValue) ?>">
</span><?php echo $rooms->id->CustomMsg ?></td>
	</tr>
<?php } ?>
<?php if ($rooms->name->Visible) { // name ?>
	<tr<?php echo $rooms->name->RowAttributes ?>>
		<td class="ewTableHeader"><?php echo $rooms->name->FldCaption() ?></td>
		<td<?php echo $rooms->name->CellAttributes() ?>><span id="el_name">
<input type="text" name="x_name" id="x_name" title="<?php echo $rooms->name->FldTitle() ?>" size="30" maxlength="25" value="<?php echo $rooms->name->EditValue ?>"<?php echo $rooms->name->EditAttributes() ?>>
</span><?php echo $rooms->name->CustomMsg ?></td>
	</tr>
<?php } ?>
<?php if ($rooms->description->Visible) { // description ?>
	<tr<?php echo $rooms->description->RowAttributes ?>>
		<td class="ewTableHeader"><?php echo $rooms->description->FldCaption() ?></td>
		<td<?php echo $rooms->description->CellAttributes() ?>><span id="el_description">
<input type="text" name="x_description" id="x_description" title="<?php echo $rooms->description->FldTitle() ?>" size="30" maxlength="50" value="<?php echo $rooms->description->EditValue ?>"<?php echo $rooms->description->EditAttributes() ?>>
</span><?php echo $rooms->description->CustomMsg ?></td>
	</tr>
<?php } ?>
</table>
</div>
</td></tr></table>
<p>
<input type="submit" name="btnAction" id="btnAction" value="<?php echo ew_BtnCaption($Language->Phrase("EditBtn")) ?>">
</form>
<script language="JavaScript" type="text/javascript">
<!--

// Write your table-specific startup script here
// document.write("page loaded");
//-->

</script>
<?php include "footer.php" ?>
<?php
$rooms_edit->Page_Terminate();
?>
<?php

//
// Page class
//
class crooms_edit {

	// Page ID
	var $PageID = 'edit';

	// Table name
	var $TableName = 'rooms';

	// Page object name
	var $PageObjName = 'rooms_edit';

	// Page name
	function PageName() {
		return ew_CurrentPage();
	}

	// Page URL
	function PageUrl() {
		$PageUrl = ew_CurrentPage() . "?";
		global $rooms;
		if ($rooms->UseTokenInUrl) $PageUrl .= "t=" . $rooms->TableVar . "&"; // Add page token
		return $PageUrl;
	}

	// Page URLs
	var $AddUrl;
	var $EditUrl;
	var $CopyUrl;
	var $DeleteUrl;
	var $ViewUrl;
	var $ListUrl;

	// Export URLs
	var $ExportPrintUrl;
	var $ExportHtmlUrl;
	var $ExportExcelUrl;
	var $ExportWordUrl;
	var $ExportXmlUrl;
	var $ExportCsvUrl;

	// Update URLs
	var $InlineAddUrl;
	var $InlineCopyUrl;
	var $InlineEditUrl;
	var $GridAddUrl;
	var $GridEditUrl;
	var $MultiDeleteUrl;
	var $MultiUpdateUrl;

	// Message
	function getMessage() {
		return @$_SESSION[EW_SESSION_MESSAGE];
	}

	function setMessage($v) {
		if (@$_SESSION[EW_SESSION_MESSAGE] <> "") { // Append
			$_SESSION[EW_SESSION_MESSAGE] .= "<br>" . $v;
		} else {
			$_SESSION[EW_SESSION_MESSAGE] = $v;
		}
	}

	// Show message
	function ShowMessage() {
		$sMessage = $this->getMessage();
		$this->Message_Showing($sMessage);
		if ($sMessage <> "") { // Message in Session, display
			echo "<p><span class=\"ewMessage\">" . $sMessage . "</span></p>";
			$_SESSION[EW_SESSION_MESSAGE] = ""; // Clear message in Session
		}
	}

	// Validate page request
	function IsPageRequest() {
		global $objForm, $rooms;
		if ($rooms->UseTokenInUrl) {
			if ($objForm)
				return ($rooms->TableVar == $objForm->GetValue("t"));
			if (@$_GET["t"] <> "")
				return ($rooms->TableVar == $_GET["t"]);
		} else {
			return TRUE;
		}
	}

	//
	// Page class constructor
	//
	function crooms_edit() {
		global $conn, $Language;

		// Language object
		$Language = new cLanguage();

		// Table object (rooms)
		$GLOBALS["rooms"] = new crooms();

		// Table object (useraccounts)
		$GLOBALS['useraccounts'] = new cuseraccounts();

		// Page ID
		if (!defined("EW_PAGE_ID"))
			define("EW_PAGE_ID", 'edit', TRUE);

		// Table name (for backward compatibility)
		if (!defined("EW_TABLE_NAME"))
			define("EW_TABLE_NAME", 'rooms', TRUE);

		// Start timer
		$GLOBALS["gsTimer"] = new cTimer();

		// Open connection
		$conn = ew_Connect();
	}

	// 
	//  Page_Init
	//
	function Page_Init() {
		global $gsExport, $gsExportFile, $UserProfile, $Language, $Security, $objForm;
		global $rooms;

		// Security
		$Security = new cAdvancedSecurity();
		if (!$Security->IsLoggedIn()) $Security->AutoLogin();
		if (!$Security->IsLoggedIn()) {
			$Security->SaveLastUrl();
			$this->Page_Terminate("login.php");
		}
		$Security->TablePermission_Loading();
		$Security->LoadCurrentUserLevel($this->TableName);
		$Security->TablePermission_Loaded();
		if (!$Security->IsLoggedIn()) {
			$Security->SaveLastUrl();
			$this->Page_Terminate("login.php");
		}
		if (!$Security->CanEdit()) {
			$Security->SaveLastUrl();
			$this->Page_Terminate("roomslist.php");
		}
		$Security->UserID_Loading();
		if ($Security->IsLoggedIn()) $Security->LoadUserID();
		$Security->UserID_Loaded();

		// Create form object
		$objForm = new cFormObj();

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

		// Page Load event
		$this->Page_Load();
	}

	//
	// Page_Terminate
	//
	function Page_Terminate($url = "") {
		global $conn;

		// Page Unload event
		$this->Page_Unload();

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

		 // Close connection
		$conn->Close();

		// Go to URL if specified
		$this->Page_Redirecting($url);
		if ($url <> "") {
			if (!EW_DEBUG_ENABLED && ob_get_length())
				ob_end_clean();
			header("Location: " . $url);
		}
		exit();
	}
	var $sDbMasterFilter;
	var $sDbDetailFilter;

	// 
	// Page main
	//
	function Page_Main() {
		global $objForm, $Language, $gsFormError, $rooms;

		// Load key from QueryString
		if (@$_GET["id"] <> "")
			$rooms->id->setQueryStringValue($_GET["id"]);
		if (@$_POST["a_edit"] <> "") {
			$rooms->CurrentAction = $_POST["a_edit"]; // Get action code
			$this->LoadFormValues(); // Get form values

			// Validate form
			if (!$this->ValidateForm()) {
				$rooms->CurrentAction = ""; // Form error, reset action
				$this->setMessage($gsFormError);
				$rooms->EventCancelled = TRUE; // Event cancelled
				$this->RestoreFormValues();
			}
		} else {
			$rooms->CurrentAction = "I"; // Default action is display
		}

		// Check if valid key
		if ($rooms->id->CurrentValue == "")
			$this->Page_Terminate("roomslist.php"); // Invalid key, return to list
		switch ($rooms->CurrentAction) {
			case "I": // Get a record to display
				if (!$this->LoadRow()) { // Load record based on key
					$this->setMessage($Language->Phrase("NoRecord")); // No record found
					$this->Page_Terminate("roomslist.php"); // No matching record, return to list
				}
				break;
			Case "U": // Update
				$rooms->SendEmail = TRUE; // Send email on update success
				if ($this->EditRow()) { // Update record based on key
					$this->setMessage($Language->Phrase("UpdateSuccess")); // Update success
					$sReturnUrl = $rooms->getReturnUrl();
					if (ew_GetPageName($sReturnUrl) == "roomsview.php")
						$sReturnUrl = $rooms->ViewUrl(); // View paging, return to View page directly
					$this->Page_Terminate($sReturnUrl); // Return to caller
				} else {
					$rooms->EventCancelled = TRUE; // Event cancelled
					$this->RestoreFormValues(); // Restore form values if update failed
				}
		}

		// Render the record
		$rooms->RowType = EW_ROWTYPE_EDIT; // Render as Edit
		$this->RenderRow();
	}

	// Get upload files
	function GetUploadFiles() {
		global $objForm, $rooms;

		// Get upload data
	}

	// Load form values
	function LoadFormValues() {

		// Load from form
		global $objForm, $rooms;
		$rooms->id->setFormValue($objForm->GetValue("x_id"));
		$rooms->name->setFormValue($objForm->GetValue("x_name"));
		$rooms->description->setFormValue($objForm->GetValue("x_description"));
	}

	// Restore form values
	function RestoreFormValues() {
		global $objForm, $rooms;
		$this->LoadRow();
		$rooms->id->CurrentValue = $rooms->id->FormValue;
		$rooms->name->CurrentValue = $rooms->name->FormValue;
		$rooms->description->CurrentValue = $rooms->description->FormValue;
	}

	// Load row based on key values
	function LoadRow() {
		global $conn, $Security, $rooms;
		$sFilter = $rooms->KeyFilter();

		// Call Row Selecting event
		$rooms->Row_Selecting($sFilter);

		// Load SQL based on filter
		$rooms->CurrentFilter = $sFilter;
		$sSql = $rooms->SQL();
		$res = FALSE;
		$rs = ew_LoadRecordset($sSql);
		if ($rs && !$rs->EOF) {
			$res = TRUE;
			$this->LoadRowValues($rs); // Load row values

			// Call Row Selected event
			$rooms->Row_Selected($rs);
			$rs->Close();
		}
		return $res;
	}

	// Load row values from recordset
	function LoadRowValues(&$rs) {
		global $conn, $rooms;
		$rooms->id->setDbValue($rs->fields('id'));
		$rooms->name->setDbValue($rs->fields('name'));
		$rooms->description->setDbValue($rs->fields('description'));
	}

	// Render row values based on field settings
	function RenderRow() {
		global $conn, $Security, $Language, $rooms;

		// Initialize URLs
		// Call Row_Rendering event

		$rooms->Row_Rendering();

		// Common render codes for all row types
		// id

		$rooms->id->CellCssStyle = ""; $rooms->id->CellCssClass = "";
		$rooms->id->CellAttrs = array(); $rooms->id->ViewAttrs = array(); $rooms->id->EditAttrs = array();

		// name
		$rooms->name->CellCssStyle = ""; $rooms->name->CellCssClass = "";
		$rooms->name->CellAttrs = array(); $rooms->name->ViewAttrs = array(); $rooms->name->EditAttrs = array();

		// description
		$rooms->description->CellCssStyle = ""; $rooms->description->CellCssClass = "";
		$rooms->description->CellAttrs = array(); $rooms->description->ViewAttrs = array(); $rooms->description->EditAttrs = array();
		if ($rooms->RowType == EW_ROWTYPE_VIEW) { // View row

			// id
			$rooms->id->ViewValue = $rooms->id->CurrentValue;
			$rooms->id->CssStyle = "";
			$rooms->id->CssClass = "";
			$rooms->id->ViewCustomAttributes = "";

			// name
			$rooms->name->ViewValue = $rooms->name->CurrentValue;
			$rooms->name->CssStyle = "";
			$rooms->name->CssClass = "";
			$rooms->name->ViewCustomAttributes = "";

			// description
			$rooms->description->ViewValue = $rooms->description->CurrentValue;
			$rooms->description->CssStyle = "";
			$rooms->description->CssClass = "";
			$rooms->description->ViewCustomAttributes = "";

			// id
			$rooms->id->HrefValue = "";
			$rooms->id->TooltipValue = "";

			// name
			$rooms->name->HrefValue = "";
			$rooms->name->TooltipValue = "";

			// description
			$rooms->description->HrefValue = "";
			$rooms->description->TooltipValue = "";
		} elseif ($rooms->RowType == EW_ROWTYPE_EDIT) { // Edit row

			// id
			$rooms->id->EditCustomAttributes = "";
			$rooms->id->EditValue = $rooms->id->CurrentValue;
			$rooms->id->CssStyle = "";
			$rooms->id->CssClass = "";
			$rooms->id->ViewCustomAttributes = "";

			// name
			$rooms->name->EditCustomAttributes = "";
			$rooms->name->EditValue = ew_HtmlEncode($rooms->name->CurrentValue);

			// description
			$rooms->description->EditCustomAttributes = "";
			$rooms->description->EditValue = ew_HtmlEncode($rooms->description->CurrentValue);

			// Edit refer script
			// id

			$rooms->id->HrefValue = "";

			// name
			$rooms->name->HrefValue = "";

			// description
			$rooms->description->HrefValue = "";
		}

		// Call Row Rendered event
		if ($rooms->RowType <> EW_ROWTYPE_AGGREGATEINIT)
			$rooms->Row_Rendered();
	}

	// Validate form
	function ValidateForm() {
		global $Language, $gsFormError, $rooms;

		// Initialize form error message
		$gsFormError = "";

		// Check if validation required
		if (!EW_SERVER_VALIDATE)
			return ($gsFormError == "");
		if (!is_null($rooms->id->FormValue) && $rooms->id->FormValue == "") {
			$gsFormError .= ($gsFormError <> "") ? "<br>" : "";
			$gsFormError .= $Language->Phrase("EnterRequiredField") . " - " . $rooms->id->FldCaption();
		}

		// Return validate result
		$ValidateForm = ($gsFormError == "");

		// Call Form_CustomValidate event
		$sFormCustomError = "";
		$ValidateForm = $ValidateForm && $this->Form_CustomValidate($sFormCustomError);
		if ($sFormCustomError <> "") {
			$gsFormError .= ($gsFormError <> "") ? "<br>" : "";
			$gsFormError .= $sFormCustomError;
		}
		return $ValidateForm;
	}

	// Update record based on key values
	function EditRow() {
		global $conn, $Security, $Language, $rooms;
		$sFilter = $rooms->KeyFilter();
		$rooms->CurrentFilter = $sFilter;
		$sSql = $rooms->SQL();
		$conn->raiseErrorFn = 'ew_ErrorFn';
		$rs = $conn->Execute($sSql);
		$conn->raiseErrorFn = '';
		if ($rs === FALSE)
			return FALSE;
		if ($rs->EOF) {
			$EditRow = FALSE; // Update Failed
		} else {

			// Save old values
			$rsold =& $rs->fields;
			$rsnew = array();

			// id
			// name

			$rooms->name->SetDbValueDef($rsnew, $rooms->name->CurrentValue, NULL, FALSE);

			// description
			$rooms->description->SetDbValueDef($rsnew, $rooms->description->CurrentValue, NULL, FALSE);

			// Call Row Updating event
			$bUpdateRow = $rooms->Row_Updating($rsold, $rsnew);
			if ($bUpdateRow) {
				$conn->raiseErrorFn = 'ew_ErrorFn';
				$EditRow = $conn->Execute($rooms->UpdateSQL($rsnew));
				$conn->raiseErrorFn = '';
			} else {
				if ($rooms->CancelMessage <> "") {
					$this->setMessage($rooms->CancelMessage);
					$rooms->CancelMessage = "";
				} else {
					$this->setMessage($Language->Phrase("UpdateCancelled"));
				}
				$EditRow = FALSE;
			}
		}

		// Call Row_Updated event
		if ($EditRow)
			$rooms->Row_Updated($rsold, $rsnew);
		$rs->Close();
		return $EditRow;
	}

	// Page Load event
	function Page_Load() {

		//echo "Page Load";
	}

	// Page Unload event
	function Page_Unload() {

		//echo "Page Unload";
	}

	// Page Redirecting event
	function Page_Redirecting(&$url) {

		// Example:
		//$url = "your URL";

	}

	// Message Showing event
	function Message_Showing(&$msg) {

		// Example:
		//$msg = "your new message";

	}

	// Form Custom Validate event
	function Form_CustomValidate(&$CustomError) {

		// Return error message in CustomError
		return TRUE;
	}
}
?>
Return current item: Science Lab Inventory & Order Management