Exemplo n.º 1
0
        public void UpdateDefaultImage(int newsId, int typeId, string newsImage,
                                       bool autoResize, bool deleteOld)
        {
            LINQ.NewsManager lnMgr      = new LINQ.NewsManager();
            LINQ.NewsDetail  newsDetail = lnMgr.GetNews(newsId);

            string thumbName  = newsDetail.ThumbImage;
            string largeName  = newsDetail.LargeImage;
            string mediumName = newsDetail.MediumImage;

            string path = Folders.NewsImages;

            path = Path.Combine(path, string.Format("News{0}", newsId));
            path = lw.WebTools.WebContext.Server.MapPath("~/" + path);
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            deleteOld = deleteOld || !String.IsNullOrEmpty(newsImage);

            if (deleteOld && !StringUtils.IsNullOrWhiteSpace(newsDetail.LargeImage))
            {
                if (File.Exists(Path.Combine(path, newsDetail.LargeImage)))
                {
                    File.Delete(Path.Combine(path, newsDetail.LargeImage));
                }
                if (!StringUtils.IsNullOrWhiteSpace(newsDetail.ThumbImage) && File.Exists(Path.Combine(path, newsDetail.ThumbImage)))
                {
                    File.Delete(Path.Combine(path, newsDetail.ThumbImage));
                }
                if (!StringUtils.IsNullOrWhiteSpace(newsDetail.MediumImage) && File.Exists(Path.Combine(path, newsDetail.MediumImage)))
                {
                    File.Delete(Path.Combine(path, newsDetail.MediumImage));
                }

                newsDetail.ThumbImage  = "";
                newsDetail.LargeImage  = "";
                newsDetail.MediumImage = "";
            }
            if (File.Exists(newsImage))
            {
                string largePath = Path.Combine(path, "Large");
                if (!Directory.Exists(largePath))
                {
                    Directory.CreateDirectory(largePath);
                }

                string thumbPath = Path.Combine(path, "Thumb");
                if (!Directory.Exists(thumbPath))
                {
                    Directory.CreateDirectory(thumbPath);
                }


                string originalPath = Path.Combine(path, "Original");
                if (!Directory.Exists(originalPath))
                {
                    Directory.CreateDirectory(originalPath);
                }

                string mediumPath;


                string fileName = Path.GetFileNameWithoutExtension(newsImage);
                string ext      = Path.GetExtension(newsImage);
                thumbName  = fileName + "-s.jpg";               // +ext;
                largeName  = fileName + "-l.jpg";               // +ext;
                mediumName = fileName + "-m.jpg";               // +ext;
                largePath  = Path.Combine(path, largeName);
                thumbPath  = Path.Combine(path, thumbName);
                mediumPath = Path.Combine(path, mediumName);

                if (!File.Exists(largePath))
                {
                    File.Copy(newsImage, largePath);
                }
                File.Delete(newsImage);

                Dimension largeSize = null, mediumSize = null, thumbSize = null;

                DataView newsType = GetNewsTypes("TypeId=" + typeId.ToString());
                if (newsType.Count > 0)
                {
                    DataRowView type = newsType[0];

                    largeSize  = new Dimension(type["LargeSize"].ToString());
                    mediumSize = new Dimension(type["MediumSize"].ToString());
                    thumbSize  = new Dimension(type["ThumbSize"].ToString());
                }

                Config cfg = new Config();

                if (!largeSize.Valid)
                {
                    largeSize = new Dimension(cfg.GetKey(lw.CTE.Settings.News_DefaultImageSize));
                }
                if (!mediumSize.Valid)
                {
                    mediumSize = new Dimension(cfg.GetKey(lw.CTE.Settings.News_MediumImageSize));
                }
                if (!thumbSize.Valid)
                {
                    thumbSize = new Dimension(cfg.GetKey(lw.CTE.Settings.News_ThumbImageSize));
                }



                // Thumb get Croped for cases that use precise dimensions (Width AND Height)
                // Medium Resized depending on the the Width or the Height
                // Large Resize only if AutoResize Checked

                if (thumbSize.Valid)
                {
                    string resizeType = cfg.GetKey(Settings.ThumbImageResizeType);

                    if (resizeType.ToLower() == lw.CTE.Enum.ImageResizeType.Resize.ToString().ToLower())
                    {
                        ImageUtils.Resize(largePath, thumbPath, thumbSize.IntWidth, thumbSize.IntHeight);
                    }
                    else
                    {
                        ImageUtils.CropImage(largePath, thumbPath, thumbSize.IntWidth, thumbSize.IntHeight, ImageUtils.AnchorPosition.Default);
                    }
                }
                if (mediumSize.Valid)
                {
                    string resizeType = cfg.GetKey(Settings.MediumImageResizeType);

                    if (resizeType.ToLower() == lw.CTE.Enum.ImageResizeType.Resize.ToString().ToLower())
                    {
                        ImageUtils.Resize(largePath, mediumPath, mediumSize.IntWidth, mediumSize.IntHeight);
                    }
                    else
                    {
                        ImageUtils.CropImage(largePath, mediumPath, mediumSize.IntWidth, mediumSize.IntHeight, ImageUtils.AnchorPosition.Default);
                    }
                }
                if (largeSize.Valid && autoResize)
                {
                    string resizeType = cfg.GetKey(Settings.News_DefaultImageResizeType);

                    if (resizeType.ToLower() == lw.CTE.Enum.ImageResizeType.Crop.ToString().ToLower())
                    {
                        ImageUtils.CropImage(largePath, largePath, largeSize.IntWidth, largeSize.IntHeight, ImageUtils.AnchorPosition.Default);
                    }
                    else
                    {
                        ImageUtils.Resize(largePath, largePath, largeSize.IntWidth, largeSize.IntHeight);
                    }
                }

                newsDetail.ThumbImage  = thumbName;
                newsDetail.LargeImage  = largeName;
                newsDetail.MediumImage = mediumName;
            }

            lnMgr.Save();
        }
Exemplo n.º 2
0
        /// <summary>
        /// change the ranking for the current record and updates the other ones accordingly.
        /// </summary>
        /// <param name="id"></param>
        /// <param name="d"></param>
        /// <param name="ss"></param>
        /// <param name="pid"></param>
        /// <returns></returns>
        public void UpdateRanking(int id, int d, int ss, int?pid)
        {
            if ((d != 1) && (d != -1))
            {
                throw new Exception("Direction value incorrect");
            }
            IQueryable <New>      allNews  = null;
            IQueryable <NewsType> allTypes = null;

            if (pid.HasValue && pid.Value > 0)
            {
                NewsManager nMgr           = new NewsManager();
                New         selectedRecord = nMgr.NewsData.News.SingleOrDefault(x => x.NewsId == id);
                if (selectedRecord == null)
                {
                    throw new Exception("Record not found");
                }
                allNews = from newsTypeView in nMgr.NewsData.News
                          where
                          newsTypeView.NewsType == pid
                          select newsTypeView;
                // increasing priority
                if (d == -1)
                {
                    //select all where priority > currentPriority and limiting results to maximum the stepsize
                    var recordsToUpdate = (from s in allNews
                                           where s.Ranking > selectedRecord.Ranking
                                           orderby s.Ranking
                                           select s).Take(ss);
                    if (recordsToUpdate.Count() > 0)
                    {
                        int maxPriority = Convert.ToInt32(recordsToUpdate.Max(x => x.Ranking));
                        foreach (var s in recordsToUpdate)
                        {
                            s.Ranking--;
                        }
                        selectedRecord.Ranking = maxPriority;
                        nMgr.Save();
                    }
                }
                // lowering priority
                if (d == 1)
                {
                    if (selectedRecord == null)
                    {
                        throw new Exception("Record not found");
                    }
                    //select all where priority < currentPriority and limiting results to maximum the stepsize
                    var recordsToUpdate = (from s in allNews
                                           where s.Ranking < selectedRecord.Ranking
                                           orderby s.Ranking descending
                                           select s).Take(ss);
                    if (recordsToUpdate.Count() > 0)
                    {
                        int minimumPriority = Convert.ToInt32(recordsToUpdate.Min(x => x.Ranking));
                        foreach (var s in recordsToUpdate)
                        {
                            s.Ranking++;
                        }
                        selectedRecord.Ranking = minimumPriority;
                        nMgr.Save();
                    }
                }
            }
            else
            {
                NewsType selectedRecord = GetType(id);
                allTypes = from newsType in GetTypes()
                           where
                           newsType.ParentId == 12
                           select newsType;
                if (selectedRecord == null)
                {
                    throw new Exception("Record not found");
                }
                // increasing priority
                if (d == -1)
                {
                    //select all where priority > currentPriority and limiting results to maximum the stepsize
                    var recordsToUpdate = (from s in allTypes
                                           where s.Ranking > selectedRecord.Ranking
                                           orderby s.Ranking
                                           select s).Take(ss);
                    if (recordsToUpdate.Count() > 0)
                    {
                        int maxPriority = Convert.ToInt32(recordsToUpdate.Max(x => x.Ranking));
                        foreach (var s in recordsToUpdate)
                        {
                            s.Ranking--;
                        }
                        selectedRecord.Ranking = maxPriority;
                        this.Save();
                    }
                }
                // lowering priority
                if (d == 1)
                {
                    //select all where priority < currentPriority and limiting results to maximum the stepsize
                    var recordsToUpdate = (from s in allTypes
                                           where s.Ranking < selectedRecord.Ranking
                                           orderby s.Ranking descending
                                           select s).Take(ss);
                    if (recordsToUpdate.Count() > 0)
                    {
                        int minimumPriority = Convert.ToInt32(recordsToUpdate.Min(x => x.Ranking));
                        foreach (var s in recordsToUpdate)
                        {
                            s.Ranking++;
                        }
                        selectedRecord.Ranking = minimumPriority;
                        this.Save();
                    }
                }
            }
        }