Location: PHPKode > projects > Science Lab Inventory & Order Management > sciencelabinv/unitadd.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 "unitinfo.php" ?>
<?php include "useraccountsinfo.php" ?>
<?php include "userfn7.php" ?>
<?php

// Create page object
$unit_add = new cunit_add();
$Page =& $unit_add;

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

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

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

// page properties
unit_add.PageID = "add"; // page ID
unit_add.FormID = "funitadd"; // form ID
var EW_PAGE_ID = unit_add.PageID; // for backward compatibility

// extend page with ValidateForm function
unit_add.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 + "_unit"];
		if (elm && !ew_HasValue(elm))
			return ew_OnError(this, elm, ewLanguage.Phrase("EnterRequiredField") + " - <?php echo ew_JsEncode2($unit->unit->FldCaption()) ?>");

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

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

 	// Your custom validation code here, return false if invalid. 
 	return true;
 }
unit_add.SelectAllKey = function(elem) {
	ew_SelectAll(elem);
	ew_ClickAll(elem);
}
<?php if (EW_CLIENT_VALIDATE) { ?>
unit_add.ValidateRequired = true; // uses JavaScript validation
<?php } else { ?>
unit_add.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("Add") ?>&nbsp;<?php echo $Language->Phrase("TblTypeTABLE") ?><?php echo $unit->TableCaption() ?><br><br>
<a href="<?php echo $unit->getReturnUrl() ?>"><?php echo $Language->Phrase("GoBack") ?></a></span></p>
<?php
if (EW_DEBUG_ENABLED)
	echo ew_DebugMsg();
$unit_add->ShowMessage();
?>
<form name="funitadd" id="funitadd" action="<?php echo ew_CurrentPage() ?>" method="post" onsubmit="return unit_add.ValidateForm(this);">
<p>
<input type="hidden" name="t" id="t" value="unit">
<input type="hidden" name="a_add" id="a_add" value="A">
<table cellspacing="0" class="ewGrid"><tr><td class="ewGridContent">
<div class="ewGridMiddlePanel">
<table cellspacing="0" class="ewTable">
<?php if ($unit->unit->Visible) { // unit ?>
	<tr<?php echo $unit->unit->RowAttributes ?>>
		<td class="ewTableHeader"><?php echo $unit->unit->FldCaption() ?><?php echo $Language->Phrase("FieldRequiredIndicator") ?></td>
		<td<?php echo $unit->unit->CellAttributes() ?>><span id="el_unit">
<input type="text" name="x_unit" id="x_unit" title="<?php echo $unit->unit->FldTitle() ?>" size="30" maxlength="5" value="<?php echo $unit->unit->EditValue ?>"<?php echo $unit->unit->EditAttributes() ?>>
</span><?php echo $unit->unit->CustomMsg ?></td>
	</tr>
<?php } ?>
</table>
</div>
</td></tr></table>
<p>
<input type="submit" name="btnAction" id="btnAction" value="<?php echo ew_BtnCaption($Language->Phrase("AddBtn")) ?>">
</form>
<script language="JavaScript" type="text/javascript">
<!--

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

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

//
// Page class
//
class cunit_add {

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

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

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

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

	// Page URL
	function PageUrl() {
		$PageUrl = ew_CurrentPage() . "?";
		global $unit;
		if ($unit->UseTokenInUrl) $PageUrl .= "t=" . $unit->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, $unit;
		if ($unit->UseTokenInUrl) {
			if ($objForm)
				return ($unit->TableVar == $objForm->GetValue("t"));
			if (@$_GET["t"] <> "")
				return ($unit->TableVar == $_GET["t"]);
		} else {
			return TRUE;
		}
	}

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

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

		// Table object (unit)
		$GLOBALS["unit"] = new cunit();

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

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

		// Table name (for backward compatibility)
		if (!defined("EW_TABLE_NAME"))
			define("EW_TABLE_NAME", 'unit', 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 $unit;

		// 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->CanAdd()) {
			$Security->SaveLastUrl();
			$this->Page_Terminate("unitlist.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 = "";
	var $lPriv = 0;

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

		// Load key values from QueryString
		$bCopy = TRUE;
		if (@$_GET["unit"] != "") {
		  $unit->unit->setQueryStringValue($_GET["unit"]);
		} else {
		  $bCopy = FALSE;
		}

		// Process form if post back
		if (@$_POST["a_add"] <> "") {
		   $unit->CurrentAction = $_POST["a_add"]; // Get form action
		  $this->LoadFormValues(); // Load form values

			// Validate form
			if (!$this->ValidateForm()) {
				$unit->CurrentAction = "I"; // Form error, reset action
				$this->setMessage($gsFormError);
			}
		} else { // Not post back
		  if ($bCopy) {
		    $unit->CurrentAction = "C"; // Copy record
		  } else {
		    $unit->CurrentAction = "I"; // Display blank record
		    $this->LoadDefaultValues(); // Load default values
		  }
		}

		// Perform action based on action code
		switch ($unit->CurrentAction) {
		  case "I": // Blank record, no action required
				break;
		  case "C": // Copy an existing record
		   if (!$this->LoadRow()) { // Load record based on key
		      $this->setMessage($Language->Phrase("NoRecord")); // No record found
		      $this->Page_Terminate("unitlist.php"); // No matching record, return to list
		    }
				break;
		  case "A": // ' Add new record
				$unit->SendEmail = TRUE; // Send email on add success
		    if ($this->AddRow()) { // Add successful
		      $this->setMessage($Language->Phrase("AddSuccess")); // Set up success message
					$sReturnUrl = $unit->getReturnUrl();
					if (ew_GetPageName($sReturnUrl) == "unitview.php")
						$sReturnUrl = $unit->ViewUrl(); // View paging, return to view page with keyurl directly
					$this->Page_Terminate($sReturnUrl); // Clean up and return
		    } else {
		      $this->RestoreFormValues(); // Add failed, restore form values
		    }
		}

		// Render row based on row type
		$unit->RowType = EW_ROWTYPE_ADD;  // Render add type

		// Render row
		$this->RenderRow();
	}

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

		// Get upload data
	}

	// Load default values
	function LoadDefaultValues() {
		global $unit;
	}

	// Load form values
	function LoadFormValues() {

		// Load from form
		global $objForm, $unit;
		$unit->unit->setFormValue($objForm->GetValue("x_unit"));
	}

	// Restore form values
	function RestoreFormValues() {
		global $objForm, $unit;
		$unit->unit->CurrentValue = $unit->unit->FormValue;
	}

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

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

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

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

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

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

		// Initialize URLs
		// Call Row_Rendering event

		$unit->Row_Rendering();

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

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

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

			// unit
			$unit->unit->HrefValue = "";
			$unit->unit->TooltipValue = "";
		} elseif ($unit->RowType == EW_ROWTYPE_ADD) { // Add row

			// unit
			$unit->unit->EditCustomAttributes = "";
			$unit->unit->EditValue = ew_HtmlEncode($unit->unit->CurrentValue);
		}

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

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

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

		// Check if validation required
		if (!EW_SERVER_VALIDATE)
			return ($gsFormError == "");
		if (!is_null($unit->unit->FormValue) && $unit->unit->FormValue == "") {
			$gsFormError .= ($gsFormError <> "") ? "<br>" : "";
			$gsFormError .= $Language->Phrase("EnterRequiredField") . " - " . $unit->unit->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;
	}

	// Add record
	function AddRow() {
		global $conn, $Language, $Security, $unit;

		// Check if key value entered
		if ($unit->unit->CurrentValue == "") {
			$this->setMessage($Language->Phrase("InvalidKeyValue"));
			return FALSE;
		}

		// Check for duplicate key
		$bCheckKey = TRUE;
		$sFilter = $unit->KeyFilter();
		if ($bCheckKey) {
			$rsChk = $unit->LoadRs($sFilter);
			if ($rsChk && !$rsChk->EOF) {
				$sKeyErrMsg = str_replace("%f", $sFilter, $Language->Phrase("DupKey"));
				$this->setMessage($sKeyErrMsg);
				$rsChk->Close();
				return FALSE;
			}
		}
		$rsnew = array();

		// unit
		$unit->unit->SetDbValueDef($rsnew, $unit->unit->CurrentValue, "", FALSE);

		// Call Row Inserting event
		$bInsertRow = $unit->Row_Inserting($rsnew);
		if ($bInsertRow) {
			$conn->raiseErrorFn = 'ew_ErrorFn';
			$AddRow = $conn->Execute($unit->InsertSQL($rsnew));
			$conn->raiseErrorFn = '';
		} else {
			if ($unit->CancelMessage <> "") {
				$this->setMessage($unit->CancelMessage);
				$unit->CancelMessage = "";
			} else {
				$this->setMessage($Language->Phrase("InsertCancelled"));
			}
			$AddRow = FALSE;
		}
		if ($AddRow) {

			// Call Row Inserted event
			$unit->Row_Inserted($rsnew);
		}
		return $AddRow;
	}

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