Exemplo n.º 1
0
        /// <summary>
        /// Maps the given post type to an archive model.
        /// </summary>
        /// <param name="api">The current api</param>
        /// <param name="type">The post type</param>
        /// <param name="page">The requested page</param>
        /// <param name="year">Optional year</param>
        /// <param name="month">Optional month</param>
        /// <returns>The archive model</returns>
        private static T Map <T>(Api api, Piranha.Models.PostType type, int?page, int?year, int?month) where T : ArchiveModel
        {
            if (type != null)
            {
                var model = Activator.CreateInstance <T>();

                Mapper.Map <Piranha.Models.PostType, ArchiveModel>(type, model);

                model.Year  = year;
                model.Month = month;
                model.Page  = page.HasValue ? page.Value : 1;

                // Build query
                Expression <Func <Piranha.Models.Post, bool> > query = null;
                var now = DateTime.Now;

                if (year.HasValue)
                {
                    DateTime from;
                    DateTime to;

                    if (month.HasValue)
                    {
                        from = new DateTime(year.Value, month.Value, 1);
                        to   = from.AddMonths(1);
                    }
                    else
                    {
                        from = new DateTime(year.Value, 1, 1);
                        to   = from.AddYears(1);
                    }
                    query = p => p.TypeId == type.Id && p.Published <= now && p.Published >= from && p.Published < to;
                }
                else
                {
                    query = p => p.TypeId == type.Id && p.Published <= now;
                }

                // Get posts
                var posts = api.Posts.Get(where : query).Select(p => p.Id).ToList();
                var count = posts.Count();

                // Get pages
                model.TotalPages = Math.Max(Convert.ToInt32(Math.Ceiling((double)count / Config.Site.ArchivePageSize)), 1);
                model.Page       = Math.Min(model.Page, model.TotalPages);

                for (var n = (model.Page - 1) * Config.Site.ArchivePageSize; n < Math.Min(model.Page * Config.Site.ArchivePageSize, count); n++)
                {
                    model.Posts.Add(PostModel.GetById(posts[n]));
                }

                return(model);
            }
            return(null);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Saves the current post type.
        /// </summary>
        /// <param name="api">The current api</param>
        public void Save(Api api)
        {
            var newModel = false;

            var type = api.PostTypes.GetSingle(where : t => t.Id == Id);

            if (type == null)
            {
                type     = new Piranha.Models.PostType();
                newModel = true;
            }

            Mapper.Map <EditModel, Piranha.Models.PostType>(this, type);

            if (newModel)
            {
                api.PostTypes.Add(type);
            }
            api.SaveChanges();

            this.Id = type.Id;
        }
Exemplo n.º 3
0
        /// <summary>
        /// Starts the MVC application.
        /// </summary>
        protected void Application_Start()
        {
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            AreaRegistration.RegisterAllAreas();

            // Initialize the application instance with the ASP.NET runtime
            Piranha.App.Init(c => {
                c.Cache    = new Piranha.AspNet.Cache.HttpCache();
                c.Env      = new Piranha.AspNet.Env();
                c.Mail     = new Piranha.AspNet.Mail.SmtpMail();
                c.Security = new Piranha.AspNet.Security.SimpleSecurity("admin", "password");
                c.Store    = new Piranha.EntityFramework.Store();
            });

            #region Seed test data
            //
            // Let's get some default data going
            //
            using (var api = new Piranha.Api()) {
                // Only seed if we don't have any authors
                var seed = !api.Authors.Get().Any();

                if (!seed)
                {
                    return;
                }
                var author = api.Authors.GetSingle(@where: a => a.Name == "Håkan Edling");
                if (author == null)
                {
                    author = new Piranha.Models.Author()
                    {
                        Name  = "Håkan Edling",
                        Email = "*****@*****.**"
                    };
                    api.Authors.Add(author);
                    api.SaveChanges();
                }

                // Post type
                var type = api.PostTypes.GetSingle(@where: t => t.Slug == "blog");
                if (type == null)
                {
                    type = new Piranha.Models.PostType()
                    {
                        Name            = "Blog post",
                        EnableArchive   = true,
                        ArchiveTitle    = "Blog",
                        Slug            = "blog",
                        MetaKeywords    = "Piranha CMS, .NET, MVC, CMS, Blog",
                        MetaDescription = "Read the latest toughts and rambles about your favourite framework."
                    };
                    api.PostTypes.Add(type);
                    api.SaveChanges();
                }

                // Page type
                var pageType = api.PageTypes.GetSingle(@where: t => t.Slug == "standard");
                if (pageType == null)
                {
                    pageType = new Piranha.Models.PageType()
                    {
                        Name = "Standard"
                    };
                    api.PageTypes.Add(pageType);
                    api.SaveChanges();
                }

                // Categories
                var cat = api.Categories.GetSingle(@where: c => c.Slug == "development");
                if (cat == null)
                {
                    cat = new Piranha.Models.Category()
                    {
                        Title = "Development"
                    };
                    api.Categories.Add(cat);
                    api.SaveChanges();
                }

                // Posts
                var post = api.Posts.GetSingle(@where: p => p.Slug == "my-first-post");
                if (post == null)
                {
                    post = new Piranha.Models.Post()
                    {
                        AuthorId    = author.Id,
                        TypeId      = type.Id,
                        Title       = "My first post",
                        Keywords    = "Piranha CMS, Blog, First",
                        Description = "The first post of the blog",
                        Excerpt     = "Maecenas faucibus mollis interdum. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit. Morbi leo risus, porta ac consectetur ac, vestibulum at eros.",
                        Body        = "<p>Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Etiam porta sem malesuada magna mollis euismod. Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum. Morbi leo risus, porta ac consectetur ac, vestibulum at eros. Nullam quis risus eget urna mollis ornare vel eu leo.</p>" +
                                      "<p>Curabitur blandit tempus porttitor. Cras mattis consectetur purus sit amet fermentum. Aenean lacinia bibendum nulla sed consectetur. Sed posuere consectetur est at lobortis. Morbi leo risus, porta ac consectetur ac, vestibulum at eros. Vestibulum id ligula porta felis euismod semper.</p>",
                        Published = DateTime.Now
                    };

                    post.Categories.Add(cat);

                    api.Posts.Add(post);
                    api.SaveChanges();

                    api.Comments.Add(new Piranha.Models.Comment()
                    {
                        PostId     = post.Id,
                        Author     = "Håkan Edling",
                        Email      = "*****@*****.**",
                        IsApproved = true,
                        WebSite    = "http://piranhacms.org",
                        Body       = "I hope you enjoy this new version of Piranha CMS. Remember to give me your feedback at the GitHub repo."
                    });
                    api.SaveChanges();
                }

                // Pages
                var page = api.Pages.GetSingle(@where: p => p.Slug == "welcome-to-piranha-cms");
                if (page == null)
                {
                    // Get startpage body from resource file
                    string body;
                    using (var reader = new StreamReader(AppDomain.CurrentDomain.BaseDirectory + @"\App_Data\Import\startcontent.html")) {
                        body = reader.ReadToEnd();
                    }

                    // Create page
                    page = new Piranha.Models.Page()
                    {
                        AuthorId    = author.Id,
                        TypeId      = pageType.Id,
                        Title       = "Welcome to Piranha vNext",
                        IsHidden    = true,
                        Keywords    = "Piranha CMS, CMS, ASP.NET MVC, ASP.NET WebPages, Entity Framework",
                        Description = "Piranha is the fun, fast and lightweight .NET framework for developing cms-based web applications with an extra bite. It's built on ASP.NET MVC 5, Web Pages 3 & Entity Framework 6",
                        Body        = body,
                        Published   = DateTime.Now
                    };
                    api.Pages.Add(page);
                    api.SaveChanges();
                }
            }
            #endregion
        }
Exemplo n.º 4
0
		/// <summary>
		/// Saves the current post type.
		/// </summary>
		/// <param name="api">The current api</param>
		public void Save(Api api) {
			var newModel = false;

			var type = Id.HasValue ? api.PostTypes.GetSingle(Id.Value) : null;
			if (type == null) {
				type = new Piranha.Models.PostType();
				newModel = true;
			}

			Mapper.Map<EditModel, Piranha.Models.PostType>(this, type);

			if (newModel)
				api.PostTypes.Add(type);
			api.SaveChanges();

			this.Id = type.Id;
		}
Exemplo n.º 5
0
		/// <summary>
		/// Starts the MVC application.
		/// </summary>
		protected void Application_Start() {
			RouteConfig.RegisterRoutes(RouteTable.Routes);
			AreaRegistration.RegisterAllAreas();

			// Initialize the application instance with the ASP.NET runtime
			Piranha.App.Init(c => {
				c.Cache = new Piranha.AspNet.Cache.HttpCache();
				c.Env = new Piranha.AspNet.Env();
				c.Mail = new Piranha.AspNet.Mail.SmtpMail();
				c.Security = new Piranha.AspNet.Security.SimpleSecurity("admin", "password");
				c.Store = new Piranha.EntityFramework.Store();
			});

			#region Seed test data
			//
			// Let's get some default data going
			//
			using (var api = new Piranha.Api()) {
				// Only seed if we don't have any authors
				var seed = !api.Authors.Get().Any();

				if (!seed) return;
				var author = api.Authors.GetSingle(@where: a => a.Name == "Håkan Edling");
				if (author == null) {
					author = new Piranha.Models.Author() {
						Name = "Håkan Edling",
						Email = "*****@*****.**"
					};
					api.Authors.Add(author);
					api.SaveChanges();
				}

				// Post type
				var type = api.PostTypes.GetSingle(@where: t => t.Slug == "blog");
				if (type == null) {
					type = new Piranha.Models.PostType() {
						Name = "Blog post",
						EnableArchive = true,
						ArchiveTitle = "Blog",
						Slug = "blog",
						MetaKeywords = "Piranha CMS, .NET, MVC, CMS, Blog",
						MetaDescription = "Read the latest toughts and rambles about your favourite framework."
					};
					api.PostTypes.Add(type);
					api.SaveChanges();
				}

				// Page type
				var pageType = api.PageTypes.GetSingle(@where: t => t.Slug == "standard");
				if (pageType == null) {
					pageType = new Piranha.Models.PageType() {
						Name = "Standard"
					};
					api.PageTypes.Add(pageType);
					api.SaveChanges();
				}

				// Categories
				var cat = api.Categories.GetSingle(@where: c => c.Slug == "development");
				if (cat == null) {
					cat = new Piranha.Models.Category() {
						Title = "Development"
					};
					api.Categories.Add(cat);
					api.SaveChanges();
				}

				// Posts
				var post = api.Posts.GetSingle(@where: p => p.Slug == "my-first-post");
				if (post == null) {
					post = new Piranha.Models.Post() {
						AuthorId = author.Id,
						TypeId = type.Id,
						Title = "My first post",
						Keywords = "Piranha CMS, Blog, First",
						Description = "The first post of the blog",
						Excerpt = "Maecenas faucibus mollis interdum. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit. Morbi leo risus, porta ac consectetur ac, vestibulum at eros.",
						Body = "<p>Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Etiam porta sem malesuada magna mollis euismod. Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum. Morbi leo risus, porta ac consectetur ac, vestibulum at eros. Nullam quis risus eget urna mollis ornare vel eu leo.</p>" +
							   "<p>Curabitur blandit tempus porttitor. Cras mattis consectetur purus sit amet fermentum. Aenean lacinia bibendum nulla sed consectetur. Sed posuere consectetur est at lobortis. Morbi leo risus, porta ac consectetur ac, vestibulum at eros. Vestibulum id ligula porta felis euismod semper.</p>",
						Published = DateTime.Now
					};

					post.Categories.Add(cat);

					api.Posts.Add(post);
					api.SaveChanges();

					api.Comments.Add(new Piranha.Models.Comment() {
						PostId = post.Id,
						Author = "Håkan Edling",
						Email = "*****@*****.**",
						IsApproved = true,
						WebSite = "http://piranhacms.org",
						Body = "I hope you enjoy this new version of Piranha CMS. Remember to give me your feedback at the GitHub repo."
					});
					api.SaveChanges();
				}

				// Pages
				var page = api.Pages.GetSingle(@where: p => p.Slug == "welcome-to-piranha-cms");
				if (page == null) {
					// Get startpage body from resource file
					string body;
					using (var reader = new StreamReader(AppDomain.CurrentDomain.BaseDirectory + @"\App_Data\Import\startcontent.html")) {
						body = reader.ReadToEnd();
					}

					// Create page
					page = new Piranha.Models.Page() {
						AuthorId = author.Id,
						TypeId = pageType.Id,
						Title = "Welcome to Piranha vNext",
						IsHidden = true,
						Keywords = "Piranha CMS, CMS, ASP.NET MVC, ASP.NET WebPages, Entity Framework",
						Description = "Piranha is the fun, fast and lightweight .NET framework for developing cms-based web applications with an extra bite. It's built on ASP.NET MVC 5, Web Pages 3 & Entity Framework 6",
						Body = body,
						Published = DateTime.Now
					};
					api.Pages.Add(page);
					api.SaveChanges();
				}
			}
			#endregion
		}