<commit_before>﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using DotNetNuke.Common;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;
using DotNetNuke.Entities.Icons;
using R7.University;
using R7.University.Extensions;

namespace R7.University.Launchpad
{
	public partial class EditEduProgram : LaunchpadPortalModuleBase
	{
        #region Types

        public enum EditEduProgramTab { Common, Documents };

        #endregion

        #region Bindable icons

        protected string EditIconUrl
        {
            get { return IconController.IconURL ("Edit"); }
        }

        protected string DeleteIconUrl
        {
            get { return IconController.IconURL ("Delete"); }
        }

        #endregion

        protected EditEduProgramTab SelectedTab
        {
            get 
            {
                // get postback initiator
                var eventTarget = Request.Form ["__EVENTTARGET"];

                // document URL control is on Documents tab
                if (!string.IsNullOrEmpty (eventTarget) && eventTarget.Contains ("$" + urlDocumentUrl.ID +"$"))
                {
                    ViewState ["SelectedTab"] = EditEduProgramTab.Documents;
                    return EditEduProgramTab.Documents;
                }

                // otherwise, get current tab from viewstate
                var obj = ViewState ["SelectedTab"];
                return (obj != null) ? (EditEduProgramTab) obj : EditEduProgramTab.Common;
            }
            set { ViewState ["SelectedTab"] = value; }
        }

        private ViewModelContext viewModelContext;
        protected ViewModelContext ViewModelContext
        {
            get { return viewModelContext ?? (viewModelContext = new ViewModelContext (this)); }
        }

		// ALT: private int itemId = Null.NullInteger;
		private int? itemId = null;

        #region Documents & document types

        protected void RememberDocumentTypes (IEnumerable<DocumentTypeInfo> documentTypes)
        {
            ViewState ["documentTypes"] = documentTypes.ToList ();
        }

        protected string GetDocumentType (int? documentTypeId)
        {
            if (documentTypeId != null)
            {
                var documentTypes = (List<DocumentTypeInfo>) ViewState ["documentTypes"];
                return documentTypes.Single (dt => dt.DocumentTypeID == documentTypeId.Value).Type;
            }

            return string.Empty;
        }

        private List<DocumentInfo> GetDocuments ()
        {
            if (ViewStateDocuments != null)
            {
                return ViewStateDocuments.Select (dvm => dvm.NewDocumentInfo ()).ToList ();
            }

            return new List<DocumentInfo> ();
        }

        protected List<DocumentViewModel> ViewStateDocuments
        {
            get { return XmlSerializationHelper.Deserialize<List<DocumentViewModel>> (ViewState ["documents"]); }
            set { ViewState ["documents"] = XmlSerializationHelper.Serialize<List<DocumentViewModel>> (value); }
        }

        #endregion

		#region Handlers

		/// <summary>
		/// Handles Init event for a control.
		/// </summary>
		/// <param name="e">Event args.</param>
		protected override void OnInit (EventArgs e)
		{
			base.OnInit (e);

			// set url for Cancel link
			linkCancel.NavigateUrl = Globals.NavigateURL ();

			// add confirmation dialog to delete button
			buttonDelete.Attributes.Add ("onClick", "javascript:return confirm('" + Localization.GetString ("DeleteItem") + "');");

			// bind education levels
            comboEduLevel.DataSource = LaunchpadController.GetObjects<EduLevelInfo> ();
            comboEduLevel.DataBind ();

            // bind document types
            var documentTypes = LaunchpadController.GetObjects<DocumentTypeInfo> ();
            RememberDocumentTypes (documentTypes);

            comboDocumentType.DataSource = DocumentTypeViewModel.GetBindableList (documentTypes, LocalResourceFile, true);
            comboDocumentType.DataBind ();
           
            gridDocuments.LocalizeColumns (LocalResourceFile);
		}

		/// <summary>
		/// Handles Load event for a control.
		/// </summary>
		/// <param name="e">Event args.</param>
		protected override void OnLoad (EventArgs e)
		{
			base.OnLoad (e);
            	
			try
			{
				// parse querystring parameters
				itemId = Utils.ParseToNullableInt (Request.QueryString ["eduprogram_id"]);
      
				if (!IsPostBack)
				{
					// load the data into the control the first time we hit this page

					// check we have an item to lookup
					// ALT: if (!Null.IsNull (itemId) 
					if (itemId.HasValue)
					{
						// load the item
						var item = LaunchpadController.Get<EduProgramInfo> (itemId.Value);

						if (item != null)
						{
							textCode.Text = item.Code;
							textTitle.Text = item.Title;
                            textGeneration.Text = item.Generation;
                            dateAccreditedToDate.SelectedDate = item.AccreditedToDate;
                            datetimeStartDate.SelectedDate = item.StartDate;
                            datetimeEndDate.SelectedDate = item.EndDate;
                            Utils.SelectByValue (comboEduLevel, item.EduLevelID.ToString ());<commit_msg>Enable AJAX, if not already enabled<commit_after>﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using DotNetNuke.Common;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;
using DotNetNuke.Entities.Icons;
using R7.University;
using R7.University.Extensions;

namespace R7.University.Launchpad
{
	public partial class EditEduProgram : LaunchpadPortalModuleBase
	{
        #region Types

        public enum EditEduProgramTab { Common, Documents };

        #endregion

        #region Bindable icons

        protected string EditIconUrl
        {
            get { return IconController.IconURL ("Edit"); }
        }

        protected string DeleteIconUrl
        {
            get { return IconController.IconURL ("Delete"); }
        }

        #endregion

        protected EditEduProgramTab SelectedTab
        {
            get 
            {
                // get postback initiator
                var eventTarget = Request.Form ["__EVENTTARGET"];

                // document URL control is on Documents tab
                if (!string.IsNullOrEmpty (eventTarget) && eventTarget.Contains ("$" + urlDocumentUrl.ID +"$"))
                {
                    ViewState ["SelectedTab"] = EditEduProgramTab.Documents;
                    return EditEduProgramTab.Documents;
                }

                // otherwise, get current tab from viewstate
                var obj = ViewState ["SelectedTab"];
                return (obj != null) ? (EditEduProgramTab) obj : EditEduProgramTab.Common;
            }
            set { ViewState ["SelectedTab"] = value; }
        }

        private ViewModelContext viewModelContext;
        protected ViewModelContext ViewModelContext
        {
            get { return viewModelContext ?? (viewModelContext = new ViewModelContext (this)); }
        }

		// ALT: private int itemId = Null.NullInteger;
		private int? itemId = null;

        #region Documents & document types

        protected void RememberDocumentTypes (IEnumerable<DocumentTypeInfo> documentTypes)
        {
            ViewState ["documentTypes"] = documentTypes.ToList ();
        }

        protected string GetDocumentType (int? documentTypeId)
        {
            if (documentTypeId != null)
            {
                var documentTypes = (List<DocumentTypeInfo>) ViewState ["documentTypes"];
                return documentTypes.Single (dt => dt.DocumentTypeID == documentTypeId.Value).Type;
            }

            return string.Empty;
        }

        private List<DocumentInfo> GetDocuments ()
        {
            if (ViewStateDocuments != null)
            {
                return ViewStateDocuments.Select (dvm => dvm.NewDocumentInfo ()).ToList ();
            }

            return new List<DocumentInfo> ();
        }

        protected List<DocumentViewModel> ViewStateDocuments
        {
            get { return XmlSerializationHelper.Deserialize<List<DocumentViewModel>> (ViewState ["documents"]); }
            set { ViewState ["documents"] = XmlSerializationHelper.Serialize<List<DocumentViewModel>> (value); }
        }

        #endregion

		#region Handlers

		/// <summary>
		/// Handles Init event for a control.
		/// </summary>
		/// <param name="e">Event args.</param>
		protected override void OnInit (EventArgs e)
		{
			base.OnInit (e);

			// set url for Cancel link
			linkCancel.NavigateUrl = Globals.NavigateURL ();

			// add confirmation dialog to delete button
			buttonDelete.Attributes.Add ("onClick", "javascript:return confirm('" + Localization.GetString ("DeleteItem") + "');");

			// bind education levels
            comboEduLevel.DataSource = LaunchpadController.GetObjects<EduLevelInfo> ();
            comboEduLevel.DataBind ();

            // bind document types
            var documentTypes = LaunchpadController.GetObjects<DocumentTypeInfo> ();
            RememberDocumentTypes (documentTypes);

            comboDocumentType.DataSource = DocumentTypeViewModel.GetBindableList (documentTypes, LocalResourceFile, true);
            comboDocumentType.DataBind ();
           
            gridDocuments.LocalizeColumns (LocalResourceFile);
		}

		/// <summary>
		/// Handles Load event for a control.
		/// </summary>
		/// <param name="e">Event args.</param>
		protected override void OnLoad (EventArgs e)
		{
			base.OnLoad (e);
            	
            if (DotNetNuke.Framework.AJAX.IsInstalled ())
                DotNetNuke.Framework.AJAX.RegisterScriptManager ();
            
			try
			{
				// parse querystring parameters
				itemId = Utils.ParseToNullableInt (Request.QueryString ["eduprogram_id"]);
      
				if (!IsPostBack)
				{
					// load the data into the control the first time we hit this page

					// check we have an item to lookup
					// ALT: if (!Null.IsNull (itemId) 
					if (itemId.HasValue)
					{
						// load the item
						var item = LaunchpadController.Get<EduProgramInfo> (itemId.Value);

						if (item != null)
						{
							textCode.Text = item.Code;
							textTitle.Text = item.Title;
                            textGeneration.Text = item.Generation;
                            dateAccreditedToDate.SelectedDate = item.AccreditedToDate;
                            datetimeStartDate.SelectedDate = item.StartDate;
                            datetimeEndDate.SelectedDate = item.EndDate;
                            Utils.SelectByValue (comboEduLevel, item.EduLevelID.ToString ());
{'fraction_non_alphanumeric': 0.09365475200886672, 'fraction_numerical': 0.0005541701302299807, 'mean_word_length': 2.093714285714286, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3512042', 'n_tokens_mistral': 3054, 'n_tokens_neox': 2710, 'n_words': 658}