bvcms /CmsWeb/Code/StandardExtraValues.cs

Language C# Lines 221
MD5 Hash 61b84a2efe0906ae746f231af1228dc3
Repository https://github.com/vs06/bvcms.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Xml.Linq;
using CmsData;
using System.Web.Mvc;
using System.Xml.Serialization;
using System.IO;
using Newtonsoft.Json;
using NPOI.SS.Formula.Functions;
using UtilityExtensions;

namespace CmsWeb.Code
{
    public class StandardExtraValues
    {
        [Serializable]
        public class Fields
        {
            [XmlElement("Field")]
            public Field[] fields { get; set; }
        }
        [Serializable]
        public class Field
        {
            [XmlAttribute]
            public string name { get; set; }
            [XmlAttribute]
            public string type { get; set; }
            [XmlAttribute]
            public string location { get; set; }
            [XmlAttribute]
            public string VisibilityRoles { get; set; }
            public List<string> Codes { get; set; }
            internal int order;
            public int peopleid;
            public bool nonstandard;

            internal PeopleExtra extravalue;
            internal static Field AddField(Field f, PeopleExtra v)
            {
                if (f == null)
                {
                    f = new Field
                    {
                        name = v.Field,
                        nonstandard = true,
                        peopleid = v.PeopleId,
                        extravalue = v,
                    };
                    f.type = v.StrValue.HasValue() ? "Code"
                        : v.Data.HasValue() ? "Data"
                        : v.DateValue.HasValue ? "Date"
                        : v.IntValue.HasValue ? "Int"
                        : v.BitValue.HasValue ? "Bit"
                        : "Code";
                }
                f.extravalue = v;
                return f;
            }
            public bool UserCanView()
            {
                if (!VisibilityRoles.HasValue())
                    return true;
                var a = VisibilityRoles.SplitStr(",");
                var user = HttpContext.Current.User;
				foreach (var role in a)
					if (user.IsInRole(role.Trim()))
						return true;
				return false;
            }
            public bool UserCanEdit()
            {
                var user = HttpContext.Current.User;
                return user.IsInRole("Edit");
            }
            public override string ToString()
            {
                if (extravalue == null && type != "Bits")
                    return "Click to edit";
                switch (type)
                {
                    case "Code":
                        return extravalue.StrValue;
                    case "Data":
                        return extravalue.Data;
                    case "Date":
                        return extravalue.DateValue.FormatDate();
                    case "Bit":
                        return extravalue.BitValue.ToString();
                    case "Bits":
                        {
                            var q = from e in DbUtil.Db.PeopleExtras
                                    where e.BitValue == true
                                    where e.PeopleId == peopleid
                                    where Codes.Contains(e.Field)
                                    select e.Field;
                            return string.Join(",", q);
                        }
                    case "Int":
                        if (extravalue.IntValue2.HasValue)
                            return "{0} {1}".Fmt(extravalue.IntValue, extravalue.IntValue2);
                        return extravalue.IntValue.ToString();
                }
				return null;
            }

                }
        public static IEnumerable<Field> GetExtraValues()
        {
            if (DbUtil.Db.Setting("UseStandardExtraValues", "false") != "true")
				return new List<Field>();
            var xml = DbUtil.StandardExtraValues();
            var sr = new StringReader(xml);
			var fields = (new XmlSerializer(typeof(Fields)).Deserialize(sr) as Fields).fields;
			if (fields == null)
				return new List<Field>();
			return fields;
        }


        public static IEnumerable<Field> GetExtraValues(int PeopleId, string location = "default")
        {
			var fields = GetExtraValues();

            var n = 1;
			foreach (var f in fields)
            {
                f.order = n++;
                f.peopleid = PeopleId;
                if (f.location == null)
                    f.location = "default";
            }

			var exvalues = DbUtil.Db.PeopleExtras.Where(ee => ee.PeopleId == PeopleId).ToList();


			var qfields = from f in fields
						  join v in exvalues on f.name equals v.Field into j
                           from v in j.DefaultIfEmpty()
						  where f.location == location || location == null
                           orderby f.order
                           select Field.AddField(f, v);
			if (location == "default")
            {
				var qvalues = from v in exvalues
							  join f in fields on v.Field equals f.name into j
                        from f in j.DefaultIfEmpty()
                        where f == null
							  where !fields.Any(ff => ff.Codes.Any(cc => cc == v.Field))
                        orderby v.Field
                        select Field.AddField(f, v);
				return qfields.Concat(qvalues);
            }
			return qfields;
        }
        public static List<SelectListItem> ExtraValueCodes()
        {
            var q = from e in DbUtil.Db.PeopleExtras
                    where e.StrValue != null || e.BitValue != null
                    group e by new { e.Field, val = e.StrValue ?? (e.BitValue == true ? "1" : "0") }
                        into g
                        select g.Key;
            var list = q.ToList();

            var ev = GetExtraValues();
            var q2 = from e in list
                     let f = ev.SingleOrDefault(ff => ff.name == e.Field)
                     where f == null || f.UserCanView()
                     orderby e.Field, e.val
                     select new SelectListItem()
                            {
                                Text = e.Field + ":" + e.val,
                                Value = e.Field + ":" + e.val,
                            };
            return q2.ToList();
        }
        public static List<SelectListItem> FamilyExtraValueCodes()
        {
            var q = from e in DbUtil.Db.FamilyExtras
                    where e.StrValue != null || e.BitValue != null
                    group e by new { e.Field, val = e.StrValue ?? (e.BitValue == true ? "1" : "0") }
                        into g
                        select g.Key;
            var list = q.ToList();

            var ev = GetExtraValues();
            var q2 = from e in list
                     let f = ev.SingleOrDefault(ff => ff.name == e.Field)
                     where f == null || f.UserCanView()
                     orderby e.Field, e.val
                     select new SelectListItem()
                            {
                                Text = e.Field + ":" + e.val,
                                Value = e.Field + ":" + e.val,
                            };
            return q2.ToList();
        }
        public static Dictionary<string, string> Codes(string name)
        {
            var f = GetExtraValues().Single(ee => ee.name == name);
            return f.Codes.ToDictionary(ee => ee, ee => ee);
        }

        public static Dictionary<string, bool> ExtraValueBits(string name, int PeopleId)
        {
            var f = GetExtraValues().Single(ee => ee.name == name);
            var list = DbUtil.Db.PeopleExtras.Where(pp => pp.PeopleId == PeopleId && f.Codes.Contains(pp.Field)).ToList();
            var q = from c in f.Codes
                    join e in list on c equals e.Field into j
                    from e in j.DefaultIfEmpty()
                    select new { value = c, selected = (e != null && (e.BitValue ?? false)) };
            return q.ToDictionary(ee => ee.value, ee => ee.selected);
        }
        }



}
Back to Top