Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Rob Watkins 370 posts 702 karma points
    Nov 26, 2010 @ 17:33
    Rob Watkins
    0

    Editing diary / news items as a list

    I am creating a diary for a client, using a slightly modified version of kipuseop's DateFolders for 4.5 (http://our.umbraco.org/projects/developer-tools/datefolders) and PDCalendar (http://our.umbraco.org/projects/website-utilities/pdcalendar) for the event date, to get the nice frontend control and recurrences.

    However, the client is used to editing these items in a simple list, and the content tree is a bit of a pain for this sort of thing, so I want them to edit them from the root folder. I figured by far the simplest thing to do would be to simply provide the root folder with a list of items and a "Name" text box, and then just link edit / new functionality off to the Umbraco built in editing, which has proved easier than I expected; I created a datatype with the user control wrapper then just recursively grabbed all events from the root folder and listed them. This has the advantage that if your dated folders are the same document type as your root folder, then you get automatic filtering of the list at each level.

    Code is below in case anyone has any use for it, but I also have a couple of questions, being a newbie :o)

    N.B. The root folder node is hardwired currently, but the plan to get it is to walk up the tree until the parent of the node is not a valid diary folder, at which point you know you are at the diary root.

    Q1: My edit links are simple editContent.aspx?id=1234 href / redirects - is there a more future proof "API" way of getting the edit link?

    Q2: When you create an item, it goes in the current dated folder, then is moved by the DateFolder package to the correct one when the date is edited; this marks the current dated folder as unpublished even if the document is then moved straight out; can I avoid this without having to republish the whole folder?

    Thanks!

    Code behind:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    using System.Text.RegularExpressions;
    
    using umbraco.BusinessLogic;
    using umbraco.cms.businesslogic.web;
    using umbraco.cms.businesslogic.property;
    
    namespace cFront.Umbraco
    {
        public class UDT_Diary : UserControl, umbraco.editorControls.userControlGrapper.IUsercontrolDataEditor
        {
            #region IUsercontrolDataEditor Members
            public object value { get { return ""; } set { } }
            #endregion
    
            protected Repeater DiaryItemsRepeater;
            protected TextBox NameEdit;
            protected RequiredFieldValidator NameVRF;
    
            protected int DiaryRootID { get { return 1476; } } // TODO: Dynamically get root folder node
    
            protected void Page_Load(object sender, EventArgs e)
            {
                ShowItems();
            }
    
            protected void ShowItems()
            {
                List<DocumentProperties> docs = new List<DocumentProperties>();
    
                GetItems(docs, new Document(Convert.ToInt32(Request["id"])));
    
                DiaryItemsRepeater.DataSource = docs;
                DiaryItemsRepeater.DataBind();
            }
    
            protected void GetItems(List<DocumentProperties> docs, Document document)
            {
                if (document.ContentType.Alias == "DiaryItem")
                    docs.Add(new DocumentProperties(document));
    
                foreach (Document child in document.Children)
                    GetItems(docs, child);
            }
    
            protected void CreateItemClicked(object sender, EventArgs e)
            {
                if (NameVRF.IsValid)
                {
                    DocumentType dt = DocumentType.GetByAlias("DiaryItem");
                    User u = User.GetCurrent();
    
                    Document doc = Document.MakeNew(NameEdit.Text, dt, u, DiaryRootID);
                    Response.Redirect("editContent.aspx?id=" + doc.Id.ToString());
                }
            }
    
            protected object V(object dataitem, String prop, string format)
            {
                object res = V(dataitem, prop);
                return res != null ? String.Format(format, res) : null;
            }
    
            protected object V(object dataitem, String prop)
            {
                object res = DataBinder.Eval(dataitem, prop);
    
                if (Regex.IsMatch(prop, "eventDate"))
                {
                    Match m = Regex.Match(Convert.ToString(res), "<pdcstart>(.+?)</pdcstart>");
                    if (m.Success)
                        res = DateTime.Parse(m.Groups[1].Value);
                }
    
                return res;
            }
        }
    
        public class DocumentProperties : Dictionary<String, object>
        {
            public Document Document { get; set; }
    
            public DocumentProperties(Document doc)
            {
                Document = doc;
    
                foreach (Property prop in doc.GenericProperties)
                    this[prop.PropertyType.Alias] = prop.Value;
    
            }
        }
    }

     

    Control:

    <%@ Control Language="C#" AutoEventWireup="true" Inherits="cFront.Umbraco.UDT_Diary" %>
    <asp:Panel ID="pEventDiary" runat="server">
        <div class="udt_editor udt_diary">
            <div class="new_event">
                <asp:RequiredFieldValidator CssClass="error" id="NameVRF" ErrorMessage="You must enter the event title!" Display="Dynamic" ControlToValidate="NameEdit" runat="server"/>
                <asp:TextBox id="NameEdit" runat="server"/>
                <asp:Button OnClick="CreateItemClicked" id="NewButton" Text="Create" runat="server"/>
            </div>
            <table>
                <thead>
                    <tr>
                        <th></th>
                        <th>Date</th>
                        <th>Event</th>
                        <th>Tags</th>
                    </tr>
                </thead>
                <tbody>
            <asp:Repeater id="DiaryItemsRepeater" EnableViewState="false" runat="server">
                <ItemTemplate>
                    <tr>
                        <td><a href="editContent.aspx?id=<%# V(Container.DataItem, "Document.Id") %>">Edit</a>
                        <td><%# V(Container.DataItem, "[eventDate]", "{0:dd MMMM yyyy}") %></td>
                        <td><%# V(Container.DataItem, "Document.Text")  %></td>
                        <td><%# V(Container.DataItem, "[tags]")  %></td>
                    </tr>
                </ItemTemplate>
            </asp:Repeater>
                    </tbody>
            </table>
        </div>
    </asp:Panel>
  • Rob Watkins 370 posts 702 karma points
    Nov 26, 2010 @ 17:44
    Rob Watkins
    0

    Just for completeness, here is the code I used to get the root folder:

            protected void GetRootFolder()
            {
                Document d = new Document(Convert.ToInt32(Request["id"]));
    
                while (d != null && new Document(d.ParentId).ContentType.Alias == "DiaryFolder")
                    d = new Document(d.ParentId);
    
                DiaryRootID = d != null ? d.Id : -1;
            }
    
  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies