//
        // GET: /Module/
        public JsonResult AddModule(string title, string moduleType, string paneLocation, string viewPermission, string pageId, string ModuleId)
        {
            // All new modules go to the end of the content pane
            var m = new ModuleItem();
            m.Title = title;
            m.ModuleDefID = Int32.Parse(moduleType);
            m.Order = 999;

            // save to database
            var mod = new ModulesDB();
            var modId = Int32.Parse(ModuleId);

            m.ID = mod.AddModule(
                Int32.Parse(pageId),
                m.Order,
                paneLocation,
                m.Title,
                m.ModuleDefID,
                0,
                PortalSecurity.GetEditPermissions(modId),
                viewPermission,
                PortalSecurity.GetAddPermissions(modId),
                PortalSecurity.GetDeletePermissions(modId),
                PortalSecurity.GetPropertiesPermissions(modId),
                PortalSecurity.GetMoveModulePermissions(modId),
                PortalSecurity.GetDeleteModulePermissions(modId),
                false,
                PortalSecurity.GetPublishPermissions(modId),
                false,
                false,
                false);

            // End Change [email protected]

            //// reload the portalSettings from the database

            //this.Context.Items["PortalSettings"] = new PortalSettings(this.PageID, this.PortalSettings.PortalAlias);
            //this.PortalSettings = (PortalSettings)this.Context.Items["PortalSettings"];

            // reorder the modules in the content pane
            //var modules = GetModules("ContentPane", Int32.Parse(pageId), Int32.Parse(portalId));
            //this.OrderModules(modules);

            //// resave the order
            //foreach (ModuleItem item in modules) {
            //    mod.UpdateModuleOrder(item.ID, item.Order, "ContentPane");
            //}

            //// Redirect to the same page to pick up changes
            ////this.Response.Redirect(this.AppendModuleID(this.Request.RawUrl, m.ID));
            var list = GetModules(paneLocation, Int32.Parse(pageId));

            StringBuilder ls = new StringBuilder();
            foreach (ModuleItem md in list)
            {
                ls.AppendFormat("<option value=\"{0}\">{1}</option>", md.ID, md.Title);
            }

            return Json(new { value = ls.ToString() });
        }
 public static int Compare(ModuleItem x, ModuleItem y)
 {
     int result = 1;
     if (x != null && x is ModuleItem &&
         y != null && y is ModuleItem) {
         ModuleItem itemX = (ModuleItem)x;
         ModuleItem itemY = (ModuleItem)y;
         result = itemX.CompareTo(itemY);
     }
     return result;
 }
        protected void AddModuleToPane_Click(object sender, EventArgs e)
        {
            // All new modules go to the end of the content pane
            var m = new ModuleItem();
            m.Title = this.moduleTitle.Text;
            m.ModuleDefID = Int32.Parse(this.moduleType.SelectedItem.Value);
            m.Order = 999;

            // save to database
            var mod = new ModulesDB();

            // Change by [email protected]
            // Date: 6/2/2003
            // Original:             m.ID = _mod.AddModule(tabID, m.Order, "ContentPane", m.Title, m.ModuleDefID, 0, "Admins", "All Users", "Admins", "Admins", "Admins", false);
            // Changed by Mario Endara <*****@*****.**> (2004/11/09)
            // The new module inherits security from Pages module (current ModuleID)
            // so who can edit the tab properties/content can edit the module properties/content (except view that remains =)
            m.ID = mod.AddModule(
                this.PageID,
                m.Order,
                this.paneLocation.SelectedItem.Value,
                m.Title,
                m.ModuleDefID,
                0,
                PortalSecurity.GetEditPermissions(this.ModuleID),
                this.viewPermissions.SelectedItem.Value,
                PortalSecurity.GetAddPermissions(this.ModuleID),
                PortalSecurity.GetDeletePermissions(this.ModuleID),
                PortalSecurity.GetPropertiesPermissions(this.ModuleID),
                PortalSecurity.GetMoveModulePermissions(this.ModuleID),
                PortalSecurity.GetDeleteModulePermissions(this.ModuleID),
                false,
                PortalSecurity.GetPublishPermissions(this.ModuleID),
                false,
                false,
                false);

            // End Change [email protected]

            // reload the portalSettings from the database
            this.Context.Items["PortalSettings"] = PortalSettings.GetPortalSettings(this.PageID, this.PortalSettings.PortalAlias);
            this.PortalSettings = (PortalSettings)this.Context.Items["PortalSettings"];

            // reorder the modules in the content pane
            var modules = this.GetModules("ContentPane");
            this.OrderModules(modules);

            // resave the order
            foreach (ModuleItem item in modules)
            {
                mod.UpdateModuleOrder(item.ID, item.Order, "ContentPane");
            }

            // Redirect to the same page to pick up changes
            this.Response.Redirect(this.AppendModuleID(this.Request.RawUrl, m.ID));
        }
        /// <summary>
        /// The GetModules helper method is used to get the modules
        ///   for a single pane within the tab
        /// </summary>
        /// <param name="pane">
        /// The pane.
        /// </param>
        /// <returns>
        /// </returns>
        /// <remarks>
        /// </remarks>
        private ArrayList GetModules(string pane)
        {
            var paneModules = new ArrayList();

            foreach (ModuleSettings _module in this.PortalSettings.ActivePage.Modules)
            {
                if (_module.PaneName.ToLower() == pane.ToLower() &&
                    this.PortalSettings.ActivePage.PageID == _module.PageID)
                {
                    var m = new ModuleItem();
                    m.Title = _module.ModuleTitle;
                    m.ID = _module.ModuleID;
                    m.ModuleDefID = _module.ModuleDefID;
                    m.Order = _module.ModuleOrder;
                    paneModules.Add(m);
                }
            }

            return paneModules;
        }
        private ArrayList GetModules(string pane, int pageId)
        {
            var paneModules = new ArrayList();
            var modules = new Appleseed.Framework.Site.Data.ModulesDB();

            foreach (ModuleSettings _module in modules.getModulesSettingsInPage(pageId, pane))
            {
                if (_module.PaneName.ToLower() == pane.ToLower() &&
                    pageId == _module.PageID)
                {
                    var m = new ModuleItem();
                    m.Title = _module.ModuleTitle;
                    m.ID = _module.ModuleID;
                    m.ModuleDefID = _module.ModuleDefID;
                    m.Order = _module.ModuleOrder;
                    paneModules.Add(m);
                }
            }

            InsertionSort(paneModules, ModuleItem.Compare);

            return paneModules;
        }
        public JsonResult Clone(int id, int parentId)
        {
            try
            {
                var generalModuleDef = Guid.Parse("F9F9C3A4-6E16-43B4-B540-984DDB5F1CD2");
                object[] queryargs = { generalModuleDef, PortalSettings.PortalID };

                int moduleDefinition;

                try
                {
                    moduleDefinition =
                        new rb_ModuleDefinitions().All(where: "GeneralModDefID = @0 and PortalID = @1", args: queryargs).Single().ModuleDefID;
                }
                catch(Exception e)
                {
                    // Shortcut module doesn't exist in current Portal

                    var modules = new ModulesDB();

                    modules.UpdateModuleDefinitions(
                            generalModuleDef,
                            PortalSettings.PortalID,
                            true);

                    moduleDefinition =
                        new rb_ModuleSettings().All(where: "GeneralModDefID = @0 and PortalID = @1", args: queryargs).Single().ModuleDefID;

                }

                var db = new PagesDB();

                PortalPages = db.GetPagesFlat(PortalSettings.PortalID);
                var t = new PageItem
                            {
                                Name = General.GetString("TAB_NAME", "New Page Name"),
                                ID = -1,
                                Order = 990000
                            };

                PortalPages.Add(t);

                var tabs = new PagesDB();
                t.ID = tabs.AddPage(PortalSettings.PortalID, t.Name, t.Order);

                db.UpdatePageParent(t.ID, parentId, PortalSettings.PortalID);

                OrderPages();
                //JsonResult treeData = GetTreeData();

                // Coping Modules

                var pagesModules = new rb_Modules().All(where: "TabID = @0", args: id);

                foreach (var module in pagesModules)
                {
                    var m = new ModuleItem();
                    m.Title = module.ModuleTitle;
                    m.ModuleDefID = moduleDefinition;
                    m.Order = module.ModuleOrder;

                    // save to database
                    var mod = new ModulesDB();

                    m.ID = mod.AddModule(
                        t.ID,
                        m.Order,
                        module.PaneName,
                        module.ModuleTitle,
                        m.ModuleDefID,
                        0,
                        module.AuthorizedEditRoles,
                        module.AuthorizedViewRoles,
                        module.AuthorizedAddRoles,
                        module.AuthorizedDeleteRoles,
                        module.AuthorizedPropertiesRoles,
                        module.AuthorizedMoveModuleRoles,
                        module.AuthorizedDeleteModuleRoles,
                        false,
                        PortalSecurity.GetDeleteModulePermissions(module.ModuleID),
                        false,
                        false,
                        false);

                    var settings = new rb_ModuleSettings();
                    settings.Insert(new { ModuleID = m.ID, SettingName = "LinkedModule", SettingValue = module.ModuleID });

                }

                return Json(new {pageId = t.ID});
            }
            catch(Exception e)
            {
                ErrorHandler.Publish(LogLevel.Error, e);
                Response.StatusCode = 500;
                return Json("");
            }
        }
        /*[Ajax.AjaxMethod]*/
        /*
        public System.Collections.Specialized.StringCollection ModuleChangeStrings(string moduleType, string moduleName)
        {
            SetDatata(moduleType);
            moduleTitle.Text = moduleName;

            System.Collections.Specialized.StringCollection s = new System.Collections.Specialized.StringCollection();

            s.Add(moduleTitle.Text);

            if (AddModuleHelp.Visible)
            {
                s.Add(AddModuleHelp.Attributes["onclick"].ToString());
                s.Add(AddModuleHelp.NavigateUrl);
                s.Add(AddModuleHelp.ImageUrl);
                s.Add(AddModuleHelp.ToolTip);
            }

            return s;
        }
         * */
        /// <summary>
        /// The AddModule_Click server event handler
        ///   on this page is used to add a new portal module
        ///   into the tab
        /// </summary>
        /// <param name="sender">
        /// The source of the event.
        /// </param>
        /// <param name="e">
        /// The <see cref="System.EventArgs"/> instance containing the event data.
        /// </param>
        /// <remarks>
        /// </remarks>
        private void AddModule_Click(object sender, EventArgs e)
        {
            // TODO: IF PAGE ID = 0 Then we know it's home page, cant we get from db the id?
            // PagesDB _d = new PagesDB();
            var pid = this.PageID;
            if (pid == 0)
            {
                pid = PagesDB.PortalHomePageId(this.PortalID);
            }

            if (pid != 0)
            {
                // All new modules go to the end of the content pane
                var selectedModule = this.moduleType.SelectedItem.Value;
                var start = selectedModule.IndexOf("|");
                var moduleID = Convert.ToInt32(selectedModule.Substring(0, start).Trim());

                // Hide error message in case there was a previous error.
                this.moduleError.Visible = false;

                // This allows the user to pick what type of people can view the module being added.
                // If Authorized Roles is selected from the dropdown then every role that has view permission for the
                // Add Role module will be added to the view permissions of the module being added.
                var viewPermissionRoles = this.viewPermissions.SelectedValue;
                if (viewPermissionRoles == "Authorised Roles")
                {
                    viewPermissionRoles = PortalSecurity.GetViewPermissions(this.ModuleID);
                }

                try
                {
                    var m = new ModuleItem { Title = this.TitleTextBox.Value, ModuleDefID = moduleID, Order = 999 };

                    // save to database
                    var mod = new ModulesDB();
                    m.ID = mod.AddModule(
                        pid,
                        m.Order,
                        this.paneLocation.SelectedValue,
                        m.Title,
                        m.ModuleDefID,
                        0,
                        PortalSecurity.GetEditPermissions(this.ModuleID),
                        viewPermissionRoles,
                        PortalSecurity.GetAddPermissions(this.ModuleID),
                        PortalSecurity.GetDeletePermissions(this.ModuleID),
                        PortalSecurity.GetPropertiesPermissions(this.ModuleID),
                        PortalSecurity.GetMoveModulePermissions(this.ModuleID),
                        PortalSecurity.GetDeleteModulePermissions(this.ModuleID),
                        false,
                        PortalSecurity.GetPublishPermissions(this.ModuleID),
                        false,
                        false,
                        false);
                }
                catch (Exception ex)
                {
                    this.moduleError.Visible = true;
                    ErrorHandler.Publish(
                        LogLevel.Error,
                        "There was an error with the Add Module Module while trying to add a new module.",
                        ex);
                }
                finally
                {
                    if (this.moduleError.Visible == false)
                    {
                        // Reload page to pick up changes
                        this.Response.Redirect(this.Request.RawUrl, false);
                    }
                }
            }
            else
            {
                // moduleError.TextKey = "ADDMODULE_HOMEPAGEERROR";
                this.moduleError.Text = General.GetString(
                    "ADDMODULE_HOMEPAGEERROR",
                    "You are currently on the homepage using the default virtual ID (The default ID is set when no specific page is selected. e.g. www.yourdomain.com. Please select your homepage from the Navigation menu e.g. 'Home' so that you can add a module against the page's actual ID.");
                this.moduleError.Visible = true;
            }
        }