Exemplo n.º 1
0
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (!(String.IsNullOrEmpty(editor1.Text) ||
                  String.IsNullOrEmpty(title.Text) ||
                  String.IsNullOrEmpty(Abstract.Text) ||
                  String.IsNullOrEmpty(Tags.Text) ||
                  String.IsNullOrEmpty(KeyWords.Text) || SelectedSubGroups.Items.Count == 0 || Abstract.Text.Count() < 130))
            {
                if (Session["newPostIDForEdit"] != null)
                {
                    int id = Session["newPostIDForEdit"].ToString().ToInt();
                    Session.Remove("newPostIDForEdit");
                    ArticleRepository repArt = new ArticleRepository();
                    GroupsRepository  repo   = new GroupsRepository();
                    Article           art    = repArt.FindeArticleByID(id);

                    art.Title   = title.Text;
                    art.Content = editor1.Text;

                    if (FileUpload1.HasFile)
                    {
                        if (FileUpload1.FileBytes.Length > 1024 * 1024)
                        {
                            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('حجم فایل بارگذاری شده بیشتر از 1 مگابایت است!')", true);

                            return;
                        }
                        string ext = Path.GetExtension(FileUpload1.FileName).ToLower();
                        if ((ext != ".jpg") && (ext != ".png"))
                        {
                            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('فرمت فایل بارگذاری شده باید .jpg  یا .png  باشد!')", true);

                            return;
                        }
                        string filename = Path.GetFileName(FileUpload1.FileName);
                        string rand     = DBManager.CurrentTimeWithoutColons() + DBManager.CurrentPersianDateWithoutSlash();
                        filename = rand + filename;
                        string ps = Server.MapPath(@"~\img\") + filename;
                        FileUpload1.SaveAs(ps);
                        FileStream fStream  = File.OpenRead(ps);
                        byte[]     contents = new byte[fStream.Length];
                        fStream.Read(contents, 0, (int)fStream.Length);
                        fStream.Close();
                        FileInfo fi = new FileInfo(Server.MapPath(@"~\img\") + art.Image.Substring(5));
                        fi.Delete();
                        FileInfo fil = new FileInfo(Server.MapPath(@"~\img\") + art.ImgFirstPage.Substring(5));
                        fil.Delete();
                        art.Image = "/img/" + filename;
                        System.Drawing.Image img   = imgResize.ToImage(contents);
                        System.Drawing.Image image = imgResize.Resize(img, 358, 358);



                        string stream = Server.MapPath(@"~\img\") + "s" + filename;
                        switch (FileUpload1.FileName.Substring(FileUpload1.FileName.IndexOf('.') + 1).ToLower())
                        {
                        case "jpg":
                            image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
                            break;

                        case "jpeg":
                            image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
                            break;

                        case "png":
                            image.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
                            break;
                        }

                        art.ImgFirstPage = "/img/" + "s" + filename;
                    }


                    art.Abstract = Abstract.Text;
                    art.Visits   = 0;
                    art.Tags     = Tags.Text;
                    art.KeyWords = KeyWords.Text;
                    ArticleRepository ARTRep = new ArticleRepository();
                    if (ARTRep.SaveArticle(art))
                    {
                        bool result = true;
                        GroupsConRepository GRConRepo = new GroupsConRepository();
                        GRConRepo.DeletArticleConnections(id);
                        List <int> SelectedSubGroupsList = new List <int>();

                        int lastid = id;
                        int count  = SelectedSubGroups.Items.Count;
                        if (count > 0)
                        {
                            for (int i = 0; i < count; i++)
                            {
                                GroupConnection GC = new GroupConnection();
                                GC.ArticleID = lastid;
                                GC.GroupID   = SelectedSubGroups.Items[i].Value.ToInt();
                                if (!GRConRepo.SaveGroupCon(GC))
                                {
                                    result = false;
                                }
                                else
                                {
                                    Response.Redirect("/Admin/ManageBlogs");
                                }
                            }
                        }
                        else
                        {
                            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('هیچ زیر گروهی انتخاب نشده است!')", true);
                        }

                        if (!result)
                        {
                            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('مشکلی در زمان ثبت به وجود آمد،لطفا دوباره سعی کنید یا با پشتیبانی تماس بگیرید ! ');window.location ='/Admin/ManageBlogs'", true);
                        }
                        else
                        {
                            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('ثبت با موفقیت انجام شد!');window.location ='/Admin/ManageBlogs'", true);
                        }
                    }
                    else
                    {
                        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('مشکلی در زمان ثبت به وجود آمد،لطفا دوباره سعی کنید یا با پشتیبانی تماس بگیرید ! ');window.location ='/Admin/ManageBlogs'", true);
                    }
                }
                else
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert(' مشکلی در زمان لود کردن به وجود آمد دوباره سعی کنید ! ');window.location ='/Admin/ManageBlogs'", true);
                }
            }
        }
Exemplo n.º 2
0
        protected void gvPosts_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            diverror.InnerHtml = "";

            if (e.CommandName == "Edit")
            {
                // Retrieve the row index stored in the
                // CommandArgument property.
                int index = Convert.ToInt32(e.CommandArgument);

                // Retrieve the row that contains the button
                // from the Rows collection.
                GridViewRow row = gvPosts.Rows[index];
                Session.Add("PostIDForEdit", row.Cells[0].Text);

                Response.Redirect("/Admin/EditBlog");
            }
            if (e.CommandName == "Show")
            {
                // Retrieve the row index stored in the
                // CommandArgument property.
                int index = Convert.ToInt32(e.CommandArgument);

                // Retrieve the row that contains the button
                // from the Rows collection.
                GridViewRow row = gvPosts.Rows[index];

                string id = row.Cells[0].Text;

                Response.Redirect("/Blogs/" + id + "/" + row.Cells[1].Text.Replace(' ', '-'));
            }
            if (e.CommandName == "Delet")
            {
                // Retrieve the row index stored in the
                // CommandArgument property.
                int index = Convert.ToInt32(e.CommandArgument);

                // Retrieve the row that contains the button
                // from the Rows collection.
                GridViewRow         row      = gvPosts.Rows[index];
                int                 id       = row.Cells[0].Text.ToInt();
                ArticleRepository   repart   = new ArticleRepository();
                GroupsConRepository repgpCon = new GroupsConRepository();
                string              img      = "";
                string              fimg     = "";

                Article pr = repart.FindeArticleByID(id);
                if (pr != null)
                {
                    img  = pr.Image;
                    fimg = pr.ImgFirstPage;
                }
                if (repgpCon.DeletArticleConnections(id) && repart.DeletArticleByID(id))
                {
                    subgroup();
                    FileInfo fi = new FileInfo(Server.MapPath(@"~\img\") + img.Substring(5));
                    fi.Delete();
                    FileInfo fil = new FileInfo(Server.MapPath(@"~\img\") + fimg.Substring(5));
                    fil.Delete();
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('حذف با موفقیت انجام شد ');", true);
                }
                else
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('حذف با خطا مواجه شد ، بعدا سعی کنید یا با پشتیبانی تماس بگیرید!');", true);
                }
            }
        }