<commit_before>﻿using System.Linq;
using Orchard.AuditTrail.Models;
using Orchard.AuditTrail.Services;
using Orchard.AuditTrail.ViewModels;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers;
using Orchard.Security;

namespace Orchard.AuditTrail.Drivers {
    public class AuditTrailSettingsPartDriver : ContentPartDriver<AuditTrailSettingsPart> {
        private readonly IAuditTrailManager _auditTrailManager;
        private readonly IAuthorizer _authorizer;

        public AuditTrailSettingsPartDriver(IAuditTrailManager auditTrailManager, IAuthorizer authorizer) {
            _auditTrailManager = auditTrailManager;
            _authorizer = authorizer;
        }

        protected override DriverResult Editor(AuditTrailSettingsPart part, dynamic shapeHelper) {
            return Editor(part, null, shapeHelper);
        }

        protected override DriverResult Editor(AuditTrailSettingsPart part, IUpdateModel updater, dynamic shapeHelper) {
            if (!_authorizer.Authorize(Permissions.ManageAuditTrailSettings))
                return null;

            return ContentShape("Parts_AuditTrailSettings_Edit", () => {
                var descriptors = _auditTrailManager.DescribeCategories();
                var eventSettings = part.EventSettings.ToList();
                var viewModel = new AuditTrailSettingsViewModel {
                    Categories = (from categoryDescriptor in descriptors
                        select new AuditTrailCategorySettingsViewModel {
                            Category = categoryDescriptor.Category,
                            Name = categoryDescriptor.Name,
                            Events = 
                                (from eventDescriptor in categoryDescriptor.Events
                                let eventSetting = eventSettings.FirstOrDefault(x => x.EventName == eventDescriptor.Event)
                                select new AuditTrailEventSettingsViewModel {
                                    Event = eventDescriptor.Event,
                                    Name = eventDescriptor.Name,
                                    Description = eventDescriptor.Description,
                                    IsEnabled = eventDescriptor.IsMandatory || (eventSetting != null ? eventSetting.IsEnabled : eventDescriptor.IsEnabledByDefault),
                                    IsMandatory = eventDescriptor.IsMandatory
                                }).ToList()
                        }).ToList()
                };

                if (updater != null) {
                    if (updater.TryUpdateModel(viewModel, Prefix, new[] { "IsEnabled" }, null)) {
                        foreach (var eventSettingViewModel in viewModel.Categories.SelectMany(x => x.Events)) {
                            var eventSetting = eventSettings.FirstOrDefault(x => x.EventName == eventSettingViewModel.Event);

                            if (eventSetting == null) {
                                eventSetting = new AuditTrailEventSetting { EventName = eventSettingViewModel.Event};
                                eventSettings.Add(eventSetting);
                            }

                            eventSetting.IsEnabled = eventSettingViewModel.IsEnabled || eventSettingViewModel.IsMandatory;
                        }
                        part.EventSettings = eventSettings;<commit_msg>Fixed a model binding issue.<commit_after>﻿using System.Linq;
using Orchard.AuditTrail.Models;
using Orchard.AuditTrail.Services;
using Orchard.AuditTrail.ViewModels;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers;
using Orchard.Security;

namespace Orchard.AuditTrail.Drivers {
    public class AuditTrailSettingsPartDriver : ContentPartDriver<AuditTrailSettingsPart> {
        private readonly IAuditTrailManager _auditTrailManager;
        private readonly IAuthorizer _authorizer;

        public AuditTrailSettingsPartDriver(IAuditTrailManager auditTrailManager, IAuthorizer authorizer) {
            _auditTrailManager = auditTrailManager;
            _authorizer = authorizer;
        }

        protected override DriverResult Editor(AuditTrailSettingsPart part, dynamic shapeHelper) {
            return Editor(part, null, shapeHelper);
        }

        protected override DriverResult Editor(AuditTrailSettingsPart part, IUpdateModel updater, dynamic shapeHelper) {
            if (!_authorizer.Authorize(Permissions.ManageAuditTrailSettings))
                return null;

            return ContentShape("Parts_AuditTrailSettings_Edit", () => {
                var descriptors = _auditTrailManager.DescribeCategories();
                var eventSettings = part.EventSettings.ToList();
                var viewModel = new AuditTrailSettingsViewModel {
                    Categories = (from categoryDescriptor in descriptors
                        select new AuditTrailCategorySettingsViewModel {
                            Category = categoryDescriptor.Category,
                            Name = categoryDescriptor.Name,
                            Events = 
                                (from eventDescriptor in categoryDescriptor.Events
                                let eventSetting = eventSettings.FirstOrDefault(x => x.EventName == eventDescriptor.Event)
                                select new AuditTrailEventSettingsViewModel {
                                    Event = eventDescriptor.Event,
                                    Name = eventDescriptor.Name,
                                    Description = eventDescriptor.Description,
                                    IsEnabled = eventDescriptor.IsMandatory || (eventSetting != null ? eventSetting.IsEnabled : eventDescriptor.IsEnabledByDefault),
                                    IsMandatory = eventDescriptor.IsMandatory
                                }).ToList()
                        }).ToList()
                };

                if (updater != null) {
                    if (updater.TryUpdateModel(viewModel, Prefix, null, null)) {
                        foreach (var eventSettingViewModel in viewModel.Categories.SelectMany(x => x.Events)) {
                            var eventSetting = eventSettings.FirstOrDefault(x => x.EventName == eventSettingViewModel.Event);

                            if (eventSetting == null) {
                                eventSetting = new AuditTrailEventSetting { EventName = eventSettingViewModel.Event};
                                eventSettings.Add(eventSetting);
                            }

                            eventSetting.IsEnabled = eventSettingViewModel.IsEnabled || eventSettingViewModel.IsMandatory;
                        }
                        part.EventSettings = eventSettings;
{'fraction_non_alphanumeric': 0.06200059189109204, 'fraction_numerical': 0.0, 'mean_word_length': 1.8080598255089322, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1894506', 'n_tokens_mistral': 1543, 'n_tokens_neox': 1443, 'n_words': 298}