public void DeleteCustomField(int fieldID)
        {
            if (!TSAuthentication.IsSystemAdmin)
            {
                return;
            }

            LoginUser   login = TSAuthentication.GetLoginUser();
            CustomField field = CustomFields.GetCustomField(login, fieldID);

            int?crmLinkFieldId = null;

            try
            {
                crmLinkFieldId = CRMLinkFields.FindIdByCustomFieldId(fieldID, login);
            }
            catch (Exception ex)
            {
                crmLinkFieldId = null;
            }

            field.Delete();
            field.Collection.Save();

            if (crmLinkFieldId != null && crmLinkFieldId > 0)
            {
                CRMLinkFields crmLinkFieldsDelete = new CRMLinkFields(login);
                crmLinkFieldsDelete.DeleteFromDB((int)crmLinkFieldId);
            }
        }
        public static string GetCRMLinkField(RestCommand command, int cRMFieldID)
        {
            CRMLinkField cRMLinkField = CRMLinkFields.GetCRMLinkField(command.LoginUser, cRMFieldID);

            if (cRMLinkField.OrganizationID != command.Organization.OrganizationID)
            {
                throw new RestException(HttpStatusCode.Unauthorized);
            }
            return(cRMLinkField.GetXml("CRMLinkField", true));
        }
        public static string GetCRMLinkFields(RestCommand command)
        {
            CRMLinkFields cRMLinkFields = new CRMLinkFields(command.LoginUser);

            cRMLinkFields.LoadByOrganizationID(command.Organization.OrganizationID);

            if (command.Format == RestFormat.XML)
            {
                return(cRMLinkFields.GetXml("CRMLinkFields", "CRMLinkField", true, command.Filters));
            }
            else
            {
                throw new RestException(HttpStatusCode.BadRequest, "Invalid data format");
            }
        }
    public static string ReplaceType(SelectedType type, int oldID, int newID, string arg)
    {
        if (!UserSession.CurrentUser.IsSystemAdmin)
        {
            return("");
        }
        switch (type)
        {
        case SelectedType.ActionTypes:
            if (ActionTypes.GetActionType(UserSession.LoginUser, oldID).OrganizationID != UserSession.LoginUser.OrganizationID)
            {
                return("");
            }
            if (ActionTypes.GetActionType(UserSession.LoginUser, newID).OrganizationID != UserSession.LoginUser.OrganizationID)
            {
                return("");
            }
            (new Actions(UserSession.LoginUser)).ReplaceActionType(oldID, newID);
            ActionTypes actionTypes = new ActionTypes(UserSession.LoginUser);
            actionTypes.DeleteFromDB(oldID);
            actionTypes.ValidatePositions(UserSession.LoginUser.OrganizationID);
            break;

        case SelectedType.PhoneTypes:
            if (PhoneTypes.GetPhoneType(UserSession.LoginUser, oldID).OrganizationID != UserSession.LoginUser.OrganizationID)
            {
                return("");
            }
            if (PhoneTypes.GetPhoneType(UserSession.LoginUser, newID).OrganizationID != UserSession.LoginUser.OrganizationID)
            {
                return("");
            }
            (new PhoneNumbers(UserSession.LoginUser)).ReplacePhoneType(oldID, newID);
            PhoneTypes phoneTypes = new PhoneTypes(UserSession.LoginUser);
            phoneTypes.DeleteFromDB(oldID);
            phoneTypes.ValidatePositions(UserSession.LoginUser.OrganizationID);
            break;

        case SelectedType.ProductVersionStatuses:
            if (ProductVersionStatuses.GetProductVersionStatus(UserSession.LoginUser, oldID).OrganizationID != UserSession.LoginUser.OrganizationID)
            {
                return("");
            }
            if (ProductVersionStatuses.GetProductVersionStatus(UserSession.LoginUser, newID).OrganizationID != UserSession.LoginUser.OrganizationID)
            {
                return("");
            }
            (new ProductVersions(UserSession.LoginUser)).ReplaceProductVersionStatus(oldID, newID);
            ProductVersionStatuses productVersionStatuses = new ProductVersionStatuses(UserSession.LoginUser);
            productVersionStatuses.DeleteFromDB(oldID);
            productVersionStatuses.ValidatePositions(UserSession.LoginUser.OrganizationID);
            break;

        case SelectedType.TicketSeverities:
            if (TicketSeverities.GetTicketSeverity(UserSession.LoginUser, oldID).OrganizationID != UserSession.LoginUser.OrganizationID)
            {
                return("");
            }
            if (TicketSeverities.GetTicketSeverity(UserSession.LoginUser, newID).OrganizationID != UserSession.LoginUser.OrganizationID)
            {
                return("");
            }
            (new Tickets(UserSession.LoginUser)).ReplaceTicketSeverity(oldID, newID);
            TicketSeverities ticketSeverities = new TicketSeverities(UserSession.LoginUser);
            ticketSeverities.DeleteFromDB(oldID);
            ticketSeverities.ValidatePositions(UserSession.LoginUser.OrganizationID);
            break;

        case SelectedType.TicketStatuses:
            TicketStatus oldStatus = TicketStatuses.GetTicketStatus(UserSession.LoginUser, oldID);
            if (oldStatus.OrganizationID != UserSession.LoginUser.OrganizationID)
            {
                return("");
            }
            if (TicketStatuses.GetTicketStatus(UserSession.LoginUser, newID).OrganizationID != UserSession.LoginUser.OrganizationID)
            {
                return("");
            }
            (new Tickets(UserSession.LoginUser)).ReplaceTicketStatus(oldID, newID);
            TicketStatuses ticketStatuses = new TicketStatuses(UserSession.LoginUser);
            ticketStatuses.DeleteFromDB(oldID);
            ticketStatuses.ValidatePositions(oldStatus.TicketTypeID);
            break;

        case SelectedType.TicketTypes:
            if (TicketTypes.GetTicketType(UserSession.LoginUser, oldID).OrganizationID != UserSession.LoginUser.OrganizationID)
            {
                return("");
            }
            if (TicketTypes.GetTicketType(UserSession.LoginUser, newID).OrganizationID != UserSession.LoginUser.OrganizationID)
            {
                return("");
            }
            (new Tickets(UserSession.LoginUser)).ReplaceTicketType(oldID, newID);
            TicketTypes ticketTypes = new TicketTypes(UserSession.LoginUser);

            CustomFields customFields = new CustomFields(UserSession.LoginUser);
            customFields.LoadByTicketTypeID(UserSession.LoginUser.OrganizationID, oldID);

            ticketTypes.DeleteFromDB(oldID);
            ticketTypes.ValidatePositions(UserSession.LoginUser.OrganizationID);

            int?crmLinkFieldId = null;

            foreach (CustomField customField in customFields)
            {
                try
                {
                    crmLinkFieldId = CRMLinkFields.FindIdByCustomFieldId(customField.CustomFieldID, UserSession.LoginUser);
                }
                catch (Exception ex)
                {
                    crmLinkFieldId = null;
                }

                if (crmLinkFieldId != null && crmLinkFieldId > 0)
                {
                    CRMLinkFields crmLinkFieldsDelete = new CRMLinkFields(UserSession.LoginUser);
                    crmLinkFieldsDelete.DeleteFromDB((int)crmLinkFieldId);
                }
            }

            break;

        case SelectedType.ActivityTypes:
            if (ActivityTypes.GetActivityType(UserSession.LoginUser, oldID).OrganizationID != UserSession.LoginUser.OrganizationID)
            {
                return("");
            }
            var types = Enum.GetValues(typeof(ActivityTypeEnum));
            if (newID > types.Length)
            {
                if (ActivityTypes.GetActivityType(UserSession.LoginUser, newID).OrganizationID != UserSession.LoginUser.OrganizationID)
                {
                    return("");
                }
            }
            (new Notes(UserSession.LoginUser)).ReplaceActivityType(oldID, newID);
            ActivityTypes activityTypes = new ActivityTypes(UserSession.LoginUser);
            activityTypes.DeleteFromDB(oldID);
            activityTypes.ValidatePositions(UserSession.LoginUser.OrganizationID);
            break;

        default:
            break;
        }
        return(GetTypesHtml(type, arg));
    }