Exemple #1
0
        /// <summary>
        /// Saves the given model.
        /// </summary>
        /// <param name="model">The model</param>
        /// <param name="publish">The state of the model</param>
        private static void SaveModel(Piranha.Models.Manager.PageModels.EditModel model, bool publish)
        {
            var js = new JavaScriptSerializer();

            using (var db = new Db()) {
                var translation = db.PageTranslations
                                  .Include(p => p.Regions)
                                  .Where(p => p.PageId == model.Page.Id && p.IsDraft != publish)
                                  .SingleOrDefault();

                if (translation == null)
                {
                    translation = new Entities.PageTranslation()
                    {
                        Id      = Guid.NewGuid(),
                        PageId  = model.Page.Id,
                        IsDraft = !publish,
                        Culture = CultureInfo.CurrentUICulture.Name
                    };
                    db.PageTranslations.Add(translation);
                }

                // Map page values
                translation.Title           = model.Page.Title;
                translation.NavigationTitle = model.Page.NavigationTitle;
                translation.Keywords        = model.Page.Keywords;
                translation.Description     = model.Page.Description;

                // Delete old region translations for simplicity
                while (translation.Regions.Count > 0)
                {
                    db.RegionTranslations.Remove(translation.Regions[0]);
                }

                // Map regions
                for (var n = 0; n < model.Regions.Count; n++)
                {
                    var region = model.Regions[n];

                    var reg = new Entities.RegionTranslation()
                    {
                        Id         = Guid.NewGuid(),
                        PageId     = translation.Id,
                        RegionId   = region.Id,
                        TemplateId = region.RegiontemplateId,
                        IsDraft    = !publish,
                        Type       = region.Body.GetType().FullName,
                        Culture    = CultureInfo.CurrentUICulture.Name
                    };
                    translation.Regions.Add(reg);

                    if (region.Body is IHtmlString)
                    {
                        reg.Body = ((IHtmlString)region.Body).ToHtmlString();
                    }
                    else
                    {
                        reg.Body = js.Serialize(region.Body);
                    }
                }
                db.SaveChanges();
            }
        }
		/// <summary>
		/// Saves the given model.
		/// </summary>
		/// <param name="model">The model</param>
		/// <param name="publish">The state of the model</param>
		private static void SaveModel(Piranha.Models.Manager.PageModels.EditModel model, bool publish) {
			var js = new JavaScriptSerializer();

			using (var db = new Db()) {
				var translation = db.PageTranslations
					.Include(p => p.Regions)
					.Where(p => p.PageId == model.Page.Id && p.IsDraft != publish)
					.SingleOrDefault();

				if (translation == null) {
					translation = new Entities.PageTranslation() {
						Id = Guid.NewGuid(),
						PageId = model.Page.Id,
						IsDraft = !publish,
						Culture = CultureInfo.CurrentUICulture.Name
					};
					db.PageTranslations.Add(translation);
				}

				// Map page values
				translation.Title = model.Page.Title;
				translation.NavigationTitle = model.Page.NavigationTitle;
				translation.Keywords = model.Page.Keywords;
				translation.Description = model.Page.Description;

				// Delete old region translations for simplicity
				while (translation.Regions.Count > 0)
					db.RegionTranslations.Remove(translation.Regions[0]);

				// Map regions
				for (var n = 0; n < model.Regions.Count; n++) {
					var region = model.Regions[n];

					var reg = new Entities.RegionTranslation() {
						Id = Guid.NewGuid(),
						PageId = translation.Id,
						RegionId = region.Id,
						TemplateId = region.RegiontemplateId,
						IsDraft = !publish,
						Type = region.Body.GetType().FullName,
						Culture = CultureInfo.CurrentUICulture.Name
					};
					translation.Regions.Add(reg);

					if (region.Body is IHtmlString)
						reg.Body = ((IHtmlString)region.Body).ToHtmlString();
					else reg.Body = js.Serialize(region.Body);
				}
				db.SaveChanges();
			}
		}