Esempio n. 1
0
        public bool UpdateNews(int newsId, string uniqueName, string title, string header, string content, string date,
                               int NewsType, short Language, byte Status, bool DeleteFile,
                               HttpPostedFile RelatedFile,
                               HttpPostedFile DefaultImage, bool autoResizePicture, bool deleteOldPicture,
                               int?Ranking, int?UserRating, int?Views, DateTime?publishDate)
        {
            NewsDs _ds = new NewsDs();

            DataRow news = this.GetNews(string.Format("NewsId={0}", newsId))[0].Row;

            string oldImage = NewsPicture(newsId);

            IDataAdapter Adp = base.GetAdapter(cte.NewsAdp);

            NewsDs.NewsRow row = _ds.News.NewNewsRow();

            row.NewsId = newsId;
            _ds.News.AddNewsRow(row);
            row.AcceptChanges();

            row.Title = title;

            if (String.IsNullOrEmpty(uniqueName) || (uniqueName == news["UniqueName"].ToString() && uniqueName == StringUtils.ToURL(news["Title"].ToString())))
            {
                uniqueName = StringUtils.ToURL(title);
            }

            row.UniqueName = uniqueName;
            row.Header     = header;
            row.NewsText   = content;
            if (date != null && date != "")
            {
                row.NewsDate = DateTime.Parse(date);
            }

            row.NewsType     = NewsType;
            row.NewsLanguage = Language;
            row.Status       = Status;
            row.NewsFile     = string.Format("{0}", news["NewsFile"]);
            row.DateModified = DateTime.Now;
            row.ModifierId   = WebContext.Profile.UserId;

            if (Ranking != null)
            {
                row.Ranking = Ranking.Value;
            }
            if (UserRating != null)
            {
                row.UserRating = UserRating.Value;
            }
            if (Views != null)
            {
                row.Views = Views.Value;
            }
            if (publishDate != null)
            {
                row.PublishDate = publishDate.Value;
            }

            base.UpdateData(Adp, _ds);

            string path = "";

            try
            {
                if (!StringUtils.IsNullOrWhiteSpace(oldImage) && title != news["Title"].ToString())
                {
                    path = WebContext.Server.MapPath("~/" + lw.CTE.Folders.NewsImages);

                    path = System.IO.Path.Combine(path, string.Format("News{0}", newsId));

                    path = Path.Combine(path, StringUtils.ToURL(title) + ".JPG");

                    File.Move(WebContext.Server.MapPath(oldImage), path);
                }
            }
            catch (Exception Ex)
            {
                ErrorContext.Add("default-picture", "Could not re-copy default picture. " + Ex.Message);
            }

            path = WebContext.Server.MapPath("~/");
            path = Path.Combine(path, Folders.NewsFile);

            DeleteFile = (DeleteFile || (RelatedFile != null && RelatedFile.ContentLength > 0)) &&
                         !StringUtils.IsNullOrWhiteSpace(news["NewsFile"]);

            if (DeleteFile)
            {
                string fileName = Path.Combine(path, news["NewsFile"].ToString());
                if (File.Exists(fileName))
                {
                    File.Delete(fileName);
                }

                row.NewsFile = "";
            }

            if (RelatedFile != null && RelatedFile.ContentLength > 0)
            {
                string ext = StringUtils.GetFileExtension(RelatedFile.FileName);

                string fileName = StringUtils.GetFriendlyFileName(RelatedFile.FileName);

                if (title.Trim() == "")
                {
                    row.Title = fileName;
                }

                fileName = string.Format("{0}_{1}.{2}",
                                         fileName, row.NewsId, ext);

                try
                {
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    RelatedFile.SaveAs(Path.Combine(path, fileName));
                }
                catch (IOException ex)
                {
                    throw new Exception("IO ERROR, Could not save related news file: " + ex.Message);
                }
                catch (Exception ex1)
                {
                    throw ex1;
                }
                row.NewsFile = fileName;
            }

            if (row.NewsFile != string.Format("{0}", news["NewsFile"]))
            {
                base.UpdateData(Adp, _ds);
            }

            try
            {
                string sql = string.Format("Update News set UniqueName=N'{0}' where NewsId={1}",
                                           uniqueName, row.NewsId);
                DBUtils.ExecuteQuery(sql, cte.lib);
            }
            catch (Exception Ex)
            {
                ErrorContext.Add("unique-name", "cannot update unique name. " + Ex.Message);
            }

            if ((DefaultImage != null && DefaultImage.ContentLength > 0) || deleteOldPicture)
            {
                UpdateDefaultImage(row.NewsId, NewsType, DefaultImage, autoResizePicture, deleteOldPicture, row.UniqueName);
            }

            //Search.NewsSearch.UpdateIndex(row.NewsId);

            return(true);
        }
Esempio n. 2
0
        public int AddNews(string uniqueName, string title, string header, string content, string date,
                           int NewsType, short Language, byte Status,
                           HttpPostedFile RelatedFile,
                           HttpPostedFile DefaultImage, bool autoResizePicture, DateTime?publishDate)
        {
            NewsDs _ds = new NewsDs();

            IDataAdapter Adp = base.GetAdapter(cte.NewsAdp);

            NewsDs.NewsRow row = _ds.News.NewNewsRow();

            row.Title    = title;
            row.Header   = header;
            row.NewsText = content;
            if (date != null && date != "")
            {
                row.NewsDate = DateTime.Parse(date);
            }
            else
            {
                row.NewsDate = DateTime.Now;
            }

            if (String.IsNullOrEmpty(uniqueName))
            {
                uniqueName = StringUtils.ToURL(title);
            }

            row.NewsType     = NewsType;
            row.NewsLanguage = Language;
            row.Status       = Status;
            row.DateAdded    = DateTime.Now;
            row.DateModified = DateTime.Now;
            row.CreatorId    = WebContext.Profile.UserId;
            row.ModifierId   = WebContext.Profile.UserId;

            row.Views      = 0;
            row.Ranking    = 0;
            row.UserRating = 0;

            if (publishDate != null)
            {
                row.PublishDate = publishDate.Value;
            }
            else
            {
                row.PublishDate = DateTime.Now;
            }
            row.NewsFile    = "";
            row.ThumbImage  = "";
            row.LargeImage  = "";
            row.MediumImage = "";

            _ds.News.AddNewsRow(row);
            base.UpdateData(Adp, _ds);

            string path = WebContext.Server.MapPath("~/" + lw.CTE.Folders.NewsImages);

            path = System.IO.Path.Combine(path, string.Format("News{0}", row.NewsId));


            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            Directory.CreateDirectory(Path.Combine(path, "Original"));
            Directory.CreateDirectory(Path.Combine(path, "Large"));
            Directory.CreateDirectory(Path.Combine(path, "Thumb"));



            if (RelatedFile != null && RelatedFile.ContentLength > 0)
            {
                string ext = StringUtils.GetFileExtension(RelatedFile.FileName);

                string fileName = StringUtils.GetFriendlyFileName(RelatedFile.FileName);

                if (title.Trim() == "")
                {
                    row.Title = fileName;
                }

                path = WebContext.Server.MapPath("~/");
                path = Path.Combine(path, Folders.NewsFile);

                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }

                fileName = string.Format("{0}_{1}.{2}",
                                         fileName, row.NewsId, ext);

                try
                {
                    RelatedFile.SaveAs(Path.Combine(path, fileName));
                }
                catch (IOException ex)
                {
                    throw new Exception("IO ERROR, Could not save related news file: " + ex.Message);
                }
                catch (Exception ex1)
                {
                    throw ex1;
                }
                row.NewsFile = fileName;
                base.UpdateData(Adp, _ds);
            }

            try
            {
                string sql = string.Format("Update News set UniqueName=N'{0}' where NewsId={1}",
                                           uniqueName, row.NewsId);
                DBUtils.ExecuteQuery(sql, cte.lib);
            }
            catch (Exception Ex)
            {
                ErrorContext.Add("unique-name", "cannot update unique name. " + Ex.Message);
            }
            if (DefaultImage != null && DefaultImage.ContentLength > 0)
            {
                UpdateDefaultImage(row.NewsId, NewsType, DefaultImage, autoResizePicture, false, uniqueName);
            }

            // search need to be checked for the path cannot be value null

            ///		Search.NewsSearch.AddToIndex(row.NewsId);

            return(row.NewsId);
        }