public ActionResult InsertGalleryFromLeaf(int ParentGalleryId, string name, string PreviewGallery)
        {
            var gal = new Gallery
            {
                ParentId = ParentGalleryId,
                DateCreated = DateTime.Now,
                Name = name,
                Description = "",
                Order = 999,
                Year = DateTime.Now.Year.ToString(),
                OwnerId = UserSession.OwnerId,
            };

            var isPreviewGallery = false;

            if (!string.IsNullOrEmpty(PreviewGallery) && bool.TryParse(PreviewGallery, out isPreviewGallery) && isPreviewGallery)
            {
                gal.GalleryType = (int) GalleryTypes.Preview;
            }
            else
            {
                gal.GalleryType = (int) GalleryTypes.Content;
            }

            var newId = GalleryService.Insert(gal);

            return RedirectToAction("ProcessUploadedPhotos");
        }
        //EDIT|UPDATE
        private List<SelectListItem> GetListForGalleryUpdate(Gallery gallery)
        {
            var gals = GalleryService.GetGalleriesForUser(UserSession.OwnerId);
            var retList = new List<SelectListItem>();

            foreach(var gal in gals)
            {
                retList.Add(new SelectListItem { Selected = gal.GalleryId == gallery.ParentId, Text = gal.Name, Value = gal.GalleryId.ToString() });
            }
            return retList;
        }
        private string GetPreviewUrlsJson(Gallery childGallery)
        {
            var retList = new List<PreviewImage>();
            if (childGallery.PreviewPhotos != null && childGallery.PreviewPhotos.Any())
            {
                foreach (var previewPhoto in childGallery.PreviewPhotos)
                {
                    retList.Add(new PreviewImage
                        {
                            ImageId = previewPhoto.PhotoId,
                            ImagePath = PhotoService.GetPhotoURL(previewPhoto.PhotoId, "square200")
                        });
                }
            }

            return JsonSerializer.SerializeToString(retList);
        }
        public ActionResult GalleryEdit(GalleryEdit galEdit, string hdnPreviewPhotosShadow, string hdnPhotosShadow, string hdnTrashShadow, string hdnIsRootGallery)
        {
            var allOk = true;
            if (ModelState.IsValid)
            {
                try
                {
                    var gal = new Gallery
                        {
                            DateCreated = DateTime.Now,
                            Name = galEdit.Name,
                            Description = galEdit.Description,
                            Order = galEdit.Order,
                            Year = galEdit.Year,
                            OwnerId = UserSession.OwnerId,
                            GalleryType = (int)GalleryTypes.Content
                        };

                    if (galEdit.ParentGalleryId == "0")
                    {
                        gal.ParentId = null;
                    }
                    else
                    {
                        gal.ParentId = int.Parse(galEdit.ParentGalleryId);
                    }

                    if (galEdit.PreviewGallery)
                    {
                        gal.GalleryType = (int) GalleryTypes.Preview;
                    }

                    if (hdnIsRootGallery.ToLower() == "true")
                    {
                        gal.GalleryType = (int) GalleryTypes.Root;
                    }

                    if (galEdit.GalleryId.HasValue) //UPDATE
                    {
                        gal.GalleryId = galEdit.GalleryId.Value;
                        gal = ProcessGalleryPhotos(gal, hdnPreviewPhotosShadow, hdnPhotosShadow, hdnTrashShadow);
                        GalleryService.Update(gal);
                        galEdit.AddOKMessage(string.Format("Update galerie {0} proběhl úspěšně.", gal.Name));
                    }
                    else //INSERT
                    {
                        var newId = GalleryService.Insert(gal);
                        galEdit.AddOKMessage(string.Format("Uložení nové galerie {0} proběhlo úspěšně.", gal.Name));
                        galEdit.GalleryId = newId;
                    }
                }
                catch (Exception ex)
                {
                    allOk = false;
                    galEdit.AddErrorMessage("Při ukládání galerie došlo k chybě: " + ex.Message);
                }
            }
            else
            {
                allOk = false;
                galEdit.AddErrorMessage("Některá povinná položka není vyplněná.");
            }

            GalleryEdit retModel;

            if (allOk)
            {
                var editedGallery = GalleryService.GetById(galEdit.GalleryId.Value);
                retModel = MapGalleryToGalleryEdit(editedGallery);
            }
            else
            {
                retModel = galEdit;
            }

            if(galEdit.ErrorMessages.Count > 0)
            {
                foreach (var errorMessage in galEdit.ErrorMessages)
                {
                    retModel.AddErrorMessage(errorMessage);
                }
            }

            if(galEdit.OKMessages.Count > 0)
            {
                foreach(var okMessage in galEdit.OKMessages)
                {
                    retModel.AddOKMessage(okMessage);
                }
            }

            return View(retModel);
        }
 public void UpdateGallery(Gallery gallery)
 {
     Update(gallery);
 }
 public void DeleteGallery(Gallery gallery)
 {
     Delete(gallery);
 }
 private void DeleteGallery(Gallery gallery)
 {
     _galleryRepo.DeleteGallery(gallery);
 }
 public void EnsurePhotoTypes(Gallery gallery, string[] types)
 {
     if (gallery != null && gallery.GalleryPhotos != null && gallery.GalleryPhotos.Any())
     {
         foreach (var type in types)
         {
             var photoType = PhotoService.GetByPhotoTypeName(type);
             foreach (var photo in gallery.GalleryPhotos)
             {
                 if (!PhotoService.PhotoTypeExist(photo.Photo, photoType))
                 {
                     PhotoService.CreateTypeOfPhoto(photo.Photo, photoType);
                 }
             }
         }
     }
 }
 public void Update(Gallery gallery)
 {
     _baseService.Cacher.RemoveAll(new[] { GALLERIES_ALL, GALLERIES_BY_OWNERID.Fmt(gallery.OwnerId), GALLERY_BY_ID.Fmt(gallery.GalleryId) });
     UpdateGalleryPhotos(gallery.GalleryId, gallery.GalleryPhotos);
     _galleryRepo.UpdateGallery(gallery);
 }
        private void CreateTrashGallery(Owner owner)
        {
            var trashGal = new Gallery
            {
                DateCreated = DateTime.Now,
                Description = "Trash galerie uživatele {0}.".Fmt(owner.OwnerName),
                GalleryType = (int)GalleryTypes.Trash,
                Name = "Trash galerie uživatele {0}.".Fmt(owner.OwnerName),
                Order = 9999,
                OwnerId = owner.OwnerId,
                ParentId = null,
                Year = DateTime.Now.Year.ToString()
            };

            _galleryRepo.InsertGallery(trashGal);
        }
 public int Insert(Gallery gallery)
 {
     _baseService.Cacher.RemoveAll(new []{GALLERIES_ALL, GALLERIES_BY_OWNERID.Fmt(gallery.OwnerId)});
     return _galleryRepo.InsertGallery(gallery);
 }
        public List<GalleryPhoto> GetGalleryPhotos(Gallery gallery)
        {
            var galleryPhotos = _galleryRepo.GetGalleryPhotos(gallery.GalleryId);
            if (galleryPhotos != null && galleryPhotos.Any())
            {
                galleryPhotos.ForEach(gp => { gp.Photo = PhotoService.GetPhoto(gp.PhotoId); });
            }

            if (gallery.PreviewPhotos != null && gallery.PreviewPhotos.Any())
            {
                gallery.PreviewPhotos.ForEach(p => p.Photo = PhotoService.GetPhoto(p.PhotoId));
            }

            return galleryPhotos;
        }
        public List<GalleryBreadcrumb> GenerateGalleryBreadcrumb(Gallery gallery)
        {
            var retColl = new List<GalleryBreadcrumb>();
            var currentGallery = gallery;
            var owner = UserService.GetOwnerById(gallery.OwnerId);
            var baseDir = owner.OwnerDirectory;

            while (currentGallery.GalleryType != (int)GalleryTypes.Root)
            {
                retColl.Add(new GalleryBreadcrumb
                    {
                        GalleryId = currentGallery.GalleryId,
                        GalleryName = currentGallery.Name,
                        UserBaseDir = baseDir,
                        ViewType = currentGallery.GalleryType == (int) GalleryTypes.Content ? "show" : "preview"
                    });

                if (currentGallery.ParentId.HasValue)
                {
                    currentGallery = GetById(currentGallery.ParentId.Value);
                }
            }

            //now currentGallery must be root, but check just to be sure:
            if (currentGallery.GalleryType == (int) GalleryTypes.Root)
            {
                retColl.Add(new GalleryBreadcrumb
                {
                    GalleryId = currentGallery.GalleryId,
                    GalleryName = currentGallery.Name,
                    UserBaseDir = baseDir,
                    ViewType = currentGallery.GalleryType == (int)GalleryTypes.Content ? "show" : "preview"
                });
            }

            return retColl;
        }
        private GalleryEdit MapGalleryToGalleryEdit(Gallery gal)
        {
            var trashGalleryPhotos = GalleryService.GetTrashGallery(gal.OwnerId).GalleryPhotos;

            return new GalleryEdit
                {
                    GalleryId = gal.GalleryId,
                    Name = gal.Name,
                    Description = gal.Description,
                    ParentGalleryId = gal.ParentId.HasValue ? gal.ParentId.Value.ToString() : "0",
                    GalleryList = GetListForGalleryUpdate(gal),
                    GalleryListForPreviewGalleries = GetListForGalleryUpdate(),
                    Order = gal.Order,
                    Year = gal.Year,
                    PreviewGallery = gal.GalleryType == (int)GalleryTypes.Preview,
                    IsRootGallery = gal.GalleryType == (int)GalleryTypes.Root,
                    //Diaries = gal.Diaries,
                    Photos = gal.GalleryPhotos,
                    PreviewPhotos = gal.PreviewPhotos,
                    TrashPhotos = trashGalleryPhotos
                };
        }
 /*
 public List<Photo> GetGalleryPhotos(int galleryId)
 {
     using (IDbConnection conn = _db.OpenDbConnection())
     {
         var photos = conn.Select<Photo>(p => Sql.In(p.PhotoId, PhotoIdsInGallery(galleryId)));
         return photos;
     }
 }
 */
 public int InsertGallery(Gallery gallery)
 {
     using (IDbConnection conn = _db.OpenDbConnection())
     {
         conn.Insert(gallery);
         return (int)conn.GetLastInsertId();
     }
 }
        private Gallery ProcessGalleryPhotos(Gallery gallery, string previewPhotos, string photos, string trash)
        {
            var previewPhotosList = new List<GalleryPhoto>();
            var photosList = new List<GalleryPhoto>();
            var separator = new[] {','};

            if (!string.IsNullOrEmpty(previewPhotos) && previewPhotos.Length > 1)
            {
                previewPhotos = previewPhotos.Trim(separator);
                var previewPhotosArr = previewPhotos.Split(separator);

                for (int i = 0; i < previewPhotosArr.Length; i++)
                {
                    previewPhotosList.Add(new GalleryPhoto{GalleryId = gallery.GalleryId, PhotoId = int.Parse(previewPhotosArr[i]), Order = i+1});
                }

                gallery.PreviewPhotos = previewPhotosList;
            }

            if (!string.IsNullOrEmpty(photos) && photos.Length > 1)
            {
                photos = photos.Trim(separator);
                var photosArr = photos.Split(separator);

                for (int i = 0; i < photosArr.Length; i++)
                {
                    photosList.Add(new GalleryPhoto{GalleryId = gallery.GalleryId, PhotoId = int.Parse(photosArr[i]), Order = i+1});
                }

                gallery.GalleryPhotos = photosList;
            }

            if (!string.IsNullOrEmpty(trash) && trash.Length > 1)
            {
                trash = trash.Trim(separator);
                var trashArr = trash.Split(separator, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();

                if (trashArr.Length > 0)
                {
                    GalleryService.ClearTrashGallery(UserSession.OwnerId);
                    GalleryService.AddPhotosToGallery(GalleryService.GetTrashGallery(UserSession.OwnerId).GalleryId, trashArr);
                }
            }

            return gallery;
        }
        public void Delete(Gallery gallery, bool movePhotosToTrash)
        {
            var galleryPhotos = _galleryRepo.GetGalleryPhotos(gallery.GalleryId);
            if (galleryPhotos != null && galleryPhotos.Any())
            {
                var trashGallery = GetTrashGallery(gallery.OwnerId);
                var photoIdsToRemove = new List<int>();
                foreach (var galleryPhoto in galleryPhotos)
                {
                    galleryPhoto.GalleryId = trashGallery.GalleryId;
                    photoIdsToRemove.Add(galleryPhoto.PhotoId);
                }

                if (movePhotosToTrash)
                {
                    _galleryRepo.AddPhotosToGallery(galleryPhotos);
                }
                _galleryRepo.RemovePhotosFromGallery(photoIdsToRemove, gallery.GalleryId);
            }

            DeleteGallery(gallery);
        }