public ActionResult SubscriptionList(GridCommand command, NewsLetterSubscriptionListModel model)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageNewsletterSubscribers))
                return AccessDeniedView();

            var newsletterSubscriptions = _newsLetterSubscriptionService.GetAllNewsLetterSubscriptions(
				model.SearchEmail, command.Page - 1, command.PageSize, true, model.StoreId);

            var gridModel = new GridModel<NewsLetterSubscriptionModel>
            {
                Data = newsletterSubscriptions.Select(x =>
				{
					var m = x.ToModel();
					var store = _storeService.GetStoreById(x.StoreId);

					m.CreatedOn = _dateTimeHelper.ConvertToUserTime(x.CreatedOnUtc, DateTimeKind.Utc);
					m.StoreName = store != null ? store.Name : "Unknown";

					return m;
				}),
                Total = newsletterSubscriptions.TotalCount
            };
            return new JsonResult
            {
                Data = gridModel
            };
		}
		public ActionResult List()
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageNewsletterSubscribers))
                return AccessDeniedView();

            var newsletterSubscriptions = _newsLetterSubscriptionService.GetAllNewsLetterSubscriptions(String.Empty, 0, _adminAreaSettings.GridPageSize, true);
			var model = new NewsLetterSubscriptionListModel();
			PrepareNewsLetterSubscriptionListModel(model);

			model.NewsLetterSubscriptions = new GridModel<NewsLetterSubscriptionModel>
			{
				Data = newsletterSubscriptions.Select(x => 
				{
					var m = x.ToModel();
					var store = _storeService.GetStoreById(x.StoreId);

					m.CreatedOn = _dateTimeHelper.ConvertToUserTime(x.CreatedOnUtc, DateTimeKind.Utc);
					m.StoreName = store != null ? store.Name : "Unknown";

					return m;
				}),
				Total = newsletterSubscriptions.TotalCount
			};
			return View(model);
		}
		private void PrepareNewsLetterSubscriptionListModel(NewsLetterSubscriptionListModel model)
		{
			var stores = _storeService.GetAllStores().ToList();

			model.GridPageSize = _adminAreaSettings.GridPageSize;

			model.AvailableStores.Add(new SelectListItem() { Text = _localizationService.GetResource("Admin.Common.All"), Value = "0" });
			model.AvailableStores.AddRange(stores.ToSelectListItems());

		}
		public ActionResult ExportCsv(NewsLetterSubscriptionListModel model)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageNewsletterSubscribers))
                return AccessDeniedView();

			string fileName = String.Format("newsletter_emails_{0}_{1}.txt", DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss"), CommonHelper.GenerateRandomDigitCode(4));

			var sb = new StringBuilder();
			var newsLetterSubscriptions = _newsLetterSubscriptionService.GetAllNewsLetterSubscriptions(model.SearchEmail, 0, int.MaxValue, true);
			if (newsLetterSubscriptions.Count == 0)
			{
				// codehint: sm-edit
				//throw new SmartException("No emails to export");
				NotifyInfo(_localizationService.GetResource("Admin.Common.ExportNoData"));
				return RedirectToAction("List");
			}
			for (int i = 0; i < newsLetterSubscriptions.Count; i++)
			{
				var subscription = newsLetterSubscriptions[i];
				sb.Append(subscription.Email);
                sb.Append(",");
                sb.Append(subscription.Active);
                sb.Append(Environment.NewLine);  //new line
			}
			string result = sb.ToString();

			return File(Encoding.UTF8.GetBytes(result), "text/csv", fileName);
		}
        public ActionResult SubscriptionDelete(int id, GridCommand command)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageNewsletterSubscribers))
                return AccessDeniedView();

            var subscription = _newsLetterSubscriptionService.GetNewsLetterSubscriptionById(id);
            if (subscription == null)
                throw new ArgumentException("No subscription found with the specified id");

            _newsLetterSubscriptionService.DeleteNewsLetterSubscription(subscription);

			var listModel = new NewsLetterSubscriptionListModel();
			PrepareNewsLetterSubscriptionListModel(listModel);

			return SubscriptionList(command, listModel);
        }
        public ActionResult SubscriptionUpdate(NewsLetterSubscriptionModel model, GridCommand command)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageNewsletterSubscribers))
                return AccessDeniedView();
            
            if (!ModelState.IsValid)
            {
                //display the first model error
                var modelStateErrors = this.ModelState.Values.SelectMany(x => x.Errors).Select(x => x.ErrorMessage);
                return Content(modelStateErrors.FirstOrDefault());
            }

            var subscription = _newsLetterSubscriptionService.GetNewsLetterSubscriptionById(model.Id);
            subscription.Email = model.Email;
            subscription.Active = model.Active;

            _newsLetterSubscriptionService.UpdateNewsLetterSubscription(subscription);

			var listModel = new NewsLetterSubscriptionListModel();
			PrepareNewsLetterSubscriptionListModel(listModel);

            return SubscriptionList(command, listModel);
        }
        public ActionResult SubscriptionList(GridCommand command, NewsLetterSubscriptionListModel model)
        {
            var gridModel = new GridModel<NewsLetterSubscriptionModel>();

            if (_permissionService.Authorize(StandardPermissionProvider.ManageNewsletterSubscribers))
            {
                var newsletterSubscriptions = _newsLetterSubscriptionService.GetAllNewsLetterSubscriptions(
                    model.SearchEmail, command.Page - 1, command.PageSize, true, model.StoreId);

                gridModel.Data = newsletterSubscriptions.Select(x =>
                {
                    var m = x.ToModel();
                    var store = _storeService.GetStoreById(x.StoreId);

                    m.CreatedOn = _dateTimeHelper.ConvertToUserTime(x.CreatedOnUtc, DateTimeKind.Utc);
                    m.StoreName = store != null ? store.Name : "".NaIfEmpty();

                    return m;
                });

                gridModel.Total = newsletterSubscriptions.TotalCount;
            }
            else
            {
                gridModel.Data = Enumerable.Empty<NewsLetterSubscriptionModel>();

                NotifyAccessDenied();
            }

            return new JsonResult
            {
                Data = gridModel
            };
        }
        public ActionResult SubscriptionDelete(int id, GridCommand command)
        {
            if (_permissionService.Authorize(StandardPermissionProvider.ManageNewsletterSubscribers))
            {
                var subscription = _newsLetterSubscriptionService.GetNewsLetterSubscriptionById(id);

                _newsLetterSubscriptionService.DeleteNewsLetterSubscription(subscription);
            }

            var listModel = new NewsLetterSubscriptionListModel();
            PrepareNewsLetterSubscriptionListModel(listModel);

            return SubscriptionList(command, listModel);
        }
        private void PrepareNewsLetterSubscriptionListModel(NewsLetterSubscriptionListModel model)
        {
            var stores = _storeService.GetAllStores().ToList();

            model.GridPageSize = _adminAreaSettings.GridPageSize;

            model.AvailableStores = stores.ToSelectListItems();
        }