Copied to clipboard

Flag this post as spam?

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


  • MrFlo 159 posts 403 karma points
    Jul 19, 2012 @ 02:05
    MrFlo
    0

    Custom Data Type: Tag with checkbox list

    Hello,

    I've created a custom data type for tags. I wanted to have all tags in a checkboxes list.
    It's working well but the tag content does not seems to be saved on the node.
    There is only the groupname stored in the page node.

    Can you help me on this? Thank in advance!

    Here is my code:

    namespace CustomTagsDataType
    {
        /// <summary>
        /// This class is used for the actual datatype dataeditor, i.e. the control you will get in the content section of umbraco. 
        /// </summary>
        public class CustomTagsDataType : Panel
        {
            public string Group { get; set; }
            string pageId; 
            public CheckBoxList tagCheckList = new CheckBoxList();
            public void Save()
            {
                CheckBoxList items = tagCheckList;
                pageId = this.Page.Request.QueryString["id"];
                int _nodeID;
                int.TryParse(pageId, out _nodeID);
                int tagId = 0;
                //remove all tags
                umbraco.cms.businesslogic.Tags.Tag.RemoveTagsFromNode(_nodeID, Group);
                //add all tags
                foreach (ListItem li in items.Items)
                {
                    if (li.Selected)
                    {
                        if (li.Value == "0")
                        {
                            // NH 4.7.1 if a tag doesn't have an id associated we'll do a 2nd check in case the XHR request went wrong
                            // from Codeplex 30151
                            tagId = umbraco.cms.businesslogic.Tags.Tag.GetTagId(li.Text, Group);
                            if (tagId == 0)
                                tagId = umbraco.cms.businesslogic.Tags.Tag.AddTag(li.Text, Group);
                            li.Value = tagId.ToString();
                        }
                        else
                        {
                            int.TryParse(li.Value, out tagId);
                        }
                        if (tagId > 0)
                        {
                            umbraco.cms.businesslogic.Tags.Tag.AssociateTagToNode(_nodeID, tagId);
                            
                            tagId = 0;
                            //allTags += "," + li.Text;
                        }
                    }
                }
            }
            public static IEnumerable<Tag> GetTags(int nodeId, string group)
            {
                var sql = @"SELECT cmsTags.id, cmsTags.tag, cmsTags.[group], count(cmsTagRelationShip.tagid) AS nodeCount FROM cmsTags
                      INNER JOIN cmsTagRelationship ON cmsTagRelationShip.tagId = cmsTags.id
                      WHERE cmsTags.[group] = @group AND cmsTagRelationship.nodeid = @nodeid
                      GROUP BY cmsTags.id, cmsTags.tag, cmsTags.[group] ORDER BY cmsTags.tag";
                return ConvertSqlToTags(sql,
                    SqlHelper.CreateParameter("@group", group),
                    SqlHelper.CreateParameter("@nodeid", nodeId));
            }
            public static IEnumerable<Tag> GetTags()
            {
                string sql = @"SELECT cmsTags.id, cmsTags.tag, cmsTags.[group], count(cmsTagRelationShip.tagid) AS nodeCount FROM cmsTags
                                LEFT JOIN cmsTagRelationShip ON cmsTagRelationShip.tagid = cmsTags.id
                                GROUP BY cmsTags.id, cmsTags.tag, cmsTags.[group] ORDER BY cmsTags.tag";
                return ConvertSqlToTags(sql);
            }
            private static ISqlHelper SqlHelper
            {
                get
                {
                    return Application.SqlHelper;
                }
            }
            private static IEnumerable<Tag> ConvertSqlToTags(string sql, params IParameter[] param)
            {
                List<Tag> tags = new List<Tag>();
                using (IRecordsReader rr = SqlHelper.ExecuteReader(sql, param))
                {
                    while (rr.Read())
                    {
                        tags.Add(new Tag(
                            rr.GetInt("id"),
                            rr.GetString("tag"),
                            rr.GetString("group"),
                            rr.GetInt("nodeCount")));
                    }
                }

                return tags;
            }
            /// <summary>
            /// Raises the <see cref="E:System.Web.UI.Control.Init"/> event.
            /// </summary>
            /// <param name="e">An <see cref="T:System.EventArgs"/> object that contains the event data.</param>
            protected override void OnInit(EventArgs e)
            {
                base.OnInit(e);
                Group = this.Group;
                //making sure that we have a ID for context
                pageId = this.Page.Request.QueryString["id"];
                if (string.IsNullOrEmpty(pageId))
                {
                    // we need an empty try/catch as Node.GetCurrent() will throw an exception if we're outside of Umbraco Context
                    try
                    {
                        umbraco.NodeFactory.Node currentNode = umbraco.NodeFactory.Node.GetCurrent();
                        if (currentNode != null)
                        {
                            pageId = currentNode.Id.ToString();
                        }
                    }
                    catch
                    {
                    }
                }
                
                var tags = GetTags();
                var selectedTags = GetTags(int.Parse(pageId), Group);
                foreach (var t in tags)
                {
                    if (t.Group == Group)
                    {                    
                        ListItem li = new ListItem(t.TagCaption, t.Id.ToString());
                        foreach (var st in selectedTags)
                        {
                            if (st.Id == t.Id)
                            {  
                                li.Selected = true;
                                break;
                            }
                        }
                       
                        tagCheckList.Items.Add(li);
                    }
                }
                tagCheckList.RepeatColumns = 4;
                Button btn = new Button {ID="btnSave",Text="Save" };
                btn.Click += new EventHandler(btn_Click);
                
                this.Controls.Add(tagCheckList);
                this.Controls.Add(btn);
            }
            void btn_Click(object sender, EventArgs e)
            {
                Save();
            }
        }
    }

  • MrFlo 159 posts 403 karma points
    Jul 27, 2012 @ 10:57
    MrFlo
    0

    Hi,

    I didn't find what's wrong in this. Somebody can help me ?

    Thanks a lot,

    MrFLo

  • Tom Maton 387 posts 660 karma points
    Jul 28, 2012 @ 14:02
    Tom Maton
    0

    Hi Mr Flo,

    I dont seem to see a

    base.Data.Value = "my value";

    My understanding is that you need this to save your value, or am I just misunderstanding your issue?

    Tom

Please Sign in or register to post replies

Write your reply to:

Draft