Exemple #1
0
        public string Execute([NotNull] string databaseName, [NotNull] string itemId, [NotNull] string languageName, [NotNull] string version, [NotNull] string fieldName, [NotNull] string xml)
        {
            var database = Factory.GetDatabase(databaseName);

            if (database == null)
            {
                throw new Exception("Database not found");
            }

            var item = database.GetItem(itemId, Language.Parse(languageName), Data.Version.Parse(version));

            if (item == null)
            {
                throw new Exception("Item not found");
            }

            var field = item.Fields[fieldName];

            item.Editing.BeginEdit();
            SetFieldValuePipeline.Run().WithParameters(field, xml);
            item.Editing.EndEdit();

            item = database.GetItem(itemId);

            var pipeline = GetFieldValuePipeline.Run().WithParameters(item, fieldName);

            return(pipeline.Value);
        }
Exemple #2
0
        public override void Check(ValidationWriter output, Item item)
        {
            Assert.ArgumentNotNull(output, nameof(output));
            Assert.ArgumentNotNull(item, nameof(item));

            if (item.TemplateID != TemplateIDs.Template)
            {
                return;
            }

            var layoutField = item.Fields[FieldIDs.LayoutField];

            if (layoutField.ContainsStandardValue)
            {
                return;
            }

            var value = GetFieldValuePipeline.Run().WithParameters(layoutField).Value;

            if (string.IsNullOrEmpty(value))
            {
                return;
            }

            output.Write(SeverityLevel.Suggestion, "Layout on template", "Assigning layouts on templates are no longer recommended.", "Assign the layout on the standard values item.", item);
        }
Exemple #3
0
        private bool UpdateItem(Query query, [NotNull] Item item)
        {
            Debug.ArgumentNotNull(item, nameof(item));

            var find = GetStringValue(query, Find, item);
            var with = GetStringValue(query, With, item);

            var result = false;

            item.Editing.BeginEdit();

            if (Field != null)
            {
                var value = GetFieldValuePipeline.Run().WithParameters(item, Field).Value;

                value = value.Replace(find, with);

                if (value != item[Field])
                {
                    SetFieldValuePipeline.Run().WithParameters(item, Field, value);
                    result = true;
                }
            }
            else
            {
                foreach (Field field in item.Fields)
                {
                    var value = GetFieldValuePipeline.Run().WithParameters(field).Value;

                    value = value.Replace(find, with);

                    if (value == field.Value)
                    {
                        continue;
                    }

                    SetFieldValuePipeline.Run().WithParameters(field, value);
                    result = true;
                }
            }

            item.Editing.EndEdit();

            return(result);
        }
Exemple #4
0
        public override void Check(ValidationWriter output, Item item)
        {
            var layout = GetFieldValuePipeline.Run().WithParameters(item.Fields[FieldIDs.LayoutField]).Value ?? string.Empty;

            if (string.IsNullOrEmpty(layout))
            {
                return;
            }

            var devices = item.Database.GetItem(ItemIDs.DevicesRoot);

            foreach (Item deviceItem in devices.Children)
            {
                var device = new DeviceItem(deviceItem);

                if (item.Visualization.GetLayoutID(device) == Page)
                {
                    CheckDevice(output, item, device);
                }
            }
        }
        private void ProcessLayout([NotNull] XmlTextWriter output, [NotNull] ValidationWriter writer, [NotNull] ValidationAnalyzerOptions options, [NotNull] Item item)
        {
            Debug.ArgumentNotNull(output, nameof(output));
            Debug.ArgumentNotNull(writer, nameof(writer));
            Debug.ArgumentNotNull(options, nameof(options));
            Debug.ArgumentNotNull(item, nameof(item));

            var layout = GetFieldValuePipeline.Run().WithParameters(item.Fields[FieldIDs.LayoutField]).Value ?? string.Empty;

            if (string.IsNullOrEmpty(layout))
            {
                return;
            }

            XDocument doc;

            try
            {
                doc = XDocument.Parse(layout);
            }
            catch
            {
                return;
            }

            var root = doc.Root;

            if (root == null)
            {
                return;
            }

            foreach (var renderingElement in root.Elements().Elements())
            {
                ProcessLayoutRendering(output, writer, options, item, renderingElement);
            }
        }
        private void GetFields([NotNull] XmlTextWriter output, [NotNull] Item item, bool allFields)
        {
            Debug.ArgumentNotNull(output, nameof(output));
            Debug.ArgumentNotNull(item, nameof(item));

            var fields = item.Fields;

            if (allFields)
            {
                fields.ReadAll();
            }

            fields.Sort();

            var sections = new Dictionary <string, SectionDefinition>();

            foreach (Field field in fields)
            {
                if (field.Name.Length <= 0)
                {
                    continue;
                }

                var templateField = field.GetTemplateField();

                SectionDefinition section;
                if (!sections.TryGetValue(field.Section, out section))
                {
                    section = new SectionDefinition();
                    sections[field.Section] = section;

                    var sectionItem = item.Database.GetItem(templateField.Section.ID);

                    section.SectionSortOrder = field.SectionSortorder.ToString();
                    section.ExpandByDefault  = sectionItem["Collapsed By Default"] == "1" ? "0" : "1";
                }

                var isOwnField = templateField.Template.ID == item.TemplateID;

                output.WriteStartElement("field");

                output.WriteAttributeString("itemid", item.ID.ToString());
                output.WriteAttributeString("language", item.Language.ToString());
                output.WriteAttributeString("version", item.Version.ToString());

                output.WriteAttributeString("fieldid", field.ID.ToString());
                output.WriteAttributeString("templatefieldid", templateField.ID.ToString());
                output.WriteAttributeString("name", field.Name);
                output.WriteAttributeString("title", field.Title);
                output.WriteAttributeString("type", field.Type);
                output.WriteAttributeString("source", field.Source);
                output.WriteAttributeString("section", field.Section);
                output.WriteAttributeString("tooltip", field.ToolTip);
                output.WriteAttributeString("sortorder", field.Sortorder.ToString());
                output.WriteAttributeString("sectionsortorder", section.SectionSortOrder);
                output.WriteAttributeString("sectionexpandedbydefault", section.ExpandByDefault);
                output.WriteAttributeString("sectionid", templateField.Section.ID.ToString());
                output.WriteAttributeString("sectionicon", Images.GetThemedImageSource(templateField.Section.Icon, ImageDimension.id16x16));
                output.WriteAttributeString("isdeclaringtemplate", isOwnField ? "1" : "0");

                output.WriteAttributeString("shared", field.Shared ? "1" : "0");
                output.WriteAttributeString("unversioned", field.Unversioned ? "1" : "0");
                output.WriteAttributeString("standardvalue", field.ContainsStandardValue ? "1" : "0");
                output.WriteAttributeString("isblob", field.IsBlobField ? "1" : "0");
                output.WriteAttributeString("iscloned", field.InheritsValueFromOtherItem ? "1" : "0");

                WriteDisplayDataIfApplicable(output, item, field);

                output.WriteStartElement("value");
                output.WriteValue(GetFieldValuePipeline.Run().WithParameters(field).Value ?? string.Empty);
                output.WriteEndElement();

                GetFieldLookups(output, field);
                GetFieldRoot(output, field);

                output.WriteEndElement();
            }
        }
Exemple #7
0
        public override void Check(ValidationWriter output, Item item)
        {
            Assert.ArgumentNotNull(output, nameof(output));
            Assert.ArgumentNotNull(item, nameof(item));

            var layoutField = item.Fields[FieldIDs.LayoutField];

            if (layoutField.ContainsStandardValue)
            {
                return;
            }

            var value = GetFieldValuePipeline.Run().WithParameters(layoutField).Value;

            if (string.IsNullOrEmpty(value))
            {
                return;
            }

            var layoutDefinition = LayoutDefinition.Parse(value);

            if (layoutDefinition == null)
            {
                return;
            }

            foreach (DeviceDefinition device in layoutDefinition.Devices)
            {
                var renderings = device.Renderings;
                if (renderings == null || renderings.Count == 0)
                {
                    continue;
                }

                var layoutId = device.Layout;
                if (string.IsNullOrEmpty(layoutId))
                {
                    continue;
                }

                var layoutItem = item.Database.GetItem(layoutId);
                if (layoutItem == null)
                {
                    continue;
                }

                var deviceName = "Unknown";
                var deviceId   = device.ID;

                if (!string.IsNullOrEmpty(deviceId))
                {
                    var deviceItem = item.Database.GetItem(deviceId);
                    if (deviceItem != null)
                    {
                        deviceName = deviceItem.Name;
                    }
                }

                var placeholders = new List <string>();
                placeholders.AddRange(Analyze(layoutItem, string.Empty));

                foreach (RenderingDefinition rendering in renderings)
                {
                    var itemId = rendering.ItemID;
                    if (string.IsNullOrEmpty(itemId))
                    {
                        continue;
                    }

                    var renderingItem = item.Database.GetItem(itemId);
                    if (renderingItem == null)
                    {
                        continue;
                    }

                    var parameters = new UrlString(rendering.Parameters);
                    var id         = parameters["Id"];

                    placeholders.AddRange(Analyze(renderingItem, id));
                }

                foreach (RenderingDefinition rendering in renderings)
                {
                    var placeholder = rendering.Placeholder;
                    if (string.IsNullOrEmpty(placeholder))
                    {
                        continue;
                    }

                    var renderingName = rendering.UniqueId ?? string.Empty;

                    var itemId = rendering.ItemID;
                    if (!string.IsNullOrEmpty(itemId))
                    {
                        var renderingItem = item.Database.GetItem(itemId);
                        if (renderingItem != null)
                        {
                            renderingName = renderingItem.Name;
                        }
                    }

                    if (!Contains(placeholders, placeholder))
                    {
                        output.Write(SeverityLevel.Warning, "Referenced placeholder does not exist", string.Format("The placeholder \"{0}\" which is referenced by the \"{1}\" rendering in the \"{2}\" device does not exist.", placeholder, renderingName, deviceName), string.Format("Set the placeholder of the \"{0}\" rendering in the \"{2}\" device to one of the following placeholders: {1}.", renderingName, string.Join(", ", placeholders.ToArray()), deviceName), item);
                    }
                }
            }
        }
        public override void Check(ValidationWriter output, Item item)
        {
            Assert.ArgumentNotNull(output, nameof(output));
            Assert.ArgumentNotNull(item, nameof(item));

            var layoutField = item.Fields[FieldIDs.LayoutField];

            if (layoutField.ContainsStandardValue)
            {
                return;
            }

            var value = GetFieldValuePipeline.Run().WithParameters(layoutField).Value;

            if (string.IsNullOrEmpty(value))
            {
                return;
            }

            var layoutDefinition = LayoutDefinition.Parse(value);

            if (layoutDefinition == null)
            {
                return;
            }

            foreach (DeviceDefinition device in layoutDefinition.Devices)
            {
                var renderings = device.Renderings;
                if (renderings != null && renderings.Count > 0)
                {
                    continue;
                }

                var layoutId = device.Layout;
                if (string.IsNullOrEmpty(layoutId))
                {
                    continue;
                }

                var layoutItem = item.Database.GetItem(layoutId);
                if (layoutItem == null)
                {
                    continue;
                }

                var layoutFile = layoutItem["Path"];

                var    path = FileUtil.MapPath(layoutFile);
                string source;

                try
                {
                    source = FileUtil.ReadFromFile(path);
                }
                catch
                {
                    continue;
                }

                var matches = Regex.Matches(source, "<[^>]*Placeholder[^>]*Key=\"([^\"]*)\"[^>]*>", RegexOptions.IgnoreCase);
                if (matches.Count == 0)
                {
                    continue;
                }

                var list = new List <string>();

                foreach (Match match in matches)
                {
                    list.Add(match.Groups[1].ToString());
                }

                var placeholders = string.Join(", ", list.ToArray());

                output.Write(SeverityLevel.Suggestion, "Placeholders but no renderings", string.Format("The layout \"{0}\" in contains the placeholder(s) \"{1}\" but no renderings are assigned. When the layout is rendered the placeholders will be empty.", layoutFile, placeholders), "Either remove the placeholder (if they are not used by any other item) or assign renderings.", item);
            }
        }
        public override void WriteField([NotNull] Item item, [NotNull] Field field)
        {
            Assert.ArgumentNotNull(item, nameof(item));
            Assert.ArgumentNotNull(field, nameof(field));

            var value = GetFieldValuePipeline.Run().WithParameters(field).Value;

            if (string.IsNullOrEmpty(value))
            {
                return;
            }

            var layoutDefinition = LayoutDefinition.Parse(value);

            foreach (DeviceDefinition deviceDefinition in layoutDefinition.Devices)
            {
                if (string.IsNullOrEmpty(deviceDefinition.ID))
                {
                    continue;
                }

                var deviceItem = item.Database.GetItem(deviceDefinition.ID);
                if (deviceItem == null)
                {
                    continue;
                }

                var layoutDisplayName = string.Empty;
                var layoutIcon        = string.Empty;

                var layoutId = deviceDefinition.Layout;
                if (!string.IsNullOrEmpty(layoutId))
                {
                    LayoutItem layoutItem = item.Database.GetItem(layoutId);
                    if (layoutItem != null)
                    {
                        layoutDisplayName = layoutItem.DisplayName;
                        layoutIcon        = Images.GetThemedImageSource(layoutItem.Icon, ImageDimension.id16x16);
                    }
                }

                Writer.WriteStartElement(LayoutStruct.Node.Device);
                Writer.WriteAttributeString(LayoutStruct.Attribute.DisplayName, deviceItem.DisplayName);
                Writer.WriteAttributeString(LayoutStruct.Attribute.Icon, Images.GetThemedImageSource(deviceItem.Appearance.Icon, ImageDimension.id32x32));

                Writer.WriteStartElement(LayoutStruct.Node.Layout);
                Writer.WriteAttributeString(LayoutStruct.Attribute.DisplayName, layoutDisplayName);
                Writer.WriteAttributeString(LayoutStruct.Attribute.Icon, layoutIcon);

                var renderings     = deviceDefinition.Renderings;
                var renderingItems = new RenderingRecords(item.Database);
                foreach (var rendering in from object renref in renderings
                         where renref != null && renref.GetType() == typeof(RenderingDefinition)
                         select renref as RenderingDefinition)
                {
                    var renderingItem = renderingItems[rendering.ItemID];
                    if (renderingItem == null)
                    {
                        continue;
                    }

                    Writer.WriteStartElement(LayoutStruct.Node.Rendering);

                    Writer.WriteAttributeString(LayoutStruct.Attribute.DisplayName, renderingItem.DisplayName);
                    Writer.WriteAttributeString(LayoutStruct.Attribute.Icon, Images.GetThemedImageSource(renderingItem.Icon, ImageDimension.id16x16));
                    Writer.WriteAttributeString(LayoutStruct.Attribute.ItemId, rendering.ItemID);

                    Writer.WriteEndElement();
                }

                Writer.WriteEndElement();
                Writer.WriteEndElement();
            }
        }