protected void lnkSubscribeForTopic_Click(object sender, EventArgs e)
        {
            if (CurrentUserSession != null && ViewedTopicID != -1)
            {
                GroupTopic groupTopic = GroupTopic.Fetch(ViewedTopicID);

                if (groupTopic != null)
                {
                    var groupTopicSubscription = new GroupTopicSubscription(CurrentUserSession.Username, ViewedTopicID,
                                                                            GroupID)
                                                     {DateUpdated = groupTopic.DateUpdated};
                    groupTopicSubscription.Save();
                }
            }
        }
        /// <summary>
        /// Fetches group topic subscriptions by specified arguments.
        /// It returns an empty array if there are no group topic subscriptions in DB by specified arguments.
        /// If these arguments are null it returns all group topic subscriptions from DB.
        /// </summary>
        /// <param name="id">The ID.</param>
        /// <param name="username">The username.</param>
        /// <param name="groupTopicID">The group topic ID.</param>
        /// <param name="groupID">The group ID.</param>
        /// <param name="dateUpdated">The date updated.</param>
        /// <returns></returns>
        public static GroupTopicSubscription[] Fetch(int? id, string username, int? groupTopicID, int? groupID, DateTime? dateUpdated)
        {
            //using (var conn = Config.DB.Open())
            {
                List<GroupTopicSubscription> lGroupTopicSubscriptions = new List<GroupTopicSubscription>();

                using (var reader = SqlHelper.GetDB().ExecuteReader("FetchGroupTopicSubscriptions",
                                                                    id, username, groupTopicID, groupID, dateUpdated))
                {

                    while (reader.Read())
                    {
                        GroupTopicSubscription groupTopicSubscription = new GroupTopicSubscription();

                        groupTopicSubscription.id = (int) reader["ID"];
                        groupTopicSubscription.username = (string) reader["Username"];
                        groupTopicSubscription.groupTopicID = (int) reader["GroupTopicID"];
                        groupTopicSubscription.groupID = (int) reader["GroupID"];
                        groupTopicSubscription.dateUpdated = (DateTime) reader["DateUpdated"];

                        lGroupTopicSubscriptions.Add(groupTopicSubscription);
                    }
                    reader.Close();
                }

                return lGroupTopicSubscriptions.ToArray();
            }
        }
Example #3
0
        protected void btnStartNewTopic_Click(object sender, EventArgs e)
        {
            string topicName = Config.Misc.EnableBadWordsFilterGroups ? Parsers.ProcessBadWords(txtTopicName.Text.Trim()) : txtTopicName.Text.Trim();
            string post = Config.Misc.EnableBadWordsFilterGroups ? Parsers.ProcessBadWords(txtPost.Text.Trim()) : txtPost.Text.Trim();

            if (topicName.Length == 0)
            {
                type = eType.AddTopic;

                lblError.Text = Lang.Trans("Please enter topic name.");
                return;
            }

            if (post.Length == 0)
            {
                type = eType.AddTopic;

                lblError.Text = Lang.Trans("Please enter post text.");
                return;
            }

            #region Find unclosed [quote] tags

            int openQuotesCount = Regex.Matches(post, @"\[quote", RegexOptions.IgnoreCase).Count;
            int closedQuotesCount = Regex.Matches(post, @"\[/quote", RegexOptions.IgnoreCase).Count;
            if (openQuotesCount != closedQuotesCount)
            {
                type = eType.AddTopic;
                this.post = post;

                lblError.Text = Lang.Trans("Please close all [quote] tags with a [/quote] tag!");
                return;
            }

            #endregion

            if (CurrentUserSession != null)
            {
                var groupTopic = new GroupTopic(GroupID, CurrentUserSession.Username)
                                     {
                                         Name = topicName,
                                         Posts = 1,
                                         Locked = cbLocked.Checked
                                     };

                if (cbCreatePoll.Checked)
                {
                    if (validatePollsChoices())
                    {
                        groupTopic.IsPoll = true;
                    }
                    else
                    {
                        lblError.Text = Lang.Trans("Please enter at least one choice!");
                        return;
                    }
                }

                if (cbSticky.Checked)
                {
                    if (!DatePicker1.ValidDateEntered)
                    {
                        lblError.Text = Lang.Trans("Please select date!");
                        return;
                    }
                    groupTopic.StickyUntil = DatePicker1.SelectedDate;
                }

                groupTopic.Save();

                User.AddScore(CurrentUserSession.Username, Config.UserScores.NewTopic, "NewTopic");

                var groupPost = new GroupPost(groupTopic.ID, CurrentUserSession.Username) {Post = post};
                groupPost.Save();

                #region Subscribe automatically for this topic

                GroupTopicSubscription groupTopicSubscription = new GroupTopicSubscription(CurrentUserSession.Username, groupTopic.ID, GroupID);
                groupTopicSubscription.DateUpdated = groupTopic.DateUpdated;
                groupTopicSubscription.Save();

                #endregion

                #region create a poll

                if (cbCreatePoll.Checked)
                {
                    foreach (RepeaterItem item in rptChoices.Items)
                    {
                        TextBox txtChoice = item.FindControl("txtChoice") as TextBox;

                        if (txtChoice != null && txtChoice.Text.Trim() != String.Empty)
                        {
                            GroupPollsChoice choice = new GroupPollsChoice(groupTopic.ID);
                            choice.Answer = txtChoice.Text.Trim();
                            choice.Save();
                        }
                    }
                }

                #endregion

                #region Add NewGroupTopic Event

                Event newEvent = new Event(CurrentUserSession.Username);

                newEvent.FromGroup = GroupID;
                newEvent.Type = Event.eType.NewGroupTopic;
                NewGroupTopic newGroupTopic = new NewGroupTopic();
                newGroupTopic.GroupTopicID = groupTopic.ID;
                newEvent.DetailsXML = Misc.ToXml(newGroupTopic);

                newEvent.Save();

                Group group = Group.Fetch(groupTopic.GroupID);

                string[] usernames = User.FetchMutuallyFriends(CurrentUserSession.Username);

                foreach (string friendUsername in usernames)
                {
                    if (Config.Users.NewEventNotification)
                    {
                        if (group != null)
                        {
                            string text =
                                String.Format("Your friend {0} has posted a new topic {1} in the {2} group".Translate(),
                                              "<b>" + CurrentUserSession.Username + "</b>",
                                              Server.HtmlEncode(groupTopic.Name), Server.HtmlEncode(group.Name));
                            int imageID = 0;
                            try
                            {
                                imageID = Photo.GetPrimary(CurrentUserSession.Username).Id;
                            }
                            catch (NotFoundException)
                            {
                                imageID = ImageHandler.GetPhotoIdByGender(CurrentUserSession.Gender);
                            }
                            string thumbnailUrl = ImageHandler.CreateImageUrl(imageID, 50, 50, false, true, true);
                            User.SendOnlineEventNotification(CurrentUserSession.Username, friendUsername, text,
                                                             thumbnailUrl,
                                                             UrlRewrite.CreateShowGroupTopicsUrl(
                                                                 groupTopic.ID.ToString()));
                        }
                    }
                }

                GroupMember[] groupMembers = GroupMember.Fetch(GroupID, true);

                foreach (GroupMember groupMember in groupMembers)
                {
                    // A user should not receive events for their topics
                    if (groupMember.Username == CurrentUserSession.Username) continue;

                    if (Config.Users.NewEventNotification)
                    {
                        if (group != null)
                        {
                            string text =
                                String.Format("There is a new topic {0} in the {1} group".Translate(),
                                              "<b>" + Server.HtmlEncode(groupTopic.Name) + "</b>",
                                              Server.HtmlEncode(group.Name));
                            string thumbnailUrl = GroupIcon.CreateImageUrl(group.ID, 50, 50, true);
                            User.SendOnlineEventNotification(CurrentUserSession.Username, groupMember.Username, text,
                                                             thumbnailUrl, UrlRewrite.CreateShowGroupUrl(group.ID.ToString()));
                        }
                    }
                }

                #endregion
            }

            Response.Redirect(UrlRewrite.CreateShowGroupTopicsUrl(GroupID.ToString()));
            
        }