Exemplo n.º 1
0
        public string saveContentPropertyValue([FromBody] string paramValues) // int contentId, string propertyAlias, [FromBody] string propertyValue)
        {
            string result          = "Unexpected error.";
            bool   errorAddedToLog = false;

            try
            {
                // Initializations
                dynamic paramObject = null;

                // Check whether parameters have a value
                if (paramValues == null)
                {
                    result = "Parameters are null.";
                    return(result);
                }

                // Parse parameters
                paramObject = JObject.Parse(paramValues);
                if (paramObject == null)
                {
                    result = "Parameters are incorrect.";
                    return(result);
                }

                // Get the values of the parameters
                result = "Error getting the parameters value";
                int    contentId     = int.MinValue;
                string propertyAlias = string.Empty;
                string propertyValue = string.Empty;
                contentId     = paramObject.contentId;
                propertyAlias = paramObject.propertyAlias;
                propertyValue = paramObject.propertyValue;

                result = "Error saving property value.";
                var content = ApplicationContext.Current.Services.ContentService.GetById(contentId);
                if (content != null)
                {
                    // Check whether the property alias contains dots. If the property alias contains any dot then it means that the property is inside an archetype property (since dots are forbidden in Umbraco content property aliases)
                    if (propertyAlias.Contains("."))
                    {
                        content = ArchetypeHelper.SetArchetypePropertyValue(content, propertyAlias, propertyValue);
                    }
                    else
                    {
                        var property = content.Properties.Where(p => p.Alias == propertyAlias).FirstOrDefault();
                        if (property != null)
                        {
                            property.Value = propertyValue;
                        }
                    }
                    if (content.Published)
                    {
                        ApplicationContext.Services.ContentService.SaveAndPublishWithStatus(content);
                    }
                    else
                    {
                        ApplicationContext.Services.ContentService.Save(content);
                    }
                    result = string.Empty;
                }
            }

            catch (Exception ex)
            {
                LogHelper.Error <uCKEditorApiController>(result, ex);
                errorAddedToLog = true;
                result          = string.Format("{1}: {0}", result, ex.Message);
                return(result);
            }

            finally
            {
                // If there is an error add it to the log file
                if (!errorAddedToLog && !string.IsNullOrWhiteSpace(result))
                {
                    LogHelper.Error <uCKEditorApiController>(result, null);
                }
            }

            return(result);
        }
Exemplo n.º 2
0
        public static string InlineEditorCreate(string htmlEditorContainerId, string contentPropertyAlias, int dataTypeDefinitionId)
        {
            var result = string.Empty;

            try
            {
                // Check whether the current user has edit permissions in order to create the editor
                if (HasCurrentUserPermissionsToEditCurrentContentNode())
                {
                    // Check whether current page's content node is not null
                    UmbracoHelper umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
                    if (umbracoHelper.AssignedContentItem != null)
                    {
                        // Get the content node
                        var content = ApplicationContext.Current.Services.ContentService.GetById(umbracoHelper.AssignedContentItem.Id);
                        if (content != null)
                        {
                            // Check whether the Property Datatype id is provided when invoking this method
                            // If it is not the case then retrieve the Datatype id from the content's property
                            if (dataTypeDefinitionId == int.MinValue)
                            {
                                // Check whether the property alias contains dots. If the property alias contains any dot then it means that the property is inside an archetype property (since dots are forbidden in Umbraco content property aliases)
                                if (contentPropertyAlias.Contains("."))
                                {
                                    dataTypeDefinitionId = ArchetypeHelper.GetArchetypePropertyDatatypeId(content, contentPropertyAlias);
                                }
                                else
                                {
                                    var datatypeAlias     = content.Properties[contentPropertyAlias].Alias;
                                    var propertyDataTypes = content.PropertyTypes.Where(p => p.Alias == datatypeAlias);
                                    if (propertyDataTypes.Any())
                                    {
                                        dataTypeDefinitionId = propertyDataTypes.FirstOrDefault().DataTypeDefinitionId;
                                    }
                                }
                            }

                            if (dataTypeDefinitionId != int.MinValue)
                            {
                                // Get the property prevalues in order to setup CKEditor
                                var datatypePrevalues = ApplicationContext.Current.Services.DataTypeService.GetPreValuesCollectionByDataTypeId(dataTypeDefinitionId).PreValuesAsDictionary;

                                // Javascript statements to setup CKEditor
                                var script = new System.Text.StringBuilder();
                                script.AppendFormat(@"<script type='text/javascript'> 

                                                $(document).ready(function() {{

                                                    // Editor settings
                                                    editorSettings = {{
                                                        customConfigurationFile: ""{3}"",
                                                        width: ""{4}"",
                                                        height: ""{5}"",
                                                        language: ""{6}"",
                                                        font_names: ""{7}"", 
                                                        font_style: ""{8}"", 
                                                        format_tags: ""{9}"", 
                                                        allowedContent: ""{10}"", 
                                                        extraAllowedContent: ""{11}"", 
                                                        toolbar: ""{12}"",
                                                        toolbarGroups: ""{13}"",
                                                        removeButtons: ""{14}"", 
                                                        extraPlugins: ""{15}"", 
                                                        removePlugins: ""{16}"",
                                                        stylesSet: ""{17}""
                                                    }}; 

                                                    // Create inline editor
                                                    editor = createEditor('{0}', editorSettings);

                                                    // Add data required by the save button
                                                    $('#' + editor.name).attr('data-contentId', {1});
                                                    $('#' + editor.name).attr('data-contentPropertyAlias', '{2}');

                                                    // Highlight the html element containing the editor
                                                    var border = $('#' + editor.name).css('border');
                                                    $('#' + editor.name).hover(
                                                        function(e){{
                                                            $(this).css('border', '1px solid red');
                                                        }},function(e){{
                                                            // Restore previous values (saved)
                                                            $(this).css('border', border);
                                                        }}
                                                    );

                                                }} );

                                            </script>",
                                                    htmlEditorContainerId,
                                                    content.Id,
                                                    contentPropertyAlias,
                                                    processPreValue(datatypePrevalues, "customConfigurationFile"),
                                                    processPreValue(datatypePrevalues, "width"),
                                                    processPreValue(datatypePrevalues, "height"),
                                                    processPreValue(datatypePrevalues, "language"),
                                                    processPreValue(datatypePrevalues, "font_names"),
                                                    processPreValue(datatypePrevalues, "font_style"),
                                                    processPreValue(datatypePrevalues, "format_tags"),
                                                    processPreValue(datatypePrevalues, "allowedContent"),
                                                    processPreValue(datatypePrevalues, "extraAllowedContent"),
                                                    processPreValue(datatypePrevalues, "toolbar"),
                                                    processPreValue(datatypePrevalues, "toolbarGroups"),
                                                    processPreValue(datatypePrevalues, "removeButtons"),
                                                    processPreValue(datatypePrevalues, "extraPlugins"),
                                                    processPreValue(datatypePrevalues, "removePlugins"),
                                                    processPreValue(datatypePrevalues, "stylesSet")
                                                    );
                                result = script.ToString();
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error(typeof(InlineEditorHelper), string.Format("Error creating the inline editor for the property: {0}", contentPropertyAlias), ex);
            }
            return(result);
        }