PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/LC3Solution/LC3/LINQControls/Admin/LinqProductDetailsAdmin.ascx.cs

#
C# | 237 lines | 168 code | 26 blank | 43 comment | 9 complexity | ada7d23432e89ea491e29cff5d6abb03 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. using System;
  2. using System.Configuration;
  3. using System.Web.UI.WebControls;
  4. using System.Data.Linq;
  5. using Telerik.Web.UI;
  6. using System.IO;
  7. using System.Drawing.Imaging;
  8. using System.Linq;
  9. using LinqCommerce;
  10. public partial class LINQControls_Admin_LinqProductDetailsAdmin : System.Web.UI.UserControl
  11. {
  12. public double _thumbMaxWidth = 0;
  13. public double _thumbMaxHeight = 0;
  14. public double _detailMaxWidth = 0;
  15. public double _detailMaxHeight = 0;
  16. public string _productImagePath = ""; // default but should be set in config section
  17. protected void Page_Load(object sender, EventArgs e)
  18. {
  19. // Get the values for image resizing and path from the config section
  20. SectionConfigurationGroup s =
  21. (SectionConfigurationGroup)ConfigurationManager.GetSection("LinqCommerce/SiteSettings");
  22. _thumbMaxWidth = Convert.ToDouble(s.ProductSettings.ImageThumbMaxWidth);
  23. _thumbMaxHeight = Convert.ToDouble(s.ProductSettings.ImageThumbMaxHeight);
  24. _detailMaxWidth = Convert.ToDouble(s.ProductSettings.ImageDetailMaxWidth);
  25. _detailMaxHeight = Convert.ToDouble(s.ProductSettings.ImageDetailMaxHeight);
  26. _productImagePath = s.ProductSettings.ProductImagePath;
  27. lblThumbSize.Text = "(" + _thumbMaxWidth.ToString() + "x" + _thumbMaxHeight.ToString() + ")";
  28. lblDetailSize.Text = "(" + _detailMaxWidth.ToString() + "x" + _detailMaxHeight.ToString() + ")";
  29. if (!Page.IsPostBack)
  30. {
  31. LinqProductAccess lp = new LinqProductAccess();
  32. lc_Product p = lp.GetProduct();
  33. lblProductName.Text = lblProductName2.Text = p.Name;
  34. RenderProductImages(p.Image1FileName, p.Image2FileName);
  35. }
  36. }
  37. /// <summary>
  38. /// Upload the Product Thumbnail and Detail Images
  39. /// The images will be scaled based on the ProductSettings set in the web.config file.
  40. /// </summary>
  41. /// <param name="sender"></param>
  42. /// <param name="e"></param>
  43. protected void upload1Button_Click(object sender, EventArgs e)
  44. {
  45. try
  46. {
  47. UploadedFile thumbFile = null;
  48. UploadedFile detailFile = null;
  49. // Check that there is a file in the Thumbnail Upload Control
  50. if (thumbFileUpload.UploadedFiles.Count > 0)
  51. {
  52. //if the file uploaded is not a picture, then show an error message
  53. if (thumbFileUpload.InvalidFiles.Count > 0)
  54. {
  55. statusLabel.Text = "File upload failed. Allowed extensions are .jpg, .png, .bmp and .gif";
  56. statusLabel.CssClass = "error";
  57. }
  58. else
  59. {
  60. // Get the thumbnail image
  61. thumbFile = thumbFileUpload.UploadedFiles[0];
  62. // Check to see if there is a different image for the detail view
  63. if (detailFileUpload.UploadedFiles.Count > 0)
  64. {
  65. detailFile = detailFileUpload.UploadedFiles[0];
  66. }
  67. else
  68. {
  69. // If there is no detail image, set it to the thumb image
  70. detailFile = thumbFile;
  71. }
  72. // Upload, save and resize the images
  73. LinqAdminAccess la = new LinqAdminAccess();
  74. string folderName = Server.MapPath(_productImagePath);
  75. string thumbFileName = thumbFile.GetName();
  76. string detailFileName = detailFile.GetName();
  77. try
  78. {
  79. // Save the thumbnail image to disk
  80. thumbFile.SaveAs(Path.Combine(folderName, thumbFileName), true);
  81. // Resize the thumbnail image based on config settings
  82. la.ResizeImage(thumbFile.ContentType, folderName + thumbFileName, _thumbMaxWidth, _thumbMaxHeight, false, true);
  83. // Check for existing image w/ same name as detail image and save
  84. detailFileName = CheckForFile(_productImagePath, detailFile);
  85. // Resize the detail image based on config settings
  86. la.ResizeImage(detailFile.ContentType, folderName + detailFileName, _detailMaxWidth, _detailMaxHeight, false, true);
  87. // Save the names of the product images to the db
  88. la.UpdateProductImage(thumbFileName, detailFileName, LinqProductAccess.ProductID);
  89. // Update status message
  90. statusLabel.Text = "File uploaded successfully!";
  91. statusLabel.CssClass = "success";
  92. RenderProductImages(thumbFileName, detailFileName);
  93. }
  94. catch (Exception ex)
  95. {
  96. statusLabel.Text = "File not uploaded.<br/>" + ex.Message + "<br/>" + ex.StackTrace;
  97. statusLabel.CssClass = "error";
  98. }
  99. }
  100. }
  101. else
  102. {
  103. statusLabel.Text = "No file(s) selected. The minimum requirement is to select an image for the Thumbnail view.<br/>";
  104. statusLabel.Text += "It can be larger and will be scaled down for the thumbnail and used for the detail view as well.";
  105. statusLabel.CssClass = "error";
  106. }
  107. }
  108. catch (System.UnauthorizedAccessException ex)
  109. {
  110. statusLabel.Text = "Your user account is unauthorized to upload pictures to the " + _productImagePath + " directory. Please contact a system admin.";
  111. statusLabel.CssClass = "error";
  112. }
  113. }
  114. /// <summary>
  115. /// Upload multiple alternate images for use in ProductImages table
  116. /// </summary>
  117. /// <param name="sender"></param>
  118. /// <param name="e"></param>
  119. protected void btnInsert2_Click(object sender, EventArgs e)
  120. {
  121. try
  122. {
  123. // proceed with uploading only if the user selected a file
  124. if (alternateFileUpload.UploadedFiles.Count > 0)
  125. {
  126. foreach (UploadedFile validFile in alternateFileUpload.UploadedFiles)
  127. {
  128. // Save image to the server
  129. string folderName = Server.MapPath(_productImagePath);
  130. validFile.SaveAs(Path.Combine(folderName, validFile.GetName()), true);
  131. try
  132. {
  133. string fileName = validFile.GetName();
  134. string location = Server.MapPath(_productImagePath) + fileName;
  135. //Resize the image to 150 px for the product details page
  136. LinqAdminAccess la = new LinqAdminAccess();
  137. la.ResizeImage(validFile.ContentType, folderName + validFile.GetName(), _detailMaxWidth, _detailMaxHeight, false, true);
  138. // Save image name and id to db
  139. la.UpdateAlternateProductImages(validFile.GetName(), LinqProductAccess.ProductID);
  140. // Update the status message
  141. statusLabel.Text = "File(s) uploaded successfully!";
  142. statusLabel.CssClass = "success";
  143. // Update the image grid
  144. ProductAlternateImagesRadGrid.Rebind();
  145. }
  146. catch (Exception ex)
  147. {
  148. statusLabel.Text = "File not uploaded. If the problem persists, contact your system admin.";
  149. statusLabel.Text += "<br/>" + ex.Message + "<br/>" + ex.StackTrace;
  150. statusLabel.CssClass = "error";
  151. }
  152. }
  153. }
  154. if (alternateFileUpload.InvalidFiles.Count > 0)
  155. {
  156. statusLabel.Text = "Not all files were uploaded. Allowed extensions are .jpg, .png, .bmp and .gif";
  157. statusLabel.CssClass = "error";
  158. }
  159. }
  160. catch (System.UnauthorizedAccessException ex)
  161. {
  162. statusLabel.Text = "Your user account is unauthorized to upload pictures to the " + _productImagePath + " directory. Please contact a system admin.";
  163. statusLabel.CssClass = "error";
  164. }
  165. }
  166. /// <summary>
  167. /// Check for existing file and if exsits update name, save and return new name
  168. /// ToDo - this needs to be improved so that it can be used for alternative images as well
  169. /// need to add a global "overwrite images" setting to config as well.
  170. /// </summary>
  171. /// <param name="targetFolder"></param>
  172. /// <param name="fileToSave"></param>
  173. /// <returns></returns>
  174. protected string CheckForFile(string targetFolder, UploadedFile fileToSave)
  175. {
  176. int counter = 0;
  177. string imagePath = Server.MapPath(targetFolder);
  178. string targetFileName = fileToSave.GetName();
  179. string targetFilePath = Path.Combine(imagePath,
  180. fileToSave.GetName());//.GetNameWithoutExtension() + counter.ToString() + fileToSave.GetExtension());
  181. while (System.IO.File.Exists(targetFilePath))
  182. {
  183. counter++;
  184. targetFilePath = Path.Combine(imagePath,
  185. fileToSave.GetNameWithoutExtension() + "-" + counter.ToString() + fileToSave.GetExtension());
  186. targetFileName = fileToSave.GetNameWithoutExtension() + "-" + counter.ToString() + fileToSave.GetExtension();
  187. }
  188. fileToSave.SaveAs(targetFilePath, true);
  189. return targetFileName;
  190. }
  191. /// <summary>
  192. /// Update the product images on page load and when uploaded
  193. /// </summary>
  194. /// <param name="imageThumb"></param>
  195. /// <param name="imageDetail"></param>
  196. protected void RenderProductImages(string imageThumb, string imageDetail)
  197. {
  198. if (!String.IsNullOrEmpty(imageThumb))
  199. {
  200. imgThumbnail.Visible = true;
  201. imgThumbnail.ImageUrl = _productImagePath + imageThumb;
  202. imgThumbnail.Width = new Unit(_thumbMaxWidth);
  203. }
  204. if (!String.IsNullOrEmpty(imageDetail))
  205. {
  206. imgDetail.Visible = true;
  207. imgDetail.ImageUrl = _productImagePath + imageDetail;
  208. imgDetail.Width = new Unit(_thumbMaxWidth);
  209. }
  210. }
  211. }