PageRenderTime 37ms CodeModel.GetById 7ms app.highlight 23ms RepoModel.GetById 2ms app.codeStats 0ms

/pigeoncms/Modules/PigeonCms.UpdatesAdmin/views/Default.ascx.cs

http://pigeoncms.googlecode.com/
C# | 385 lines | 293 code | 55 blank | 37 comment | 32 complexity | f08dcecd17acda05dbfed07978ae2b93 MD5 | raw file
  1using System;
  2using System.Data;
  3using System.Configuration;
  4using System.Collections;
  5using System.Web;
  6using System.Web.Security;
  7using System.Web.UI;
  8using System.Web.UI.WebControls;
  9using System.Web.UI.WebControls.WebParts;
 10using System.Web.UI.HtmlControls;
 11using System.Web.Caching;
 12using System.Collections.Generic;
 13using PigeonCms;
 14using Ionic.Zip;
 15using System.IO;
 16using System.Data.Common;
 17
 18public partial class Controls_UpdatesAdmin : PigeonCms.BaseModuleControl
 19{
 20    private enum ViewIndex
 21    {
 22        Install = 0, 
 23        SeeModules, 
 24        SeeTemplates, 
 25        EditModule, 
 26        Sql
 27    }
 28
 29    int targetLabelsPopup = 0;
 30    protected int TargetLabelsPopup
 31    {
 32        get { return GetIntParam("TargetLabelsPopup", targetLabelsPopup); }
 33        set { targetLabelsPopup = value; }
 34    }
 35
 36    string labelsPopupUrl = "";
 37    protected string LabelsPopupUrl
 38    {
 39        get
 40        {
 41            if (string.IsNullOrEmpty(labelsPopupUrl) && this.TargetLabelsPopup > 0)
 42            {
 43                var menuMan = new MenuManager();
 44                var menuTarget = new PigeonCms.Menu();
 45                menuTarget = menuMan.GetByKey(this.TargetLabelsPopup);
 46                labelsPopupUrl = Utility.GetRoutedUrl(menuTarget);
 47            }
 48            return labelsPopupUrl;
 49        }
 50    }
 51
 52    protected void Page_Load(object sender, EventArgs e)
 53    {
 54        if (!Roles.IsUserInRole("admin"))
 55            throw new HttpException(404, "Page not found");
 56
 57        if (!Page.IsPostBack)
 58        {
 59            loadDropCoreFilter();
 60        }
 61        else
 62        {
 63            ////reload params on every postback, because cannot manage dinamically fields
 64            //PigeonCms.Menu currMenu = new PigeonCms.Menu();
 65            //PigeonCms.Module currModule = new PigeonCms.Module();
 66            //if (base.CurrentId > 0)
 67            //{
 68            //}
 69            //else
 70            //{
 71            //}
 72            //MenuHelper.LoadListMenu(ListParentId, base.CurrentId);
 73        }
 74    }
 75
 76    protected void DropCoreFilter_SelectedIndexChanged(object sender, EventArgs e)
 77    {
 78        try { GridModules.DataBind(); }
 79        catch (Exception ex)
 80        {
 81            LblErr.Text = ex.Message;
 82        }
 83    }
 84
 85    protected void TxtNameFilter_TextChanged(object sender, EventArgs e)
 86    {
 87        try { GridModules.DataBind(); }
 88        catch (Exception ex)
 89        {
 90            LblErr.Text = ex.Message;
 91        }
 92    }
 93
 94    protected void ObjDs1_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
 95    {
 96        var filter = new ModuleTypeFilter();
 97
 98        switch (DropCoreFilter.SelectedValue)
 99        {
100            case "1":
101                filter.IsCore = Utility.TristateBool.True;
102                break;
103            case "0":
104                filter.IsCore = Utility.TristateBool.False;
105                break;
106            default:
107                filter.IsCore = Utility.TristateBool.NotSet;
108                break;
109        }
110
111        if (!string.IsNullOrEmpty(TxtNameFilter.Text))
112        {
113            filter.FullNamePart = TxtNameFilter.Text;
114        }
115
116        e.InputParameters["filter"] = filter;
117    }
118
119    protected void ObjDsTemplates_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
120    {
121        ThemeObjFilter filter = new ThemeObjFilter();
122        e.InputParameters["filter"] = filter;
123    }
124
125    protected void GridModules_RowCommand(object sender, GridViewCommandEventArgs e)
126    {
127        if (e.CommandName == "Select")
128        {
129            editModule(e.CommandArgument.ToString());
130        }
131        if (e.CommandName == "DeleteRow")
132        {
133            uninstallModule(e.CommandArgument.ToString());
134        }
135    }
136
137    protected void GridModules_RowCreated(object sender, GridViewRowEventArgs e)
138    {
139        if (e.Row.RowType == DataControlRowType.Header)
140            Utility.AddGlyph(GridModules, e.Row);
141    }
142
143    protected void GridModules_RowDataBound(object sender, GridViewRowEventArgs e)
144    {
145        if (e.Row.RowType == DataControlRowType.DataRow)
146        {
147            PigeonCms.ModuleType item = new PigeonCms.ModuleType();
148            item = (PigeonCms.ModuleType)e.Row.DataItem;
149
150
151            //Literal LitName = (Literal)e.Row.FindControl("LitName");
152            //if (item.ContentType == MenuContentType.Url && !string.IsNullOrEmpty(item.Link))
153            //    LitName.Text = "<a href='" + item.Link + "' target='_blank' title='" + item.Link + "'>" +
154            //        Utility.GetTextPreview(item.Name, 20, "") + "</a>";
155            //else
156            //    LitName.Text = Utility.GetTextPreview(item.Name, 20, "");
157
158            var LnkModuleLabels = (HyperLink)e.Row.FindControl("LnkModuleLabels");
159            LnkModuleLabels.NavigateUrl = this.LabelsPopupUrl
160                + "?ModuleFullName=" + item.FullName;
161
162
163            //Uninstall button
164            ImageButton img1 = (ImageButton)e.Row.FindControl("LnkDel");
165            if (item.IsCore)
166                img1.Visible = false;
167            else
168                img1.Visible = true;
169        }
170    }
171
172    protected void GridTemplates_RowCommand(object sender, GridViewCommandEventArgs e)
173    {
174    }
175
176    protected void GridTemplates_RowCreated(object sender, GridViewRowEventArgs e)
177    {
178        if (e.Row.RowType == DataControlRowType.Header)
179            Utility.AddGlyph(GridModules, e.Row);
180    }
181
182    protected void GridTemplates_RowDataBound(object sender, GridViewRowEventArgs e)
183    {
184        if (e.Row.RowType == DataControlRowType.DataRow)
185        {
186
187        }
188    }
189
190    protected void FileUpload1_AfterUpload()
191    {
192        string destFolder = "";
193        if (FileUpload1.UploadedFiles.Count > 0)
194        {
195            using (ZipFile zip =
196                ZipFile.Read(
197                HttpContext.Current.Request.MapPath(
198                FileUpload1.FilePath + FileUpload1.UploadedFiles[0])))
199            {
200                destFolder = HttpContext.Current.Request.MapPath("~/installation/tmp/" +
201                    FileUpload1.UploadedFiles[0].Substring(0, FileUpload1.UploadedFiles[0].Length - 4));
202
203                if (Directory.Exists(destFolder)) Directory.Delete(destFolder, true);
204
205                zip.ExtractExistingFile = ExtractExistingFileAction.OverwriteSilently;
206                zip.ExtractAll(
207                    HttpContext.Current.Request.MapPath("~/installation/tmp/"));
208
209            }
210            InstallHelper.Install(destFolder);
211        }
212    }
213
214    protected void BtnSaveModule_Click(object sender, EventArgs e)
215    {
216        LblErr.Text = "";
217        LblOk.Text = "";
218
219        try
220        {
221            //PigeonCms.Menu o1 = new PigeonCms.Menu();
222            //Module currModule = new Module();
223            //if (base.CurrentId == 0)
224            //{
225
226            //}
227            //else
228            //{
229
230            //}
231            //GridModules.DataBind();
232            //LblOk.Text = Utility.GetLabel("RECORD_SAVED_MSG");
233            //MultiView1.ActiveViewIndex = 0;
234        }
235        catch (Exception e1)
236        {
237            LblErr.Text = Utility.GetLabel("RECORD_ERR_MSG") + "<br />" + e1.ToString();
238        }
239        finally
240        {
241        }
242    }
243
244    protected void BtnCancelModule_Click(object sender, EventArgs e)
245    {
246        LblErr.Text = "";
247        LblOk.Text = "";
248        MultiView1.ActiveViewIndex = (int)ViewIndex.SeeModules;
249    }
250
251    protected void BtnSqlRun_Click(object sender, EventArgs e)
252    {
253        DbProviderFactory myProv = Database.ProviderFactory;
254        DbConnection myConn = myProv.CreateConnection();
255        DbDataReader myRd = null;
256        DbCommand myCmd = myConn.CreateCommand();
257
258        LitSqlResult.Text = "";
259        string sqlQuery = "";    //full sql query
260
261        try
262        {
263            myConn.ConnectionString = Database.ConnString;
264            myConn.Open();
265            myCmd.Connection = myConn;
266
267            sqlQuery = Database.ParseSql(TxtSql.Text);
268            LitSqlResult.Text = Database.ExecuteQuery(myRd, myCmd, sqlQuery);
269        }
270        catch (Exception ex)
271        {
272            LitSqlResult.Text = ex.ToString();
273        }
274        finally
275        {
276            myConn.Dispose();
277        }
278    }
279
280    protected void BtnSqlCancel_Click(object sender, EventArgs e)
281    {
282        TxtSql.Text = "";
283        LitSqlResult.Text = "";
284    }
285
286    protected void MultiView1_ActiveViewChanged(object sender, EventArgs e)
287    {
288        if (MultiView1.ActiveViewIndex == (int)ViewIndex.EditModule)
289        { }
290        else if (MultiView1.ActiveViewIndex == (int)ViewIndex.Install)
291        { }
292        else if (MultiView1.ActiveViewIndex == (int)ViewIndex.SeeModules)
293        { }
294        else if (MultiView1.ActiveViewIndex == (int)ViewIndex.SeeTemplates)
295        { }
296    }
297
298    protected void LnkInstall_Click(object sender, EventArgs e)
299    {
300        LblErr.Text = "";
301        LblOk.Text = "";
302        MultiView1.ActiveViewIndex = (int)ViewIndex.Install;
303    }
304
305    protected void LnkModules_Click(object sender, EventArgs e)
306    {
307        LblErr.Text = "";
308        LblOk.Text = "";
309        MultiView1.ActiveViewIndex = (int)ViewIndex.SeeModules;
310    }
311
312    protected void LnkTemplates_Click(object sender, EventArgs e)
313    {
314        LblErr.Text = "";
315        LblOk.Text = "";
316        MultiView1.ActiveViewIndex = (int)ViewIndex.SeeTemplates;
317    }
318
319    protected void LnkSql_Click(object sender, EventArgs e)
320    {
321        LblErr.Text = "";
322        LblOk.Text = "";
323        MultiView1.ActiveViewIndex = (int)ViewIndex.Sql;
324    }
325
326    #region private methods
327
328    private void clearForm()
329    {
330        LblId.Text = "";
331        TxtName.Text = "";
332    }
333
334    private void form2obj(PigeonCms.Menu obj)
335    {
336        obj.Id = base.CurrentId;
337        //obj.MenuType = DropMenuTypes.SelectedValue;
338    }
339
340    private void obj2form(PigeonCms.ModuleType obj)
341    {
342        LblModuleId.Text = obj.FullName;
343    }
344
345    private void editModule(string recordId)
346    {
347        var obj = new PigeonCms.ModuleType();
348        LblOk.Text = "";
349        LblErr.Text = "";
350
351        clearForm();
352        base.CurrentKey = recordId;
353
354        obj = new ModuleTypeManager().GetByFullName(base.CurrentKey);
355        obj2form(obj);
356
357        MultiView1.ActiveViewIndex = (int)ViewIndex.EditModule;
358    }
359
360    private void uninstallModule(string recordId)
361    {
362        LblOk.Text = "";
363        LblErr.Text = "";
364
365        //try
366        //{
367        //    new MenuManager().DeleteById(recordId);
368        //}
369        //catch (Exception e)
370        //{
371        //    LblErr.Text = e.Message;
372        //}
373        GridModules.DataBind();
374    }
375
376    private void loadDropCoreFilter()
377    {
378        DropCoreFilter.Items.Clear();
379        DropCoreFilter.Items.Add(new ListItem(Utility.GetLabel("LblSelectCore", "Select core state"), ""));
380        DropCoreFilter.Items.Add(new ListItem("Core item", "1"));
381        DropCoreFilter.Items.Add(new ListItem("Optional item", "0"));
382    }
383
384    #endregion
385}