Beispiel #1
0
        public IActionResult CreateCategory(Category category, string description)
        {
            //TODO: authorize, validate...
            using (var db = CMSContextFactory.Create(Settings.MySqlConnectionString))
            {
                //TODO: encapsulate in Category Model
                var customFields = this.Request.Form["CustomFields"].ToString();

                //check if it's the default custom fields, discard
                var parsedCustomFields = JsonConvert.DeserializeObject <List <CustomField> >(customFields);
                if (parsedCustomFields.Count == 0 || parsedCustomFields[0].Name.Length == 0)
                {
                    category.CustomFields = null;
                }
                else
                {
                    category.CustomFields = customFields;
                }

                db.Add(category);

                Category_Language category_language = new Category_Language()
                {
                    LanguageId = 1, Category = category, Description = description
                };
                db.Add(category_language);


                db.SaveChanges();
            }
            return(Redirect("/backend/categories"));
        }
Beispiel #2
0
        public IActionResult DeleteItem(int category_id)
        {
            using (var db = CMSContextFactory.Create(Settings.MySqlConnectionString))
            {
                var itemToRemove = db.Category_Language
                                   .Include(catLang => catLang.Category)
                                   .ThenInclude(cat => cat.Items)
                                   .SingleOrDefault(x => x.Id == category_id);

                if (itemToRemove != null)
                {
                    db.Category_Language.Remove(itemToRemove);

                    //delete the default item language will delete other items languages and the origin item
                    if (itemToRemove.LanguageId == 1)
                    {
                        var OtherItemsToRemove = db.Category_Language.Where(x => x.CategoryId == itemToRemove.CategoryId).ToList();
                        foreach (var v in OtherItemsToRemove)
                        {
                            db.Category_Language.Remove(v);
                        }

                        var item = db.Categories.SingleOrDefault(x => x.Id == itemToRemove.CategoryId);
                        db.Categories.Remove(item);

                        //delete all items that the category has
                        db.Items.RemoveRange(itemToRemove.Category.Items);
                    }
                }

                db.SaveChanges();
                return(Redirect("/backend/categories"));
            }
        }
Beispiel #3
0
        public IActionResult DeleteItem(int menuItem_id)
        {
            using (var db = CMSContextFactory.Create(Settings.MySqlConnectionString))
            {
                var itemToRemove = db.MenuItem_Language.SingleOrDefault(x => x.Id == menuItem_id);

                if (itemToRemove != null)
                {
                    db.MenuItem_Language.Remove(itemToRemove);

                    //delete the default item language will delete other items languages and the origin item
                    if (itemToRemove.LanguageId == 1)
                    {
                        var OtherItemsToRemove = db.MenuItem_Language.Where(x => x.MenuItemId == itemToRemove.MenuItemId).ToList();
                        foreach (var v in OtherItemsToRemove)
                        {
                            db.MenuItem_Language.Remove(v);
                        }

                        var item = db.MenuItems.SingleOrDefault(x => x.Id == itemToRemove.MenuItemId);
                        db.MenuItems.Remove(item);
                    }
                }

                db.SaveChanges();
                return(Redirect("/backend/menu-items"));
            }
        }
        public IActionResult CreateItem(Item item, Item_Language item_language)
        {
            using (var db = CMSContextFactory.Create(Settings.MySqlConnectionString))
            {
                //get chosen category, to parse custom fields if existed...
                var category = db.Categories.SingleOrDefault(cat => cat.Id == item.CategoryId);

                string customValuesBuilder = null;

                if (category.CustomFields != null)
                {
                    var customFields = JsonConvert.DeserializeObject <List <CustomField> >(category.CustomFields);

                    customValuesBuilder = "{";
                    foreach (var customField in customFields)
                    {
                        var nameOfField  = customField.Name;
                        var valueOfField = this.Request.Form[nameOfField];
                        customValuesBuilder += "\"" + nameOfField + "\":" + valueOfField;
                    }

                    customValuesBuilder += "}";
                }

                item.CustomValues = customValuesBuilder;
                db.Add(item);

                item_language.LanguageId = 1;//default language
                item_language.Item       = item;
                db.Add(item_language);

                db.SaveChanges();
            }
            return(Redirect("/backend/items"));
        }
Beispiel #5
0
        public IActionResult EditCategory(Category category, Category_Language categroyLanguage, int categoryId, int category_languageId, int languageId, string customFields)
        {
            using (var db = CMSContextFactory.Create(Settings.MySqlConnectionString))
            {
                category.Id               = categoryId;
                categroyLanguage.Id       = category_languageId;
                categroyLanguage.Category = category;

                //check if default custom fields provided then discard
                var parsedCustomFields = JsonConvert.DeserializeObject <List <CustomField> >(customFields);
                if (parsedCustomFields.Count == 0 || parsedCustomFields[0].Name.Length == 0)
                {
                    category.CustomFields = null;
                }
                else
                {
                    category.CustomFields = customFields;
                }

                db.Entry(category).State         = EntityState.Modified;
                db.Entry(categroyLanguage).State = EntityState.Modified;
                db.SaveChanges();

                return(Redirect("/backend/categories"));
            }
        }
        public IActionResult GetEditItem(int item_language_id)
        {
            using (var db = CMSContextFactory.Create(Settings.MySqlConnectionString))
            {
                Item_Language itemLanguage = db.Item_Language
                                             .Include(_itemLanguage => _itemLanguage.Item)
                                             .ThenInclude(item => item.Category)
                                             .Include(_itemLanguage => _itemLanguage.Item)
                                             .ThenInclude(item => item.Role)
                                             .SingleOrDefault(_itemLanguage => _itemLanguage.Id == item_language_id);

                ViewBag.itemLanguage = itemLanguage;

                if (itemLanguage.Item.Category.CustomFields != null)
                {
                    ViewBag.catCustomFields = JsonConvert.DeserializeObject <List <CustomField> >(itemLanguage.Item.Category.CustomFields);
                    ViewBag.itemCustomValue = JsonConvert.DeserializeObject <dynamic>(itemLanguage.Item.CustomValues) as JObject;
                }
                else
                {
                    ViewBag.catCustomFields = new List <CustomField>();
                }

                ViewBag.cats = db.Category_Language.Where(cat_lang => cat_lang.LanguageId == itemLanguage.LanguageId).ToList();

                ViewBag.roles = db.Roles.ToList();

                return(View("~/Components/Main/Item/Views/Backend/edit.cshtml"));
            }
        }
Beispiel #7
0
        //TODO: remove @connectioString param
        public IActionResult GetMenuItemView(string link, string connectionString, int langId, IBundle bundle)
        {
            //first fetch menu item
            using (var db = CMSContextFactory.Create(connectionString))
            {
                if (link == null)
                {                                                                              // requesting index page
                    var indexLink = db.MenuItems.SingleOrDefault(_item => _item.IsIndexPage).Link;
                    return(this.GetMenuItemView(indexLink, connectionString, langId, bundle)); //invoke self with the new link
                }

                var menuItem = db.MenuItems
                               .Include(item => item.MenuItemType)
                               .ThenInclude(menuItemType => menuItemType.Extension)
                               .Include(item => item.Role)
                               .SingleOrDefault(item => item.Link == link);



                if (menuItem == null)
                {
                    return(new StatusCodeResult(404));
                }

                Type componentType = Type.GetType("LightCMS." + menuItem.MenuItemType.Extension.Namespace);
                IMenuItemTypeComponent component = Activator.CreateInstance(componentType) as IMenuItemTypeComponent;

                return(component.Render(menuItem, connectionString, langId, bundle));
            }
        }
        public void Bootstrap(string connectionString)
        {
            using (var db = CMSContextFactory.Create(connectionString))
            {
                //init extendsion
                var extension = db.Extensions.SingleOrDefault(ext => ext.Namespace == "Components.CategoryList.CategoryListComponentController");
                if (extension == null)
                {
                    extension = new Extension()
                    {
                        Namespace = "Components.CategoryList.CategoryListComponentController"
                    };
                    db.Add(extension);
                }


                //init 'Single Article' menu type
                var menuItemType = db.MenuItemTypes.SingleOrDefault(type => type.Id == 2);
                if (menuItemType == null)
                {
                    menuItemType = new MenuItemType()
                    {
                        Id          = 2,
                        Name        = "Category List",
                        ExtensionId = 2
                    };
                    db.Add(menuItemType);
                }

                db.SaveChanges();
            }
        }
Beispiel #9
0
 public IActionResult AddMenuItem(MenuItem_Language menuItem_language)
 {
     using (var db = CMSContextFactory.Create(Settings.MySqlConnectionString))
     {
         db.Add(menuItem_language);
         db.SaveChanges();
     }
     return(Redirect("/backend/menu-items"));
 }
Beispiel #10
0
        public IActionResult GetCreateMenus()
        {
            using (var db = CMSContextFactory.Create(Settings.MySqlConnectionString))
            {
                ViewBag.cats = db.Category_Language.Where(cat_lang => cat_lang.LanguageId == 1).ToList();

                return(View("~/Components/Main/Menu/Views/Backend/Create.cshtml"));
            }
        }
Beispiel #11
0
 public IActionResult AddCategory(Category_Language category_language)
 {
     //TODO: authorize, validate...
     using (var db = CMSContextFactory.Create(Settings.MySqlConnectionString))
     {
         db.Add(category_language);
         db.SaveChanges();
     }
     return(Redirect("/backend/categories"));
 }
Beispiel #12
0
        public IActionResult GetAddMenuItem()
        {
            using (var db = CMSContextFactory.Create(Settings.MySqlConnectionString))
            {
                ViewBag.languages = db.Language.Where(lags => lags.Id != 1).ToList();
                ViewBag.menuItems = db.MenuItem_Language.Where(menu_item_lag => menu_item_lag.LanguageId == 1)
                                    .ToList();

                return(View("~/Components/Main/MenuItem/Views/Backend/add.cshtml"));
            }
        }
 public IActionResult GetAddItem()
 {
     //TODO: authorize...
     using (var db = CMSContextFactory.Create(Settings.MySqlConnectionString))
     {
         ViewBag.languages = db.Language.Where(lang => lang.Id != 1).ToList();
         ViewBag.items     = db.Item_Language.Where(cat_lang => cat_lang.LanguageId == 1)//show items within the default language..the English one
                             .ToList();
         return(View("~/Components/Main/Item/Views/Backend/add.cshtml"));
     }
 }
Beispiel #14
0
        public IActionResult GetAddMenu()
        {
            using (var db = CMSContextFactory.Create(Settings.MySqlConnectionString))
            {
                ViewBag.languages = db.Language.Where(lang => lang.Id != 1).ToList();
                ViewBag.menus     = db.Menu_Language.Where(menu_lang => menu_lang.LanguageId == 1)//show the menu description within the default language..the English one
                                    .ToList();

                return(View("~/Components/Main/Menu/Views/Backend/add.cshtml"));
            }
        }
        public IActionResult GetCreateItem()
        {
            //TODO: authorize...
            using (var db = CMSContextFactory.Create(Settings.MySqlConnectionString))
            {
                ViewBag.cats = db.Category_Language.Where(cat_lang => cat_lang.LanguageId == 1).ToList();

                ViewBag.roles = db.Roles.ToList();

                return(View("~/Components/Main/Item/Views/Backend/Create.cshtml"));
            }
        }
 public IActionResult ListItems()
 {
     using (var db = CMSContextFactory.Create(Settings.MySqlConnectionString))
     {
         ViewBag.items = db.Item_Language
                         .Include(_item_language => _item_language.Item)
                         .ThenInclude(item => item.Category)
                         .Include(_item_language => _item_language.Language)
                         .OrderBy(_item_language => _item_language.ItemId)
                         .ToList();
         return(View("~/Components/Main/Item/Views/Backend/Index.cshtml"));
     }
 }
Beispiel #17
0
        public IActionResult GetAddCategory()
        {
            using (var db = CMSContextFactory.Create(Settings.MySqlConnectionString))
            {
                ViewBag.languages = db.Language.Where(lang => lang.Id != 1).ToList();

                ViewBag.cats = db.Categories
                               .Where(cat => cat.Category_Language.Count == 1)      //show the categories that doesn't have but one Category_Language which should be in defualt language
                               .Include(cat => cat.Category_Language)
                               .ToList();
                return(View("~/Components/Main/Category/Views/Backend/add.cshtml"));
            }
        }
Beispiel #18
0
        public IActionResult GetCreateCategory()
        {
            using (var db = CMSContextFactory.Create(Settings.MySqlConnectionString))
            {
                ViewBag.Categories = db.Categories
                                     .Where(cat => cat.Items.Count == 0)        // a suitable category to be Parent Cateogry (no items)
                                     .Include(cat => cat.Category_Language)
                                     .ToList();

                ViewBag.roles = db.Roles.ToList();
                return(View("~/Components/Main/Category/Views/Backend/Create.cshtml"));
            }
        }
Beispiel #19
0
        public IActionResult GetEditItem(int menu_id)
        {
            using (var db = CMSContextFactory.Create(Settings.MySqlConnectionString))
            {
                Menu_Language ml = db.Menu_Language.SingleOrDefault(_menu_lang => _menu_lang.Id == menu_id);
                ViewBag.menu = db.Menu_Language.Where(_menu => _menu.Id == menu_id)
                               .Include(_item => _item.Menu)
                               .SingleOrDefault();

                ViewBag.cats = db.Category_Language.Where(cat_lang => cat_lang.LanguageId == ml.LanguageId).ToList();
                return(View("~/Components/Main/Menu/Views/Backend/edit.cshtml"));
            }
        }
Beispiel #20
0
        public IActionResult ListMenuItems()
        {
            using (var db = CMSContextFactory.Create(Settings.MySqlConnectionString))
            {
                ViewBag.menuItems = db.MenuItem_Language
                                    .Include(menu_item_lang => menu_item_lang.MenuItem)
                                    .ThenInclude(menu_item => menu_item.MenuItemType)
                                    .ToList();

                ViewBag.menus = db.Menu_Language.ToList();

                return(View("~/Components/Main/MenuItem/Views/Backend/Index.cshtml"));
            }
        }
Beispiel #21
0
 public IActionResult ListCategories()
 {
     using (var db = CMSContextFactory.Create(Settings.MySqlConnectionString))
     {
         ViewBag.cats_langs = db.Category_Language
                              .Include(cat => cat.Language)
                              .Include(cat => cat.Category)
                              .ThenInclude(cat => cat.ParentCategory)
                              .ThenInclude(cat => cat.Category_Language)
                              .OrderBy(cat => cat.CategoryId)
                              .ToList();
         return(View("~/Components/Main/Category/Views/Backend/Index.cshtml"));
     }
 }
Beispiel #22
0
        public IActionResult ListMenus()
        {
            using (var db = CMSContextFactory.Create(Settings.MySqlConnectionString))
            {
                ViewBag.menus = db.Menu_Language
                                .Include(menu_lang => menu_lang.Language)
                                .Include(menu_lang => menu_lang.Menu)
                                .ThenInclude(menu_cat => menu_cat.Category)
                                .OrderBy(menu_lan => menu_lan.MenuId)
                                .ToList();



                return(View("~/Components/Main/Menu/Views/Backend/Index.cshtml"));
            }
        }
Beispiel #23
0
        public IActionResult CreateMenuItem(string Label, MenuItem menuItem, int roleId)
        {
            using (var db = CMSContextFactory.Create(Settings.MySqlConnectionString))
            {
                //TODO: bind checkbox with menuItem.IsIndexPage & menuItem.IsMenu
                menuItem.IsMenu      = this.Request.Form["IsMenu"].ToString().Equals("") ? false : true;
                menuItem.IsIndexPage = this.Request.Form["IsIndexPage"].ToString().Equals("") ? false : true;

                if (menuItem.IsIndexPage)
                //user choose to set as index page
                {
                    //prev item that was set as an index page
                    var _menuItem = db.MenuItems.SingleOrDefault(item => item.IsIndexPage);
                    _menuItem.IsIndexPage     = false;
                    db.Entry(_menuItem).State = EntityState.Modified;
                    db.SaveChanges();
                }

                if (!menuItem.IsMenu)
                {
                    if (menuItem.MenuItemTypeId == 1)
                    { // Single Article
                        //TODO: use FormCollection
                        menuItem.Params = "{ItemId : " + this.Request.Form["ItemId"].ToString() + "}";
                    }
                    else if (menuItem.MenuItemTypeId == 2)
                    {
                        //Category List
                        //TODO: use FormCollection
                        menuItem.Params = "{CategoryId : " + this.Request.Form["CategoryId"].ToString() + "}";
                    }
                }

                menuItem.Role = db.Roles.SingleOrDefault(role => role.Id == roleId);

                db.Add(menuItem);

                MenuItem_Language menuItem_langaue = new MenuItem_Language()
                {
                    LanguageId = 1, MenuItem = menuItem, Label = Label
                };
                db.Add(menuItem_langaue);

                db.SaveChanges();
            }
            return(Redirect("/backend/menu-items"));
        }
Beispiel #24
0
        public IActionResult EditItem(MenuItem menu_item, FormCollection form, int menuItem_languageId, int LangaugeId, string Label)
        {
            using (var db = CMSContextFactory.Create(Settings.MySqlConnectionString))
            {
                menu_item.IsMenu      = this.Request.Form["IsMenu"].ToString().Equals("") ? false : true;
                menu_item.IsIndexPage = this.Request.Form["IsIndexPage"].ToString().Equals("") ? false : true;

                if (menu_item.IsIndexPage)
                //user choose to set as index page
                {
                    //prev item that was set as an index page
                    var _menuItem = db.MenuItems.SingleOrDefault(item => item.IsIndexPage);
                    _menuItem.IsIndexPage     = false;
                    db.Entry(_menuItem).State = EntityState.Modified;
                    db.SaveChanges();
                }

                if (!menu_item.IsMenu)
                {
                    if (menu_item.MenuItemTypeId == 1)
                    { // Single Article
                      //TODO: use FormCollection
                        menu_item.Params = "{ItemId : " + this.Request.Form["ItemId"].ToString() + "}";
                    }
                    else if (menu_item.MenuItemTypeId == 2)
                    {
                        //Category List
                        //TODO: use FormCollection
                        menu_item.Params = "{CategoryId : " + this.Request.Form["CategoryId"].ToString() + "}";
                    }
                }

                MenuItem_Language mil = new MenuItem_Language()
                {
                    Id = menuItem_languageId, LanguageId = LangaugeId, Label = Label, MenuItem = menu_item
                };

                db.Entry(menu_item).State = EntityState.Modified;
                db.SaveChanges();

                db.Entry(mil).State = EntityState.Modified;
                db.SaveChanges();
                return(Redirect("/backend/menu-items"));
            }
        }
Beispiel #25
0
        public IActionResult CreateMenu(int CategoryId, string Description)
        {
            using (var db = CMSContextFactory.Create(Settings.MySqlConnectionString))
            {
                Menu menu = new Menu();
                menu.CategoryId = CategoryId;
                db.Add(menu);

                Menu_Language menu_language = new Menu_Language();
                menu_language.Description = Description;
                menu_language.LanguageId  = 1;//default laguage
                menu_language.Menu        = menu;
                db.Add(menu_language);

                db.SaveChanges();
                return(Redirect("/backend/menus"));
            }
        }
Beispiel #26
0
        public IActionResult Index(string link)
        {
            if (Context.HttpContext.Session.GetInt32("language") == null)
            {
                Context.HttpContext.Session.SetInt32("language", 1); // default is english
            }
            int lang = (int)Context.HttpContext.Session.GetInt32("language");

            using (var db = CMSContextFactory.Create(Settings.MySqlConnectionString))
            {
                var selected_language = db.Language.SingleOrDefault(_lang => _lang.Id == lang);
                TempData["orientation"] = selected_language.orientation;


                IBundle bundle = cookMainBundle(lang, db);
                return((new MainComponentController()).GetMenuItemView(link, Settings.MySqlConnectionString, lang, bundle));
            }
        }
Beispiel #27
0
        public IActionResult GetEditCategory(int category_id)
        {
            using (var db = CMSContextFactory.Create(Settings.MySqlConnectionString))
            {
                ViewBag.categoryLanguage = db.Category_Language
                                           .Where(_cat => _cat.Id == category_id)
                                           .Include(catLang => catLang.Category)
                                           .ThenInclude(cat => cat.Role)
                                           .SingleOrDefault();

                ViewBag.Categories = db.Categories
                                     .Where(cat => cat.Items.Count == 0)        // a suitable category to be Parent Cateogry (no items)
                                     .Include(cat => cat.Category_Language)
                                     .ToList();

                ViewBag.roles = db.Roles.ToList();

                return(View("~/Components/Main/Category/Views/Backend/edit.cshtml"));
            }
        }
Beispiel #28
0
        public IActionResult GetCustomFields(int catId)
        {
            using (var db = CMSContextFactory.Create(Settings.MySqlConnectionString))
            {
                var cat = db.Categories
                          .SingleOrDefault(_cat => _cat.Id == catId);

                if (cat == null)
                {
                    return(new StatusCodeResult(404)); // not found
                }
                ViewBag.Category = cat;

                if (cat.CustomFields != null)
                {
                    JArray customFields = JsonConvert.DeserializeObject(cat.CustomFields) as JArray;
                    ViewBag.CustomFields = customFields;
                }
                return(PartialView("~/Components/Main/Category/Views/Backend/custom_fields.cshtml"));
            }
        }
Beispiel #29
0
        public IActionResult EditItem(int MenuId, int Menu_LanguageId, int LanguageId, int CategoryId, string Description)
        {
            Menu m = new Menu()
            {
                Id = MenuId, CategoryId = CategoryId
            };
            Menu_Language il = new Menu_Language()
            {
                Id = Menu_LanguageId, LanguageId = LanguageId, Description = Description, MenuId = MenuId
            };

            using (var db = CMSContextFactory.Create(Settings.MySqlConnectionString))
            {
                db.Entry(m).State = EntityState.Modified;

                db.Entry(il).State = EntityState.Modified;
                db.SaveChanges();

                return(Redirect("/backend/menus"));
            }
        }
Beispiel #30
0
        private void createSuperUser(string connectionString)
        {
            using (var db = CMSContextFactory.Create(connectionString))
            {
                //findOrCreate pattern:
                var user = db.Users.SingleOrDefault(_user => _user.Username.Equals("Admin"));

                if (user == null)
                {
                    var role = db.Roles.SingleOrDefault(_role => _role.Name.Equals("Admin"));


                    User superUser = new User()
                    {
                        Username   = "******",
                        FirstName  = "Admin",
                        LastName   = "Admin",
                        Email      = "*****@*****.**",
                        Password   = "******",
                        InsertedAt = DateTime.Now,
                        IsApproved = true
                    };

                    UserRole userRole = new UserRole()
                    {
                        Role = role,
                        User = superUser
                    };

                    superUser.UserRoles.Add(userRole);
                    role.UserRoles.Add(userRole);


                    db.Add(userRole);
                    db.Add(superUser);

                    db.SaveChanges();
                }
            }
        }