protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string id = this.Page.RouteData.Values["id"].ToString();
                if (!String.IsNullOrEmpty(id))
                {
                    ///address
                    ContactUsRepository repo = new ContactUsRepository();
                    ContactWay          cnw  = repo.Findcwy(1);
                    BlogAddress.InnerText = cnw.Adrees;
                    BlogMail.InnerText    = cnw.Email;
                    BlogPhone.InnerText   = cnw.PhoneNumber;
                    /////////////
                    ArticleRepository ART  = new ArticleRepository();
                    Article           post = ART.FindeArticleByID(id.ToInt());
                    PageTitle.InnerText = post.Title;
                    //META
                    HtmlMeta meta2 = new HtmlMeta();
                    meta2.Name    = "KeyWords";
                    meta2.Content = post.KeyWords.Replace('\n', ' ');
                    MetaPlaceHolder.Controls.Add(meta2);
                    HtmlMeta meta = new HtmlMeta();
                    meta.Name    = "Description";
                    meta.Content = post.Abstract.Replace('\n', ' ');
                    MetaPlaceHolder.Controls.Add(meta);
                    //Article
                    if (post.Image != null)
                    {
                        ImageTag.Src = post.Image;
                    }
                    DivPostDate.InnerText  = post.PostDateTime;
                    DivHeadTitle.InnerText = post.Title;
                    DivTitle.InnerText     = post.Title;
                    DivBody.InnerHtml      = post.Content;

                    string[] words = post.Tags.Split(',');
                    string   text  = "";
                    foreach (string word in words)
                    {
                        text += " <div class='badge badge-pill badge-warning myTag'>" + word + "</div>";
                    }
                    DivTags.InnerHtml = text;
                    //Recent
                    List <Article> ArticleList = ART.LatestArticles();
                    text = "";
                    foreach (Article article in ArticleList)
                    {
                        text += "<div class='media recentblog'><div class='media-left'><a href = '" + "/Blog/" + article.ArticleID + "/" + article.Title.Replace(' ', '-') + "'><img src='" + article.ImgFirstPage + "' runat='server'  alt='عکس' class='img - responsive'/></a></div><div class='media-body'><a href = '" + "/Blog/" + article.ArticleID + "/" + article.Title.Replace(' ', '-') + "'><h5 class='media-heading'>" + article.Title + "</h5></a></div></div>";
                    }

                    DivRecenPosts.InnerHtml = text;
                }
                else
                {
                    Response.Redirect("/Blogs");
                }
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["adminid"] != null)
            {
                if (!IsPostBack)
                {
                    if (Session["PostIDForEdit"] != null)
                    {
                        int id = Session["PostIDForEdit"].ToString().ToInt();
                        Session.Add("newPostIDForEdit", id);
                        Session.Remove("PostIDForEdit");
                        ArticleRepository repArt = new ArticleRepository();
                        GroupsRepository  repo   = new GroupsRepository();
                        Article           art    = repArt.FindeArticleByID(id);
                        title.Text    = art.Title;
                        Abstract.Text = art.Abstract;
                        editor1.Text  = art.Content;
                        KeyWords.Text = art.KeyWords;
                        Tags.Text     = art.Tags;
                        SelectedSubGroups.DataSource     = repo.FindSubGroupsOfAnArticle(id);
                        SelectedSubGroups.DataTextField  = "Title";
                        SelectedSubGroups.DataValueField = "GroupID";
                        SelectedSubGroups.DataBind();
                        for (int i = 0; i < SelectedSubGroups.Items.Count; i++)
                        {
                            if (SelectedSubGroups.Items[i].Value == "-1")
                            {
                                SelectedSubGroups.Items[i].Text = "گروه : " + SelectedSubGroups.Items[i].Text;
                            }
                        }

                        DDLGroups.DataSource     = repo.LoadAllGroups();
                        DDLGroups.DataTextField  = "Title";
                        DDLGroups.DataValueField = "GroupID";
                        DDLGroups.DataBind();
                        DDLGroups.Items.Insert(0, new ListItem("یک گروه انتخاب کنید", "-2"));
                        oldPhoto.ImageUrl = art.ImgFirstPage;
                    }
                    else
                    {
                        Response.Redirect("/Admin/ManageBlogs");
                    }
                }
            }
            else
            {
                Response.Redirect("/AdminLogin");
            }
        }
        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);
                }
            }
        }
Beispiel #4
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);
                }
            }
        }