PageRenderTime 69ms CodeModel.GetById 45ms app.highlight 18ms RepoModel.GetById 2ms app.codeStats 0ms

/pigeoncms/Controls/MessageControl.ascx.cs

http://pigeoncms.googlecode.com/
C# | 403 lines | 352 code | 47 blank | 4 comment | 39 complexity | 5d877417aca883b6724fd5ebf97b9162 MD5 | raw file
  1using System;
  2using System.Collections;
  3using System.Configuration;
  4using System.Data;
  5using System.Linq;
  6using System.Web;
  7using System.Web.Security;
  8using System.Web.UI;
  9using System.Web.UI.HtmlControls;
 10using System.Web.UI.WebControls;
 11using System.Web.UI.WebControls.WebParts;
 12using System.Collections.Generic;
 13using PigeonCms;
 14using System.Net.Mail;
 15
 16
 17public partial class Controls_MessageControl : PigeonCms.BaseModuleControl
 18{
 19    #region events
 20
 21    public delegate void MessageDelegate(object sender, Message.MessageEventArgs e);
 22    public event MessageDelegate AfterDelete;
 23    public event MessageDelegate ItemSelected;
 24    public event MessageDelegate MessageSent;
 25
 26    //public delegate void ItemSelectedDelegate(object sender, Message.MessageEventArgs e);
 27
 28    #endregion
 29
 30    const int COL_DELETE_INDEX = 5;
 31    const int VIEW_LIST_INDEX = 0;
 32    const int VIEW_MESSAGE_INDEX = 1;
 33    const int VIEW_NEW_INDEX = 2;
 34
 35    public enum MessagesViewMode
 36    {
 37        ListMessages = 0,
 38        ShowMessage,
 39        InsertMessage
 40    }
 41
 42    public enum FolderType
 43    {
 44        Inbox = 0,
 45        Outbox
 46    }
 47
 48    public MessagesViewMode ViewMode
 49    {
 50        get
 51        {
 52            var res = MessagesViewMode.ListMessages;
 53            if (ViewState["ViewMode"] != null)
 54                res = (MessagesViewMode)ViewState["ViewMode"];
 55            return res;
 56        }
 57        set
 58        {
 59            ViewState["ViewMode"] = value;
 60            setViewMode();
 61        }
 62    }
 63
 64    public FolderType Folder
 65    {
 66        get
 67        {
 68            var res = FolderType.Inbox;
 69            if (ViewState["Folder"] != null)
 70                res = (FolderType)ViewState["Folder"];
 71            return res;
 72        }
 73        set
 74        {
 75            ViewState["Folder"] = value;
 76        }
 77    }
 78
 79    public Utility.TristateBool IsRead
 80    {
 81        get
 82        {
 83            var res = Utility.TristateBool.NotSet;
 84            if (ViewState["IsRead"] != null)
 85                res = (Utility.TristateBool)ViewState["IsRead"];
 86            return res;
 87        }
 88        set { ViewState["IsRead"] = value; }
 89    }
 90
 91    private string lastMessage = "";
 92    public string LastMessage
 93    {
 94        get { return lastMessage; }
 95    }
 96
 97    public bool AllowDelete
 98    {
 99        get 
100        {
101            var res = false;
102            if (ViewState["AllowDelete"] != null)
103                res = (bool)ViewState["AllowDelete"];
104            return res;
105        }
106        set { ViewState["AllowDelete"] = value; }
107    }
108
109    public int NumberOfRowsPerPage
110    {
111        get
112        {
113            var res = 10;
114            if (ViewState["NumberOfRowsPerPage"] != null)
115                res = (int)ViewState["NumberOfRowsPerPage"];
116            return res;
117        }
118        set
119        {
120            ViewState["NumberOfRowsPerPage"] = value;
121        }
122    }
123
124    protected new void Page_Init(object sender, EventArgs e)
125    {
126        this.BaseModule = new Module();
127        this.BaseModule.ModuleNamespace = "PigeonCms";
128        this.BaseModule.ModuleName = "MessagesListControl";
129        base.Page_Init(sender, e);
130
131        Grid1.PageSize = this.NumberOfRowsPerPage;
132        Grid1.Columns[COL_DELETE_INDEX].Visible = this.AllowDelete;
133    }
134
135    protected void Page_Load(object sender, EventArgs e)
136    {
137    }
138
139    protected void ObjDs1_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
140    {
141        var filter = new MessagesFilter();
142
143        if (this.Folder == FolderType.Outbox)
144            filter.FromUser = PgnUserCurrent.UserName;
145        else
146            filter.ToUserLike = PgnUserCurrent.UserName;
147        filter.IsRead = this.IsRead;
148        e.InputParameters["filter"] = filter;
149        e.InputParameters["sort"] = "";
150    }
151
152    protected void Grid1_PreRender(object sender, EventArgs e)
153    {
154        Grid1.UseAccessibleHeader = true;
155        if (Grid1.HeaderRow != null)
156            Grid1.HeaderRow.TableSection = TableRowSection.TableHeader;
157    }
158
159    protected void Grid1_RowCommand(object sender, GridViewCommandEventArgs e)
160    {
161        if (e.CommandName == "Select")
162        {
163            selectRow(int.Parse(e.CommandArgument.ToString()));
164        }
165        if (e.CommandName == "DeleteRow")
166        {
167            deleteRow(int.Parse(e.CommandArgument.ToString()));
168        }
169        //Enabled
170        if (e.CommandName == "ImgStarredOk")
171        {
172            setFlag(Convert.ToInt32(e.CommandArgument), false, "starred");
173            Grid1.DataBind();
174        }
175        if (e.CommandName == "ImgStarredKo")
176        {
177            setFlag(Convert.ToInt32(e.CommandArgument), true, "starred");
178            Grid1.DataBind();
179        }
180    }
181
182    protected void Grid1_RowCreated(object sender, GridViewRowEventArgs e)
183    {
184        if (e.Row.RowType == DataControlRowType.Header)
185            Utility.AddGlyph(Grid1, e.Row);
186    }
187
188    protected void Grid1_RowDataBound(object sender, GridViewRowEventArgs e)
189    {
190        if (e.Row.RowType == DataControlRowType.DataRow)
191        {
192            var item = new PigeonCms.Message();
193            item = (PigeonCms.Message)e.Row.DataItem;
194
195            LinkButton LnkSubject = (LinkButton)e.Row.FindControl("LnkSubject");
196            string subject = Utility.Html.GetTextPreview(item.Title, 50, "");
197            if (item.IsRead)
198                LnkSubject.Text = subject;
199            else
200                LnkSubject.Text = "<b>" + subject + "</b>";
201
202            Literal LitBody = (Literal)e.Row.FindControl("LitBody");
203            LitBody.Text = Utility.Html.GetTextPreview(item.Description, 200, "");
204
205            Literal LitDateInserted = (Literal)e.Row.FindControl("LitDateInserted");
206            LitDateInserted.Text = item.DateInserted.ToString();
207
208            //Starred
209            if (item.IsStarred)
210            {
211                Image img1 = (Image)e.Row.FindControl("ImgStarredOk");
212                img1.Visible = true;
213            }
214            else
215            {
216                Image img1 = (Image)e.Row.FindControl("ImgStarredKo");
217                img1.Visible = true;
218            }
219        }
220    }
221
222    protected void MultiView1_ActiveViewChanged(object sender, EventArgs e)
223    {
224    }
225
226    public void ReloadList()
227    {
228        Grid1.DataBind();
229    }
230
231    public void ShowMessage(int recordId)
232    {
233        var obj = new PigeonCms.Message();
234        MultiView1.ActiveViewIndex = VIEW_MESSAGE_INDEX;
235
236        clearMessage();
237        CurrentId = recordId;
238        if (CurrentId > 0)
239        {
240            obj = new MessagesManager().GetByKey(CurrentId);
241            obj2message(obj);
242        }
243    }
244
245    public void CreateMessage(string to, string subject, string body)
246    {
247        MultiView1.ActiveViewIndex = VIEW_NEW_INDEX;
248        TxtTo.Text = to;
249        TxtTitle.Text = subject;
250        TxtDescription.Text = body;
251    }
252
253    public void SendMessage(MessageProvider.SendMessageEnum sendLocalMessage, 
254        MessageProvider.SendMessageEnum sendEmail)
255    {
256        bool res = false;
257
258        try
259        {
260            var o1 = new Message();
261            form2obj(o1);
262            MessageProvider.SendMessage(Utility.String2List(o1.ToUser, ";"), o1, 
263                sendLocalMessage, sendEmail);
264            res = true;
265        }
266        catch (Exception e1)
267        {
268            this.lastMessage = Utility.GetLabel("RECORD_ERR_MSG") + "<br />" + e1.ToString();
269        }
270        finally
271        {
272        }
273        if (this.MessageSent != null)
274            this.MessageSent(this,
275                new Message.MessageEventArgs(0, "send", this.LastMessage, res));
276    }
277
278
279    private void selectRow(int recordId)
280    {
281        if (this.ItemSelected != null)
282        {
283            var man = new MessagesManager();
284            var o1 = man.GetByKey(recordId);
285            if (!o1.IsRead)
286            {
287                //flag as read
288                o1.IsRead = true;
289                man.Update(o1);
290                Grid1.DataBind();
291            }
292            this.ItemSelected(this,
293                new Message.MessageEventArgs(recordId, "select", "", true));
294        }
295    }
296
297    private void clearMessage()
298    {
299        LitSubject.Text = "";
300        LitCreated.Text = "";
301        LitUser.Text = "";
302        LitDescription.Text = "";
303        LitImages.Text = "";
304        LitFiles.Text = "";
305    }
306
307    private void obj2message(Message obj)
308    {
309        LitSubject.Text = obj.Title;
310        LitCreated.Text = obj.DateInserted.ToString();
311        LitUser.Text = "From " + obj.FromUser + " to " + obj.ToUser;
312        LitDescription.Text = obj.Description;
313        LitImages.Text = "";
314        LitFiles.Text = "";
315    }
316
317    private void form2obj(Message obj)
318    {
319        obj.ToUser = TxtTo.Text;
320        obj.Title = TxtTitle.Text;
321        obj.Description = TxtDescription.Text;
322    }
323
324    private bool checkForm()
325    {
326        bool res = true;
327
328        return res;
329    }
330
331    private void clearForm()
332    {
333        TxtTo.Text = "";
334        TxtTitle.Text = "";
335        TxtDescription.Text = "";
336    }
337
338    private void deleteRow(int recordId)
339    {
340        bool res = true;
341        try
342        {
343            if (!PgnUserCurrent.IsAuthenticated)
344                throw new Exception("user not authenticated");
345
346            new MessagesManager().DeleteById(recordId);
347            Grid1.DataBind();
348        }
349        catch (Exception e)
350        {
351            res = false;
352            this.lastMessage = e.Message;
353        }
354        if (this.AfterDelete != null)
355            this.AfterDelete(this, 
356                new Message.MessageEventArgs(recordId, "delete", this.LastMessage, res));
357    }
358
359    private void setFlag(int recordId, bool value, string flagName)
360    {
361        try
362        {
363            var man = new MessagesManager();
364            var o1 = man.GetByKey(recordId);
365            switch (flagName.ToLower())
366            {
367                case "starred":
368                    o1.IsStarred = value;
369                    break;
370                case "read":
371                    o1.IsRead = value;
372                    break;
373                default:
374                    break;
375            }
376            man.Update(o1);
377        }
378        catch (Exception e1)
379        {
380            this.lastMessage = Utility.GetLabel("RECORD_ERR_MSG") + "<br />" + e1.ToString();
381        }
382        finally { }
383    }
384
385    private void setViewMode()
386    {
387        switch (ViewMode)
388        {
389            case MessagesViewMode.ListMessages:
390                MultiView1.ActiveViewIndex = VIEW_LIST_INDEX;
391                break;
392            case MessagesViewMode.ShowMessage:
393                MultiView1.ActiveViewIndex = VIEW_MESSAGE_INDEX;
394                break;
395            case MessagesViewMode.InsertMessage:
396                MultiView1.ActiveViewIndex = VIEW_NEW_INDEX;
397                break;
398            default:
399                break;
400        }
401
402    }
403}