Пример #1
0
 private List <int> GetExistingArticleIds(List <int> articlesIdList) => ArticleRepository.CheckForArticleExistence(articlesIdList, string.Empty, _contentId);
Пример #2
0
        private static void UpdateM2MValues(List <KeyValuePair <int, FieldValue> > values)
        {
            var m2MFields = new Dictionary <string, Field>();

            foreach (var fieldV in values)
            {
                if (!m2MFields.Keys.Contains(fieldV.Value.Field.Name))
                {
                    m2MFields.Add(fieldV.Value.Field.Name, fieldV.Value.Field);
                }
            }

            var doc   = new XDocument();
            var items = new XElement("items");

            doc.Add(items);

            foreach (var field in m2MFields.Values)
            {
                var m2MField  = field;
                var condition = m2MField.RelationCondition;
                var linkId    = m2MField.LinkId ?? 0;
                if (m2MField.RelateToContentId != null)
                {
                    var contentId      = m2MField.RelateToContentId.Value;
                    var filteredValues = values.Where(f => f.Value.Field.Name == m2MField.Name).ToArray();
                    var relatedIds     = filteredValues
                                         .Where(n => n.Value.NewRelatedItems != null)
                                         .SelectMany(n => n.Value.NewRelatedItems)
                                         .Distinct()
                                         .ToList();

                    var validatedIds = new HashSet <int>(ArticleRepository.CheckForArticleExistence(relatedIds, condition, contentId));
                    var grantedIds   = field.UseRelationSecurity
                        ? new HashSet <int>(ArticleRepository.CheckRelationSecurity(contentId, validatedIds.ToArray(), false).Where(n => n.Value).Select(m => m.Key))
                        : validatedIds;

                    foreach (var item in filteredValues)
                    {
                        var value = string.Empty;
                        if (item.Value.NewRelatedItems != null)
                        {
                            var notValidIds = item.Value.NewRelatedItems.Where(n => !validatedIds.Contains(n)).ToArray();
                            if (notValidIds.Any())
                            {
                                throw new ArgumentException(string.Format(ImportStrings.IncorrectM2M, item.Key, item.Value.Field.Name, string.Join(",", notValidIds)));
                            }

                            var notGrantedIds = item.Value.NewRelatedItems.Where(n => !grantedIds.Contains(n)).ToArray();
                            if (notGrantedIds.Any())
                            {
                                throw new ArgumentException(string.Format(ImportStrings.InaccessibleM2M, item.Key, item.Value.Field.Name, string.Join(",", notGrantedIds)));
                            }

                            value = string.Join(",", item.Value.NewRelatedItems);
                        }

                        var itemXml = new XElement("item");
                        itemXml.Add(new XAttribute("id", item.Key));
                        itemXml.Add(new XAttribute("linkId", linkId));
                        itemXml.Add(new XAttribute("value", value));
                        doc.Root?.Add(itemXml);
                    }
                }
            }

            ArticleRepository.UpdateM2MValues(doc.ToString(SaveOptions.None));
        }