Exemple #1
0
        /// <summary>
        /// Fetches Profile Choice from the DB
        /// </summary>
        /// <param name="Id">Id of the choice</param>
        /// <returns>ProfileChoice object</returns>
        /// <exception cref="NotFoundException">No choice was found with the requested Id</exception>
        public static ProfileChoice Fetch(int Id)
        {
            string cacheKey = String.Format("ProfileChoice_Fetch_{0}", Id);
            if (HttpContext.Current != null && HttpContext.Current.Cache[cacheKey] != null)
            {
                return HttpContext.Current.Cache[cacheKey] as ProfileChoice;
            }

            //using (var conn = Config.DB.Open())
            {
                var choice = new ProfileChoice { id = Id };

                using (var reader = SqlHelper.GetDB().ExecuteReader("FetchProfileChoice", Id))
                {

                    if (reader.Read())
                    {
                        choice.questionId = (int) reader["QuestionID"];
                        choice._value = (string) reader["Value"];
                        reader.Close();
                    }
                    else
                    {
                        throw new NotFoundException
                            (Lang.Trans("The requested choice does not exist!"));
                    }
                }
                if (HttpContext.Current != null)
                {
                    //Global.AddCacheItem("ProfileChoices", cacheKey, choice);
                    HttpContext.Current.Cache.Insert(cacheKey, choice, null, Cache.NoAbsoluteExpiration,TimeSpan.FromHours(1), CacheItemPriority.NotRemovable, null);
                }

                return choice;
            }
        }
Exemple #2
0
        /// <summary>
        /// Fetches Profile Choices from the DB for the specified question
        /// </summary>
        /// <param name="QuestionID">ID of the question</param>
        /// <returns>Array of ProfileChoice objects</returns>
        public static ProfileChoice[] FetchByQuestionID(int QuestionID)
        {
            string cacheKey = String.Format("ProfileChoice_FetchByQuestionID_{0}", QuestionID);
            if (HttpContext.Current != null && HttpContext.Current.Cache[cacheKey] != null)
            {
                return HttpContext.Current.Cache[cacheKey] as ProfileChoice[];
            }

            //using (var conn = Config.DB.Open())
            {
                var lChoices = new List<ProfileChoice>();

                using (var reader = SqlHelper.GetDB().ExecuteReader("FetchProfileChoiceByQuestion", QuestionID))
                {

                    while (reader.Read())
                    {
                        var choice = new ProfileChoice
                            {
                                id = ((int) reader["ID"]),
                                questionId = QuestionID,
                                _value = ((string) reader["Value"])
                            };

                        lChoices.Add(choice);
                    }
                    reader.Close();
                }
                ProfileChoice[] result = lChoices.ToArray();
                if (HttpContext.Current != null)
                {
                    HttpContext.Current.Cache.Insert(cacheKey, result, null, Cache.NoAbsoluteExpiration,
                                                     TimeSpan.FromHours(1), CacheItemPriority.NotRemovable, null);
                }

                if (result.Length > 0)
                {
                    return result;
                }
                return null;
            }
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!HasWriteAccess)
                return;
            
            ProfileQuestion question = ProfileQuestion.Fetch(questionID);
            question.Name = txtName.Text;
            question.AltName = txtAltName.Text;
            question.Description = txtDescription.Text;
            question.Hint = txtHint.Text;
            question.EditStyle = (ProfileQuestion.eEditStyle)
                                 Enum.Parse(typeof (ProfileQuestion.eEditStyle), dropEditStyle.SelectedValue);
            question.ShowStyle = (ProfileQuestion.eShowStyle)
                                 Enum.Parse(typeof (ProfileQuestion.eShowStyle), dropShowStyle.SelectedValue);
            question.SearchStyle = (ProfileQuestion.eSearchStyle)
                                   Enum.Parse(typeof (ProfileQuestion.eSearchStyle), dropSearchStyle.SelectedValue);
            question.Required = cbRequired.Checked;
            question.RequiresApproval = cbRequiresApproval.Checked;
            question.VisibleForPaidOnly = cbVisibleOnlyForPaidMembers.Checked;
            question.EditableForPaidOnly = cbEditableOnlyByPaidMembers.Checked;
            question.VisibleForMale = cbVisibleToMale.Checked;
            question.VisibleForFemale = cbVisibleToFemale.Checked;
            question.VisibleForCouple = cbVisibleToCouple.Checked;
            question.MatchField = ddMatchFieldQuestion.SelectedValue != "-1"
                                      ? (int?) Convert.ToInt32(ddMatchFieldQuestion.SelectedValue)
                                      : null;

            foreach (DataGridItem item in dgChoices.Items)
            {
                HtmlInputCheckBox cbSelect =
                    (HtmlInputCheckBox) item.FindControl("cbSelect");
                TextBox txtValue = (TextBox) item.FindControl("txtValue");

                string choiceID = cbSelect.Value;
                string _value = txtValue.Text;

                if (_value.Trim() != String.Empty)
                {
                    ProfileChoice choice;

                    if (choiceID.IndexOf("Temp") == -1)
                    {
                        choice = new ProfileChoice(Convert.ToInt32(choiceID));
                    }
                    else
                    {
                        choice = new ProfileChoice();
                    }

                    choice.QuestionID = questionID;
                    choice.Value = _value;
                    choice.Save();
                }
                Choices = null;
            }

            question.ParentQuestionChoices = null;
            question.ParentQuestionID = null;
            if (ddCondition.SelectedValue != "-1")
            {
                foreach (ListItem item in cblConditionAnswers.Items)
                {
                    if (item.Selected) question.ParentQuestionChoices += item.Text + ":";
                }
                if (question.ParentQuestionChoices != null)
                    question.ParentQuestionChoices = question.ParentQuestionChoices.TrimEnd(':');
                else
                {
                    MessageBox.Show(Lang.TransA("Please select conditional answers!"), Misc.MessageType.Error);
                    return;
                }
                question.ParentQuestionID = Convert.ToInt32(ddCondition.SelectedValue);
            }

            question.Save();

            try
            {
                ProfileQuestion[] questions = null;
                questions = ProfileTopic.Fetch(question.TopicID).FetchQuestions();
                if (questions != null)
                {
                    ProfileQuestion[] childQuestions = questions.Where(q => q.ParentQuestionID.HasValue && q.ParentQuestionID.Value == question.Id).ToArray();
                    if (childQuestions.Length > 0) // this question is parent
                    {
                        ProfileChoice[] parentChoices = question.FetchChoices();
                        if (parentChoices != null)
                        {
                            List<string> lParentChoices = new List<string>();
                            foreach (ProfileChoice parentChoice in parentChoices)
                                lParentChoices.Add(parentChoice.Value);

                            foreach (ProfileQuestion childQuestion in childQuestions)
                            {
                                string[] childParentQuestionChoices = childQuestion.ParentQuestionChoices.Split(new string[] { ":" },
                                                                                      StringSplitOptions.RemoveEmptyEntries);
                                if (childParentQuestionChoices.Any(c => !lParentChoices.Contains(c)))
                                {
                                    childQuestion.ParentQuestionChoices = String.Empty;
                                    var existedChoices =
                                        childParentQuestionChoices.Where(c => lParentChoices.Contains(c));
                                    foreach (string existedChoice in existedChoices)
                                        childQuestion.ParentQuestionChoices += existedChoice + ":";
                                    childQuestion.ParentQuestionChoices = childQuestion.ParentQuestionChoices.TrimEnd(':');
                                    childQuestion.Save();
                                }
                            }
                        }
                    }
                }
            }
            catch (NotFoundException)
            {
            }

            string url = String.Format("EditTopic.aspx?tid={0}", question.TopicID);
            Response.Redirect(url);
        }