/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

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