Beispiel #1
0
        protected IIndexableItem GetIndexableItem(Index indexConfig, ContentType contentTypeConfig, IContentBase content)
        {
            var itemToIndex = new IndexableItem(content.Id, content.Name, content.GetContentType().Alias);

            Dictionary <string, object> fieldGroups = new Dictionary <string, object>();

            var propertiesToIndex = contentTypeConfig.IncludeAllProperties
                ? content.Properties.ToList()
                : content.Properties.Where(p => contentTypeConfig.Properties.Select(c => c.Alias).Contains(p.Alias));

            foreach (var property in propertiesToIndex)
            {
                var indexField = new IndexFieldEvent
                {
                    UmbracoProperty = property,
                    Value           = property.Value
                };

                var propertyConfig = contentTypeConfig.Properties.FirstOrDefault(p => p.Alias == property.Alias);

                //Execute the property Index Strategy if one is configured
                if (propertyConfig != null && !string.IsNullOrEmpty(propertyConfig.IndexStrategy))
                {
                    var strategy = IndexStrategyResolver.GetPropertyIndexStrategy(propertyConfig.IndexStrategy);

                    if (strategy != null)
                    {
                        strategy.Execute(indexField);
                    }
                }

                //Add the value to a group field if one is configured
                if (propertyConfig != null && !string.IsNullOrEmpty(propertyConfig.Group))
                {
                    if (fieldGroups.ContainsKey(propertyConfig.Group))
                    {
                        fieldGroups[propertyConfig.Group] += $" {indexField.Value}";
                    }
                    else
                    {
                        fieldGroups.Add(propertyConfig.Group, indexField.Value);
                    }
                }

                if (propertyConfig == null || (propertyConfig != null && !propertyConfig.ExcludeField))
                {
                    itemToIndex.AddOrUpdate(property.Alias, indexField.Value);
                }
            }

            foreach (var fieldGroup in fieldGroups)
            {
                itemToIndex.AddOrUpdate(fieldGroup.Key, fieldGroup.Value);
            }

            return(itemToIndex);
        }
        /// <summary>
        /// Stores a file.
        /// </summary>
        /// <param name="content"><see cref="IContentBase"/>A content item.</param>
        /// <param name="propertyTypeAlias">The property alias.</param>
        /// <param name="filename">The name of the file.</param>
        /// <param name="filestream">A stream containing the file data.</param>
        /// <param name="filepath">The original file path, if any.</param>
        /// <returns>The path to the file, relative to the media filesystem.</returns>
        /// <remarks>
        /// <para>Does NOT set the property value, so one should probably store the file and then do
        /// something alike: property.Value = MediaHelper.FileSystem.GetUrl(filepath).</para>
        /// <para>The original file path is used, in the old media file path scheme, to try and reuse
        /// the "folder number" that was assigned to the previous file referenced by the property,
        /// if any.</para>
        /// </remarks>
        public static string StoreFile(this IContentBase content, string propertyTypeAlias, string filename, Stream filestream, string filepath)
        {
            var propertyType = content.GetContentType()
                               .CompositionPropertyTypes.FirstOrDefault(x => x.Alias.InvariantEquals(propertyTypeAlias));

            if (propertyType == null)
            {
                throw new ArgumentException("Invalid property type alias " + propertyTypeAlias + ".");
            }
            return(FileSystemProviderManager.Current.MediaFileSystem.StoreFile(content, propertyType, filename, filestream, filepath));
        }
Beispiel #3
0
        public void Remove(IContentBase content)
        {
            var config = ConfigProvider.GetRootConfig();

            var indexer = LoadIndexer(config);

            foreach (var indexAlias in config.Indexes)
            {
                indexer.Remove(indexAlias, content.GetContentType().Alias, content.Id);
            }
        }
        /// <summary>
        /// Gets the url segment for a specified content and culture.
        /// </summary>
        /// <param name="content">The content.</param>
        /// <param name="culture">The culture.</param>
        /// <returns>
        /// The url segment.
        /// </returns>
        /// <remarks>
        /// This is for when Umbraco is capable of managing more than one url
        /// per content, in 1-to-1 multilingual configurations. Then there would be one
        /// url per culture.
        /// </remarks>
        public string GetUrlSegment(IContentBase content, CultureInfo culture)
        {
            if (content.GetContentType().Alias != "realEstate")
            {
                return(null);
            }

            var segment = _provider.GetUrlSegment(content);

            return($"{content.GetValue("type")}-{content.GetValue<string>("city")}-{segment}".ToUrlSegment());
        }
        /// <summary>
        /// Gets or creates a property for a content item.
        /// </summary>
        /// <param name="content">The content item.</param>
        /// <param name="propertyTypeAlias">The property type alias.</param>
        /// <returns>The property.</returns>
        private static Property GetProperty(IContentBase content, string propertyTypeAlias)
        {
            var property = content.Properties.FirstOrDefault(x => x.Alias.InvariantEquals(propertyTypeAlias));
            if (property != null) return property;

            var propertyType = content.GetContentType().CompositionPropertyTypes
                .FirstOrDefault(x => x.Alias.InvariantEquals(propertyTypeAlias));
            if (propertyType == null)
                throw new Exception("No property type exists with alias " + propertyTypeAlias + ".");

            property = new Property(propertyType);
            content.Properties.Add(property);
            return property;
        }
Beispiel #6
0
        public string GetUrlSegment(IContentBase content, CultureInfo culture)
        {
            // only for product doctypes
            var productContentType = PublishedContentModels.Product.ModelTypeAlias;

            if (content.GetContentType().Alias != productContentType)
            {
                return(null);
            }

            var segment = _provider.GetUrlSegment(content);

            // Watch the "ToUrlSegment()"
            var productName = content.GetValue <string>("productName")?.ToUrlSegment();

            return($"{segment}-{productName}");
        }
Beispiel #7
0
        /// <summary>
        /// Indexes a content item
        /// </summary>
        /// <param name="content">The content item</param>
        /// <param name="indexName">Optional index name to index specific index, ignoring others</param>
        public void Index(IContentBase content, Source source, string indexName = null)
        {
            var config = ConfigProvider.GetRootConfig();

            var indexer = LoadIndexer(config);

            var indexes = indexName != null ? new[] { indexName } : config.Indexes.ToArray();

            foreach (var indexAlias in indexes)
            {
                var indexConfig = ConfigProvider.GetIndexConfig(indexAlias);

                var contentTypeConfig = source == Source.Content
                    ? indexConfig.ContentTypes.FirstOrDefault(c => c.Alias == content.GetContentType().Alias)
                    : indexConfig.MediaTypes.FirstOrDefault(c => c.Alias == content.GetContentType().Alias);

                if (contentTypeConfig == null)
                {
                    contentTypeConfig = indexConfig.ContentTypes.FirstOrDefault(c => string.IsNullOrWhiteSpace(c.Alias));
                    if (contentTypeConfig == null)
                    {
                        continue;
                    }
                }

                var indexableItem = GetIndexableItem(indexConfig, contentTypeConfig, content);

                switch (source)
                {
                case Source.Content:
                    indexableItem.AddOrUpdate("_umbracoSource", "content");
                    break;

                case Source.Media:
                    indexableItem.AddOrUpdate("_umbracoSource", "media");
                    break;
                }

                var indexContentEvent = new IndexContentEvent
                {
                    Content   = content,
                    IndexItem = indexableItem,
                    Cancel    = false
                };
                foreach (var contentIndexStrategy in contentTypeConfig.IndexStrategies.Concat(indexConfig.IndexStrategies))
                {
                    var strategy = IndexStrategyResolver.GetContentIndexStrategy(contentIndexStrategy);
                    strategy.Execute(indexContentEvent);

                    if (indexContentEvent.Cancel)
                    {
                        break;
                    }
                }

                if (!indexContentEvent.Cancel)
                {
                    indexer.Index(indexConfig.Alias, indexableItem);
                }
            }
        }