private void form2obj(TicketItem obj)
    {
        obj.ThreadId = this.CurrentThreadId;
        obj.Id = this.CurrentId;
        obj.Enabled = true;
        obj.CategoryId = int.Parse(DropCategories.SelectedValue);
        obj.Priority = int.Parse(DropPriority.SelectedValue);

        try { obj.CustomerId = int.Parse(DropCustomers.SelectedValue); }
        catch { }

        obj.TitleTranslations.Clear();
        obj.TitleTranslations.Add(Config.CultureDefault, TxtTitle.Text);

        obj.DescriptionTranslations.Clear();
        obj.DescriptionTranslations.Add(Config.CultureDefault, TxtDescription.Text);
    }
    private void loadDropThreadActions(int threadId)
    {
        DropThreadActions.Items.Clear();

        var o1 = new TicketItem();
        if (threadId > 0)
        {
            o1 = new ItemsManager<TicketItem, TicketItemFilter>(true, true).GetByKey(threadId);

            var status = TicketItem.TicketStatusEnum.Open;
            status = (TicketItem.TicketStatusEnum)int.Parse(o1.Status.ToString());

            DropThreadActions.Items.Add(new ListItem(base.GetLabel("Actions", "--actions--"), ""));

            if (Roles.IsUserInRole("admin")
                || Roles.IsUserInRole(SUPPORT_MANAGER_ROLE)
                || Roles.IsUserInRole(SUPPORT_OPERATOR_ROLE))
            {
                //DropThreadActions.Items.Add(new ListItem(base.GetLabel("Close", "close"), "close"));
                DropThreadActions.Items.Add(new ListItem(base.GetLabel("Working", "in progress"), "working"));
                DropThreadActions.Items.Add(new ListItem(base.GetLabel("Reopen", "re-open"), "open"));
                DropThreadActions.Items.Add(new ListItem(base.GetLabel("Lock", "lock"), "lock"));
            }
            else if (Roles.IsUserInRole(SUPPORT_USER_ROLE))
            {
                if (status == TicketItem.TicketStatusEnum.Locked)
                { }
                else if (status == TicketItem.TicketStatusEnum.Open || status == TicketItem.TicketStatusEnum.WorkInProgress)
                {
                    //DropThreadActions.Items.Add(new ListItem(base.GetLabel("Close", "close"), "close"));
                }
                else if (status == TicketItem.TicketStatusEnum.Closed)
                {
                    DropThreadActions.Items.Add(new ListItem(base.GetLabel("Reopen", "re-open"), "open"));
                }
            }
        }
    }
    private void changeThreadStatus(int threadId, string statusCommand)
    {
        LblOk.Text = "";
        LblErr.Text = "";
        bool sendMessageToUserInserted = false;
        try
        {
            if (!PgnUserCurrent.IsAuthenticated)
                throw new Exception("user not authenticated");

            string previousStatus = "";
            string nextStatus = "";

            var o1 = new TicketItem();
            var man = new ItemsManager<TicketItem, TicketItemFilter>();
            o1 = man.GetByKey(threadId);  //precarico i campi esistenti e nn gestiti dal form

            try { previousStatus = Enum.GetName(typeof(TicketItem.TicketStatusEnum), o1.Status); }
            catch { }

            switch (statusCommand)
            {
                case "close":
                    o1.Status =  (int)TicketItem.TicketStatusEnum.Closed;
                    sendMessageToUserInserted = true;
                    break;
                case "open":
                    o1.Status =  (int)TicketItem.TicketStatusEnum.Open;
                    break;
                case "working":
                    o1.Status = (int)TicketItem.TicketStatusEnum.WorkInProgress;
                    sendMessageToUserInserted = true;
                    break;
                case "lock":
                    o1.Status =  (int)TicketItem.TicketStatusEnum.Locked;
                    break;
                default:
                    break;
            }
            man.Update(o1);

            try { nextStatus = Enum.GetName(typeof(TicketItem.TicketStatusEnum), o1.Status); }
            catch { }

            previousStatus = base.GetLabel(previousStatus, previousStatus);
            nextStatus = base.GetLabel(nextStatus, nextStatus);

            sendMessage(o1.UserAssigned, o1, "MessageTicketTitle", "MessageTicketDescription", "status changed from "+ previousStatus +" to "+ nextStatus);
            sendMessage(filterSystemMessagesManager(o1.CategoryId), o1, "MessageTicketTitle", "MessageTicketDescription", "status changed from " + previousStatus + " to " + nextStatus);
            if (sendMessageToUserInserted && !o1.UserInserted.Equals(o1.UserAssigned))
                sendMessage(o1.UserInserted, o1, "MessageTicketTitle", "MessageTicketDescription", "status changed from " + previousStatus + " to " + nextStatus);

            loadThread(threadId);
        }
        catch (Exception e)
        {
            LblErr.Text = e.Message;
        }
    }
 private string fillMessagePlaceholders(TicketItem ticket, string textToParse, string extra)
 {
     textToParse = textToParse.Replace("[[ItemId]]", ticket.ThreadId.ToString())
         .Replace("[[ItemTitle]]", ticket.Title)
         .Replace("[[ItemDescription]]", ticket.Description)
         .Replace("[[ItemUserUpdated]]", ticket.UserUpdated)
         .Replace("[[ItemDateUpdated]]", ticket.DateUpdated.ToLongDateString())
         .Replace("[[Extra]]", extra);
     return textToParse;
 }
    private void changeAssignedUser(int threadId, string userAssigned)
    {
        LblOk.Text = "";
        LblErr.Text = "";

        try
        {
            if (!PgnUserCurrent.IsAuthenticated)
                throw new Exception("user not authenticated");

            string previousUser = "";
            var o1 = new TicketItem();
            var man = new ItemsManager<TicketItem, TicketItemFilter>(true, true);
            o1 = man.GetByKey(threadId);  //precarico i campi esistenti e nn gestiti dal form
            previousUser = o1.UserAssigned;
            o1.UserAssigned = userAssigned;
            man.Update(o1);

            sendMessage(previousUser, o1, "MessageTicketTitle", "MessageTicketDescription", "user assigned changed from " + previousUser + " to " + userAssigned);
            sendMessage(o1.UserAssigned, o1, "MessageTicketTitle", "MessageTicketDescription", "user assigned changed from " + previousUser + " to " + userAssigned);
            sendMessage(filterSystemMessagesManager(o1.CategoryId), o1, "MessageTicketTitle", "MessageTicketDescription", "user assigned changed from " + previousUser + " to " + userAssigned);

            loadThread(threadId);
        }
        catch (Exception e)
        {
            LblErr.Text = e.Message;
        }
    }
    private void changeCustomer(int threadId, int customerId)
    {
        LblOk.Text = "";
        LblErr.Text = "";

        try
        {
            if (!PgnUserCurrent.IsAuthenticated)
                throw new Exception("user not authenticated");

            var o1 = new TicketItem();
            var man = new ItemsManager<TicketItem, TicketItemFilter>(true, true);
            o1 = man.GetByKey(threadId);  //precarico i campi esistenti e nn gestiti dal form
            o1.CustomerId = customerId;
            man.Update(o1);

            loadThread(threadId);
        }
        catch (Exception e)
        {
            LblErr.Text = e.Message;
        }
    }
    private void sendMessage(List<string> toUsers, TicketItem ticket, string titleLabel, string descriptionLabel, string extra)
    {
        if (toUsers.Count == 0)
            return;

        var msg = new Message();
        msg.Title = fillMessagePlaceholders(ticket, base.GetLabel(titleLabel), extra);
        msg.Description = fillMessagePlaceholders(ticket, base.GetLabel(descriptionLabel), extra);
        msg.ItemId = ticket.Id;
        MessageProvider.SendMessage(toUsers, msg,
            MessageProvider.SendMessageEnum.UserSetting,
            MessageProvider.SendMessageEnum.UserSetting);
    }
 private void sendMessage(string toUsers, TicketItem ticket, string titleLabel, string descriptionLabel, string extra)
 {
     if (string.IsNullOrEmpty(toUsers))
         return;
     sendMessage(Utility.String2List(toUsers, ";"), ticket, titleLabel, descriptionLabel, extra);
 }
    private bool saveForm(bool saveAndClose)
    {
        bool res = false;
        LblErr.Text = "";
        LblOk.Text = "";
        string extraMessage = "";
        bool sendMessageToUserInserted = false;

        try
        {
            var o1 = new TicketItem();
            var man = new ItemsManager<TicketItem, TicketItemFilter>();
            if (CurrentId == 0)
            {
                form2obj(o1);

                if (o1.IsThreadRoot)
                {
                    string role2Add = "";
                    if (Roles.IsUserInRole(SUPPORT_USER_ROLE))
                        role2Add = PgnUserCurrent.Current.UserName;  //role as username

                    o1.ReadAccessType = MenuAccesstype.Registered;
                    o1.ReadRolenames.Add(SUPPORT_OPERATOR_ROLE);
                    o1.ReadRolenames.Add(role2Add);
                    //o1.ReadRolenames.Add(SUPPORT_MANAGER_ROLE);
                }

                o1 = man.Insert(o1);
                if (!o1.IsThreadRoot)
                {
                    var root = new TicketItem();
                    root = man.GetByKey(o1.ThreadId);
                    man.Update(root);   //update dateUpdated
                    extraMessage = "new answer inserted";
                }
                else
                    extraMessage = "new ticket inserted";

                //copy temp attachments in item folder
                new FilesGallery("~/Public/Files/items/", o1.Id.ToString()).MoveTempFiles();

                loadGrid();
            }
            else
            {
                o1 = man.GetByKey(CurrentId);
                form2obj(o1);
                man.Update(o1);
                extraMessage = "ticket updated";
            }

            if (saveAndClose)
            {
                changeThreadStatus(o1.ThreadId, "close");
                extraMessage = "ticket closed";
            }

            sendMessage(o1.UserAssigned, o1, "MessageTicketTitle", "MessageTicketDescription", extraMessage);
            sendMessage(filterSystemMessagesManager(o1.CategoryId), o1, "MessageTicketTitle", "MessageTicketDescription", extraMessage);

            if (ChkSendEmailToUserInserted.Visible == false)
                sendMessageToUserInserted = true;
            else
                sendMessageToUserInserted = ChkSendEmailToUserInserted.Checked;

            if (sendMessageToUserInserted && !o1.UserInserted.Equals(o1.UserAssigned))
                sendMessage(o1.UserInserted, o1, "MessageTicketTitle", "MessageTicketDescription", extraMessage);

            loadThread(o1.ThreadId);
            LblOk.Text = Utility.GetLabel("RECORD_SAVED_MSG");
            res = true;
        }
        catch (CustomException e1)
        {
            LblErr.Text = e1.CustomMessage;
        }
        catch (Exception e1)
        {
            LblErr.Text = Utility.GetLabel("RECORD_ERR_MSG") + "<br />" + e1.ToString();
        }
        finally
        {
        }
        return res;
    }
    private void obj2form(TicketItem obj)
    {
        Utility.SetDropByValue(DropCategories, obj.ThreadRoot.CategoryId.ToString());
        Utility.SetDropByValue(DropPriority, obj.ThreadRoot.CustomInt2.ToString());
        Utility.SetDropByValue(DropCustomers, obj.ThreadRoot.CustomInt3.ToString());
        Utility.SetDropByValue(DropTemplates, "0");

        TxtTitle.Text = obj.ThreadRoot.Title;

        string description = "";
        obj.DescriptionTranslations.TryGetValue(Config.CultureDefault, out description);
        TxtDescription.Text = description;

        ChkSendEmailToUserInserted.Text = "";
        if (string.IsNullOrEmpty(obj.ThreadRoot.UserInserted))
            ChkSendEmailToUserInserted.Text = PgnUserCurrent.Current.Email;
        else
        {
            var member = Membership.GetUser(obj.ThreadRoot.UserInserted);
            if (member != null)
            {
                if (Utility.IsValidEmail(member.Email))
                    ChkSendEmailToUserInserted.Text = member.Email;
            }
        }
    }
    private void loadThread(int ticketItemdId)
    {
        this.CurrentThreadId = ticketItemdId;
        loadDropThreadActions(ticketItemdId);
        loadDropAssignedUser(ticketItemdId);
        loadDropThreadOrder();

        var currItem = new TicketItem();
        if (ticketItemdId > 0)
            currItem = new ItemsManager<TicketItem, TicketItemFilter>(true, true)
                .GetByKey(ticketItemdId);

        if (currItem.Status == (int)TicketItem.TicketStatusEnum.Locked)
            BtnReply.Visible = false;
        else
            BtnReply.Visible = true;

        int counter = 0;
        string status = Enum.GetName(typeof(TicketItem.TicketStatusEnum), currItem.Status);
        status = base.GetLabel(status, status);
        string userAssigned = base.GetLabel("NotAssigned", "not assigned");
        if (!string.IsNullOrEmpty(currItem.UserAssigned))
            userAssigned = currItem.UserAssigned;

        ListString = base.GetLabel("Status", "Status") + ": <i>" + status + "</i><br />";
        ListString += base.GetLabel("AssignedTo", "Assigned to") + ": <i>" + userAssigned + "</i><br />";
        if (currItem.CustomerId > 0)
            ListString += base.GetLabel("Customer", "Customer") + ": <i>" + currItem.Customer.CompanyName + "</i><br />";
        ListString += base.GetLabel("TicketId", "Ticket ID") + ": <i>" + currItem.Id + "</i><br />";

        string flagged = "";
        if (currItem.Flagged)
            flagged += "<img src='"+ Utility.GetThemedImageSrc("starOn.png") +"' alt='flag' />&nbsp;";

        ListString += "<div class='itemTitle'>"+ flagged + base.GetLabel("Subject", "Subject") + ": " + currItem.Title + "</div>";

        var comparer = new Item.ItemComparer("", this.ThreadSortDirection);
        currItem.ThreadList.Sort(comparer);

        ListString += "<ul class='"+ base.BaseModule.CssClass +"'>";
        for (int i = 0; i < currItem.ThreadList.Count; i++)
        {
            var item = currItem.ThreadList[i];
            string imgUrl = "";
            string link = "";
            string sFiles = "";
            string sImages = "";
            string cssClass = base.BaseModule.CssClass;

            if (item.Files.Count > 0)
            {
                sFiles = "<ul class='" + base.BaseModule.CssClass + "'>";
                foreach (var file in item.Files)
                {
                    sFiles += "<li class='" + base.BaseModule.CssClass + "'>"
                    + "<a class='" + base.BaseModule.CssClass + "' href='" + file.FileUrl + "' "
                    + Utility.AddTracking(file.FileUrl, this.StaticFilesTracking) + " target='blank'>"
                    + "<img src='" + PhotoManager.GetFileIconSrc(file) + "' />"
                    + "<span>" + file.FileName + "</span>"
                    + "</a>"
                    + "</li>";
                }
                sFiles += "</ul>";
            }
            if (item.Images.Count > 0)
            {
                sImages = "<ul class='" + base.BaseModule.CssClass + "'>";
                foreach (var file in item.Images)
                {
                    sImages += "<li class='" + base.BaseModule.CssClass + "'>"
                    + "<a class='" + base.BaseModule.CssClass + "' href='" + file.FileUrl + "' "
                    + Utility.AddTracking(file.FileUrl, this.StaticFilesTracking) + " target='blank'>"
                    + "<span>" + file.FileName + "</span>"
                    + "</a>"
                    + "</li>";
                }
                sImages += "</ul>";
            }

            //link = base.GetLinkAddress(item);
            ListString += "<li class='" + cssClass + "'>"
            + "<div class='" + cssClass + " itemDate'>" + item.DateInserted + " - "+ item.UserInserted +"</div>"
            + "<div class='" + cssClass + " itemUser'></div>"
            + "<div class='" + cssClass + " itemTitle'></div>"
            + "<div class='" + cssClass + " itemDescription'>" + item.DescriptionParsed + "</div>"
            + "<div class='" + cssClass + " itemFiles'>" + sFiles + "</div>"
            + "<div class='" + cssClass + " itemImages'>" + sImages + "</div>"
            + "</li>";

            counter++;
        }
        ListString += "</ul>";
        MultiView1.ActiveViewIndex = VIEW_THREAD;
    }