public function btnSave_Click($strFormId, $strControlId, $strParameter) { try { // Get an instance of the database $objDatabase = QApplication::$Database[1]; // Begin a MySQL Transaction to be either committed or rolled back $objDatabase->TransactionBegin(); // This happens whether or not they are creating a new one or editing an existing one $this->objInventoryModel->ShortDescription = $this->txtShortDescription->Text; $this->objInventoryModel->CategoryId = $this->lstCategory->SelectedValue; $this->objInventoryModel->ManufacturerId = $this->lstManufacturer->SelectedValue; $this->objInventoryModel->LongDescription = $this->txtLongDescription->Text; $this->objInventoryModel->InventoryModelCode = $this->txtInventoryModelCode->Text; $blnError = false; // If a new inventory model is being created if (!$this->blnEditMode) { // Check to see if the InventoryModelCode already exists $InventoryModelDuplicate = InventoryModel::LoadbyInventoryModelCode($this->objInventoryModel->InventoryModelCode); if ($InventoryModelDuplicate) { $blnError = true; $this->txtInventoryModelCode->Warning = "That inventory code is already in use. Please try another."; } if (!$blnError) { // Object should be saved only if it is new, to obtain the proper InventoryModelId to add to the custom field tables $this->objInventoryModel->Save(); } } // Assign input values to custom fields if ($this->arrCustomFields) { // Save the values from all of the custom field controls to save the inventory model CustomField::SaveControls($this->objInventoryModel->objCustomFieldArray, $this->blnEditMode, $this->arrCustomFields, $this->objInventoryModel->InventoryModelId, 2); } if ($this->blnEditMode) { // Check to see if the InventoryModelCode already exists and it is not the code for the inventory model that you are currently working with $InventoryModelDuplicate = InventoryModel::LoadbyInventoryModelCode($this->objInventoryModel->InventoryModelCode); if ($InventoryModelDuplicate && $InventoryModelDuplicate->InventoryModelId != $this->objInventoryModel->InventoryModelId) { $blnError = true; $this->txtInventoryModelCode->Warning = "That inventory code is already in use. Please try another."; } if (!$blnError) { // Update the values of all fields for an Ajax reload $this->UpdateInventoryFields(); // If inventory model is not new, it must be saved after updating the inventoryfields $this->objInventoryModel->Save(); // Setup the InventoryModel again to retrieve the latest Modified information $this->objParentObject->SetupInventoryModel($this); // Give the labels their appropriate values before display $this->UpdateInventoryLabels(); // This was necessary because it was not saving the changes of a second edit/save in a row // Reload all custom fields $this->objInventoryModel->objCustomFieldArray = CustomField::LoadObjCustomFieldArray(2, $this->blnEditMode, $this->objInventoryModel->InventoryModelId); // Hide inputs and display labels $this->displayLabels(); // Enable the appropriate transaction buttons $this->EnableTransactionButtons(); // Commit the above transactions to the database $objDatabase->TransactionCommit(); } } elseif (!$blnError) { // Commit the above transactions to the database $objDatabase->TransactionCommit(); // Reload the edit inventory page with the newly created model $strRedirect = "inventory_edit.php?intInventoryModelId=" . $this->objInventoryModel->InventoryModelId; QApplication::Redirect($strRedirect); } } catch (QOptimisticLockingException $objExc) { // Rollback the database $objDatabase->TransactionRollback(); // Output the error $this->btnCancel->Warning = sprintf('This inventory has been updated by another user. You must <a href="inventory_edit.php?intInventoryModelId=%s">Refresh</a> to edit this Inventory.', $this->objInventoryModel->InventoryModelId); } }