public ActionResult Create(LanguageModel model, bool continueEditing)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageLanguages))
                return AccessDeniedView();

            if (ModelState.IsValid)
            {
                var language = model.ToEntity();
                _languageService.InsertLanguage(language);

                //Stores
                _storeMappingService.SaveStoreMappings<Language>(language, model.SelectedStoreIds);

                var plugins = _pluginFinder.GetPluginDescriptors(true);
                var filterLanguages = new List<Language>() { language };

                foreach (var plugin in plugins)
                {
                    _localizationService.ImportPluginResourcesFromXml(plugin, null, false, filterLanguages);
                }

                NotifySuccess(_localizationService.GetResource("Admin.Configuration.Languages.Added"));
                return continueEditing ? RedirectToAction("Edit", new { id = language.Id }) : RedirectToAction("List");
            }

            //flags
            PrepareFlagsModel(model);

            //Stores
            PrepareStoresMappingModel(model, null, true);

            //If we got this far, something failed, redisplay form
            return View(model);
        }
        private void PrepareFlagsModel(LanguageModel model)
        {
            if (model == null)
                throw new ArgumentNullException("model");

            model.FlagFileNames = System.IO.Directory
                .EnumerateFiles(_webHelper.MapPath("~/Content/Images/flags/"), "*.png", SearchOption.TopDirectoryOnly)
                .Select(System.IO.Path.GetFileName)
                .ToList();
        }
        public ActionResult Create()
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageLanguages))
                return AccessDeniedView();

            var model = new LanguageModel();

            //flags
            PrepareFlagsModel(model);

            //Stores
            PrepareStoresMappingModel(model, null, false);

            //default values
            //model.Published = true;
            return View(model);
        }
		private void PrepareStoresMappingModel(LanguageModel model, Language language, bool excludeProperties)
		{
			if (model == null)
				throw new ArgumentNullException("model");

			model.AvailableStores = _storeService
				.GetAllStores()
				.Select(s => s.ToModel())
				.ToList();
			if (!excludeProperties)
			{
				if (language != null)
				{
					model.SelectedStoreIds = _storeMappingService.GetStoresIdsWithAccess(language);
				}
				else
				{
					model.SelectedStoreIds = new int[0];
				}
			}
		}
        public ActionResult Edit(LanguageModel model, bool continueEditing)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageLanguages))
                return AccessDeniedView();

            var language = _languageService.GetLanguageById(model.Id);
            if (language == null)
                return RedirectToAction("List");

            if (ModelState.IsValid)
            {
                //ensure we have at least one published language
                var allLanguages = _languageService.GetAllLanguages();
                if (allLanguages.Count == 1 && allLanguages[0].Id == language.Id &&
                    !model.Published)
                {
                    NotifyError("At least one published language is required.");
                    return RedirectToAction("Edit", new { id = language.Id });
                }

                //update
                language = model.ToEntity(language);
                _languageService.UpdateLanguage(language);

                //Stores
                _storeMappingService.SaveStoreMappings<Language>(language, model.SelectedStoreIds);

                //notification
                NotifySuccess(_localizationService.GetResource("Admin.Configuration.Languages.Updated"));
                return continueEditing ? RedirectToAction("Edit", new { id = language.Id }) : RedirectToAction("List");
            }

            //flags
            PrepareFlagsModel(model);

            //Stores
            PrepareStoresMappingModel(model, language, true);

            //If we got this far, something failed, redisplay form
            return View(model);
        }
		protected void SaveStoreMappings(Language language, LanguageModel model)
		{
			var existingStoreMappings = _storeMappingService.GetStoreMappings(language);
			var allStores = _storeService.GetAllStores();
			foreach (var store in allStores)
			{
				if (model.SelectedStoreIds != null && model.SelectedStoreIds.Contains(store.Id))
				{
					//new role
					if (existingStoreMappings.Where(sm => sm.StoreId == store.Id).Count() == 0)
						_storeMappingService.InsertStoreMapping(language, store.Id);
				}
				else
				{
					//removed role
					var storeMappingToDelete = existingStoreMappings.Where(sm => sm.StoreId == store.Id).FirstOrDefault();
					if (storeMappingToDelete != null)
						_storeMappingService.DeleteStoreMapping(storeMappingToDelete);
				}
			}
		}