PageRenderTime 27ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/Trunk/src/Graffiti.Web/graffiti-admin/categories/Default.aspx.cs

#
C# | 269 lines | 219 code | 45 blank | 5 comment | 47 complexity | fef40caaa65508a92e1b697b42466b15 MD5 | raw file
  1. using System;
  2. using System.Web.UI.WebControls;
  3. using Graffiti.Core;
  4. using Repeater = Graffiti.Core.Repeater;
  5. public partial class graffiti_admin_categories_Default : ManagerControlPanelPage
  6. {
  7. private CategoryCollection cats;
  8. private int currentChildIndex;
  9. // this will hold what child we are on so we know which img to display in the hierarchical view
  10. protected void Page_Load(object sender, EventArgs e)
  11. {
  12. if (!IsPostBack)
  13. {
  14. Util.CanWriteRedirect(Context);
  15. }
  16. if (Request.QueryString["id"] != null)
  17. {
  18. if (!IsPostBack)
  19. {
  20. Category c = new Category(Request.QueryString["id"]);
  21. if (!c.IsLoaded || c.IsNew)
  22. throw new Exception("This category id does not exist");
  23. if (Request.QueryString["new"] != null && !IsPostBack)
  24. {
  25. Message.Text = "The category <strong>" + c.Name + "</strong> was created";
  26. Message.Type = StatusType.Success;
  27. }
  28. txtExistingCategory.Text = Server.HtmlDecode(c.Name);
  29. txtFeedBurner.Text = c.FeedUrlOverride;
  30. Editor.Text = c.Body;
  31. txtKeywords.Text = Server.HtmlDecode(c.MetaKeywords ?? string.Empty);
  32. txtMetaScription.Text = Server.HtmlDecode(c.MetaDescription ?? string.Empty);
  33. SortOrder.SelectedValue = c.SortOrder.ToString();
  34. txtExistingLinkName.Text = Util.CleanForUrl(c.FormattedName).Replace("-", " ");
  35. if (c.ParentId > 0)
  36. {
  37. Category parent = new CategoryController().GetCachedCategory(c.ParentId, false);
  38. existingParentLinkName.Text = parent.LinkName + "/ ";
  39. }
  40. }
  41. new_category_container.Visible = false;
  42. Category_List.Visible = false;
  43. category_edit_form.Visible = true;
  44. }
  45. else
  46. {
  47. if (!IsPostBack)
  48. {
  49. CategoryCollection parents = new CategoryController().GetTopLevelCachedCategories();
  50. foreach (Category parent in parents)
  51. {
  52. Parent_Categories.Items.Add(new ListItem(Server.HtmlDecode(parent.Name), parent.Id.ToString()));
  53. }
  54. Parent_Categories.Items.Insert(0, new ListItem("[No parent category]", "-1"));
  55. if (Request.QueryString["upd"] != null)
  56. {
  57. Message.Text = "The category <strong>" + Request.QueryString["upd"] + "</strong> was updated";
  58. Message.Type = StatusType.Success;
  59. }
  60. }
  61. new_category_container.Visible = true;
  62. category_edit_form.Visible = false;
  63. Category_List.Visible = true;
  64. GetCategories();
  65. }
  66. }
  67. protected void EditCategory_Click(object sender, EventArgs e)
  68. {
  69. try
  70. {
  71. Category c = new Category(Request.QueryString["id"]);
  72. c.Name = Server.HtmlEncode(txtExistingCategory.Text.Trim());
  73. c.FormattedName = txtExistingLinkName.Text.Trim();
  74. c.Body = Editor.Text;
  75. c.FeedUrlOverride = txtFeedBurner.Text;
  76. c.MetaDescription = Server.HtmlEncode(txtMetaScription.Text.Trim());
  77. c.MetaKeywords = Server.HtmlEncode(txtKeywords.Text.Trim());
  78. c.SortOrder = (SortOrderType) Enum.Parse(typeof (SortOrderType), SortOrder.SelectedValue, true);
  79. c.Save();
  80. Response.Redirect("~/graffiti-admin/categories/?upd=" + c.Name);
  81. }
  82. catch (Exception ex)
  83. {
  84. string exMessage = ex.Message;
  85. if (!string.IsNullOrEmpty(exMessage) && exMessage.IndexOf("UNIQUE") > -1)
  86. exMessage = "This category already exists";
  87. Message.Text = "A category with the name of " + txtExistingCategory.Text + " could not be created <br />" +
  88. exMessage;
  89. Message.Type = StatusType.Error;
  90. }
  91. }
  92. protected void CreateCategory_Click(object sender, EventArgs e)
  93. {
  94. try
  95. {
  96. Category c = new Category();
  97. c.Name = Server.HtmlEncode(txtCategory.Text.Trim());
  98. c.ParentId = Int32.Parse(Parent_Categories.SelectedValue);
  99. c.Save();
  100. Response.Redirect("~/graffiti-admin/categories/?id=" + c.Id + "&new=true");
  101. }
  102. catch (Exception ex)
  103. {
  104. string exMessage = ex.Message;
  105. if (!string.IsNullOrEmpty(exMessage) && exMessage.IndexOf("UNIQUE") > -1)
  106. exMessage = "This category already exists";
  107. Message.Text = "A category with the name of " + txtCategory.Text + " could not be created <br />" +
  108. exMessage;
  109. Message.Type = StatusType.Error;
  110. }
  111. }
  112. private void GetCategories()
  113. {
  114. cats = new CategoryController().GetCachedCategories();
  115. var parents = cats.FindAll(
  116. delegate(Category c) { return c.ParentId >= 0; });
  117. if (cats != null && cats.Count > 0)
  118. {
  119. Category_List.Visible = true;
  120. CategoryCollection source = new CategoryController().GetTopLevelCachedCategories();
  121. source.Sort(
  122. delegate(Category c, Category c1) { return c.Name.CompareTo(c1.Name); });
  123. Categories_List.DataSource = source;
  124. Categories_List.ItemDataBound += Categories_List_ItemDataBound;
  125. Categories_List.DataBind();
  126. }
  127. else
  128. {
  129. Category_List.Visible = false;
  130. }
  131. }
  132. protected void Categories_List_ItemDataBound(object sender, RepeaterItemEventArgs e)
  133. {
  134. if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
  135. {
  136. Category cat = (Category) e.Item.DataItem;
  137. var children = cats.FindAll(
  138. delegate(Category c) { return c.ParentId == cat.Id; });
  139. if (children != null && children.Count > 0)
  140. {
  141. // reset the child index counter
  142. currentChildIndex = 0;
  143. Repeater c = (Repeater) e.Item.FindControl("NestedCategoriesList");
  144. c.ItemDataBound += NestedCategoriesList_ItemDataBound;
  145. c.DataSource = children;
  146. c.DataBind();
  147. }
  148. }
  149. }
  150. protected void NestedCategoriesList_ItemDataBound(object sender, RepeaterItemEventArgs e)
  151. {
  152. if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
  153. {
  154. // increment the child index counter
  155. ++currentChildIndex;
  156. Category cat = (Category) e.Item.DataItem;
  157. // how many children are there?
  158. var children = cats.FindAll(
  159. delegate(Category c) { return c.ParentId == cat.ParentId; });
  160. if (children != null && children.Count > 0)
  161. {
  162. Image img = (Image) e.Item.FindControl("TreeImage");
  163. if (currentChildIndex < children.Count)
  164. img.ImageUrl = ResolveUrl("~/graffiti-admin/common/img/m.gif");
  165. else
  166. img.ImageUrl = ResolveUrl("~/graffiti-admin/common/img/b.gif");
  167. }
  168. }
  169. }
  170. protected void lbDelete_Command(object sender, CommandEventArgs args)
  171. {
  172. int id = Convert.ToInt32(args.CommandArgument);
  173. Category c = new Category(id);
  174. if (c.HasChildren)
  175. {
  176. Message.Text = "You cannot delete this category because it has child categories.";
  177. Message.Type = StatusType.Error;
  178. return;
  179. }
  180. var postCounts = Post.GetPostCounts(id, null);
  181. if (postCounts != null && postCounts.Count > 0)
  182. {
  183. int totalPosts = 0;
  184. foreach (PostCount p in postCounts)
  185. totalPosts += p.Count;
  186. if (totalPosts == 1)
  187. {
  188. Message.Text = "You cannot delete this category because it is in use by " + totalPosts + " post.";
  189. Message.Text += " <a href=\"" + ResolveUrl("~/graffiti-admin/posts/?category=") + id +
  190. "\">Click here</a> to change this post to another category or delete it.";
  191. Message.Type = StatusType.Error;
  192. }
  193. else
  194. {
  195. Message.Text = "You cannot delete this category because it is in use by " + totalPosts + " posts.";
  196. Message.Text += " <a href=\"" + ResolveUrl("~/graffiti-admin/posts/?category=") + id +
  197. "\">Click here</a> to change these posts to another category or delete them.";
  198. Message.Type = StatusType.Error;
  199. }
  200. return;
  201. }
  202. try
  203. {
  204. // destroy any deleted posts in this category
  205. Post.DestroyDeletedPostCascadingForCategory(id);
  206. Category.Destroy(Category.Columns.Id, id);
  207. NavigationSettings navSettings = NavigationSettings.Get();
  208. DynamicNavigationItem item = navSettings.Items == null
  209. ? null
  210. : navSettings.Items.Find(
  211. delegate(DynamicNavigationItem itm) { return itm.CategoryId == id; });
  212. if (item != null)
  213. NavigationSettings.Remove(item.Id);
  214. CategoryController.Reset();
  215. GetCategories();
  216. Message.Text = "The category " + c.Name + " was deleted.";
  217. }
  218. catch (Exception ex)
  219. {
  220. Message.Text = ex.Message;
  221. Message.Type = StatusType.Error;
  222. }
  223. }
  224. }