protected void btnDelete_Click(object sender, EventArgs e)
    {
        link_id = Convert.ToInt32(Request.QueryString["linkID"]);

        qPtl_Link link = new qPtl_Link(link_id);
        link.Available = "No";
        link.MarkAsDelete = 1;
        link.Update();

        Response.Redirect("links-list.aspx");
    }
    protected void btnSave_OnClick(object sender, System.EventArgs e)
    {
        Page.Validate("form");

        if (Page.IsValid)
        {

            int user_id = Convert.ToInt32(Context.Items["UserID"]);

            if (!String.IsNullOrEmpty(Request.QueryString["linkID"]))
            {
                link_id = Convert.ToInt32(Request.QueryString["linkID"]);
                qPtl_Link link = new qPtl_Link(link_id);

                link.Title = txtName.Text;
                link.Description = txtSummary.Text;
                link.Available = rblAvailable.SelectedValue;
                link.LastModified = DateTime.Now;
                link.LastModifiedBy = user_id;
                link.URL = txtURL.Text;
                if (!String.IsNullOrEmpty(ddlAuthor.SelectedValue))
                    link.AuthorID = Convert.ToInt32(ddlAuthor.SelectedValue);
                if (!String.IsNullOrEmpty(ddlTheme.SelectedValue))
                    link.ThemeID = Convert.ToInt32(ddlTheme.SelectedValue);
                link.Type = ddlType.SelectedValue;
                if (Convert.ToString(ddlType.SelectedValue).Contains("Internal"))
                    link.Source = "internal";
                else
                    link.Source = "external";
                link.UploadedFrom = "manager";
                link.Language = ddlLanguage.SelectedValue;
                link.LinkType = ddlLinkType.SelectedValue;
                link.Update();
            }
            else
            {
                qPtl_Link link = new qPtl_Link();
                link.ScopeID = 1;
                link.Created = DateTime.Now;
                link.CreatedBy = user_id;
                link.LastModified = DateTime.Now;
                link.LastModifiedBy = user_id;
                link.Available = "Yes";
                link.MarkAsDelete = 0;
                link.Title = txtName.Text;
                link.Description = txtSummary.Text;
                link.URL = txtURL.Text;
                link.Available = rblAvailable.SelectedValue;
                link.Language = ddlLanguage.SelectedValue;
                link.LinkType = ddlLinkType.SelectedValue;
                if (!String.IsNullOrEmpty(ddlAuthor.SelectedValue))
                    link.AuthorID = Convert.ToInt32(ddlAuthor.SelectedValue);
                if (!String.IsNullOrEmpty(ddlTheme.SelectedValue))
                    link.ThemeID = Convert.ToInt32(ddlTheme.SelectedValue);
                link.Type = ddlType.SelectedValue;

                if (Convert.ToString(ddlType.SelectedValue).Contains("Internal"))
                    link.Source = "internal";
                else
                    link.Source = "external";
                link.UploadedFrom = "manager";
                link.Insert();

                link_id = link.LinkID;
            }

            // add keywords
            string owner_keywords = string.Empty;
            qPtl_KeywordReference.DeleteKeywordReferencesByContent((int)qSoc_ContentType.Types.Link, link_id);
            foreach (ListItem item in cblKeywords.Items)
            {
                if (item.Selected)
                {
                    if (!String.IsNullOrEmpty(owner_keywords))
                        owner_keywords += "," + item.Text;
                    else
                        owner_keywords += item.Text;
                    qPtl_KeywordReference keyword = new qPtl_KeywordReference();
                    keyword.Available = "Yes";
                    keyword.ScopeID = Convert.ToInt32(Context.Items["ScopeID"]);
                    keyword.KeywordID = Convert.ToInt32(item.Value);
                    keyword.ContentTypeID = (int)qSoc_ContentType.Types.Link;
                    keyword.ReferenceID = link_id;
                    keyword.Created = DateTime.Now;
                    keyword.LastModified = DateTime.Now;
                    keyword.CreatedBy = Convert.ToInt32(Context.Items["UserID"]);
                    keyword.LastModifiedBy = Convert.ToInt32(Context.Items["UserID"]);
                    keyword.MarkAsDelete = 0;
                    keyword.Insert();
                }
            }

            string reserved_keywords = string.Empty;
            foreach (ListItem item in chkTopics.Items)
            {
                if (item.Selected)
                {
                    if (!String.IsNullOrEmpty(reserved_keywords))
                        reserved_keywords += "," + item.Text;
                    else
                        reserved_keywords += item.Text;
                }
            }

            qSoc_Feed feed = new qSoc_Feed((int)qSoc_ContentType.Types.Link, link_id);

            if (!chkDisplayInFeed.Checked && !chkDisplayInExplore.Checked)
            {
                if (feed != null)
                {
                    if (feed.FeedID > 0)
                    {
                        feed.MarkAsDelete = 1;
                        feed.Available = "No";
                        feed.ReservedKeywords = reserved_keywords;
                        if (!chkDisplayInFeed.Checked)
                            feed.VisibleFeed = false;
                        if (!chkDisplayInExplore.Checked)
                            feed.VisibleExplore = false;
                        feed.Update();
                    }
                }
            }
            else if (chkDisplayInFeed.Checked || chkDisplayInExplore.Checked || !string.IsNullOrEmpty(reserved_keywords))
            {
                qPtl_Link link = new qPtl_Link(link_id);

                int p_user_id = user_id;
                if (post_as_user_id > 0)
                    p_user_id = post_as_user_id;

                qPtl_User user = new qPtl_User(p_user_id);
                var u_space = qSoc_UserSpace_View.GetUserSpaces(link.CreatedBy);

                // evaluate title and description
                string p_title = q_Helper.replaceSpecialCharacters(link.Title);
                string p_description = q_Helper.replaceSpecialCharacters(link.Description);

                if (feed != null)
                {
                    if (feed.FeedID > 0)
                    {
                        feed.CreatedBy = user.UserID;
                        feed.Available = link.Available;
                        if (chkMoveToTop.Checked)
                            feed.LastModified = DateTime.Now;
                        feed.LastModifiedBy = user.UserID;
                        feed.MarkAsDelete = link.MarkAsDelete;
                        feed.OwnerMarkAsDelete = link.MarkAsDelete;
                        feed.ContentTypeID = (int)qSoc_ContentType.Types.Link;
                        feed.ReferenceID = link_id;
                        feed.OwnerID = user.UserID;
                        feed.OwnerName = user.UserName;
                        feed.OwnerProfilePic = user.ProfilePict;
                        feed.Type = "link";
                        feed.Title = p_title;
                        feed.URL = link.URL;
                        feed.Description = p_description;
                        feed.Body = p_description;
                        feed.OwnerRole = user.HighestRole;
                        feed.OwnerRoleID = user.HighestRank;
                        feed.OwnerKeywords = owner_keywords;
                        feed.ReservedKeywords = reserved_keywords;
                        feed.UploadedFrom = "manager";
                        if (!String.IsNullOrEmpty(ddlTheme.SelectedValue))
                        {
                            feed.ThemeID = Convert.ToInt32(ddlTheme.SelectedValue);
                            feed.ThemeName = Convert.ToString(ddlTheme.SelectedItem);
                        }
                        if (chkDisplayInFeed.Checked)
                            feed.VisibleFeed = true;
                        else
                            feed.VisibleFeed = false;
                        if (chkDisplayInExplore.Checked)
                            feed.VisibleExplore = true;
                        else
                            feed.VisibleExplore = false;
                        feed.Update();
                    }
                    else
                    {
                        // create new feed item
                        if (u_space != null)
                        {
                            foreach (var s in u_space)
                            {
                                feed.SpaceID = s.SpaceID;
                                feed.SpaceName = s.SpaceShortName;
                            }
                        }
                        feed.ScopeID = Convert.ToInt32(Context.Items["ScopeID"]);
                        feed.Available = link.Available;
                        feed.Created = DateTime.Now;
                        feed.CreatedBy = user.UserID;
                        feed.LastModified = DateTime.Now;
                        feed.LastModifiedBy = user.UserID;
                        feed.MarkAsDelete = link.MarkAsDelete;
                        feed.OwnerMarkAsDelete = link.MarkAsDelete;
                        feed.ContentTypeID = (int)qSoc_ContentType.Types.Link;
                        feed.ReferenceID = link_id;
                        feed.OwnerID = user.UserID;
                        feed.OwnerName = user.UserName;
                        feed.OwnerProfilePic = user.ProfilePict;
                        feed.Type = "link";
                        feed.Title = p_title;
                        feed.Description = p_description;
                        feed.Body = p_description;
                        feed.OwnerRole = user.HighestRole;
                        feed.OwnerRoleID = user.HighestRank;
                        feed.URL = link.URL;

                        feed.VisibleAll = true;
                        feed.VisibleFriends = true;
                        feed.VisibleFollowers = true;
                        feed.VisibleSpace = true;
                        feed.VisiblePrivate = true;
                        feed.VisibleFeed = true;
                        feed.VisibleOwnerFeed = true;
                        feed.VisibleOwnerProfile = true;
                        if (!String.IsNullOrEmpty(ddlTheme.SelectedValue))
                        {
                            feed.ThemeID = Convert.ToInt32(ddlTheme.SelectedValue);
                            feed.ThemeName = Convert.ToString(ddlTheme.SelectedItem);
                        }
                        feed.ReservedKeywords = reserved_keywords;
                        if (!String.IsNullOrEmpty(owner_keywords))
                        {
                            feed.OwnerKeywords = owner_keywords;
                        }
                        feed.UploadedFrom = "manager";
                        if (chkDisplayInFeed.Checked)
                            feed.VisibleFeed = true;
                        else
                            feed.VisibleFeed = false;
                        if (chkDisplayInExplore.Checked)
                            feed.VisibleExplore = true;
                        else
                            feed.VisibleExplore = false;
                        feed.Insert();
                    }
                }
            }

            // redirect to page to add link + keywords
            if (!String.IsNullOrEmpty(Request.QueryString["linkID"]))
            {
                //lblMessage.Text = "*** Record Successfully Updated ***";
                //lblMessageBottom.Text = "*** Record Successfully Updated ***";
                Response.Redirect("links-list.aspx");
            }
            else
            {
                Response.Redirect(Request.Url.ToString() + "?mode=add-successful&linkID=" + link_id);
            }
        }
    }