public function Save($blnForceInsert = false, $blnForceUpdate = false) { if (!$this->__blnRestored || $blnForceInsert) { $this->CreatedBy = QApplication::$objUserAccount->UserAccountId; $this->CreationDate = new QDateTime(QDateTime::Now); parent::Save($blnForceInsert, $blnForceUpdate); } else { $this->ModifiedBy = QApplication::$objUserAccount->UserAccountId; // Load the CustomFieldValue object before modifing $objOldCustomFieldValue = CustomFieldValue::LoadByCustomFieldValueId($this->CustomFieldValueId); parent::Save($blnForceInsert, $blnForceUpdate); // If short_description have been modified if ($this->ShortDescription != $objOldCustomFieldValue->ShortDescription) { if ($objCustomField = $this->CustomField) { $objDatabase = CustomFieldValue::GetDatabase(); $objCustomFieldSelectionArray = CustomFieldSelection::LoadArrayByCustomFieldValueId($this->CustomFieldValueId); foreach ($objCustomFieldSelectionArray as $objCustomFieldSelection) { // If helper table exists if ($strHelperTableArray = $this->GetHelperTableByEntityQtypeId($objCustomFieldSelection->EntityQtypeId)) { $strHelperTable = $strHelperTableArray[0]; $strTableName = $strHelperTableArray[1]; // Update the data into helper table $strQuery = sprintf("UPDATE %s SET `cfv_%s`=%s WHERE `%s_id`='%s';", $strHelperTable, $objCustomField->CustomFieldId, $objDatabase->SqlVariable($this->ShortDescription), $strTableName, $objCustomFieldSelection->EntityId); $objDatabase->NonQuery($strQuery); } } } } } }
protected function UpdateEntityQtypeCustomFields() { // Delete any EntityQtypeCustomFields that were selected but are no longer selected $objEntityQtypeCustomFieldArray = EntityQtypeCustomField::LoadArrayByCustomFieldId($this->objCustomField->CustomFieldId); if ($objEntityQtypeCustomFieldArray) { foreach ($objEntityQtypeCustomFieldArray as $objEntityQtypeCustomField) { // Determines whether or not a entityqtypecustomfield can stay or gets deleted $blnKeep = false; if ($this->chkEntityQtype->SelectedItems) { foreach ($this->chkEntityQtype->SelectedItems as $objEntityQtypeItem) { if ($objEntityQtypeCustomField->EntityQtypeId == $objEntityQtypeItem->Value) { $blnKeep = true; } } } // If the EntityQtype needs to be deleted, you must delete the CustomFieldValues for text and textarea, and CustomFieldSelections for all types if (!$blnKeep) { /*$objAndCondition = QQ::AndCondition(QQ::Equal(QQN::CustomFieldSelection()->EntityQtypeId, $objEntityQtypeCustomField->EntityQtypeId), QQ::Equal(QQN::CustomFieldSelection()->CustomFieldValue->CustomFieldId, $this->objCustomField->CustomFieldId)); $objClauses = QQ::Clause(QQ::Expand(QQN::CustomFieldSelection()->CustomFieldValue)); // Select all CustomFieldSelections (and expanded CustomFieldValues) by EntityQtypeId and CustomFieldId $objCustomFieldSelectionArray = CustomFieldSelection::QueryArray($objAndCondition, $objClauses); if ($objCustomFieldSelectionArray) { foreach ($objCustomFieldSelectionArray as $objCustomFieldSelection) { if ($this->objCustomField->CustomFieldQtypeId != 2) { // Deleting the CustomFieldValue will MySQL CASCADE to delete the CustomFieldSelection also $objCustomFieldSelection->CustomFieldValue->Delete(); } else { // If it is a select box, only delete the CustomFieldSelection (the CustomFieldValue stays unless it is removed as a selection entirely) $objCustomFieldSelection->Delete(); } } }*/ // If the EntityQtype needs to be deleted, you must delete EntityQtypeId for all roles in RoleEntityQTypeCustomFieldAuthorization $objRoleEntityCustomAuthArray = RoleEntityQtypeCustomFieldAuthorization::LoadArrayByEntityQtypeCustomFieldId($objEntityQtypeCustomField->EntityQtypeCustomFieldId); if ($objRoleEntityCustomAuthArray) { foreach ($objRoleEntityCustomAuthArray as $objRoleEntityCustomAuth) { $objRoleEntityCustomAuth->Delete(); } } // If the helper table exists for that EntityQtype then will delete the column in the helper table if ($strHelperTableArray = CustomFieldValue::GetHelperTableByEntityQtypeId($objEntityQtypeCustomField->EntityQtypeId)) { $strHelperTable = $strHelperTableArray[0]; $objDatabase = CustomField::GetDatabase(); $strQuery = sprintf("ALTER TABLE %s DROP `cfv_%s`;", $strHelperTable, $this->objCustomField->CustomFieldId); $objDatabase->NonQuery($strQuery); } if ($objEntityQtypeCustomField->EntityQtypeId == 1) { $this->DeleteAssetCustomFieldAssetModels(); } // Delete the EntityQtypeCustomField last $objEntityQtypeCustomField->Delete(); } else { if ($objEntityQtypeCustomField->EntityQtypeId == 1) { $this->UpadateAssetModels(); } } } } // Insert the new EntityQtypeCustomFields if ($this->lstCustomFieldQtype->SelectedItems) { foreach ($this->chkEntityQtype->SelectedItems as $objEntityQtypeItem) { // If the field doesn't already exist, then it needs to be created if (!($objEntityQtypeCustomField = EntityQtypeCustomField::LoadByEntityQtypeIdCustomFieldId($objEntityQtypeItem->Value, $this->objCustomField->CustomFieldId))) { // add asset models if ($objEntityQtypeItem->Value == 1) { $this->AppendAssetModels(); } $objEntityQtypeCustomField = new EntityQtypeCustomField(); $objEntityQtypeCustomField->CustomFieldId = $this->objCustomField->CustomFieldId; $objEntityQtypeCustomField->EntityQtypeId = $objEntityQtypeItem->Value; $objEntityQtypeCustomField->Save(); // If the helper table exists for that EntityQtype then create new column in the helper table if ($strHelperTableArray = CustomFieldValue::GetHelperTableByEntityQtypeId($objEntityQtypeItem->Value)) { $strHelperTable = $strHelperTableArray[0]; $objDatabase = CustomField::GetDatabase(); $strQuery = sprintf("ALTER TABLE %s ADD `cfv_%s` TEXT DEFAULT NULL;", $strHelperTable, $this->objCustomField->CustomFieldId); $objDatabase->NonQuery($strQuery); // If the helper table exists and have no values (empty). // It happens when the QtypeItem does not yet have the custom fields. // Uses SQL-hack to fix this issue. $strParentTableName = $strHelperTableArray[1]; $strHelperTableItemId = sprintf("%s_id", $strParentTableName); $strQuery = sprintf("INSERT INTO %s (`%s`) (SELECT `%s` FROM `%s` WHERE `%s` NOT IN (SELECT `%s` FROM %s));", $strHelperTable, $strHelperTableItemId, $strHelperTableItemId, $strParentTableName, $strHelperTableItemId, $strHelperTableItemId, $strHelperTable); $objDatabase->NonQuery($strQuery); } // Insert the new EntityQtypeCustomField to the RoleEntityQTypeCustomFieldAuthorization table, to all the roles, with authorized_flag set to true, one for View Auth and another for Edit Auth foreach (Role::LoadAll() as $objRole) { //Insert the view Auth $objRoleEntityQtypeCustomFieldAuth = new RoleEntityQtypeCustomFieldAuthorization(); $objRoleEntityQtypeCustomFieldAuth->RoleId = $objRole->RoleId; $objRoleEntityQtypeCustomFieldAuth->EntityQtypeCustomFieldId = $objEntityQtypeCustomField->EntityQtypeCustomFieldId; $objRoleEntityQtypeCustomFieldAuth->AuthorizationId = 1; $objRoleEntityQtypeCustomFieldAuth->AuthorizedFlag = 1; $objRoleEntityQtypeCustomFieldAuth->Save(); //Insert the Edit Auth $objRoleEntityQtypeCustomFieldAuth = new RoleEntityQtypeCustomFieldAuthorization(); $objRoleEntityQtypeCustomFieldAuth->RoleId = $objRole->RoleId; $objRoleEntityQtypeCustomFieldAuth->EntityQtypeCustomFieldId = $objEntityQtypeCustomField->EntityQtypeCustomFieldId; $objRoleEntityQtypeCustomFieldAuth->AuthorizationId = 2; $objRoleEntityQtypeCustomFieldAuth->AuthorizedFlag = 1; $objRoleEntityQtypeCustomFieldAuth->Save(); } } // If this field is a required field if ($this->objCustomField->RequiredFlag) { // Add the DefaultValue into the helper table if ($strHelperTableArray = CustomFieldValue::GetHelperTableByEntityQtypeId($objEntityQtypeItem->Value)) { $strHelperTable = $strHelperTableArray[0]; $blnError = false; // If the custom field is text or textarea if ($this->objCustomField->CustomFieldQtypeId != 2) { if ($this->txtDefaultValue->Text != null) { $txtDefaultValue = $this->txtDefaultValue->Text; } else { $blnError = true; } } elseif ($this->objCustomField->DefaultCustomFieldValueId != null) { $txtDefaultValue = CustomFieldValue::LoadByCustomFieldValueId($this->objCustomField->DefaultCustomFieldValueId); } else { $blnError = true; } if (!$blnError) { if (!($objEntityQtypeItem->Value == 1 && $this->rblAllAssetModels->SelectedValue == 2)) { //print($objEntityQtypeItem->Value.$this->rblAllAssetModels->SelectedValue);exit; $objDatabase = CustomField::GetDatabase(); $strQuery = sprintf("UPDATE %s SET `cfv_%s`='%s' WHERE `cfv_%s` is NULL;", $strHelperTable, $this->objCustomField->CustomFieldId, $txtDefaultValue, $this->objCustomField->CustomFieldId); $objDatabase->NonQuery($strQuery); } else { // define assets to set default value $chosenAssetModels = array(); foreach ($this->arrAssetModels as $chosenAssetModel) { $assetsToFill = Asset::LoadArrayByAssetModelId($chosenAssetModel->AssetModelId); if (count($assetsToFill > 0)) { foreach ($assetsToFill as $assetToFill) { array_push($chosenAssetModels, $assetToFill->AssetId); } } } $chosenAssetModels = implode(",", $chosenAssetModels); // Check if Model has any Assets need to be updated with new custom field and update them if ($chosenAssetModels) { $objDatabase = CustomField::GetDatabase(); $strQuery = sprintf("UPDATE %s SET `cfv_%s`='%s' WHERE `asset_id` IN ({$chosenAssetModels}) AND cfv_%s IS NULL;", $strHelperTable, $this->objCustomField->CustomFieldId, $txtDefaultValue, $this->objCustomField->CustomFieldId, $this->objCustomField->CustomFieldId); $objDatabase->NonQuery($strQuery); $strQuery = sprintf("UPDATE %s SET `cfv_%s`= NULL WHERE `asset_id` NOT IN({$chosenAssetModels});", $strHelperTable, $this->objCustomField->CustomFieldId); $objDatabase->NonQuery($strQuery); } } } } } } } }
protected function UpdateAssetModelCustomFields() { //$arrAssetCustomFieldsToAdd = array(); // $this->chkAssetCustomFields->SelectedValues; // Generate array of Custom Field values for All Asset Models must be presented in all cases /* $arrAllAssetModelsFlaggedObjects = EntityQtypeCustomField::LoadArrayByEntityQtypeId(QApplication::Translate(EntityQtype::Asset)); $arrAllAssetModelsFlag = array(); foreach ($arrAllAssetModelsFlaggedObjects as $arrAllAssetModelsFlaggedObject){ if ($arrAllAssetModelsFlaggedObject->CustomField->AllAssetModelsFlag){ $arrAllAssetModelsFlag[] = $arrAllAssetModelsFlaggedObject->CustomField->CustomFieldId; } } $arrAssetCustomFieldsToAdd = array_merge($this->chkAssetCustomFields->SelectedValues,$arrAllAssetModelsFlag); */ $arrAssetCustomFieldsToAdd = array_unique($this->chkAssetCustomFields->SelectedValues); // If new asset model add AssetCustomFields for All together with selected if (!$this->blnEditMode) { foreach ($arrAssetCustomFieldsToAdd as $keyAssetCustomField) { $newAssetCustomField = new AssetCustomFieldAssetModel(); $newAssetCustomField->CustomFieldId = $keyAssetCustomField; $newAssetCustomField->AssetModelId = $this->objAssetModel->AssetModelId; $newAssetCustomField->Save(); } } else { $currentAssetCustomFields = AssetCustomFieldAssetModel::LoadArrayByAssetModelId($this->objAssetModel->AssetModelId); foreach ($currentAssetCustomFields as $currentAssetCustomField) { if (!$currentAssetCustomField->CustomField->AllAssetModelsFlag && !in_array($currentAssetCustomField->CustomField->CustomFieldId, $arrAssetCustomFieldsToAdd)) { // If blnEditMode some Assets for this Model can be already assigned and them values // for this custom field must be set to null $arrAssetsAssignedToModel = new Asset(); $arrAssetsAssignedToModel = $arrAssetsAssignedToModel->LoadArrayByAssetModelId($this->objAssetModel->AssetModelId); if (count($arrAssetsAssignedToModel) > 0) { $arrAssetOfModel = array(); foreach ($arrAssetsAssignedToModel as $objAssetAssignedToModel) { array_push($arrAssetOfModel, $objAssetAssignedToModel->AssetId); } $arrAssetOfModel = implode(",", $arrAssetOfModel); //print $arrAssetOfModel; exit; $objDatabase = CustomField::GetDatabase(); $strQuery = sprintf("UPDATE `asset_custom_field_helper` SET `cfv_%s`= NULL WHERE `asset_id` IN({$arrAssetOfModel});", $currentAssetCustomField->CustomFieldId); $objDatabase->NonQuery($strQuery); } // then Delete Associations $currentAssetCustomField->Delete(); } } foreach ($arrAssetCustomFieldsToAdd as $keyAssetCustomField) { $blnToAdd = true; foreach ($currentAssetCustomFields as $currentAssetCustomField) { if ($currentAssetCustomField->CustomField->CustomFieldId == $arrAssetCustomFieldsToAdd) { $blnToAdd = false; } } if ($blnToAdd) { $newAssetCustomField = new AssetCustomFieldAssetModel(); $newAssetCustomField->CustomFieldId = $keyAssetCustomField; $newAssetCustomField->AssetModelId = $this->objAssetModel->AssetModelId; $newAssetCustomField->Save(); // If custom field is required add default value to appropriate Assets $objCustomFieldToAdd = new CustomField(); $objCustomFieldToAdd = $objCustomFieldToAdd->Load($keyAssetCustomField); if ($objCustomFieldToAdd->RequiredFlag) { $arrAssetsAssignedToModel = new Asset(); $arrAssetsAssignedToModel = $arrAssetsAssignedToModel->LoadArrayByAssetModelId($this->objAssetModel->AssetModelId); if (count($arrAssetsAssignedToModel) > 0) { $txtDefaultValue = CustomFieldValue::LoadByCustomFieldValueId($objCustomFieldToAdd->DefaultCustomFieldValueId); $arrAssetOfModel = array(); foreach ($arrAssetsAssignedToModel as $objAssetAssignedToModel) { array_push($arrAssetOfModel, $objAssetAssignedToModel->AssetId); } $arrAssetOfModel = implode(",", $arrAssetOfModel); $objDatabase = CustomField::GetDatabase(); $strQuery = sprintf("UPDATE `asset_custom_field_helper` SET `cfv_%s`= %s WHERE `asset_id` IN({$arrAssetOfModel});", $keyAssetCustomField, $txtDefaultValue); $objDatabase->NonQuery($strQuery); } } // } } } }