Example #1
0
 protected function btnSave_Click($strFormId, $strControlId, $strParameter)
 {
     try {
         $arrRestrictedFields = array('asset code', 'asset tag', 'model', 'category', 'manufacturer', 'location', 'assets', 'name', 'asset model code', 'model number', 'inventory code', 'quantity', 'company name', 'city', 'state/province', 'country', 'title', 'company', 'email', 'address', 'shipment number', 'ship date', 'ship to company', 'ship to contact', 'ship to address', 'scheduled by', 'status', 'tracking', 'receipt number', 'receive from company', 'receive from contact', 'description', 'account', 'courier', 'account number', 'field name', 'type', 'enabled', 'required', 'role', 'username', 'user role', 'active', 'admin');
         $blnError = false;
         /*if ($this->chkRequiredFlag->Checked) {
         			if ($this->lstCustomFieldQtype->SelectedValue != 2 && $this->txtDefaultValue->Text == '') {
         				$blnError = true;
         				$this->btnCancel->Warning = 'A custom field must have a default value if it is required.';
         			}
         		}*/
         if (count($this->chkEntityQtype->SelectedItems) == 0) {
             $blnError = true;
             $this->btnCancel->Warning = 'You must select at least one field in the Apply To list box.';
         }
         if (in_array(strtolower($this->txtShortDescription->Text), $arrRestrictedFields, false)) {
             $blnError = true;
             $this->btnCancel->Warning = sprintf("'%s' is a Tracmor restricted word. Please choose another name for this custom field", $this->txtShortDescription->Text);
         }
         if (in_array('1', $this->chkEntityQtype->SelectedValues) && $this->rblAllAssetModels->SelectedValue == 2 && count($this->arrAssetModels) == 0) {
             $blnError = true;
             $this->btnCancel->Warning = 'You must apply at least one Model.';
         }
         if ($this->blnEditMode) {
             $objCustomFieldDuplicate = CustomField::QuerySingle(QQ::AndCondition(QQ::Equal(QQN::CustomField()->ShortDescription, $this->txtShortDescription->Text), QQ::NotEqual(QQN::CustomField()->CustomFieldId, $this->objCustomField->CustomFieldId)));
         } else {
             $objCustomFieldDuplicate = CustomField::QuerySingle(QQ::Equal(QQN::CustomField()->ShortDescription, $this->txtShortDescription->Text));
         }
         if ($objCustomFieldDuplicate) {
             $blnError = true;
             $this->btnCancel->Warning = 'A custom field already exists with that name. Please choose another.';
         }
         if (!$blnError) {
             $this->UpdateCustomFieldFields();
             $this->objCustomField->Save();
             // If this field is a required field
             if ($this->objCustomField->RequiredFlag) {
                 $blnDefaultIsNone = false;
                 // If this custom field is a text or textarea,
                 if ($this->lstCustomFieldQtype->SelectedValue != 2) {
                     if ($this->txtDefaultValue->Text != null) {
                         // Assign the existing DefaultCustomFieldValue
                         if ($this->blnEditMode && $this->objCustomField->DefaultCustomFieldValueId) {
                             $objCustomFieldValue = CustomFieldValue::Load($this->objCustomField->DefaultCustomFieldValueId);
                         } else {
                             $objCustomFieldValue = new CustomFieldValue();
                             $objCustomFieldValue->CustomFieldId = $this->objCustomField->CustomFieldId;
                         }
                         // Save the new CustomFieldValue
                         $objCustomFieldValue->ShortDescription = $this->txtDefaultValue->Text;
                         $objCustomFieldValue->Save();
                         // Set the DefaultCustomFieldValueId of the custom field
                         $this->objCustomField->DefaultCustomFieldValueId = $objCustomFieldValue->CustomFieldValueId;
                     } else {
                         $this->objCustomField->DefaultCustomFieldValueId = null;
                         $blnDefaultIsNone = true;
                     }
                 } elseif ($this->lstCustomFieldQtype->SelectedValue == 2) {
                     $this->objCustomField->DefaultCustomFieldValueId = $this->lstDefaultValue->SelectedValue;
                     if ($this->lstDefaultValue->SelectedValue == null) {
                         $blnDefaultIsNone = true;
                     }
                 }
                 // Save the custom field
                 $this->objCustomField->Save();
                 // Update the EntityQtypeCustomFields if they have changed (or if it is a new custom field
                 $this->UpdateEntityQtypeCustomFields();
                 // Update all of the CustomFieldSelections and values for the EntityQtypes
                 if (!$blnDefaultIsNone) {
                     $this->objCustomField->UpdateRequiredFieldSelections();
                     // impove logic for asset customfields
                     if ($this->blnAssetEntityType && $this->rblAllAssetModels->SelectedValue == 2) {
                         // 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);
                         if ($chosenAssetModels) {
                             $objDatabase = CustomField::GetDatabase();
                             $strQuery = sprintf("UPDATE `asset_custom_field_helper` SET `cfv_%s`= NULL WHERE `asset_id` NOT IN({$chosenAssetModels});", $this->objCustomField->CustomFieldId);
                             $objDatabase->NonQuery($strQuery);
                         }
                     }
                 }
             } else {
                 $this->objCustomField->DefaultCustomFieldValueId = null;
                 $this->objCustomField->Save();
                 $this->UpdateEntityQtypeCustomFields();
             }
             // If it is a new select Custom Field, then stay to add options
             if (!$this->blnEditMode && $this->objCustomField->CustomFieldQtypeId == 2) {
                 QApplication::Redirect('custom_field_edit.php?intCustomFieldId=' . $this->objCustomField->CustomFieldId);
             } else {
                 QApplication::Redirect('custom_field_list.php');
             }
         }
     } catch (QExtendedOptimisticLockingException $objExc) {
         $this->btnCancel->Warning = sprintf('This custom field has been updated by another user. You must <a href="custom_field_edit.php?intCustomFieldId=%s">Refresh</a> to edit this custom field.', $this->objCustomField->CustomFieldId);
     }
 }
Example #2
0
 /**
  * This method will update the CustomFieldSelections for one Required Custom Field
  *
  */
 public function UpdateRequiredFieldSelections()
 {
     $objEntityQtypeCustomFieldArray = EntityQtypeCustomField::LoadArrayByCustomFieldId($this->CustomFieldId);
     if ($objEntityQtypeCustomFieldArray) {
         foreach ($objEntityQtypeCustomFieldArray as $objEntityQtypeCustomField) {
             $strEntity = EntityQtype::ToStringPrimaryKeySql($objEntityQtypeCustomField->EntityQtypeId);
             $strEntityTable = EntityQtype::ToStringTable($objEntityQtypeCustomField->EntityQtypeId);
             // This query returns entities which do not have a custom_field_selection for this specific Custom Field/Entity QType combination
             $strQuery = sprintf("\n\t\t\t\t\tSELECT %s AS entity_id\n\t\t\t\t\tFROM %s\n\t\t\t\t\tLEFT JOIN (custom_field_selection JOIN custom_field_value ON custom_field_selection.custom_field_value_id = custom_field_value.custom_field_value_id AND custom_field_value.custom_field_id = %s) ON %s = custom_field_selection.entity_id AND custom_field_selection.entity_qtype_id = %s\n\t\t\t\t\tWHERE custom_field_selection.custom_field_selection_id IS NULL", $strEntity, $strEntityTable, $this->CustomFieldId, $strEntity, $objEntityQtypeCustomField->EntityQtypeId);
             $objDatabase = QApplication::$Database[1];
             $objDbResult = $objDatabase->Query($strQuery);
             while ($mixRow = $objDbResult->FetchArray()) {
                 // If it is not a SELECT custom field, then create a new CustomFieldValue
                 if ($this->CustomFieldQtypeId != 2) {
                     $objCustomFieldValue = new CustomFieldValue();
                     $objCustomFieldValue->CustomFieldId = $this->CustomFieldId;
                     $objCustomFieldValue->ShortDescription = $this->DefaultCustomFieldValue->ShortDescription;
                     $objCustomFieldValue->Save();
                     $intCustomFieldValueId = $objCustomFieldValue->CustomFieldValueId;
                 } else {
                     $intCustomFieldValueId = $this->DefaultCustomFieldValueId;
                 }
                 // Create the new CustomFieldSelection for this Entity Qtype/Entity Id/Custom Field Id
                 $objCustomFieldSelection = new CustomFieldSelection();
                 $objCustomFieldSelection->CustomFieldValueId = $intCustomFieldValueId;
                 $objCustomFieldSelection->EntityQtypeId = $objEntityQtypeCustomField->EntityQtypeId;
                 $objCustomFieldSelection->EntityId = $mixRow['entity_id'];
                 $objCustomFieldSelection->Save();
             }
         }
     }
 }
Example #3
0
    /**
     * This method will update the CustomFieldSelections for one Required Custom Field
     *
     */
    public function UpdateRequiredFieldSelections()
    {
        $objEntityQtypeCustomFieldArray = EntityQtypeCustomField::LoadArrayByCustomFieldId($this->CustomFieldId);
        if ($objEntityQtypeCustomFieldArray) {
            foreach ($objEntityQtypeCustomFieldArray as $objEntityQtypeCustomField) {
                //$strEntity = EntityQtype::ToStringPrimaryKeySql($objEntityQtypeCustomField->EntityQtypeId);
                $arrHelperTable = CustomField::ToStringHelperTable($objEntityQtypeCustomField->EntityQtypeId);
                // This query returns entities which do not have a custom_field_selection for this specific Custom Field/Entity QType combination
                /*$strQuery = sprintf("
                		SELECT %s AS entity_id
                		FROM %s
                		LEFT JOIN (custom_field_selection JOIN custom_field_value ON custom_field_selection.custom_field_value_id = custom_field_value.custom_field_value_id AND custom_field_value.custom_field_id = %s) ON %s = custom_field_selection.entity_id AND custom_field_selection.entity_qtype_id = %s
                		WHERE custom_field_selection.custom_field_selection_id IS NULL"
                		, $strEntity, $strEntityTable, $this->CustomFieldId, $strEntity, $objEntityQtypeCustomField->EntityQtypeId);*/
                if ($objEntityQtypeCustomField->EntityQtypeId === 1 && !$this->AllAssetModelsFlag) {
                    // This is an asset model specific custom field
                    $strQuery = sprintf('
						SELECT %1$s.%2$s AS entity_id
						FROM %1$s, asset_custom_field_asset_model, asset
						WHERE (%1$s.cfv_%3$s IS NULL OR %1$s.cfv_%3$s = \'\')
						AND asset_custom_field_asset_model.custom_field_id = %3$s
						AND asset.asset_model_id = asset_custom_field_asset_model.asset_model_id
						AND %1$s.asset_id = asset.asset_id
						', $arrHelperTable['strHelperTable'], $arrHelperTable['strPrimaryKey'], $this->CustomFieldId);
                } else {
                    $strQuery = sprintf("\n\t\t\t\t\t\tSELECT %s AS entity_id\n\t\t\t\t\t\tFROM %s\n\t\t\t\t\t\tWHERE cfv_%s IS NULL OR cfv_%s = ''", $arrHelperTable['strPrimaryKey'], $arrHelperTable['strHelperTable'], $this->CustomFieldId, $this->CustomFieldId);
                }
                $objDatabase = QApplication::$Database[1];
                $objDbResult = $objDatabase->Query($strQuery);
                while ($mixRow = $objDbResult->FetchArray()) {
                    // If it is not a SELECT custom field, then create a new CustomFieldValue
                    if ($this->CustomFieldQtypeId != 2) {
                        $objCustomFieldValue = new CustomFieldValue();
                        $objCustomFieldValue->CustomFieldId = $this->CustomFieldId;
                        $objCustomFieldValue->ShortDescription = $this->DefaultCustomFieldValue->ShortDescription;
                        $objCustomFieldValue->Save();
                        $intCustomFieldValueId = $objCustomFieldValue->CustomFieldValueId;
                    } else {
                        $intCustomFieldValueId = $this->DefaultCustomFieldValueId;
                    }
                    // Create the new CustomFieldSelection for this Entity Qtype/Entity Id/Custom Field Id
                    $objCustomFieldSelection = new CustomFieldSelection();
                    $objCustomFieldSelection->CustomFieldValueId = $intCustomFieldValueId;
                    $objCustomFieldSelection->EntityQtypeId = $objEntityQtypeCustomField->EntityQtypeId;
                    $objCustomFieldSelection->EntityId = $mixRow['entity_id'];
                    $objCustomFieldSelection->Save();
                }
            }
        }
    }
Example #4
0
 protected function btnSave_Click($strFormId, $strControlId, $strParameter)
 {
     try {
         $arrRestrictedFields = array('asset code', 'model', 'category', 'manufacturer', 'location', 'assets', 'name', 'asset model code', 'inventory code', 'quantity', 'company name', 'city', 'state/province', 'country', 'title', 'company', 'email', 'address', 'shipment number', 'ship date', 'ship to company', 'ship to contact', 'ship to address', 'scheduled by', 'status', 'tracking', 'receipt number', 'receive from company', 'receive from contact', 'description', 'account', 'courier', 'account number', 'field name', 'type', 'enabled', 'required', 'role', 'username', 'user role', 'active', 'admin');
         $blnError = false;
         if ($this->chkRequiredFlag->Checked) {
             if ($this->lstCustomFieldQtype->SelectedValue != 2 && $this->txtDefaultValue->Text == '') {
                 $blnError = true;
                 $this->btnCancel->Warning = 'A custom field must have a default value if it is required.';
             }
         }
         if (count($this->lstEntityQtype->SelectedItems) == 0) {
             $blnError = true;
             $this->btnCancel->Warning = 'You must select at least one field in the Apply To list box.';
         }
         if (in_array(strtolower($this->txtShortDescription->Text), $arrRestrictedFields, false)) {
             $blnError = true;
             $this->btnCancel->Warning = sprintf("'%s' is a Tracmor restricted word. Please choose another name for this custom field", $this->txtShortDescription->Text);
         }
         if ($this->blnEditMode) {
             $objCustomFieldDuplicate = CustomField::QuerySingle(QQ::AndCondition(QQ::Equal(QQN::CustomField()->ShortDescription, $this->txtShortDescription->Text), QQ::NotEqual(QQN::CustomField()->CustomFieldId, $this->objCustomField->CustomFieldId)));
         } else {
             $objCustomFieldDuplicate = CustomField::QuerySingle(QQ::Equal(QQN::CustomField()->ShortDescription, $this->txtShortDescription->Text));
         }
         if ($objCustomFieldDuplicate) {
             $blnError = true;
             $this->btnCancel->Warning = 'A custom field already exists with that name. Please choose another.';
         }
         if (!$blnError) {
             $this->UpdateCustomFieldFields();
             $this->objCustomField->Save();
             // If this field is a required field
             if ($this->objCustomField->RequiredFlag) {
                 // If this custom field is a text or textarea,
                 if ($this->lstCustomFieldQtype->SelectedValue != 2) {
                     // Assign the existing DefaultCustomFieldValue
                     if ($this->blnEditMode && $this->objCustomField->DefaultCustomFieldValueId) {
                         $objCustomFieldValue = CustomFieldValue::Load($this->objCustomField->DefaultCustomFieldValueId);
                     } else {
                         $objCustomFieldValue = new CustomFieldValue();
                         $objCustomFieldValue->CustomFieldId = $this->objCustomField->CustomFieldId;
                     }
                     // Save the new CustomFieldValue
                     $objCustomFieldValue->ShortDescription = $this->txtDefaultValue->Text;
                     $objCustomFieldValue->Save();
                     // Set the DefaultCustomFieldValueId of the custom field
                     $this->objCustomField->DefaultCustomFieldValueId = $objCustomFieldValue->CustomFieldValueId;
                 } elseif ($this->lstCustomFieldQtype->SelectedValue == 2) {
                     $this->objCustomField->DefaultCustomFieldValueId = $this->lstDefaultValue->SelectedValue;
                 }
                 // Save the custom field
                 $this->objCustomField->Save();
                 // Update the EntityQtypeCustomFields if they have changed (or if it is a new custom field
                 $this->UpdateEntityQtypeCustomFields();
                 // Update all of the CustomFieldSelections and values for the EntityQtypes
                 $this->objCustomField->UpdateRequiredFieldSelections();
             } else {
                 $this->objCustomField->DefaultCustomFieldValueId = null;
                 $this->objCustomField->Save();
                 $this->UpdateEntityQtypeCustomFields();
             }
             // If it is a new select Custom Field, then stay to add options
             if (!$this->blnEditMode && $this->objCustomField->CustomFieldQtypeId == 2) {
                 QApplication::Redirect('custom_field_edit.php?intCustomFieldId=' . $this->objCustomField->CustomFieldId);
             } else {
                 QApplication::Redirect('custom_field_list.php');
             }
         }
     } catch (QExtendedOptimisticLockingException $objExc) {
         $this->btnCancel->Warning = sprintf('This custom field has been updated by another user. You must <a href="custom_field_edit.php?intCustomFieldId=%s">Refresh</a> to edit this custom field.', $this->objCustomField->CustomFieldId);
     }
 }