PageRenderTime 34ms CodeModel.GetById 16ms app.highlight 8ms RepoModel.GetById 8ms app.codeStats 0ms

/CmsWeb/Code/StandardExtraValues.cs

https://github.com/vs06/bvcms
C# | 221 lines | 206 code | 15 blank | 0 comment | 46 complexity | 61b84a2efe0906ae746f231af1228dc3 MD5 | raw file
  1using System;
  2using System.Collections.Generic;
  3using System.Diagnostics;
  4using System.Linq;
  5using System.Web;
  6using System.Xml.Linq;
  7using CmsData;
  8using System.Web.Mvc;
  9using System.Xml.Serialization;
 10using System.IO;
 11using Newtonsoft.Json;
 12using NPOI.SS.Formula.Functions;
 13using UtilityExtensions;
 14
 15namespace CmsWeb.Code
 16{
 17    public class StandardExtraValues
 18    {
 19        [Serializable]
 20        public class Fields
 21        {
 22            [XmlElement("Field")]
 23            public Field[] fields { get; set; }
 24        }
 25        [Serializable]
 26        public class Field
 27        {
 28            [XmlAttribute]
 29            public string name { get; set; }
 30            [XmlAttribute]
 31            public string type { get; set; }
 32            [XmlAttribute]
 33            public string location { get; set; }
 34            [XmlAttribute]
 35            public string VisibilityRoles { get; set; }
 36            public List<string> Codes { get; set; }
 37            internal int order;
 38            public int peopleid;
 39            public bool nonstandard;
 40
 41            internal PeopleExtra extravalue;
 42            internal static Field AddField(Field f, PeopleExtra v)
 43            {
 44                if (f == null)
 45                {
 46                    f = new Field
 47                    {
 48                        name = v.Field,
 49                        nonstandard = true,
 50                        peopleid = v.PeopleId,
 51                        extravalue = v,
 52                    };
 53                    f.type = v.StrValue.HasValue() ? "Code"
 54                        : v.Data.HasValue() ? "Data"
 55                        : v.DateValue.HasValue ? "Date"
 56                        : v.IntValue.HasValue ? "Int"
 57                        : v.BitValue.HasValue ? "Bit"
 58                        : "Code";
 59                }
 60                f.extravalue = v;
 61                return f;
 62            }
 63            public bool UserCanView()
 64            {
 65                if (!VisibilityRoles.HasValue())
 66                    return true;
 67                var a = VisibilityRoles.SplitStr(",");
 68                var user = HttpContext.Current.User;
 69				foreach (var role in a)
 70					if (user.IsInRole(role.Trim()))
 71						return true;
 72				return false;
 73            }
 74            public bool UserCanEdit()
 75            {
 76                var user = HttpContext.Current.User;
 77                return user.IsInRole("Edit");
 78            }
 79            public override string ToString()
 80            {
 81                if (extravalue == null && type != "Bits")
 82                    return "Click to edit";
 83                switch (type)
 84                {
 85                    case "Code":
 86                        return extravalue.StrValue;
 87                    case "Data":
 88                        return extravalue.Data;
 89                    case "Date":
 90                        return extravalue.DateValue.FormatDate();
 91                    case "Bit":
 92                        return extravalue.BitValue.ToString();
 93                    case "Bits":
 94                        {
 95                            var q = from e in DbUtil.Db.PeopleExtras
 96                                    where e.BitValue == true
 97                                    where e.PeopleId == peopleid
 98                                    where Codes.Contains(e.Field)
 99                                    select e.Field;
100                            return string.Join(",", q);
101                        }
102                    case "Int":
103                        if (extravalue.IntValue2.HasValue)
104                            return "{0} {1}".Fmt(extravalue.IntValue, extravalue.IntValue2);
105                        return extravalue.IntValue.ToString();
106                }
107				return null;
108            }
109
110                }
111        public static IEnumerable<Field> GetExtraValues()
112        {
113            if (DbUtil.Db.Setting("UseStandardExtraValues", "false") != "true")
114				return new List<Field>();
115            var xml = DbUtil.StandardExtraValues();
116            var sr = new StringReader(xml);
117			var fields = (new XmlSerializer(typeof(Fields)).Deserialize(sr) as Fields).fields;
118			if (fields == null)
119				return new List<Field>();
120			return fields;
121        }
122
123
124        public static IEnumerable<Field> GetExtraValues(int PeopleId, string location = "default")
125        {
126			var fields = GetExtraValues();
127
128            var n = 1;
129			foreach (var f in fields)
130            {
131                f.order = n++;
132                f.peopleid = PeopleId;
133                if (f.location == null)
134                    f.location = "default";
135            }
136
137			var exvalues = DbUtil.Db.PeopleExtras.Where(ee => ee.PeopleId == PeopleId).ToList();
138
139
140			var qfields = from f in fields
141						  join v in exvalues on f.name equals v.Field into j
142                           from v in j.DefaultIfEmpty()
143						  where f.location == location || location == null
144                           orderby f.order
145                           select Field.AddField(f, v);
146			if (location == "default")
147            {
148				var qvalues = from v in exvalues
149							  join f in fields on v.Field equals f.name into j
150                        from f in j.DefaultIfEmpty()
151                        where f == null
152							  where !fields.Any(ff => ff.Codes.Any(cc => cc == v.Field))
153                        orderby v.Field
154                        select Field.AddField(f, v);
155				return qfields.Concat(qvalues);
156            }
157			return qfields;
158        }
159        public static List<SelectListItem> ExtraValueCodes()
160        {
161            var q = from e in DbUtil.Db.PeopleExtras
162                    where e.StrValue != null || e.BitValue != null
163                    group e by new { e.Field, val = e.StrValue ?? (e.BitValue == true ? "1" : "0") }
164                        into g
165                        select g.Key;
166            var list = q.ToList();
167
168            var ev = GetExtraValues();
169            var q2 = from e in list
170                     let f = ev.SingleOrDefault(ff => ff.name == e.Field)
171                     where f == null || f.UserCanView()
172                     orderby e.Field, e.val
173                     select new SelectListItem()
174                            {
175                                Text = e.Field + ":" + e.val,
176                                Value = e.Field + ":" + e.val,
177                            };
178            return q2.ToList();
179        }
180        public static List<SelectListItem> FamilyExtraValueCodes()
181        {
182            var q = from e in DbUtil.Db.FamilyExtras
183                    where e.StrValue != null || e.BitValue != null
184                    group e by new { e.Field, val = e.StrValue ?? (e.BitValue == true ? "1" : "0") }
185                        into g
186                        select g.Key;
187            var list = q.ToList();
188
189            var ev = GetExtraValues();
190            var q2 = from e in list
191                     let f = ev.SingleOrDefault(ff => ff.name == e.Field)
192                     where f == null || f.UserCanView()
193                     orderby e.Field, e.val
194                     select new SelectListItem()
195                            {
196                                Text = e.Field + ":" + e.val,
197                                Value = e.Field + ":" + e.val,
198                            };
199            return q2.ToList();
200        }
201        public static Dictionary<string, string> Codes(string name)
202        {
203            var f = GetExtraValues().Single(ee => ee.name == name);
204            return f.Codes.ToDictionary(ee => ee, ee => ee);
205        }
206
207        public static Dictionary<string, bool> ExtraValueBits(string name, int PeopleId)
208        {
209            var f = GetExtraValues().Single(ee => ee.name == name);
210            var list = DbUtil.Db.PeopleExtras.Where(pp => pp.PeopleId == PeopleId && f.Codes.Contains(pp.Field)).ToList();
211            var q = from c in f.Codes
212                    join e in list on c equals e.Field into j
213                    from e in j.DefaultIfEmpty()
214                    select new { value = c, selected = (e != null && (e.BitValue ?? false)) };
215            return q.ToDictionary(ee => ee.value, ee => ee.selected);
216        }
217        }
218
219
220
221}