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(""));
            }
        }
        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("");
            }
        }