Exemple #1
0
        public JsonResult GetModules(int userId)
        {
            try
            {
                List <object> header          = new List <object>();
                DataTable     dtMod           = modules.GetAvailabeModules();
                bool          is_checked      = false;
                List <int>    active_for_user = GetUserModules(userId);
                int           indx            = 0;
                foreach (DataRow row in dtMod.Rows)
                {
                    Dictionary <string, object> details = new Dictionary <string, object>();

                    if (active_for_user.Contains(Int32.Parse(row["Id"].ToString())))
                    {
                        is_checked = true;
                    }
                    else
                    {
                        is_checked = false;
                    }

                    details.Add("id", row["id"].ToString());
                    details.Add("parent", (Int32.Parse(row["ParentId"].ToString()) == 0 || row["ParentId"].ToString() == "") ? "-1" : row["ParentId"].ToString());
                    details.Add("text", row["Name"].ToString());
                    details.Add("checked", is_checked);

                    header.Add(details);
                    indx++;
                }

                response.Add("success", true);
                response.Add("error", false);
                response.Add("message", header);
            }
            catch (Exception e)
            {
                response.Add("success", false);
                response.Add("error", true);
                response.Add("message", e.ToString());
            }

            return(Json(response, JsonRequestBehavior.AllowGet));
        }
        public List <MenuObject> PrepareMenu(int child, int parent, string usertype, int userId)
        {
            Dictionary <int, MenuObject> header = new Dictionary <int, MenuObject>();
            List <MenuObject>            res    = new List <MenuObject>();
            ModuleModels mod       = new ModuleModels();
            DataTable    dtMod     = mod.GetAvailabeModules();
            DataTable    dtUserMod = mod.GetUserModules(userId);

            bool is_active = false;

            if (usertype == "superadmin")
            {
                foreach (DataRow row in dtMod.Rows)
                {
                    List <MenuObject> subitems = new List <MenuObject>();
                    MenuObject        menuobj  = new MenuObject();

                    Dictionary <string, object> details = new Dictionary <string, object>();
                    is_active = false;
                    if (Int32.Parse(row["ParentId"].ToString()) == 0)
                    {
                        if (Int32.Parse(row["Id"].ToString()) == parent || Int32.Parse(row["Id"].ToString()) == child)
                        {
                            is_active = true;
                        }

                        menuobj.Id       = Int32.Parse(row["Id"].ToString());
                        menuobj.Name     = row["Name"].ToString();
                        menuobj.Icon     = row["Icon"].ToString();
                        menuobj.IsActive = is_active;
                        menuobj.URL      = row["URL"].ToString();

                        header.Add(Int32.Parse(row["Id"].ToString()), menuobj);

                        res.Add(menuobj);
                    }
                    else
                    {
                        if (Int32.Parse(row["Id"].ToString()) == child)
                        {
                            is_active = true;
                        }

                        menuobj.Id       = Int32.Parse(row["Id"].ToString());
                        menuobj.Name     = row["Name"].ToString();
                        menuobj.Icon     = row["Icon"].ToString();
                        menuobj.IsActive = is_active;
                        menuobj.URL      = row["URL"].ToString();

                        if (header[Int32.Parse(row["ParentId"].ToString())].Items != null)
                        {
                            subitems = header[Int32.Parse(row["ParentId"].ToString())].Items;
                        }

                        subitems.Add(menuobj);

                        header[Int32.Parse(row["ParentId"].ToString())].Items = subitems;
                    }
                }
            }
            else
            {
                List <int> p = new List <int>();
                List <int> c = new List <int>();

                foreach (DataRow row in dtUserMod.Rows)
                {
                    p.Add(Int32.Parse(row["ParentId"].ToString()));
                    c.Add(Int32.Parse(row["ObjectId"].ToString()));
                }

                foreach (DataRow row in dtMod.Rows)
                {
                    List <MenuObject> subitems = new List <MenuObject>();
                    MenuObject        menuobj  = new MenuObject();

                    Dictionary <string, object> details = new Dictionary <string, object>();
                    is_active = false;
                    if (Int32.Parse(row["ParentId"].ToString()) == 0)
                    {
                        if (p.Contains(Int32.Parse(row["Id"].ToString())) || c.Contains(Int32.Parse(row["Id"].ToString())))
                        {
                            if (Int32.Parse(row["Id"].ToString()) == parent || Int32.Parse(row["Id"].ToString()) == child)
                            {
                                is_active = true;
                            }

                            menuobj.Id       = Int32.Parse(row["Id"].ToString());
                            menuobj.Name     = row["Name"].ToString();
                            menuobj.Icon     = row["Icon"].ToString();
                            menuobj.IsActive = is_active;
                            menuobj.URL      = row["URL"].ToString();

                            header.Add(Int32.Parse(row["Id"].ToString()), menuobj);

                            res.Add(menuobj);
                        }
                    }
                    else
                    {
                        if (c.Contains(Int32.Parse(row["Id"].ToString())))
                        {
                            if (Int32.Parse(row["Id"].ToString()) == child)
                            {
                                is_active = true;
                            }

                            menuobj.Id       = Int32.Parse(row["Id"].ToString());
                            menuobj.Name     = row["Name"].ToString();
                            menuobj.Icon     = row["Icon"].ToString();
                            menuobj.IsActive = is_active;
                            menuobj.URL      = row["URL"].ToString();

                            if (header[Int32.Parse(row["ParentId"].ToString())].Items != null)
                            {
                                subitems = header[Int32.Parse(row["ParentId"].ToString())].Items;
                            }

                            subitems.Add(menuobj);

                            header[Int32.Parse(row["ParentId"].ToString())].Items = subitems;
                        }
                    }
                }
            }

            return(res);
        }
        public Dictionary <string, object> GetAvailableModules(string _id, string _parentid)
        {
            ModuleModels modules = new ModuleModels();

            Dictionary <string, object> response         = new Dictionary <string, object>();
            Dictionary <string, object> menu             = new Dictionary <string, object>();
            Dictionary <string, object> submenu_temp_arr = new Dictionary <string, object>();
            List <object> submenu_header = new List <object>();
            List <object> z             = new List <object>();
            DataTable     dTableModules = modules.GetAvailabeModules();

            foreach (DataRow dataRow in dTableModules.Rows)
            {
                if (dataRow["ParentId"].ToString() == "0")
                {
                    bool   isActive = false;
                    string alvin    = dataRow["Id"].ToString();
                    if (_parentid == alvin)
                    {
                        isActive = true;
                    }
                    menu.Add(dataRow["Id"].ToString(),
                             new Dictionary <string, object> {
                        { "Id", dataRow["Id"].ToString() },
                        { "Name", dataRow["Name"].ToString() },
                        { "Icon", dataRow["Icon"].ToString() },
                        { "items", new List <object>() },
                        { "is_active", isActive },
                        { "URL", dataRow["URL"].ToString() },
                    }
                             );
                }
                else
                {
                    bool isActive = false;
                    if (_id == dataRow["Id"].ToString())
                    {
                        isActive = true;
                    }

                    Dictionary <string, object> submenu_details = new Dictionary <string, object>();
                    Dictionary <string, object> y = new Dictionary <string, object>();
                    submenu_details.Add("Id", dataRow["Id"].ToString());
                    submenu_details.Add("Name", dataRow["Name"].ToString());
                    submenu_details.Add("Icon", dataRow["Icon"].ToString());
                    submenu_details.Add("items", new Dictionary <string, object>());
                    submenu_details.Add("is_active", isActive);
                    submenu_details.Add("URL", dataRow["URL"].ToString());

                    //submenu_temp_arr.Add(dataRow["ParentId"].ToString(), submenu_details);

                    submenu_header.Add(submenu_details);
                    //menu.Add(dataRow["Id"].ToString(), new Dictionary<string, object>() {
                    //    { "items",submenu }
                    //});

                    if (menu.ContainsKey(dataRow["ParentId"].ToString()))
                    {
                        var x = menu[dataRow["ParentId"].ToString()];
                        y = x as Dictionary <string, object>;
                        z = y["items"] as List <object>;
                        z.Add(submenu_details);
                        y["items"] = z;
                    }

                    //foreach (DataColumn column in dTableModules.Columns)
                    //{
                    //    //ColumnName = column.ColumnName;
                    //    //ColumnData = dataRow[column].ToString();
                    //}
                    //Console.WriteLine("--- Row ---"); // Print separator.
                    //foreach (var item in dataRow.ItemArray) // Loop over the items.
                    //{
                    //    Console.Write("Item: "); // Print label.
                    //    Console.WriteLine(item); // Invokes ToString abstract method.
                    //}
                }
            }
            return(menu);
        }