<commit_before>﻿using lecoati.uMobile;
using lecoati.uMobile.umComponents;
using lecoati.uMobile.umCore;
using lecoati.uMobile.umHelpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Umbraco.Forms.Core;
using Umbraco.Forms.Core.Enums;
using Umbraco.Forms.Core.Services;
using Umbraco.Forms.Data.Storage;

namespace umobile4contour
{

    [umClass(category:"Contour", icon: lecoati.uMobile.umComponents.GenericIcon.ListAlt)]
    public class core : uMobile
    {

        enum Range
        { 
            hour,
            Day,
            Month,
            Year,
            Decade
        };

        enum Show
        {
            five = 5,
            ten = 10,
            fifty = 50,
            thousand = 100
        };
        
        [umMethod(title:"Forms", subtitle:"Form managment", icon: lecoati.uMobile.umComponents.GenericIcon.ListAlt)]
        public static string ListForm()
        {

            FormStorage fs = new FormStorage();
            System.Collections.Generic.List<Umbraco.Forms.Core.Form> forms = fs.GetAllForms();

            List calls = new List();

            foreach (Umbraco.Forms.Core.Form form in forms)
            {    
                calls.AddListItem(new ListItem(form.Name,
                subtitle: "Created: " + form.Created.ToString(),
                icon: GenericIcon.FolderClose,
                action: new Call("ListRecord", new string[] { form.Id.ToString(), "1" }))); 
            }

            return calls.UmGo();

        }

        [umMethod(visible: false, title: "Records")]
        public static string ListRecord(String formGuid, String pageStr)
        {

            FormState state = FormState.Approved;
            Range range = Range.Day;
            Show show = Show.five;
            int page = int.Parse(pageStr);

            if (Utils.GetPostParameter("State") != null && !string.IsNullOrEmpty(Utils.GetPostParameter("State").ToString()))
                HttpContext.Current.Session["State"] = Utils.GetPostParameter("State").ToString();

            if (Utils.GetPostParameter("Range") != null && !string.IsNullOrEmpty(Utils.GetPostParameter("Range").ToString()))
                HttpContext.Current.Session["Range"] = Utils.GetPostParameter("Range").ToString();

            if (Utils.GetPostParameter("Show") != null && !string.IsNullOrEmpty(Utils.GetPostParameter("Show").ToString()))
                HttpContext.Current.Session["Show"] = Utils.GetPostParameter("Show").ToString();

            if (HttpContext.Current.Session["State"] != null && !string.IsNullOrEmpty(HttpContext.Current.Session["State"].ToString()))
                state = (FormState)Enum.Parse(typeof(FormState), HttpContext.Current.Session["State"].ToString());

            if (HttpContext.Current.Session["Range"] != null && !string.IsNullOrEmpty(HttpContext.Current.Session["Range"].ToString()))
                range = (Range)Enum.Parse(typeof(Range), HttpContext.Current.Session["Range"].ToString());

            if (HttpContext.Current.Session["Show"] != null && !string.IsNullOrEmpty(HttpContext.Current.Session["Show"].ToString()))
                show = (Show)Enum.Parse(typeof(Show), HttpContext.Current.Session["Show"].ToString());

            FormStorage fs = new FormStorage();
            RecordsViewer rv = new RecordsViewer();

            RecordStorage recordStorage = new RecordStorage();

            Umbraco.Forms.Core.Form form = fs.GetForm(Guid.Parse(formGuid));

            System.Collections.Generic.List<Record> records = rv.GetRecords(0, 0, form, Sorting.descending, new List<FormState> { state });

            List calls = new List();

            calls.AddListItem(new ListItem("<b>Filter</b>",
                subtitle: state.ToString() + ", Last " + range.ToString() + ", " + (int)show + " records per page",
                icon: GenericIcon.Filter,
                action: new Call("ConfigFilter", new string[] { formGuid })));

            DateTime minDate = DateTime.Now;
            switch (range)
            {
                case Range.hour:
                    minDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, 0, 0);
                    break;

                case Range.Month:
                    minDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
                    break;

                case Range.Year:
                    minDate = new DateTime(DateTime.Now.Year, 1, 1);
                    break;

                case Range.Decade:
                    minDate = DateTime.MinValue;
                    break;

                case Range.Day:
                default:
                    minDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0);
                    break;
            }

            if (records.Where(r => r.Created > minDate).Any())
            {

                int total = records.Where(r => r.Created > minDate).Count();
                IEnumerable<Record> results = records.Where(r => r.Created > minDate).OrderByDescending(r => r.Created).Skip((page - 1) * (int)show).Take((int)show);

                Button nextButton = null;
                if (total > (results.Count() + (page - 1) * (int)show))
                    nextButton = new Button(icon: GenericIcon.ArrowRight, action: new Call("ListRecord", new string[] { formGuid, (page + 1).ToString() }));

                calls.AddListItem(new ListItem("<b>Total result:</b> " + total.ToString(), subtitle: "Page <b>" + page.ToString() + "</b> to <b>" + (((int)(total / (int)show)) + 1 ).ToString() + "</b>", contextual_button: nextButton));

                foreach (Record record in results)
                {
                    calls.AddListItem(new ListItem(record.Created.ToString(),
                    subtitle: "Ip: " + record.IP,
                    action: new Call("ViewData", new string[] { record.Id.ToString() })));
                }

            }

            return calls.UmGo();

        }

        [umMethod(visible: false, title: "Filter")]
        public static string ConfigFilter(String formGuid)
        {

            FormState state = FormState.Approved;
            Range range = Range.Day;
            Show show = Show.five;

            if (HttpContext.Current.Session["State"] != null && !string.IsNullOrEmpty(HttpContext.Current.Session["State"].ToString()))
                state = (FormState)Enum.Parse(typeof(FormState), HttpContext.Current.Session["State"].ToString());

            if (HttpContext.Current.Session["Range"] != null && !string.IsNullOrEmpty(HttpContext.Current.Session["Range"].ToString()))
                range = (Range)Enum.Parse(typeof(Range), HttpContext.Current.Session["Range"].ToString());

            if (HttpContext.Current.Session["Show"] != null && !string.IsNullOrEmpty(HttpContext.Current.Session["Show"].ToString()))
                show = (Show)Enum.Parse(typeof(Show), HttpContext.Current.Session["Show"].ToString());

            lecoati.uMobile.umComponents.Form form = new lecoati.uMobile.umComponents.Form();

            form.primary = new Button("Refresh", new Call("ListRecord", new string[] { formGuid, "1" }));

            FormFieldset fieldSet = new FormFieldset(title: "Filter by");
            form.AddFieldset(fieldSet);

            SelectField selectFilter = new SelectField("State", "State", state.ToString());
            foreach (FormState stateIndex in Enum.GetValues(typeof(FormState)))<commit_msg>item if no register found<commit_after>﻿using lecoati.uMobile;
using lecoati.uMobile.umComponents;
using lecoati.uMobile.umCore;
using lecoati.uMobile.umHelpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Umbraco.Forms.Core;
using Umbraco.Forms.Core.Enums;
using Umbraco.Forms.Core.Services;
using Umbraco.Forms.Data.Storage;

namespace umobile4contour
{

    [umClass(category:"Contour", icon: lecoati.uMobile.umComponents.GenericIcon.ListAlt)]
    public class core : uMobile
    {

        enum Range
        { 
            hour,
            Day,
            Month,
            Year,
            Decade
        };

        enum Show
        {
            five = 5,
            ten = 10,
            fifty = 50,
            thousand = 100
        };
        
        [umMethod(title:"Forms", subtitle:"Form managment", icon: lecoati.uMobile.umComponents.GenericIcon.ListAlt)]
        public static string ListForm()
        {

            FormStorage fs = new FormStorage();
            System.Collections.Generic.List<Umbraco.Forms.Core.Form> forms = fs.GetAllForms();

            List calls = new List();

            foreach (Umbraco.Forms.Core.Form form in forms)
            {    
                calls.AddListItem(new ListItem(form.Name,
                subtitle: "Created: " + form.Created.ToString(),
                icon: GenericIcon.FolderClose,
                action: new Call("ListRecord", new string[] { form.Id.ToString(), "1" }))); 
            }

            return calls.UmGo();

        }

        [umMethod(visible: false, title: "Records")]
        public static string ListRecord(String formGuid, String pageStr)
        {

            FormState state = FormState.Approved;
            Range range = Range.Day;
            Show show = Show.five;
            int page = int.Parse(pageStr);

            if (Utils.GetPostParameter("State") != null && !string.IsNullOrEmpty(Utils.GetPostParameter("State").ToString()))
                HttpContext.Current.Session["State"] = Utils.GetPostParameter("State").ToString();

            if (Utils.GetPostParameter("Range") != null && !string.IsNullOrEmpty(Utils.GetPostParameter("Range").ToString()))
                HttpContext.Current.Session["Range"] = Utils.GetPostParameter("Range").ToString();

            if (Utils.GetPostParameter("Show") != null && !string.IsNullOrEmpty(Utils.GetPostParameter("Show").ToString()))
                HttpContext.Current.Session["Show"] = Utils.GetPostParameter("Show").ToString();

            if (HttpContext.Current.Session["State"] != null && !string.IsNullOrEmpty(HttpContext.Current.Session["State"].ToString()))
                state = (FormState)Enum.Parse(typeof(FormState), HttpContext.Current.Session["State"].ToString());

            if (HttpContext.Current.Session["Range"] != null && !string.IsNullOrEmpty(HttpContext.Current.Session["Range"].ToString()))
                range = (Range)Enum.Parse(typeof(Range), HttpContext.Current.Session["Range"].ToString());

            if (HttpContext.Current.Session["Show"] != null && !string.IsNullOrEmpty(HttpContext.Current.Session["Show"].ToString()))
                show = (Show)Enum.Parse(typeof(Show), HttpContext.Current.Session["Show"].ToString());

            FormStorage fs = new FormStorage();
            RecordsViewer rv = new RecordsViewer();

            RecordStorage recordStorage = new RecordStorage();

            Umbraco.Forms.Core.Form form = fs.GetForm(Guid.Parse(formGuid));

            System.Collections.Generic.List<Record> records = rv.GetRecords(0, 0, form, Sorting.descending, new List<FormState> { state });

            List calls = new List();

            calls.AddListItem(new ListItem("<b>Filter</b>",
                subtitle: state.ToString() + ", Last " + range.ToString() + ", " + (int)show + " records per page",
                icon: GenericIcon.Filter,
                action: new Call("ConfigFilter", new string[] { formGuid })));

            DateTime minDate = DateTime.Now;
            switch (range)
            {
                case Range.hour:
                    minDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, 0, 0);
                    break;

                case Range.Month:
                    minDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
                    break;

                case Range.Year:
                    minDate = new DateTime(DateTime.Now.Year, 1, 1);
                    break;

                case Range.Decade:
                    minDate = DateTime.MinValue;
                    break;

                case Range.Day:
                default:
                    minDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0);
                    break;
            }

            if (records.Where(r => r.Created > minDate).Any())
            {

                int total = records.Where(r => r.Created > minDate).Count();
                IEnumerable<Record> results = records.Where(r => r.Created > minDate).OrderByDescending(r => r.Created).Skip((page - 1) * (int)show).Take((int)show);

                Button nextButton = null;
                if (total > (results.Count() + (page - 1) * (int)show))
                    nextButton = new Button(icon: GenericIcon.ArrowRight, action: new Call("ListRecord", new string[] { formGuid, (page + 1).ToString() }));

                calls.AddListItem(new ListItem("<b>Total result:</b> " + total.ToString(), subtitle: "Page <b>" + page.ToString() + "</b> to <b>" + (((int)(total / (int)show)) + 1).ToString() + "</b>", contextual_button: nextButton));

                foreach (Record record in results)
                {
                    calls.AddListItem(new ListItem(record.Created.ToString(),
                    subtitle: "Ip: " + record.IP,
                    action: new Call("ViewData", new string[] { record.Id.ToString() })));
                }

            }
            else
            {
                calls.AddListItem(new ListItem("<b>Total result:</b> 0", subtitle: "Page <b>0</b> to <b>0</b>"));
            }

            return calls.UmGo();

        }

        [umMethod(visible: false, title: "Filter")]
        public static string ConfigFilter(String formGuid)
        {

            FormState state = FormState.Approved;
            Range range = Range.Day;
            Show show = Show.five;

            if (HttpContext.Current.Session["State"] != null && !string.IsNullOrEmpty(HttpContext.Current.Session["State"].ToString()))
                state = (FormState)Enum.Parse(typeof(FormState), HttpContext.Current.Session["State"].ToString());

            if (HttpContext.Current.Session["Range"] != null && !string.IsNullOrEmpty(HttpContext.Current.Session["Range"].ToString()))
                range = (Range)Enum.Parse(typeof(Range), HttpContext.Current.Session["Range"].ToString());

            if (HttpContext.Current.Session["Show"] != null && !string.IsNullOrEmpty(HttpContext.Current.Session["Show"].ToString()))
                show = (Show)Enum.Parse(typeof(Show), HttpContext.Current.Session["Show"].ToString());

            lecoati.uMobile.umComponents.Form form = new lecoati.uMobile.umComponents.Form();

            form.primary = new Button("Refresh", new Call("ListRecord", new string[] { formGuid, "1" }));

            FormFieldset fieldSet = new FormFieldset(title: "Filter by");
            form.AddFieldset(fieldSet);

            SelectField selectFilter = new SelectField("State", "State", state.ToString());
            foreach (FormState stateIndex in Enum.GetValues(typeof(FormState)))
{'fraction_non_alphanumeric': 0.1365609788185765, 'fraction_numerical': 0.00348638337060913, 'mean_word_length': 2.1898866974402016, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 49, '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': '5717305', 'n_tokens_mistral': 4142, 'n_tokens_neox': 3948, 'n_words': 796}