PageRenderTime 43ms CodeModel.GetById 25ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/projects/PigeonCms.Core/BLL/Section.cs

http://pigeoncms.googlecode.com/
C# | 467 lines | 372 code | 49 blank | 46 comment | 21 complexity | b9482b2200ae9a512a02002ec84a9342 MD5 | raw file
  1using System;
  2using System.Data;
  3using System.Configuration;
  4using System.Web;
  5using System.Web.Security;
  6using System.Web.UI;
  7using System.Web.UI.WebControls;
  8using System.Web.UI.WebControls.WebParts;
  9using System.Web.UI.HtmlControls;
 10using System.Diagnostics;
 11using System.ComponentModel;
 12using System.IO;
 13using PigeonCms;
 14using System.Collections.Generic;
 15using System.Threading;
 16
 17
 18
 19namespace PigeonCms
 20{
 21    public class Section : ITableWithPermissions
 22    {
 23        private int id = 0;
 24        private bool enabled = true;
 25        string defaultImageName = "";
 26
 27        private Dictionary<string, string> titleTranslations = new Dictionary<string, string>();
 28        private Dictionary<string, string> descriptionTranslations = new Dictionary<string, string>();
 29
 30        //read permissions
 31        MenuAccesstype readAccessType = MenuAccesstype.Public;
 32        private int readPermissionId = 0;
 33        List<string> readRolenames = new List<string>();
 34        private string readAccessCode = "";
 35        private int readAccessLevel = 0;
 36
 37        //write permissions
 38        MenuAccesstype writeAccessType = MenuAccesstype.Public;
 39        private int writePermissionId = 0;
 40        List<string> writeRolenames = new List<string>();
 41        private string writeAccessCode = "";
 42        private int writeAccessLevel = 0;
 43
 44        //limits
 45        int maxItems = 0;
 46        int maxAttachSizeKB = 0;
 47
 48
 49        public string ImagesPath
 50        {
 51            get { return "~/public/gallery/sections/"; }
 52        }
 53
 54        public string FilesPath
 55        {
 56            get { return "~/public/files/sections/"; }
 57        }
 58
 59        /// <summary>
 60        /// Automatic Id as PKey
 61        /// </summary>
 62        [DataObjectField(true)]
 63        public int Id
 64        {
 65            [DebuggerStepThrough()]
 66            get { return id; }
 67            [DebuggerStepThrough()]
 68            set { id = value; }
 69        }
 70        public bool Enabled
 71        {
 72            [DebuggerStepThrough()]
 73            get { return enabled; }
 74            [DebuggerStepThrough()]
 75            set { enabled = value; }
 76        }
 77
 78        /// <summary>
 79        /// Title in current culture
 80        /// </summary>
 81        [DataObjectField(false)]
 82        public string Title
 83        {
 84            get
 85            {
 86                string res = "";
 87                titleTranslations.TryGetValue(Thread.CurrentThread.CurrentCulture.Name, out res);
 88                if (string.IsNullOrEmpty(res))
 89                    titleTranslations.TryGetValue(Config.CultureDefault, out res);
 90                return res;
 91            }
 92        }
 93
 94        /// <summary>
 95        /// Alias for the object in default culture
 96        /// </summary>
 97        [DataObjectField(false)]
 98        public string Alias
 99        {
100            get
101            {
102                string res = "";
103                titleTranslations.TryGetValue(Config.CultureDefault, out res);
104                res = Utility.GetUrlAlias(res);
105                return res;
106            }
107        }
108
109        /// <summary>
110        /// Title in different culture
111        /// </summary>
112        [DataObjectField(false)]
113        public Dictionary<string, string> TitleTranslations
114        {
115            [DebuggerStepThrough()]
116            get { return titleTranslations; }
117            [DebuggerStepThrough()]
118            set { titleTranslations = value; }
119        }
120
121        public bool IsTitleTranslated
122        {
123            get
124            {
125                bool res = true;
126                string val = "";
127                titleTranslations.TryGetValue(Thread.CurrentThread.CurrentCulture.Name, out val);
128                if (string.IsNullOrEmpty(val))
129                    res = false;
130                return res;
131            }
132        }
133
134        /// <summary>
135        /// Description in current culture
136        /// </summary>
137        [DataObjectField(false)]
138        public string Description
139        {
140            get
141            {
142                string res = "";
143                descriptionTranslations.TryGetValue(Thread.CurrentThread.CurrentCulture.Name, out res);
144                if (string.IsNullOrEmpty(res))
145                    descriptionTranslations.TryGetValue(Config.CultureDefault, out res);
146                return res;
147            }
148        }
149
150        /// <summary>
151        /// Title in different culture
152        /// </summary>
153        [DataObjectField(false)]
154        public Dictionary<string, string> DescriptionTranslations
155        {
156            [DebuggerStepThrough()]
157            get { return descriptionTranslations; }
158            [DebuggerStepThrough()]
159            set { descriptionTranslations = value; }
160        }
161
162        public bool IsDescriptionTranslated
163        {
164            get
165            {
166                bool res = true;
167                string val = "";
168                descriptionTranslations.TryGetValue(Thread.CurrentThread.CurrentCulture.Name, out val);
169                if (string.IsNullOrEmpty(val))
170                    res = false;
171                return res;
172            }
173        }
174
175        public string DefaultImageName
176        {
177            [DebuggerStepThrough()]
178            get { return defaultImageName; }
179            [DebuggerStepThrough()]
180            set { defaultImageName = value; }
181        }
182
183        private FileMetaInfo defaultImage = new FileMetaInfo();
184        public FileMetaInfo DefaultImage
185        {
186            get
187            {
188                bool found = false;
189                FileMetaInfo firstImg = new FileMetaInfo();
190                int counter = 0;
191                foreach (FileMetaInfo img in this.Images)
192                {
193                    if (counter == 0)
194                        firstImg = img;
195                    if (string.IsNullOrEmpty(defaultImageName))
196                        break;
197                    if (img.FileName == defaultImageName)
198                    {
199                        defaultImage = img;
200                        found = true;
201                        break;
202                    }
203                    counter++;
204                }
205                if (!found)
206                    defaultImage = firstImg;
207                return defaultImage;
208            }
209            [DebuggerStepThrough()]
210            set { defaultImage = value; }
211        }
212
213        /// <summary>
214        /// the number of images loaded
215        /// </summary>
216        public int NumOfImagesLoaded
217        {
218            get
219            {
220                return this.Images.Count;
221            }
222        }
223
224        private List<FileMetaInfo> images = null;
225        public List<FileMetaInfo> Images
226        {
227            get
228            {
229                if (images == null)
230                {
231                    images = new FilesGallery(ImagesPath, this.Id.ToString(), "*.*").GetAll();
232                }
233                return images;
234            }
235        }
236
237        public List<FileMetaInfo> ImagesNotDefault
238        {
239            get
240            {
241                List<FileMetaInfo> result = new List<FileMetaInfo>();
242                foreach (FileMetaInfo item in this.Images)
243                {
244                    if (defaultImage.FileUrl != item.FileUrl)
245                        result.Add(item);
246                }
247                return result;
248            }
249        }
250
251        private List<FileMetaInfo> files = null;
252        public List<FileMetaInfo> Files
253        {
254            get
255            {
256                if (files == null)
257                {
258                    files = new FilesGallery(FilesPath, this.Id.ToString()).GetAll();
259                }
260                return files;
261            }
262        }
263
264        public MenuAccesstype ReadAccessType
265        {
266            [DebuggerStepThrough()]
267            get { return readAccessType; }
268            [DebuggerStepThrough()]
269            set { readAccessType = value; }
270        }
271
272        public int ReadPermissionId
273        {
274            [DebuggerStepThrough()]
275            get { return readPermissionId; }
276            [DebuggerStepThrough()]
277            set { readPermissionId = value; }
278        }
279
280        /// <summary>
281        /// roles allowed depending on this.PermissionId
282        /// </summary>
283        [DataObjectField(false)]
284        public List<string> ReadRolenames
285        {
286            [DebuggerStepThrough()]
287            get { return readRolenames; }
288            [DebuggerStepThrough()]
289            set { readRolenames = value; }
290        }
291
292        public string ReadAccessCode
293        {
294            [DebuggerStepThrough()]
295            get { return readAccessCode; }
296            [DebuggerStepThrough()]
297            set { readAccessCode = value; }
298        }
299
300        public int ReadAccessLevel
301        {
302            [DebuggerStepThrough()]
303            get { return readAccessLevel; }
304            [DebuggerStepThrough()]
305            set { readAccessLevel = value; }
306        }
307
308        public MenuAccesstype WriteAccessType
309        {
310            [DebuggerStepThrough()]
311            get { return writeAccessType; }
312            [DebuggerStepThrough()]
313            set { writeAccessType = value; }
314        }
315
316        public int WritePermissionId
317        {
318            [DebuggerStepThrough()]
319            get { return writePermissionId; }
320            [DebuggerStepThrough()]
321            set { writePermissionId = value; }
322        }
323
324        [DataObjectField(false)]
325        public List<string> WriteRolenames
326        {
327            [DebuggerStepThrough()]
328            get { return writeRolenames; }
329            [DebuggerStepThrough()]
330            set { writeRolenames = value; }
331        }
332
333        public string WriteAccessCode
334        {
335            [DebuggerStepThrough()]
336            get { return writeAccessCode; }
337            [DebuggerStepThrough()]
338            set { writeAccessCode = value; }
339        }
340
341        public int WriteAccessLevel
342        {
343            [DebuggerStepThrough()]
344            get { return writeAccessLevel; }
345            [DebuggerStepThrough()]
346            set { writeAccessLevel = value; }
347        }
348
349        /// <summary>
350        /// max items for this section. 0 no limit
351        /// </summary>
352        public int MaxItems
353        {
354            [DebuggerStepThrough()]
355            get { return maxItems; }
356            [DebuggerStepThrough()]
357            set { maxItems = value; }
358        }
359
360        /// <summary>
361        /// max size of attachemnts for this section (include images and files in section, categories and items)
362        /// 0 no limit
363        /// </summary>
364        public int MaxAttachSizeKB
365        {
366            [DebuggerStepThrough()]
367            get { return maxAttachSizeKB; }
368            [DebuggerStepThrough()]
369            set { maxAttachSizeKB = value; }
370        }
371
372        int numOfItems = -1;
373        /// <summary>
374        /// number of items actually under this section
375        /// </summary>
376        public int NumOfItems
377        {
378            get
379            {
380                if (numOfItems == -1)
381                {
382                    numOfItems = 0;
383                    var itemsFilter = new ItemsFilter();
384                    itemsFilter.SectionId = this.Id==0 ? -1 : this.Id;
385                    var itemsList = new ItemsManager<Item, ItemsFilter>().GetByFilter(itemsFilter, "");
386                    numOfItems = itemsList.Count;
387                }
388                return numOfItems;
389            }
390        }
391
392        long sizeOfItems = -1;
393        /// <summary>
394        /// size in bytes of all attachments for all items of current section
395        /// </summary>
396        public long SizeOfItems
397        {
398            get
399            {
400                if (sizeOfItems == -1)
401                {
402                    sizeOfItems = 0;
403                    var itemsFilter = new ItemsFilter();
404                    itemsFilter.SectionId = this.Id == 0 ? -1 : this.Id;
405                    var itemsList = new ItemsManager<Item, ItemsFilter>().GetByFilter(itemsFilter, "");
406                    foreach (var i in itemsList)
407                        sizeOfItems += i.FilesSize + i.ImagesSize;
408                }
409                return sizeOfItems;
410            }
411        }
412
413        #region public methods
414
415        public Section() { }
416
417        /// <summary>
418        /// delete images folder content
419        /// </summary>
420        public void DeleteImages()
421        {
422            new FilesGallery(ImagesPath, this.Id.ToString()).DeleteFolderContent();
423            this.images = null;
424        }
425
426        /// <summary>
427        /// delete files folder content
428        /// </summary>
429        public void DeleteFiles()
430        {
431            new FilesGallery(FilesPath, this.Id.ToString()).DeleteFolderContent();
432            this.files = null;
433        }
434
435        #endregion
436    }
437
438
439    [Serializable]
440    public class SectionsFilter
441    {
442        #region fields definition
443
444        private int id = 0;
445        private Utility.TristateBool enabled = Utility.TristateBool.NotSet;
446
447        [DataObjectField(true)]
448        public int Id
449        {
450            [DebuggerStepThrough()]
451            get { return id; }
452            [DebuggerStepThrough()]
453            set { id = value; }
454        }
455
456        [DataObjectField(false)]
457        public Utility.TristateBool Enabled
458        {
459            [DebuggerStepThrough()]
460            get { return enabled; }
461            [DebuggerStepThrough()]
462            set { enabled = value; }
463        }
464
465        #endregion
466    }
467}