Esempio n. 1
0
        /// <summary>
        /// Saves the edit model.
        /// </summary>
        public bool SaveAll()
        {
            using (IDbTransaction tx = Database.OpenConnection().BeginTransaction()) {
                try {
                    if (Permalink.IsNew)
                    {
                        Permalink.Name = Permalink.Generate(Category.Name);
                    }
                    Permalink.Save(tx);
                    Category.Save(tx);
                    foreach (var ext in Extensions)
                    {
                        // Call OnSave
                        ext.Body.OnManagerSave(Category);

                        ext.ParentId = Category.Id;
                        ext.Save(tx);
                    }
                    tx.Commit();
                } catch { tx.Rollback(); throw; }
            }
            Refresh();

            return(true);
        }
Esempio n. 2
0
        /// <summary>
        /// Saves the model.
        /// </summary>
        /// <returns>Whether the operation succeeded or not</returns>
        public bool SaveAll()
        {
            // Ensure correct naming convention for properties
            for (var n = 0; n < Template.Properties.Count; n++)
            {
                Template.Properties[n] = Template.Properties[n].Replace(" ", "_").Trim();
            }

            using (var tx = Database.OpenTransaction()) {
                // Permalink
                if (Permalink.IsNew && String.IsNullOrEmpty(Permalink.Name))
                {
                    Permalink.Name = Permalink.Generate(Template.Name);
                }
                Permalink.Save(tx);
                Template.Save(tx);

                // Clear all implementing posts from the cache
                var posts = Post.Get("post_template_id = @0", tx, Template.Id);
                foreach (var post in posts)
                {
                    post.InvalidateRecord(post);
                }

                tx.Commit();

                return(true);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Saves the model.
        /// </summary>
        /// <returns>Whether the operation succeeded or not</returns>
        public bool SaveAll()
        {
            using (var tx = Database.OpenTransaction()) {
                // Permalink
                if (Permalink.IsNew && String.IsNullOrEmpty(Permalink.Name))
                {
                    Permalink.Name = Permalink.Generate(Template.Name);
                }
                Permalink.Save(tx);
                Template.Save(tx);

                tx.Commit();

                return(true);
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Saves the edit model.
        /// </summary>
        public bool SaveAll()
        {
            using (IDbTransaction tx = Database.OpenConnection().BeginTransaction()) {
                try {
                    if (Permalink.IsNew)
                    {
                        Permalink.Name = Permalink.Generate(Category.Name);
                    }
                    Permalink.Save(tx);
                    Category.Save(tx);
                    tx.Commit();
                } catch { tx.Rollback(); throw; }
            }
            Refresh();

            return(true);
        }
Esempio n. 5
0
		/// <summary>
		/// Saves the model.
		/// </summary>
		/// <returns>Whether the operation succeeded or not</returns>
		public bool SaveAll() {
			using (var tx = Database.OpenTransaction()) {
				// Permalink
				if (Permalink.IsNew && String.IsNullOrEmpty(Permalink.Name))
					Permalink.Name = Permalink.Generate(Template.Name);
				Permalink.Save(tx);
				Template.Save(tx);

				// Clear all implementing posts from the cache
				var posts = Post.Get("post_template_id = @0", tx, Template.Id);
				foreach (var post in posts)
					post.InvalidateRecord(post);

				tx.Commit();

				return true;
			}
		}
Esempio n. 6
0
        /// <summary>
        /// Saves the page and all of it's related regions.
        /// </summary>
        /// <param name="publish">If the page should be published</param>
        /// <returns>If the operation succeeded</returns>
        public virtual bool SaveAll(bool draft = true)
        {
            using (IDbTransaction tx = Database.OpenConnection().BeginTransaction()) {
                try {
                    bool permalinkfirst = Page.IsNew;

                    // Save permalink first if the page is new
                    if (permalinkfirst)
                    {
                        if (Permalink.IsNew && String.IsNullOrEmpty(Permalink.Name))
                        {
                            Permalink.Name = Permalink.Generate(!String.IsNullOrEmpty(Page.NavigationTitle) ?
                                                                Page.NavigationTitle : Page.Title);
                            var param = SysParam.GetByName("HIERARCHICAL_PERMALINKS");
                            if (param != null && param.Value == "1" && Page.ParentId != Guid.Empty)
                            {
                                var parent = Page.GetSingle(Page.ParentId, true);
                                Permalink.Name = parent.Permalink + "/" + Permalink.Name;
                            }
                        }
                        Permalink.Save(tx);
                    }

                    // Save page
                    if (draft)
                    {
                        Page.Save(tx);
                    }
                    else
                    {
                        Page.SaveAndPublish(tx);
                    }

                    // Save regions & properties
                    Regions.ForEach(r => {
                        r.IsDraft = r.IsPageDraft = true;

                        // Call OnSave
                        r.Body.OnManagerSave(Page);

                        r.Save(tx);
                        if (!draft)
                        {
                            if (Region.GetScalar("SELECT COUNT(region_id) FROM region WHERE region_id=@0 AND region_draft=0", r.Id) == 0)
                            {
                                r.IsNew = true;
                            }
                            r.IsDraft = r.IsPageDraft = false;
                            r.Save(tx);
                        }
                    });
                    Properties.ForEach(p => {
                        p.IsDraft = true;
                        p.Save(tx);
                        if (!draft)
                        {
                            if (Property.GetScalar("SELECT COUNT(property_id) FROM property WHERE property_id=@0 AND property_draft=0", p.Id) == 0)
                            {
                                p.IsNew = true;
                            }
                            p.IsDraft = false;
                            p.Save(tx);
                        }
                    });

                    // Save extensions
                    foreach (var ext in Extensions)
                    {
                        // Call OnSave
                        ext.Body.OnManagerSave(Page);

                        ext.ParentId = Page.Id;
                        ext.Save(tx);
                        if (!draft)
                        {
                            if (Extension.GetScalar("SELECT COUNT(extension_id) FROM extension WHERE extension_id=@0 AND extension_draft=0", ext.Id) == 0)
                            {
                                ext.IsNew = true;
                            }
                            ext.IsDraft = false;
                            ext.Save(tx);
                        }
                    }

                    // Save permalink last if the page isn't new
                    if (!permalinkfirst)
                    {
                        Permalink.Save(tx);
                    }

                    // Change global last modified
                    if (!draft)
                    {
                        Web.ClientCache.SetSiteLastModified(tx);
                    }

                    // Clear cache for all post regions if we're publishing
                    if (!String.IsNullOrEmpty(Page.Permalink) && !draft)
                    {
                        foreach (var reg in Regions)
                        {
                            if (reg.Body is Extend.Regions.PostRegion)
                            {
                                ((Extend.Regions.PostRegion)reg.Body).ClearCache(Page, reg);
                            }
                        }
                    }

                    tx.Commit();

                    if (IsSite)
                    {
                        using (var db = new DataContext()) {
                            var site = db.SiteTrees.Where(s => s.Id == Page.SiteTreeId).Single();
                            site.MetaTitle       = SiteTree.MetaTitle;
                            site.MetaDescription = SiteTree.MetaDescription;
                            db.SaveChanges();
                        }
                        if (!draft)
                        {
                            PageModel.RemoveSitePageFromCache(Page.SiteTreeId);
                            WebPages.WebPiranha.RegisterDefaultHostNames();
                        }
                    }
                } catch { tx.Rollback(); throw; }
            }
            return(true);
        }
Esempio n. 7
0
        /// <summary>
        /// Saves the edit model.
        /// </summary>
        public bool SaveAll(bool draft)
        {
            var context = HttpContext.Current;
            var hasfile = UploadedFile != null || ServerFile != null;

            byte[]    data = null;
            WebClient web  = new WebClient();

            // Check if the original URL has been updated, and if so
            if (!Content.IsNew && !String.IsNullOrEmpty(Content.OriginalUrl))
            {
                var old = Content.GetSingle(Content.Id);
                if (old != null)
                {
                    if (Content.OriginalUrl != old.OriginalUrl)
                    {
                        FileUrl = Content.OriginalUrl;
                    }
                }
            }

            // Download file from web
            if (!hasfile && !String.IsNullOrEmpty(FileUrl))
            {
                data = web.DownloadData(FileUrl);
                Content.OriginalUrl = FileUrl;
                Content.LastSynced  = Convert.ToDateTime(web.ResponseHeaders[HttpResponseHeader.LastModified]);
            }

            var media = new MediaFileContent();

            if (hasfile)
            {
                if (UploadedFile != null)
                {
                    media.Filename    = UploadedFile.FileName;
                    media.ContentType = UploadedFile.ContentType;
                    using (var reader = new BinaryReader(UploadedFile.InputStream)) {
                        media.Body = reader.ReadBytes(Convert.ToInt32(UploadedFile.InputStream.Length));
                    }
                }
                else
                {
                    media.Filename    = ServerFile.Name;
                    media.ContentType = MimeType.Get(ServerFile.Name);
                    using (var stream = ServerFile.OpenRead()) {
                        media.Body = new byte[ServerFile.Length];
                        stream.Read(media.Body, 0, media.Body.Length);
                    }
                }
            }
            else if (data != null)
            {
                media.Filename    = FileUrl.Substring(FileUrl.LastIndexOf('/') + 1);
                media.ContentType = web.ResponseHeaders["Content-Type"];
                media.Body        = data;
            }
            else
            {
                media = null;
            }

            var saved = false;

            if (!Content.IsFolder)
            {
                // Only save permalinks for non-folders
                var filename = !String.IsNullOrEmpty(Content.Filename) ? Content.Filename : (!String.IsNullOrEmpty(media.Filename) ? media.Filename : "");
                if (Permalink.IsNew && String.IsNullOrEmpty(Permalink.Name))
                {
                    Permalink.Name = Permalink.Generate(!Content.IsFolder ? filename : Content.Name, Models.Permalink.PermalinkType.MEDIA);
                }
                try {
                    Permalink.Save();
                } catch (DuplicatePermalinkException) {
                    if (Permalink.IsNew)
                    {
                        Permalink.Name = Content.Id + Permalink.Name.Substring(Permalink.Name.LastIndexOf('.'));
                        Permalink.Save();
                    }
                    else
                    {
                        throw;
                    }
                }
            }
            else
            {
                Content.PermalinkId = Guid.Empty;
            }

            if (draft)
            {
                saved = Content.Save(media);
            }
            else
            {
                saved = Content.SaveAndPublish(media);
            }

            if (saved)
            {
                // Save related information
                Relation.DeleteByDataId(Content.Id);
                List <Relation> relations = new List <Relation>();
                ContentCategories.ForEach(c => relations.Add(new Relation()
                {
                    DataId = Content.Id, RelatedId = c, IsDraft = false, Type = Relation.RelationType.CONTENTCATEGORY
                })
                                          );
                relations.ForEach(r => r.Save());

                // Save extensions
                foreach (var ext in Extensions)
                {
                    // Call OnSave
                    ext.Body.OnManagerSave(Content);

                    ext.ParentId = Content.Id;
                    ext.Save();
                    if (!draft)
                    {
                        if (Extension.GetScalar("SELECT COUNT(extension_id) FROM extension WHERE extension_id=@0 AND extension_draft=0", ext.Id) == 0)
                        {
                            ext.IsNew = true;
                        }
                        ext.IsDraft = false;
                        ext.Save();
                    }
                }
                // Reset file url
                FileUrl = "";

                return(true);
            }
            return(false);
        }
Esempio n. 8
0
        /// <summary>
        /// Saves the model.
        /// </summary>
        /// <returns>Weather the action was successful</returns>
        public bool SaveAll(bool draft = true)
        {
            using (IDbTransaction tx = Database.OpenConnection().BeginTransaction()) {
                try {
                    bool permalinkfirst = Post.IsNew;

                    // Save permalink before the post if this is an insert
                    if (permalinkfirst)
                    {
                        // Permalink
                        if (Permalink.IsNew)
                        {
                            Permalink.Name = Permalink.Generate(Post.Title);
                        }
                        Permalink.Save(tx);
                    }

                    // Post
                    if (draft)
                    {
                        Post.Save(tx);
                    }
                    else
                    {
                        Post.SaveAndPublish(tx);
                    }

                    // Save permalink after the post if this is an update
                    if (!permalinkfirst)
                    {
                        Permalink.Save(tx);
                    }
                    // Properties
                    Properties.ForEach(p => {
                        p.IsDraft = true;
                        p.Save(tx);
                        if (!draft)
                        {
                            if (Property.GetScalar("SELECT COUNT(property_id) FROM property WHERE property_id=@0 AND property_draft=0", p.Id) == 0)
                            {
                                p.IsNew = true;
                            }
                            p.IsDraft = false;
                            p.Save(tx);
                        }
                    });

                    // Update categories
                    Relation.DeleteByDataId(Post.Id, tx, true);
                    List <Relation> relations = new List <Relation>();
                    PostCategories.ForEach(pc => relations.Add(new Relation()
                    {
                        DataId = Post.Id, RelatedId = pc, Type = Relation.RelationType.POSTCATEGORY
                    })
                                           );
                    relations.ForEach(r => r.Save(tx));

                    // Publish categories
                    if (!draft)
                    {
                        Relation.DeleteByDataId(Post.Id, tx, false);
                        relations.ForEach(r => {
                            r.IsDraft = false;
                            r.IsNew   = true;
                        });
                        relations.ForEach(r => r.Save(tx));
                    }
                    tx.Commit();
                } catch { tx.Rollback(); throw; }
            }
            return(true);
        }
Esempio n. 9
0
        /// <summary>
        /// Saves the page and all of it's related regions.
        /// </summary>
        /// <param name="publish">Weather the page should be published</param>
        /// <returns>Weather the operation succeeded</returns>
        public virtual bool SaveAll(bool draft = true)
        {
            using (IDbTransaction tx = Database.OpenConnection().BeginTransaction()) {
                try {
                    bool permalinkfirst = Page.IsNew;

                    // Save permalink first if the page is new
                    if (permalinkfirst)
                    {
                        if (Permalink.IsNew)
                        {
                            Permalink.Name = Permalink.Generate(!String.IsNullOrEmpty(Page.NavigationTitle) ?
                                                                Page.NavigationTitle : Page.Title);
                        }
                        Permalink.Save(tx);
                    }

                    // Save page
                    if (draft)
                    {
                        Page.Save(tx);
                    }
                    else
                    {
                        Page.SaveAndPublish(tx);
                    }

                    // Save regions & properties
                    Regions.ForEach(r => {
                        r.IsDraft = r.IsPageDraft = true;
                        r.Save(tx);
                        if (!draft)
                        {
                            if (Region.GetScalar("SELECT COUNT(region_id) FROM region WHERE region_id=@0 AND region_draft=0", r.Id) == 0)
                            {
                                r.IsNew = true;
                            }
                            r.IsDraft = r.IsPageDraft = false;
                            r.Save(tx);
                        }
                    });
                    Properties.ForEach(p => {
                        p.IsDraft = true;
                        p.Save(tx);
                        if (!draft)
                        {
                            if (Property.GetScalar("SELECT COUNT(property_id) FROM property WHERE property_id=@0 AND property_draft=0", p.Id) == 0)
                            {
                                p.IsNew = true;
                            }
                            p.IsDraft = false;
                            p.Save(tx);
                        }
                    });

                    // Save permalink last if the page isn't new
                    if (!permalinkfirst)
                    {
                        Permalink.Save(tx);
                    }

                    // Change global last modified
                    if (!draft)
                    {
                        Web.ClientCache.SetSiteLastModified(tx);
                    }

                    tx.Commit();
                } catch { tx.Rollback(); throw; }
            }
            return(true);
        }