Exemplo n.º 1
0
        protected void btnSave_Click(object sender, EventArgs e)
        {
            CheckPermission();

            if (pnlEditAd.Visible)
            {
                string subject = txtSubject.Text.Trim();
                string location = txtLocation.Text.Trim();
                string message = htmlEditor != null ? htmlEditor.Content.Trim() : ckeditor.Text.Trim();
                DateTime expirationDate = DateTime.Now.AddDays(Int32.Parse(ddExpiration.SelectedValue));

                #region validate fields

                if (ddCategories.SelectedValue == "-1")
                {
                    lblError.Text = "Please select category".Translate();
                    return;
                }

                if (subject.Length == 0)
                {
                    lblError.Text = "Please enter subject".Translate();
                    return;
                }

                if (location.Length == 0)
                {
                    lblError.Text = "Please enter location".Translate();
                    return;
                }

                if (message.Length == 0)
                {
                    lblError.Text = "Please enter message".Translate();
                    return;
                }

                #endregion

                Ad ad = null;
                int adID;
                if (!String.IsNullOrEmpty(Request.Params["aid"]) && Int32.TryParse(Request.Params["aid"], out adID))
                {
                    ad = Ad.Fetch(adID);
                }
                else
                    ad = new Ad(Int32.Parse(ddSubcategories.SelectedValue), CurrentUserSession.Username)
                    {
                        Subject = subject,
                        Location = location,
                        Description = message,
                        ExpirationDate = expirationDate
                    };

                if (ad != null)
                {
                    if (CurrentUserSession.BillingPlanOptions.AutoApproveAds.Value
                                    || CurrentUserSession.Level != null && CurrentUserSession.Level.Restrictions.AutoApproveAds)
                    {
                        ad.Approved = true;
                    }
                    else
                    {
                        ad.Approved = false;
                    }

                    ad.CategoryID = Int32.Parse(ddSubcategories.SelectedValue);
                    ad.Subject = subject;
                    ad.Location = location;
                    ad.Description = message;
                    ad.ExpirationDate = expirationDate;
                    ad.Save();
                    AdID = ad.ID;

                    pnlEditAd.Visible = false;
                    pnlUploadAdPhotos.Visible = true;

                    loadPhotos(ad.ID);
                }
            }
            else
            {
                foreach (RepeaterItem item in rptAddPhoto.Items)
                {
                    TextBox txtAdPhotoDescription = (TextBox) item.FindControl("txtAdPhotoDescription");
                    FileUpload fuAdPhoto = (FileUpload)item.FindControl("fuAdPhoto");

                    if (fuAdPhoto.PostedFile.FileName.Length == 0) continue;

                    System.Drawing.Image image = null;
                    try
                    {
                        image = System.Drawing.Image.FromStream(fuAdPhoto.PostedFile.InputStream);
                    }
                    catch
                    {
                        lblError.Text = Lang.Trans("Invalid image!");
                        return;
                    }

                    Classes.AdPhoto photo = new Classes.AdPhoto(AdID.Value);
                    photo.Image = image;
                    photo.Description = txtAdPhotoDescription.Text.Trim();
                    photo.Save();

                    string cacheFileDir = Config.Directories.ImagesCacheDirectory + "/" + AdID % 10;
                    string cacheFileMask = String.Format("adID{0}_*.jpg", AdID);
                    foreach (string file in Directory.GetFiles(cacheFileDir, cacheFileMask))
                    {
                        File.Delete(file);
                    }
                }

                Response.Redirect("~/ShowAd.aspx?id=" + AdID);
            }
        }