<commit_before>using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Umbraco.Core;
using Umbraco.Core.IO;
using Umbraco.Core.Models;
using umbraco.BasePages;
using umbraco.cms.businesslogic;
using umbraco.cms.businesslogic.datatype;
using umbraco.cms.businesslogic.propertytype;
using umbraco.cms.businesslogic.web;
using umbraco.interfaces;
using umbraco.uicontrols;
using Content = umbraco.cms.businesslogic.Content;
using ContentType = umbraco.cms.businesslogic.ContentType;
using Media = umbraco.cms.businesslogic.media.Media;
using Property = umbraco.cms.businesslogic.property.Property;
using StylesheetProperty = umbraco.cms.businesslogic.web.StylesheetProperty;

namespace umbraco.controls
{
    public class ContentControlLoadEventArgs : CancelEventArgs { }

    /// <summary>
    /// Summary description for ContentControl.
    /// </summary>
    public class ContentControl : TabView
    {

        
        internal Dictionary<string, IDataType> DataTypes = new Dictionary<string, IDataType>();
        private readonly Content _content;
        private UmbracoEnsuredPage _prntpage;
        public event EventHandler SaveAndPublish;
        public event EventHandler SaveToPublish;
        public event EventHandler Save;
        private readonly publishModes _canPublish = publishModes.NoPublish;
        public TabPage tpProp;
        public bool DoesPublish = false;
        public TextBox NameTxt = new TextBox();
        public PlaceHolder NameTxtHolder = new PlaceHolder();
        public RequiredFieldValidator NameTxtValidator = new RequiredFieldValidator();
        private readonly CustomValidator _nameTxtCustomValidator = new CustomValidator();
        private static readonly string UmbracoPath = SystemDirectories.Umbraco;
        public Pane PropertiesPane = new Pane();
        // zb-00036 #29889 : load it only once
        List<ContentType.TabI> _virtualTabs;
        //default to true!
        private bool _savePropertyDataWhenInvalid = true;
        private ContentType _contentType;

        
        public Content ContentObject
        {
            get { return _content; }
        }

        /// <summary>
        /// This property controls whether the content property values are persisted even if validation 
        /// fails. If set to false, then the values will not be persisted.
        /// </summary>
        /// <remarks>
        /// This is required because when we are editing content we should be persisting invalid values to the database
        /// as this makes it easier for editors to come back and fix up their changes before they publish. Of course we
        /// don't publish if the page is invalid. In the case of media and members, we don't want to persist the values
        /// to the database when the page is invalid because there is no published state.
        /// Relates to: http://issues.umbraco.org/issue/U4-227
        /// </remarks>
        public bool SavePropertyDataWhenInvalid
        {
            get { return _savePropertyDataWhenInvalid; }
            set { _savePropertyDataWhenInvalid = value; }
        }

        [Obsolete("This is no longer used and will be removed from the codebase in future versions")]
        private string _errorMessage = "";

        [Obsolete("This is no longer used and will be removed from the codebase in future versions")]
        public string ErrorMessage
        {
            set { _errorMessage = value; }
        }

        [Obsolete("This is no longer used and will be removed from the codebase in future versions")]
        protected void standardSaveAndPublishHandler(object sender, EventArgs e)
        {
        }

        
        /// <summary>
        /// Constructor to set default properties.
        /// </summary>
        /// <param name="c"></param>
        /// <param name="CanPublish"></param>
        /// <param name="Id"></param>
        /// <remarks>
        /// This method used to create all of the child controls too which is BAD since
        /// the page hasn't started initializing yet. Control IDs were not being named
        /// correctly, etc... I've moved the child control setup/creation to the CreateChildControls
        /// method where they are suposed to be.
        /// </remarks>
        public ContentControl(Content c, publishModes CanPublish, string Id)
        {
            ID = Id;
            this._canPublish = CanPublish;
            _content = c;

            Width = 350;
            Height = 350;

            _prntpage = (UmbracoEnsuredPage)Page;

            // zb-00036 #29889 : load it only once
            if (_virtualTabs == null)
                _virtualTabs = _content.ContentType.getVirtualTabs.ToList();

            foreach (ContentType.TabI t in _virtualTabs)
            {
                TabPage tp = NewTabPage(t.Caption);
                AddSaveAndPublishButtons(ref tp);
            }
        }

        /// <summary>
        /// Create and setup all of the controls child controls.
        /// </summary>
        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            
            _prntpage = (UmbracoEnsuredPage)Page;
            int i = 0;
            Hashtable inTab = new Hashtable();

            // zb-00036 #29889 : load it only once
            if (_virtualTabs == null)
                _virtualTabs = _content.ContentType.getVirtualTabs.ToList();

            if(_contentType == null)
                _contentType = ContentType.GetContentType(_content.ContentType.Id);

            foreach (ContentType.TabI tab in _virtualTabs)
            {
                var tabPage = this.Panels[i] as TabPage;
                if (tabPage == null)
                {
                    throw new ArgumentException("Unable to load tab \"" + tab.Caption + "\"");
                }

                tabPage.Style.Add("text-align", "center");

                //Legacy vs New API loading of PropertyTypes
                if (_contentType.ContentTypeItem != null)
                {
                    LoadPropertyTypes(_contentType.ContentTypeItem, tabPage, inTab, tab.Id, tab.Caption);
                }
                else
                {
                    LoadPropertyTypes(tab, tabPage, inTab);
                }

                i++;
            }

            // Add property pane
            tpProp = NewTabPage(ui.Text("general", "properties", null));
            AddSaveAndPublishButtons(ref tpProp);
            tpProp.Controls.Add(
                new LiteralControl("<div id=\"errorPane_" + tpProp.ClientID +
                                   "\" style=\"display: none; text-align: left; color: red;width: 100%; border: 1px solid red; background-color: #FCDEDE\"><div><b>There were errors - data has not been saved!</b><br/></div></div>"));

            //if the property is not in a tab, add it to the general tab
            var props = _content.GenericProperties;
            foreach (Property p in props)
            {
                if (inTab[p.PropertyType.Id.ToString()] == null)
                    AddControlNew(p, tpProp, ui.Text("general", "properties", null));
            }

        }

        /// <summary>
        /// Loades PropertyTypes by Tab/PropertyGroup using the new API.
        /// </summary>
        /// <param name="contentType"></param>
        /// <param name="tabPage"></param>
        /// <param name="inTab"></param><commit_msg>Fixes issue U4-2757 where generic properties are not ordered correctly.<commit_after>using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Umbraco.Core;
using Umbraco.Core.IO;
using Umbraco.Core.Models;
using umbraco.BasePages;
using umbraco.cms.businesslogic;
using umbraco.cms.businesslogic.datatype;
using umbraco.cms.businesslogic.propertytype;
using umbraco.cms.businesslogic.web;
using umbraco.interfaces;
using umbraco.uicontrols;
using Content = umbraco.cms.businesslogic.Content;
using ContentType = umbraco.cms.businesslogic.ContentType;
using Media = umbraco.cms.businesslogic.media.Media;
using Property = umbraco.cms.businesslogic.property.Property;
using StylesheetProperty = umbraco.cms.businesslogic.web.StylesheetProperty;

namespace umbraco.controls
{
    public class ContentControlLoadEventArgs : CancelEventArgs { }

    /// <summary>
    /// Summary description for ContentControl.
    /// </summary>
    public class ContentControl : TabView
    {

        
        internal Dictionary<string, IDataType> DataTypes = new Dictionary<string, IDataType>();
        private readonly Content _content;
        private UmbracoEnsuredPage _prntpage;
        public event EventHandler SaveAndPublish;
        public event EventHandler SaveToPublish;
        public event EventHandler Save;
        private readonly publishModes _canPublish = publishModes.NoPublish;
        public TabPage tpProp;
        public bool DoesPublish = false;
        public TextBox NameTxt = new TextBox();
        public PlaceHolder NameTxtHolder = new PlaceHolder();
        public RequiredFieldValidator NameTxtValidator = new RequiredFieldValidator();
        private readonly CustomValidator _nameTxtCustomValidator = new CustomValidator();
        private static readonly string UmbracoPath = SystemDirectories.Umbraco;
        public Pane PropertiesPane = new Pane();
        // zb-00036 #29889 : load it only once
        List<ContentType.TabI> _virtualTabs;
        //default to true!
        private bool _savePropertyDataWhenInvalid = true;
        private ContentType _contentType;

        
        public Content ContentObject
        {
            get { return _content; }
        }

        /// <summary>
        /// This property controls whether the content property values are persisted even if validation 
        /// fails. If set to false, then the values will not be persisted.
        /// </summary>
        /// <remarks>
        /// This is required because when we are editing content we should be persisting invalid values to the database
        /// as this makes it easier for editors to come back and fix up their changes before they publish. Of course we
        /// don't publish if the page is invalid. In the case of media and members, we don't want to persist the values
        /// to the database when the page is invalid because there is no published state.
        /// Relates to: http://issues.umbraco.org/issue/U4-227
        /// </remarks>
        public bool SavePropertyDataWhenInvalid
        {
            get { return _savePropertyDataWhenInvalid; }
            set { _savePropertyDataWhenInvalid = value; }
        }

        [Obsolete("This is no longer used and will be removed from the codebase in future versions")]
        private string _errorMessage = "";

        [Obsolete("This is no longer used and will be removed from the codebase in future versions")]
        public string ErrorMessage
        {
            set { _errorMessage = value; }
        }

        [Obsolete("This is no longer used and will be removed from the codebase in future versions")]
        protected void standardSaveAndPublishHandler(object sender, EventArgs e)
        {
        }

        
        /// <summary>
        /// Constructor to set default properties.
        /// </summary>
        /// <param name="c"></param>
        /// <param name="CanPublish"></param>
        /// <param name="Id"></param>
        /// <remarks>
        /// This method used to create all of the child controls too which is BAD since
        /// the page hasn't started initializing yet. Control IDs were not being named
        /// correctly, etc... I've moved the child control setup/creation to the CreateChildControls
        /// method where they are suposed to be.
        /// </remarks>
        public ContentControl(Content c, publishModes CanPublish, string Id)
        {
            ID = Id;
            this._canPublish = CanPublish;
            _content = c;

            Width = 350;
            Height = 350;

            _prntpage = (UmbracoEnsuredPage)Page;

            // zb-00036 #29889 : load it only once
            if (_virtualTabs == null)
                _virtualTabs = _content.ContentType.getVirtualTabs.ToList();

            foreach (ContentType.TabI t in _virtualTabs)
            {
                TabPage tp = NewTabPage(t.Caption);
                AddSaveAndPublishButtons(ref tp);
            }
        }

        /// <summary>
        /// Create and setup all of the controls child controls.
        /// </summary>
        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            
            _prntpage = (UmbracoEnsuredPage)Page;
            int i = 0;
            Hashtable inTab = new Hashtable();

            // zb-00036 #29889 : load it only once
            if (_virtualTabs == null)
                _virtualTabs = _content.ContentType.getVirtualTabs.ToList();

            if(_contentType == null)
                _contentType = ContentType.GetContentType(_content.ContentType.Id);

            foreach (ContentType.TabI tab in _virtualTabs)
            {
                var tabPage = this.Panels[i] as TabPage;
                if (tabPage == null)
                {
                    throw new ArgumentException("Unable to load tab \"" + tab.Caption + "\"");
                }

                tabPage.Style.Add("text-align", "center");

                //Legacy vs New API loading of PropertyTypes
                if (_contentType.ContentTypeItem != null)
                {
                    LoadPropertyTypes(_contentType.ContentTypeItem, tabPage, inTab, tab.Id, tab.Caption);
                }
                else
                {
                    LoadPropertyTypes(tab, tabPage, inTab);
                }

                i++;
            }

            // Add property pane
            tpProp = NewTabPage(ui.Text("general", "properties", null));
            AddSaveAndPublishButtons(ref tpProp);
            tpProp.Controls.Add(
                new LiteralControl("<div id=\"errorPane_" + tpProp.ClientID +
                                   "\" style=\"display: none; text-align: left; color: red;width: 100%; border: 1px solid red; background-color: #FCDEDE\"><div><b>There were errors - data has not been saved!</b><br/></div></div>"));

            //if the property is not in a tab, add it to the general tab
            var props = _content.GenericProperties;
            foreach (Property p in props.OrderBy(x => x.PropertyType.SortOrder))
            {
                if (inTab[p.PropertyType.Id.ToString()] == null)
                    AddControlNew(p, tpProp, ui.Text("general", "properties", null));
            }

        }

        /// <summary>
        /// Loades PropertyTypes by Tab/PropertyGroup using the new API.
        /// </summary>
        /// <param name="contentType"></param>
        /// <param name="tabPage"></param>
        /// <param name="inTab"></param>
{'fraction_non_alphanumeric': 0.09107575459962025, 'fraction_numerical': 0.006220127021541282, 'mean_word_length': 2.03356504468719, 'pattern_counts': {'":': 0, '<': 75, '<?xml version=': 0, '>': 76, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '983601', 'n_tokens_mistral': 4055, 'n_tokens_neox': 3854, 'n_words': 1209}