private string GetDefaultWidgetFunctionMarkup(DataFieldDescriptor fieldDescriptor)
        {
            // Auto generating a widget for not code generated data types
            Type fieldType;

            if (!fieldDescriptor.ForeignKeyReferenceTypeName.IsNullOrEmpty())
            {
                string referenceTypeName = fieldDescriptor.ForeignKeyReferenceTypeName;
                Type   foreignKeyType    = TypeManager.GetType(referenceTypeName);
                Verify.IsNotNull(foreignKeyType, "Failed to find type '{0}'".FormatWith(referenceTypeName));

                var referenceTemplateType = fieldDescriptor.IsNullable ? typeof(NullableDataReference <>) : typeof(DataReference <>);

                fieldType = referenceTemplateType.MakeGenericType(foreignKeyType);
            }
            else
            {
                fieldType = fieldDescriptor.InstanceType;
            }

            var widgetFunctionProvider = StandardWidgetFunctions.GetDefaultWidgetFunctionProviderByType(fieldType);

            if (widgetFunctionProvider != null)
            {
                return(widgetFunctionProvider.SerializedWidgetFunction.ToString());
            }

            return(null);
        }
 public static WidgetFunctionProvider GetWidgetFunctionProvider(DataFieldDescriptor dataFieldDescriptor)
 {
     if ((dataFieldDescriptor.FormRenderingProfile != null) && (string.IsNullOrEmpty(dataFieldDescriptor.FormRenderingProfile.WidgetFunctionMarkup) == false))
     {
         WidgetFunctionProvider widgetFunctionProvider = new WidgetFunctionProvider(XElement.Parse(dataFieldDescriptor.FormRenderingProfile.WidgetFunctionMarkup));
         return(widgetFunctionProvider);
     }
     else
     {
         return(StandardWidgetFunctions.GetDefaultWidgetFunctionProviderByType(dataFieldDescriptor.InstanceType));
     }
 }
        private void ResetWidgetSelector()
        {
            string widgetFunctionMarkup = "";

            WidgetFunctionProvider widgetFunctionProvider = StandardWidgetFunctions.GetDefaultWidgetFunctionProviderByType(this.CurrentlySelectedWidgetReturnType);

            if (widgetFunctionProvider != null)
            {
                widgetFunctionMarkup = widgetFunctionProvider.SerializedWidgetFunction.ToString(SaveOptions.DisableFormatting);
            }

            btnWidgetFunctionMarkup.Value = widgetFunctionMarkup;

            if (widgetFunctionMarkup == "")
            {
                if (FunctionFacade.GetWidgetFunctionNamesByType(this.CurrentlySelectedWidgetReturnType).Any())
                {
                    Baloon(btnWidgetFunctionMarkup.ClientID, GetString("SpecifyWidgetTip"));
                }
            }
        }
        public MvcFunctionBuilder AddParameter(string name, Type type = null, bool?isRequired = null, BaseValueProvider defaultValueProvider = null, string label = null, string helpText = null, WidgetFunctionProvider widgetFunctionProvider = null, bool hideInSimpleView = false)
        {
            Verify.ArgumentNotNullOrEmpty(name, "name");

            var info          = _function.GetParameterInformation().FirstOrDefault(p => string.Equals(p.Name, name, StringComparison.OrdinalIgnoreCase));
            var reflectedInfo = info != null?GetParameterDefaults(info) : null;


            if (type == null)
            {
                Verify.IsNotNull(reflectedInfo, "Failed to find the type of the parameter '{0}' through reflection", name);

                type = reflectedInfo.Type;
            }

            if (label == null)
            {
                label = name;
            }

            if (helpText == null)
            {
                helpText = String.Empty;
            }

            bool isRequired2 = isRequired ?? reflectedInfo?.IsRequired ?? false;

            if (defaultValueProvider == null)
            {
                defaultValueProvider = reflectedInfo != null
                    ? (BaseValueProvider) new ConstantValueProvider(reflectedInfo.DefaultValue)
                    : new NoValueValueProvider();
            }

            widgetFunctionProvider = widgetFunctionProvider ?? StandardWidgetFunctions.GetDefaultWidgetFunctionProviderByType(type, isRequired2);

            _function.AddParameter(new ParameterProfile(name, type, isRequired2, defaultValueProvider, widgetFunctionProvider,
                                                        label, new HelpDefinition(helpText), hideInSimpleView));
            return(this);
        }
        internal static ParameterProfile BuildParameterProfile(string name, Type type, FunctionParameterAttribute attribute, Type controllerType)
        {
            BaseValueProvider defaultValueProvider = new NoValueValueProvider();
            string            label    = name;
            string            helpText = String.Empty;

            if (!attribute.Label.IsNullOrEmpty())
            {
                label = attribute.Label;
            }

            if (!attribute.Help.IsNullOrEmpty())
            {
                helpText = attribute.Help;
            }

            bool isRequired = !attribute.HasDefaultValue;

            if (!isRequired)
            {
                defaultValueProvider = new ConstantValueProvider(attribute.DefaultValue);
            }

            WidgetFunctionProvider widgetProvider = attribute.GetWidgetFunctionProvider(controllerType, null);

            bool hideInSimpleView = attribute.HideInSimpleView;

            if (widgetProvider == null)
            {
                widgetProvider = StandardWidgetFunctions.GetDefaultWidgetFunctionProviderByType(type, isRequired);
            }

            return(new ParameterProfile(name, type, isRequired, defaultValueProvider, widgetProvider, label,
                                        new HelpDefinition(helpText),
                                        hideInSimpleView));
        }
示例#6
0
        private void GenerateForm()
        {
            var fieldNameToBindingNameMapper = new Dictionary <string, string>();

            _bindingsXml = new XElement(CmsBindingsElementTemplate);
            var layout = new XElement(CmsLayoutElementTemplate);

            if (!string.IsNullOrEmpty(LayoutIconHandle))
            {
                layout.Add(new XAttribute("iconhandle", LayoutIconHandle));
            }

            // Add a read binding as the layout label
            if (!string.IsNullOrEmpty(LayoutLabel))
            {
                var labelAttribute = new XAttribute("label", LayoutLabel);
                layout.Add(labelAttribute);
            }
            else if (!string.IsNullOrEmpty(_dataTypeDescriptor.LabelFieldName))
            {
                layout.Add((new XElement(CmsNamespace + "layout.label", new XElement(CmsNamespace + "read", new XAttribute("source", _dataTypeDescriptor.LabelFieldName)))));
            }


            _panelXml = new XElement(MainNamespace + "FieldGroup");

            string formLabel = !string.IsNullOrEmpty(FieldGroupLabel) ? FieldGroupLabel : _dataTypeDescriptor.Title;

            if (!string.IsNullOrEmpty(formLabel))
            {
                _panelXml.Add(new XAttribute("Label", formLabel));
            }

            layout.Add(_panelXml);

            foreach (var fieldDescriptor in _dataTypeDescriptor.Fields)
            {
                var bindingType = GetFieldBindingType(fieldDescriptor);
                var bindingName = GetBindingName(fieldDescriptor);

                fieldNameToBindingNameMapper.Add(fieldDescriptor.Name, bindingName);

                var binding = new XElement(CmsNamespace + FormKeyTagNames.Binding,
                                           new XAttribute("name", bindingName),
                                           new XAttribute("type", bindingType));

                if (fieldDescriptor.IsNullable)
                {
                    binding.Add(new XAttribute("optional", "true"));
                }

                _bindingsXml.Add(binding);

                if (!_readOnlyFields.Contains(fieldDescriptor.Name))
                {
                    XElement widgetFunctionMarkup;
                    var      label = fieldDescriptor.FormRenderingProfile.Label;
                    if (label.IsNullOrEmpty())
                    {
                        label = fieldDescriptor.Name;
                    }

                    var helptext = fieldDescriptor.FormRenderingProfile.HelpText ?? "";

                    if (!string.IsNullOrEmpty(fieldDescriptor.FormRenderingProfile.WidgetFunctionMarkup))
                    {
                        widgetFunctionMarkup = XElement.Parse(fieldDescriptor.FormRenderingProfile.WidgetFunctionMarkup);
                    }
                    else if (!DataTypeDescriptor.IsCodeGenerated && fieldDescriptor.FormRenderingProfile.WidgetFunctionMarkup == null)
                    {
                        // Auto generating a widget for not code generated data types
                        Type fieldType;

                        if (!fieldDescriptor.ForeignKeyReferenceTypeName.IsNullOrEmpty())
                        {
                            Type foreignKeyType;

                            try
                            {
                                foreignKeyType = Type.GetType(fieldDescriptor.ForeignKeyReferenceTypeName, true);
                            }
                            catch (Exception ex)
                            {
                                throw new InvalidOperationException("Failed to get referenced foreign key type '{0}'".FormatWith(fieldDescriptor.ForeignKeyReferenceTypeName), ex);
                            }

                            var referenceTemplateType = fieldDescriptor.IsNullable ? typeof(NullableDataReference <>) : typeof(DataReference <>);

                            fieldType = referenceTemplateType.MakeGenericType(foreignKeyType);
                        }
                        else
                        {
                            fieldType = fieldDescriptor.InstanceType;
                        }

                        var widgetFunctionProvider = StandardWidgetFunctions.GetDefaultWidgetFunctionProviderByType(fieldType);
                        if (widgetFunctionProvider != null)
                        {
                            widgetFunctionMarkup = widgetFunctionProvider.SerializedWidgetFunction;
                        }
                        else
                        {
                            continue;
                        }
                    }
                    else
                    {
                        continue;
                    }

                    var widgetRuntimeTreeNode = (WidgetFunctionRuntimeTreeNode)FunctionTreeBuilder.Build(widgetFunctionMarkup);
                    widgetRuntimeTreeNode.Label             = label;
                    widgetRuntimeTreeNode.HelpDefinition    = new HelpDefinition(helptext);
                    widgetRuntimeTreeNode.BindingSourceName = bindingName;

                    var element = (XElement)widgetRuntimeTreeNode.GetValue();
                    _panelXml.Add(element);
                }
            }

            if (_showPublicationStatusSelector && _dataTypeDescriptor.SuperInterfaces.Contains(typeof(IPublishControlled)))
            {
                var placeholder = new XElement(MainNamespace + "PlaceHolder");
                _panelXml.Remove();

                placeholder.Add(_panelXml);
                layout.Add(placeholder);

                var publishFieldsXml = new XElement(MainNamespace + "FieldGroup", new XAttribute("Label", Texts.PublicationSettings_FieldGroupLabel));
                placeholder.Add(publishFieldsXml);

                var publicationStatusOptionsBinding = new XElement(CmsNamespace + FormKeyTagNames.Binding,
                                                                   new XAttribute("name", PublicationStatusOptionsBindingName),
                                                                   new XAttribute("type", typeof(object)));

                _bindingsXml.Add(publicationStatusOptionsBinding);

                var element =
                    new XElement(MainNamespace + "KeySelector",
                                 new XAttribute("OptionsKeyField", "Key"),
                                 new XAttribute("OptionsLabelField", "Value"),
                                 new XAttribute("Label", Texts.PublicationStatus_Label),
                                 new XAttribute("Help", Texts.PublicationStatus_Help),
                                 new XElement(MainNamespace + "KeySelector.Selected",
                                              new XElement(CmsNamespace + "bind", new XAttribute("source", PublicationStatusBindingName))),
                                 new XElement(MainNamespace + "KeySelector.Options",
                                              new XElement(CmsNamespace + "read", new XAttribute("source", PublicationStatusOptionsBindingName)))
                                 );


                publishFieldsXml.Add(element);


                var publishDateBinding = new XElement(CmsNamespace + FormKeyTagNames.Binding,
                                                      new XAttribute("name", "PublishDate"),
                                                      new XAttribute("type", typeof(DateTime)),
                                                      new XAttribute("optional", "true"));

                _bindingsXml.Add(publishDateBinding);

                publishFieldsXml.Add(
                    new XElement(MainNamespace + "DateTimeSelector",
                                 new XAttribute("Label", Texts.PublishDate_Label),
                                 new XAttribute("Help", Texts.PublishDate_Help),
                                 new XElement(CmsNamespace + "bind",
                                              new XAttribute("source", "PublishDate"))));

                var unpublishDateBinding = new XElement(CmsNamespace + FormKeyTagNames.Binding,
                                                        new XAttribute("name", "UnpublishDate"),
                                                        new XAttribute("type", typeof(DateTime)),
                                                        new XAttribute("optional", "true"));

                _bindingsXml.Add(unpublishDateBinding);

                publishFieldsXml.Add(
                    new XElement(MainNamespace + "DateTimeSelector",
                                 new XAttribute("Label", Texts.UnpublishDate_Label),
                                 new XAttribute("Help", Texts.UnpublishDate_Help),
                                 new XElement(CmsNamespace + "bind",
                                              new XAttribute("source", "UnpublishDate"))));
            }

            var formDefinition = new XElement(CmsFormElementTemplate);

            formDefinition.Add(_bindingsXml);
            formDefinition.Add(layout);

            if (CustomFormDefinition == null)
            {
                _generatedForm = formDefinition.ToString();
            }
            else
            {
                if (_dataTypeDescriptor.SuperInterfaces.Contains(typeof(IPublishControlled)))
                {
                    fieldNameToBindingNameMapper.Add("PublishDate", "PublishDate");
                    fieldNameToBindingNameMapper.Add("UnpublishDate", "UnpublishDate");
                }

                Func <XElement, IEnumerable <XAttribute> > getBindingsFunc =
                    doc => doc.Descendants(CmsNamespace + "binding").Attributes("name")
                    .Concat(doc.Descendants(CmsNamespace + "bind").Attributes("source"))
                    .Concat(doc.Descendants(CmsNamespace + "read").Attributes("source"));

                // Validation
                foreach (var bindingNameAttribute in getBindingsFunc(CustomFormDefinition.Root))
                {
                    var bindingName = bindingNameAttribute.Value;

                    if (!IsNotFieldBinding(bindingName) && !fieldNameToBindingNameMapper.ContainsKey(bindingName))
                    {
                        throw new ParseDefinitionFileException("Invalid binding name '{0}'".FormatWith(bindingName), bindingNameAttribute);
                    }
                }

                var formDefinitionElement = new XElement(CustomFormDefinition.Root);

                foreach (var bindingNameAttribute in getBindingsFunc(formDefinitionElement).Where(attr => !IsNotFieldBinding(attr.Value)))
                {
                    bindingNameAttribute.Value = fieldNameToBindingNameMapper[bindingNameAttribute.Value];
                }

                if (!string.IsNullOrEmpty(FieldGroupLabel))
                {
                    foreach (var fieldGroupElement in formDefinitionElement.Descendants(MainNamespace + "FieldGroup"))
                    {
                        if (fieldGroupElement.Attribute("Label") == null)
                        {
                            fieldGroupElement.Add(new XAttribute("Label", FieldGroupLabel));
                        }
                    }
                }

                _generatedForm = formDefinitionElement.ToString();
                _panelXml      = formDefinitionElement.Elements().Last().Elements().LastOrDefault();
            }
        }
        private IList <ParameterProfile> InitializeParamteres()
        {
            ParameterInfo[] parameterInfos = this.MethodInfo.GetParameters();

            var defaultValues   = new Dictionary <string, object>();
            var labels          = new Dictionary <string, string>();
            var helpTexts       = new Dictionary <string, string>();
            var widgetProviders = new Dictionary <string, WidgetFunctionProvider>();
            var parametersToHideInSimpleView = new HashSet <string>();

            foreach (object obj in this.MethodInfo.GetCustomAttributes(typeof(MethodBasedDefaultValueAttribute), true))
            {
                MethodBasedDefaultValueAttribute attribute = (MethodBasedDefaultValueAttribute)obj;
                defaultValues.Add(attribute.ParameterName, attribute.DefaultValue);
            }

            // Run through obsolete FunctionParameterDescriptionAttribute
#pragma warning disable 612,618
            foreach (
                object obj in this.MethodInfo.GetCustomAttributes(typeof(FunctionParameterDescriptionAttribute), true))
            {
                FunctionParameterDescriptionAttribute attribute = (FunctionParameterDescriptionAttribute)obj;
                if (attribute.HasDefaultValue && !defaultValues.ContainsKey(attribute.ParameterName))
                {
                    defaultValues.Add(attribute.ParameterName, attribute.DefaultValue);
                }

                labels.Add(attribute.ParameterName, attribute.ParameterLabel);
                helpTexts.Add(attribute.ParameterName, attribute.ParameterHelpText);
            }
#pragma warning restore 612,618

            // Run trhough new and improved FunctionParameterAttribute. Many may exist for one parameter.
            foreach (object obj in this.MethodInfo.GetCustomAttributes(typeof(FunctionParameterAttribute), true))
            {
                var attribute = (FunctionParameterAttribute)obj;

                Verify.That(attribute.HasName,
                            "All [FunctionParameter(...)] definitions on the method '{0}' must have 'Name' specified.",
                            this.MethodInfo.Name);

                string parameterName = attribute.Name;

                if (attribute.HasDefaultValue && !defaultValues.ContainsKey(parameterName))
                {
                    defaultValues.Add(parameterName, attribute.DefaultValue);
                }

                if (attribute.HasLabel && !labels.ContainsKey(parameterName))
                {
                    labels.Add(parameterName, attribute.Label);
                }

                if (attribute.HasHelp && !helpTexts.ContainsKey(parameterName))
                {
                    helpTexts.Add(parameterName, attribute.Help);
                }

                if (attribute.HasWidgetMarkup && !widgetProviders.ContainsKey(parameterName))
                {
                    try
                    {
                        var widgetFunctionProvider = attribute.GetWidgetFunctionProvider(null, null);
                        widgetProviders.Add(parameterName, widgetFunctionProvider);
                    }
                    catch (Exception ex)
                    {
                        string errText = "Failed to set Widget Markup for parameter '{0}' on method '{1}'. {2}"
                                         .FormatWith(parameterName, this.MethodInfo.Name, ex.Message);
                        throw new InvalidOperationException(errText);
                    }
                }

                if (attribute.HideInSimpleView)
                {
                    parametersToHideInSimpleView.Add(parameterName);
                }
            }

            var result = new List <ParameterProfile>();

            foreach (ParameterInfo parameterInfo in parameterInfos)
            {
                string parameterName = parameterInfo.Name;

                BaseValueProvider valueProvider;
                object            defaultValue = null;
                if (defaultValues.TryGetValue(parameterName, out defaultValue))
                {
                    valueProvider = new ConstantValueProvider(defaultValue);
                }
                else
                {
                    valueProvider = new NoValueValueProvider();
                }

                bool isRequired = !defaultValues.ContainsKey(parameterName);

                string parameterLabel = parameterInfo.Name;
                if (labels.ContainsKey(parameterName))
                {
                    parameterLabel = labels[parameterName];
                }

                string parameterHelpText = "";
                if (helpTexts.ContainsKey(parameterName))
                {
                    parameterHelpText = helpTexts[parameterName];
                }

                WidgetFunctionProvider widgetFunctionProvider =
                    (widgetProviders.ContainsKey(parameterName)
                         ? widgetProviders[parameterName]
                         : StandardWidgetFunctions.GetDefaultWidgetFunctionProviderByType(parameterInfo.ParameterType));

                bool hideInSimpleView = parametersToHideInSimpleView.Contains(parameterName);

                result.Add(new ParameterProfile(parameterName, parameterInfo.ParameterType, isRequired,
                                                valueProvider, widgetFunctionProvider, parameterLabel,
                                                new HelpDefinition(parameterHelpText),
                                                hideInSimpleView));
            }

            return(result);
        }