Example #1
0
        /// <summary>
        /// Delete the file
        /// </summary>
        /// <param name="sourcePath">Path to the file</param>
        /// <returns>A task that represents the completion of the operation</returns>
        public override async Task DeleteFileAsync(string sourcePath)
        {
            var filePath = _fileProvider.GetAbsolutePath(sourcePath.Split('/'));
            var picture  = await GetPictureByFileAsync(filePath);

            if (picture == null)
            {
                throw new Exception(await GetLanguageResourceAsync("E_DeletеFile"));
            }

            await _pictureService.DeletePictureAsync(picture);

            await base.DeleteFileAsync(sourcePath);
        }
Example #2
0
        public async Task <ActionResult> DeletePicture(string publicId)
        {
            var deletionResult = await _pictureService.DeletePictureAsync(publicId);

            if (deletionResult is null)
            {
                return(NotFound("Invalid publicID. Coudn't delete picture."));
            }

            if (deletionResult.Error is not null)
            {
                return(BadRequest(deletionResult.Error));
            }

            // delete from database
            await _pictureRepository.DeletePictureAsync(publicId);

            return(NoContent());
        }
        public virtual async Task <IActionResult> Edit(CategoryModel model, bool continueEditing)
        {
            if (!await _permissionService.AuthorizeAsync(StandardPermissionProvider.ManageCategories))
            {
                return(AccessDeniedView());
            }

            //try to get a category with the specified id
            var category = await _categoryService.GetCategoryByIdAsync(model.Id);

            if (category == null || category.Deleted)
            {
                return(RedirectToAction("List"));
            }

            if (ModelState.IsValid)
            {
                var prevPictureId = category.PictureId;

                //if parent category changes, we need to clear cache for previous parent category
                if (category.ParentCategoryId != model.ParentCategoryId)
                {
                    await _staticCacheManager.RemoveByPrefixAsync(NopCatalogDefaults.CategoriesByParentCategoryPrefix, category.ParentCategoryId);

                    await _staticCacheManager.RemoveByPrefixAsync(NopCatalogDefaults.CategoriesChildIdsPrefix, category.ParentCategoryId);
                }

                category = model.ToEntity(category);
                category.UpdatedOnUtc = DateTime.UtcNow;
                await _categoryService.UpdateCategoryAsync(category);

                //search engine name
                model.SeName = await _urlRecordService.ValidateSeNameAsync(category, model.SeName, category.Name, true);

                await _urlRecordService.SaveSlugAsync(category, model.SeName, 0);

                //locales
                await UpdateLocalesAsync(category, model);

                //discounts
                var allDiscounts = await _discountService.GetAllDiscountsAsync(DiscountType.AssignedToCategories, showHidden : true);

                foreach (var discount in allDiscounts)
                {
                    if (model.SelectedDiscountIds != null && model.SelectedDiscountIds.Contains(discount.Id))
                    {
                        //new discount
                        if (await _categoryService.GetDiscountAppliedToCategoryAsync(category.Id, discount.Id) is null)
                        {
                            await _categoryService.InsertDiscountCategoryMappingAsync(new DiscountCategoryMapping { DiscountId = discount.Id, EntityId = category.Id });
                        }
                    }
                    else
                    {
                        //remove discount
                        if (await _categoryService.GetDiscountAppliedToCategoryAsync(category.Id, discount.Id) is DiscountCategoryMapping mapping)
                        {
                            await _categoryService.DeleteDiscountCategoryMappingAsync(mapping);
                        }
                    }
                }

                await _categoryService.UpdateCategoryAsync(category);

                //delete an old picture (if deleted or updated)
                if (prevPictureId > 0 && prevPictureId != category.PictureId)
                {
                    var prevPicture = await _pictureService.GetPictureByIdAsync(prevPictureId);

                    if (prevPicture != null)
                    {
                        await _pictureService.DeletePictureAsync(prevPicture);
                    }
                }

                //update picture seo file name
                await UpdatePictureSeoNamesAsync(category);

                //ACL
                await SaveCategoryAclAsync(category, model);

                //stores
                await SaveStoreMappingsAsync(category, model);

                //activity log
                await _customerActivityService.InsertActivityAsync("EditCategory",
                                                                   string.Format(await _localizationService.GetResourceAsync("ActivityLog.EditCategory"), category.Name), category);

                _notificationService.SuccessNotification(await _localizationService.GetResourceAsync("Admin.Catalog.Categories.Updated"));

                if (!continueEditing)
                {
                    return(RedirectToAction("List"));
                }

                return(RedirectToAction("Edit", new { id = category.Id }));
            }

            //prepare model
            model = await _categoryModelFactory.PrepareCategoryModelAsync(model, category, true);

            //if we got this far, something failed, redisplay form
            return(View(model));
        }
        public virtual async Task <IActionResult> Edit(ManufacturerModel model, bool continueEditing)
        {
            if (!await _permissionService.AuthorizeAsync(StandardPermissionProvider.ManageManufacturers))
            {
                return(AccessDeniedView());
            }

            //try to get a manufacturer with the specified id
            var manufacturer = await _manufacturerService.GetManufacturerByIdAsync(model.Id);

            if (manufacturer == null || manufacturer.Deleted)
            {
                return(RedirectToAction("List"));
            }

            if (ModelState.IsValid)
            {
                var prevPictureId = manufacturer.PictureId;
                manufacturer = model.ToEntity(manufacturer);
                manufacturer.UpdatedOnUtc = DateTime.UtcNow;
                await _manufacturerService.UpdateManufacturerAsync(manufacturer);

                //search engine name
                model.SeName = await _urlRecordService.ValidateSeNameAsync(manufacturer, model.SeName, manufacturer.Name, true);

                await _urlRecordService.SaveSlugAsync(manufacturer, model.SeName, 0);

                //locales
                await UpdateLocalesAsync(manufacturer, model);

                //discounts
                var allDiscounts = await _discountService.GetAllDiscountsAsync(DiscountType.AssignedToManufacturers, showHidden : true);

                foreach (var discount in allDiscounts)
                {
                    if (model.SelectedDiscountIds != null && model.SelectedDiscountIds.Contains(discount.Id))
                    {
                        //new discount
                        if (await _manufacturerService.GetDiscountAppliedToManufacturerAsync(manufacturer.Id, discount.Id) is null)
                        {
                            await _manufacturerService.InsertDiscountManufacturerMappingAsync(new DiscountManufacturerMapping { EntityId = manufacturer.Id, DiscountId = discount.Id });
                        }
                    }
                    else
                    {
                        //remove discount
                        if (await _manufacturerService.GetDiscountAppliedToManufacturerAsync(manufacturer.Id, discount.Id) is DiscountManufacturerMapping discountManufacturerMapping)
                        {
                            await _manufacturerService.DeleteDiscountManufacturerMappingAsync(discountManufacturerMapping);
                        }
                    }
                }

                await _manufacturerService.UpdateManufacturerAsync(manufacturer);

                //delete an old picture (if deleted or updated)
                if (prevPictureId > 0 && prevPictureId != manufacturer.PictureId)
                {
                    var prevPicture = await _pictureService.GetPictureByIdAsync(prevPictureId);

                    if (prevPicture != null)
                    {
                        await _pictureService.DeletePictureAsync(prevPicture);
                    }
                }

                //update picture seo file name
                await UpdatePictureSeoNamesAsync(manufacturer);

                //ACL
                await SaveManufacturerAclAsync(manufacturer, model);

                //stores
                await SaveStoreMappingsAsync(manufacturer, model);

                //activity log
                await _customerActivityService.InsertActivityAsync("EditManufacturer",
                                                                   string.Format(await _localizationService.GetResourceAsync("ActivityLog.EditManufacturer"), manufacturer.Name), manufacturer);

                _notificationService.SuccessNotification(await _localizationService.GetResourceAsync("Admin.Catalog.Manufacturers.Updated"));

                if (!continueEditing)
                {
                    return(RedirectToAction("List"));
                }

                return(RedirectToAction("Edit", new { id = manufacturer.Id }));
            }

            //prepare model
            model = await _manufacturerModelFactory.PrepareManufacturerModelAsync(model, manufacturer, true);

            //if we got this far, something failed, redisplay form
            return(View(model));
        }
        public async Task <IActionResult> Configure(ConfigurationModel model)
        {
            if (!await _permissionService.AuthorizeAsync(StandardPermissionProvider.ManageWidgets))
            {
                return(AccessDeniedView());
            }

            //load settings for a chosen store scope
            var storeScope = await _storeContext.GetActiveStoreScopeConfigurationAsync();

            var nivoSliderSettings = await _settingService.LoadSettingAsync <NivoSliderSettings>(storeScope);

            //get previous picture identifiers
            var previousPictureIds = new[]
            {
                nivoSliderSettings.Picture1Id,
                nivoSliderSettings.Picture2Id,
                nivoSliderSettings.Picture3Id,
                nivoSliderSettings.Picture4Id,
                nivoSliderSettings.Picture5Id
            };

            nivoSliderSettings.Picture1Id = model.Picture1Id;
            nivoSliderSettings.Text1      = model.Text1;
            nivoSliderSettings.Link1      = model.Link1;
            nivoSliderSettings.AltText1   = model.AltText1;
            nivoSliderSettings.Picture2Id = model.Picture2Id;
            nivoSliderSettings.Text2      = model.Text2;
            nivoSliderSettings.Link2      = model.Link2;
            nivoSliderSettings.AltText2   = model.AltText2;
            nivoSliderSettings.Picture3Id = model.Picture3Id;
            nivoSliderSettings.Text3      = model.Text3;
            nivoSliderSettings.Link3      = model.Link3;
            nivoSliderSettings.AltText3   = model.AltText3;
            nivoSliderSettings.Picture4Id = model.Picture4Id;
            nivoSliderSettings.Text4      = model.Text4;
            nivoSliderSettings.Link4      = model.Link4;
            nivoSliderSettings.AltText4   = model.AltText4;
            nivoSliderSettings.Picture5Id = model.Picture5Id;
            nivoSliderSettings.Text5      = model.Text5;
            nivoSliderSettings.Link5      = model.Link5;
            nivoSliderSettings.AltText5   = model.AltText5;

            /* We do not clear cache after each setting update.
             * This behavior can increase performance because cached settings will not be cleared
             * and loaded from database after each update */
            await _settingService.SaveSettingOverridablePerStoreAsync(nivoSliderSettings, x => x.Picture1Id, model.Picture1Id_OverrideForStore, storeScope, false);

            await _settingService.SaveSettingOverridablePerStoreAsync(nivoSliderSettings, x => x.Text1, model.Text1_OverrideForStore, storeScope, false);

            await _settingService.SaveSettingOverridablePerStoreAsync(nivoSliderSettings, x => x.Link1, model.Link1_OverrideForStore, storeScope, false);

            await _settingService.SaveSettingOverridablePerStoreAsync(nivoSliderSettings, x => x.AltText1, model.AltText1_OverrideForStore, storeScope, false);

            await _settingService.SaveSettingOverridablePerStoreAsync(nivoSliderSettings, x => x.Picture2Id, model.Picture2Id_OverrideForStore, storeScope, false);

            await _settingService.SaveSettingOverridablePerStoreAsync(nivoSliderSettings, x => x.Text2, model.Text2_OverrideForStore, storeScope, false);

            await _settingService.SaveSettingOverridablePerStoreAsync(nivoSliderSettings, x => x.Link2, model.Link2_OverrideForStore, storeScope, false);

            await _settingService.SaveSettingOverridablePerStoreAsync(nivoSliderSettings, x => x.AltText2, model.AltText2_OverrideForStore, storeScope, false);

            await _settingService.SaveSettingOverridablePerStoreAsync(nivoSliderSettings, x => x.Picture3Id, model.Picture3Id_OverrideForStore, storeScope, false);

            await _settingService.SaveSettingOverridablePerStoreAsync(nivoSliderSettings, x => x.Text3, model.Text3_OverrideForStore, storeScope, false);

            await _settingService.SaveSettingOverridablePerStoreAsync(nivoSliderSettings, x => x.Link3, model.Link3_OverrideForStore, storeScope, false);

            await _settingService.SaveSettingOverridablePerStoreAsync(nivoSliderSettings, x => x.AltText3, model.AltText3_OverrideForStore, storeScope, false);

            await _settingService.SaveSettingOverridablePerStoreAsync(nivoSliderSettings, x => x.Picture4Id, model.Picture4Id_OverrideForStore, storeScope, false);

            await _settingService.SaveSettingOverridablePerStoreAsync(nivoSliderSettings, x => x.Text4, model.Text4_OverrideForStore, storeScope, false);

            await _settingService.SaveSettingOverridablePerStoreAsync(nivoSliderSettings, x => x.Link4, model.Link4_OverrideForStore, storeScope, false);

            await _settingService.SaveSettingOverridablePerStoreAsync(nivoSliderSettings, x => x.AltText4, model.AltText4_OverrideForStore, storeScope, false);

            await _settingService.SaveSettingOverridablePerStoreAsync(nivoSliderSettings, x => x.Picture5Id, model.Picture5Id_OverrideForStore, storeScope, false);

            await _settingService.SaveSettingOverridablePerStoreAsync(nivoSliderSettings, x => x.Text5, model.Text5_OverrideForStore, storeScope, false);

            await _settingService.SaveSettingOverridablePerStoreAsync(nivoSliderSettings, x => x.Link5, model.Link5_OverrideForStore, storeScope, false);

            await _settingService.SaveSettingOverridablePerStoreAsync(nivoSliderSettings, x => x.AltText5, model.AltText5_OverrideForStore, storeScope, false);

            //now clear settings cache
            await _settingService.ClearCacheAsync();

            //get current picture identifiers
            var currentPictureIds = new[]
            {
                nivoSliderSettings.Picture1Id,
                nivoSliderSettings.Picture2Id,
                nivoSliderSettings.Picture3Id,
                nivoSliderSettings.Picture4Id,
                nivoSliderSettings.Picture5Id
            };

            //delete an old picture (if deleted or updated)
            foreach (var pictureId in previousPictureIds.Except(currentPictureIds))
            {
                var previousPicture = await _pictureService.GetPictureByIdAsync(pictureId);

                if (previousPicture != null)
                {
                    await _pictureService.DeletePictureAsync(previousPicture);
                }
            }

            _notificationService.SuccessNotification(await _localizationService.GetResourceAsync("Admin.Plugins.Saved"));

            return(await Configure());
        }
        /// <returns>A task that represents the asynchronous operation</returns>
        public virtual async Task <IActionResult> Edit(VendorModel model, bool continueEditing, IFormCollection form)
        {
            if (!await _permissionService.AuthorizeAsync(StandardPermissionProvider.ManageVendors))
            {
                return(AccessDeniedView());
            }

            //try to get a vendor with the specified id
            var vendor = await _vendorService.GetVendorByIdAsync(model.Id);

            if (vendor == null || vendor.Deleted)
            {
                return(RedirectToAction("List"));
            }

            //parse vendor attributes
            var vendorAttributesXml = await ParseVendorAttributesAsync(form);

            (await _vendorAttributeParser.GetAttributeWarningsAsync(vendorAttributesXml)).ToList()
            .ForEach(warning => ModelState.AddModelError(string.Empty, warning));

            if (ModelState.IsValid)
            {
                var prevPictureId = vendor.PictureId;
                vendor = model.ToEntity(vendor);
                await _vendorService.UpdateVendorAsync(vendor);

                //vendor attributes
                await _genericAttributeService.SaveAttributeAsync(vendor, NopVendorDefaults.VendorAttributes, vendorAttributesXml);

                //activity log
                await _customerActivityService.InsertActivityAsync("EditVendor",
                                                                   string.Format(await _localizationService.GetResourceAsync("ActivityLog.EditVendor"), vendor.Id), vendor);

                //search engine name
                model.SeName = await _urlRecordService.ValidateSeNameAsync(vendor, model.SeName, vendor.Name, true);

                await _urlRecordService.SaveSlugAsync(vendor, model.SeName, 0);

                //address
                var address = await _addressService.GetAddressByIdAsync(vendor.AddressId);

                if (address == null)
                {
                    address = model.Address.ToEntity <Address>();
                    address.CreatedOnUtc = DateTime.UtcNow;

                    //some validation
                    if (address.CountryId == 0)
                    {
                        address.CountryId = null;
                    }
                    if (address.StateProvinceId == 0)
                    {
                        address.StateProvinceId = null;
                    }

                    await _addressService.InsertAddressAsync(address);

                    vendor.AddressId = address.Id;
                    await _vendorService.UpdateVendorAsync(vendor);
                }
                else
                {
                    address = model.Address.ToEntity(address);

                    //some validation
                    if (address.CountryId == 0)
                    {
                        address.CountryId = null;
                    }
                    if (address.StateProvinceId == 0)
                    {
                        address.StateProvinceId = null;
                    }

                    await _addressService.UpdateAddressAsync(address);
                }

                //locales
                await UpdateLocalesAsync(vendor, model);

                //delete an old picture (if deleted or updated)
                if (prevPictureId > 0 && prevPictureId != vendor.PictureId)
                {
                    var prevPicture = await _pictureService.GetPictureByIdAsync(prevPictureId);

                    if (prevPicture != null)
                    {
                        await _pictureService.DeletePictureAsync(prevPicture);
                    }
                }
                //update picture seo file name
                await UpdatePictureSeoNamesAsync(vendor);

                _notificationService.SuccessNotification(await _localizationService.GetResourceAsync("Admin.Vendors.Updated"));

                if (!continueEditing)
                {
                    return(RedirectToAction("List"));
                }

                return(RedirectToAction("Edit", new { id = vendor.Id }));
            }

            //prepare model
            model = await _vendorModelFactory.PrepareVendorModelAsync(model, vendor, true);

            //if we got this far, something failed, redisplay form
            return(View(model));
        }
Example #7
0
        /// <returns>A task that represents the asynchronous operation</returns>
        public virtual async Task <IActionResult> Info(VendorInfoModel model, IFormFile uploadedFile, IFormCollection form)
        {
            if (!await _customerService.IsRegisteredAsync(await _workContext.GetCurrentCustomerAsync()))
            {
                return(Challenge());
            }

            if (await _workContext.GetCurrentVendorAsync() == null || !_vendorSettings.AllowVendorsToEditInfo)
            {
                return(RedirectToRoute("CustomerInfo"));
            }

            Picture picture = null;

            if (uploadedFile != null && !string.IsNullOrEmpty(uploadedFile.FileName))
            {
                try
                {
                    var contentType         = uploadedFile.ContentType;
                    var vendorPictureBinary = await _downloadService.GetDownloadBitsAsync(uploadedFile);

                    picture = await _pictureService.InsertPictureAsync(vendorPictureBinary, contentType, null);
                }
                catch (Exception)
                {
                    ModelState.AddModelError("", await _localizationService.GetResourceAsync("Account.VendorInfo.Picture.ErrorMessage"));
                }
            }

            var vendor = await _workContext.GetCurrentVendorAsync();

            var prevPicture = await _pictureService.GetPictureByIdAsync(vendor.PictureId);

            //vendor attributes
            var vendorAttributesXml = await ParseVendorAttributesAsync(form);

            (await _vendorAttributeParser.GetAttributeWarningsAsync(vendorAttributesXml)).ToList()
            .ForEach(warning => ModelState.AddModelError(string.Empty, warning));

            if (ModelState.IsValid)
            {
                var description = Core.Html.HtmlHelper.FormatText(model.Description, false, false, true, false, false, false);

                vendor.Name        = model.Name;
                vendor.Email       = model.Email;
                vendor.Description = description;

                if (picture != null)
                {
                    vendor.PictureId = picture.Id;

                    if (prevPicture != null)
                    {
                        await _pictureService.DeletePictureAsync(prevPicture);
                    }
                }

                //update picture seo file name
                await UpdatePictureSeoNamesAsync(vendor);

                await _vendorService.UpdateVendorAsync(vendor);

                //save vendor attributes
                await _genericAttributeService.SaveAttributeAsync(vendor, NopVendorDefaults.VendorAttributes, vendorAttributesXml);

                //notifications
                if (_vendorSettings.NotifyStoreOwnerAboutVendorInformationChange)
                {
                    await _workflowMessageService.SendVendorInformationChangeNotificationAsync(vendor, _localizationSettings.DefaultAdminLanguageId);
                }

                return(RedirectToAction("Info"));
            }

            //If we got this far, something failed, redisplay form
            model = await _vendorModelFactory.PrepareVendorInfoModelAsync(model, true, vendorAttributesXml);

            return(View(model));
        }
Example #8
0
        public async Task <IActionResult> DeletePicture(int id)
        {
            await _service.DeletePictureAsync(id);

            return(Ok());
        }