Exemplo n.º 1
0
        public dynamic InitPcFieldBaseModel(PageComponentContext context, PcFieldBaseOptions options, out string label, string targetModel = "PcFieldBaseModel")
        {
            label = "";
            var model = new PcFieldBaseModel();

            if (context.Items.ContainsKey(typeof(ValidationException)))
            {
                model.ValidationErrors = ((ValidationException)context.Items[typeof(ValidationException)]).Errors;
            }

            model.LabelRenderModeOptions = ModelExtensions.GetEnumAsSelectOptions <LabelRenderMode>();

            model.FieldRenderModeOptions = ModelExtensions.GetEnumAsSelectOptions <FieldRenderMode>();

            if (context.Mode == ComponentMode.Options)
            {
                model.EntitySelectOptions = new MetaService().GetEntitiesAsSelectOptions();
            }

            var recordId = context.DataModel.GetProperty("RecordId");

            if (recordId != null && recordId is Guid)
            {
                model.RecordId = (Guid)recordId;
            }

            var entity = context.DataModel.GetProperty("Entity");

            if (entity != null && entity is Entity)
            {
                model.EntityName = ((Entity)entity).Name;
                if (!String.IsNullOrWhiteSpace(model.EntityName) && model.RecordId != null)
                {
                    model.ApiUrl = $"/api/v3/en_US/record/{model.EntityName}/{model.RecordId}/";
                }
            }

            Entity mappedEntity = null;

            if (options.ConnectedEntityId != null)
            {
                mappedEntity = new EntityManager().ReadEntity(options.ConnectedEntityId.Value).Object;
            }
            else if (options.ConnectedEntityId == null && entity is Entity)
            {
                mappedEntity = (Entity)entity;
            }

            if (mappedEntity != null)
            {
                var fieldName = options.Name;

                if (fieldName.StartsWith("$"))
                {
                    //Field with relation is set. Mapped entity should be changed
                    var fieldNameArray = fieldName.Replace("$", "").Split(".", StringSplitOptions.RemoveEmptyEntries);
                    if (fieldNameArray.Length == 2)
                    {
                        var relationName = fieldNameArray[0];
                        fieldName = fieldNameArray[1];
                        var relation = new EntityRelationManager().Read(relationName).Object;
                        if (relation != null)
                        {
                            if (relation.OriginEntityId == mappedEntity.Id)
                            {
                                mappedEntity = new EntityManager().ReadEntity(relation.TargetEntityId).Object;
                            }
                            else if (relation.TargetEntityId == mappedEntity.Id)
                            {
                                mappedEntity = new EntityManager().ReadEntity(relation.OriginEntityId).Object;
                            }
                        }
                    }
                }

                var entityField = mappedEntity.Fields.FirstOrDefault(x => x.Name == fieldName);
                if (entityField != null)
                {
                    //Connection success set local options if needed
                    if (String.IsNullOrWhiteSpace(model.Placeholder))
                    {
                        model.Placeholder = entityField.PlaceholderText;
                    }

                    if (String.IsNullOrWhiteSpace(model.Description))
                    {
                        model.Description = entityField.Description;
                    }

                    if (String.IsNullOrWhiteSpace(model.LabelHelpText))
                    {
                        model.LabelHelpText = entityField.HelpText;
                    }

                    if (String.IsNullOrWhiteSpace(label))
                    {
                        label = entityField.Label;
                    }

                    model.Required = entityField.Required;

                    if (entityField.EnableSecurity)
                    {
                        var currentUser = context.DataModel.GetProperty("CurrentUser");
                        if (currentUser != null && currentUser is ErpUser)
                        {
                            var canRead   = false;
                            var canUpdate = false;
                            var user      = (ErpUser)currentUser;
                            foreach (var role in user.Roles)
                            {
                                if (entityField.Permissions.CanRead.Any(x => x == role.Id))
                                {
                                    canRead = true;
                                }
                                if (entityField.Permissions.CanUpdate.Any(x => x == role.Id))
                                {
                                    canUpdate = true;
                                }
                            }
                            if (canUpdate)
                            {
                                model.Access = FieldAccess.Full;
                            }
                            else if (canRead)
                            {
                                model.Access = FieldAccess.ReadOnly;
                            }
                            else
                            {
                                model.Access = FieldAccess.Forbidden;
                            }
                        }
                    }

                    //Specific model properties
                    var fieldOptions = new List <SelectOption>();
                    switch (entityField.GetFieldType())
                    {
                    case FieldType.SelectField:
                        var selectField = ((SelectField)entityField);
                        model.DefaultValue = selectField.DefaultValue;
                        fieldOptions       = selectField.Options;
                        break;

                    case FieldType.MultiSelectField:
                        var multiselectField = ((MultiSelectField)entityField);
                        model.DefaultValue = multiselectField.DefaultValue;
                        fieldOptions       = multiselectField.Options;
                        break;

                    default:
                        break;
                    }
                    switch (targetModel)
                    {
                    case "PcFieldSelectModel":
                        return(PcFieldSelectModel.CopyFromBaseModel(model, fieldOptions));

                    case "PcFieldRadioListModel":
                        return(PcFieldRadioListModel.CopyFromBaseModel(model, fieldOptions));

                    case "PcFieldCheckboxListModel":
                        return(PcFieldCheckboxListModel.CopyFromBaseModel(model, fieldOptions));

                    case "PcFieldMultiSelectModel":
                        return(PcFieldMultiSelectModel.CopyFromBaseModel(model, fieldOptions));

                    case "PcFieldCheckboxGridModel":
                        return(PcFieldCheckboxGridModel.CopyFromBaseModel(model, new List <SelectOption>(), new List <SelectOption>()));

                    default:
                        return(model);
                    }
                }
            }

            switch (targetModel)
            {
            case "PcFieldSelectModel":
                return(PcFieldSelectModel.CopyFromBaseModel(model, new List <SelectOption>()));

            case "PcFieldRadioListModel":
                return(PcFieldRadioListModel.CopyFromBaseModel(model, new List <SelectOption>()));

            case "PcFieldCheckboxListModel":
                return(PcFieldCheckboxListModel.CopyFromBaseModel(model, new List <SelectOption>()));

            case "PcFieldMultiSelectModel":
                return(PcFieldMultiSelectModel.CopyFromBaseModel(model, new List <SelectOption>()));

            case "PcFieldCheckboxGridModel":
                return(PcFieldCheckboxGridModel.CopyFromBaseModel(model, new List <SelectOption>(), new List <SelectOption>()));

            default:
                return(model);
            }
        }
Exemplo n.º 2
0
        public dynamic InitPcFieldBaseModel(PageComponentContext context, PcFieldBaseOptions options, out string label, string targetModel = "PcFieldBaseModel")
        {
            label = "";
            var model = new PcFieldBaseModel();

            if (context.Items.ContainsKey(typeof(ValidationException)))
            {
                model.ValidationErrors = ((ValidationException)context.Items[typeof(ValidationException)]).Errors;
            }

            model.LabelRenderModeOptions = ModelExtensions.GetEnumAsSelectOptions <LabelRenderMode>();

            model.FieldRenderModeOptions = ModelExtensions.GetEnumAsSelectOptions <FieldRenderMode>();

            var recordId = context.DataModel.GetProperty("RecordId");

            if (recordId != null && recordId is Guid)
            {
                model.RecordId = (Guid)recordId;
            }

            var entity = context.DataModel.GetProperty("Entity");

            if (entity != null && entity is Entity)
            {
                model.EntityName = ((Entity)entity).Name;
                if (!String.IsNullOrWhiteSpace(model.EntityName) && model.RecordId != null)
                {
                    model.ApiUrl = $"/api/v3/en_US/record/{model.EntityName}/{model.RecordId}/";
                }

                if (options.TryConnectToEntity)
                {
                    var fieldName   = options.Name;
                    var entityField = ((Entity)entity).Fields.FirstOrDefault(x => x.Name == fieldName);
                    if (entityField != null)
                    {
                        //Connection success override the local options
                        //Init model
                        model.Placeholder   = entityField.PlaceholderText;
                        model.Description   = entityField.Description;
                        model.LabelHelpText = entityField.HelpText;
                        model.Required      = entityField.Required;
                        label = entityField.Label;
                        if (entityField.EnableSecurity)
                        {
                            var currentUser = context.DataModel.GetProperty("CurrentUser");
                            if (currentUser != null && currentUser is ErpUser)
                            {
                                var canRead   = false;
                                var canUpdate = false;
                                var user      = (ErpUser)currentUser;
                                foreach (var role in user.Roles)
                                {
                                    if (entityField.Permissions.CanRead.Any(x => x == role.Id))
                                    {
                                        canRead = true;
                                    }
                                    if (entityField.Permissions.CanUpdate.Any(x => x == role.Id))
                                    {
                                        canUpdate = true;
                                    }
                                }
                                if (canUpdate)
                                {
                                    model.Access = FieldAccess.Full;
                                }
                                else if (canRead)
                                {
                                    model.Access = FieldAccess.ReadOnly;
                                }
                                else
                                {
                                    model.Access = FieldAccess.Forbidden;
                                }
                            }
                        }

                        //Specific model properties
                        var fieldOptions = new List <SelectOption>();
                        switch (entityField.GetFieldType())
                        {
                        case FieldType.SelectField:
                            var selectField = ((SelectField)entityField);
                            model.DefaultValue = selectField.DefaultValue;
                            fieldOptions       = selectField.Options;
                            break;

                        case FieldType.MultiSelectField:
                            var multiselectField = ((MultiSelectField)entityField);
                            model.DefaultValue = multiselectField.DefaultValue;
                            fieldOptions       = multiselectField.Options;
                            break;

                        default:
                            break;
                        }
                        switch (targetModel)
                        {
                        case "PcFieldSelectModel":
                            return(PcFieldSelectModel.CopyFromBaseModel(model, fieldOptions));

                        case "PcFieldRadioListModel":
                            return(PcFieldRadioListModel.CopyFromBaseModel(model, fieldOptions));

                        case "PcFieldCheckboxListModel":
                            return(PcFieldCheckboxListModel.CopyFromBaseModel(model, fieldOptions));

                        case "PcFieldMultiSelectModel":
                            return(PcFieldMultiSelectModel.CopyFromBaseModel(model, fieldOptions));

                        case "PcFieldCheckboxGridModel":
                            return(PcFieldCheckboxGridModel.CopyFromBaseModel(model, new List <SelectOption>(), new List <SelectOption>()));

                        default:
                            return(model);
                        }
                    }
                }
            }

            switch (targetModel)
            {
            case "PcFieldSelectModel":
                return(PcFieldSelectModel.CopyFromBaseModel(model, new List <SelectOption>()));

            case "PcFieldRadioListModel":
                return(PcFieldRadioListModel.CopyFromBaseModel(model, new List <SelectOption>()));

            case "PcFieldCheckboxListModel":
                return(PcFieldCheckboxListModel.CopyFromBaseModel(model, new List <SelectOption>()));

            case "PcFieldMultiSelectModel":
                return(PcFieldMultiSelectModel.CopyFromBaseModel(model, new List <SelectOption>()));

            case "PcFieldCheckboxGridModel":
                return(PcFieldCheckboxGridModel.CopyFromBaseModel(model, new List <SelectOption>(), new List <SelectOption>()));

            default:
                return(model);
            }
        }