protected virtual void PrepareStoresMappingModel(AdvertisementModel model, Advertisement advertisement, bool excludeProperties)
        {
            if (model == null)
                throw new ArgumentNullException("model");

            model.AvailableStores = _storeService
                .GetAllStores()
                .Select(s => s.ToModel())
                .ToList();
            if (!excludeProperties)
            {
                if (advertisement != null)
                {
                    //model.SelectedStoreIds = _storeMappingService.GetStoresIdsWithAccess(advertisement);
                }
            }
        }
        protected virtual void PrepareAdvertisementModel(AdvertisementModel model, Advertisement advertisement,
            bool setPredefinedValues, bool excludeProperties)
        {
            if (model == null)
                throw new ArgumentNullException("model");

            if (advertisement != null)
            {
                model.CreatedOn = _dateTimeHelper.ConvertToUserTime(advertisement.CreatedOnUtc, DateTimeKind.Utc);
                model.UpdatedOn = _dateTimeHelper.ConvertToUserTime(advertisement.UpdatedOnUtc, DateTimeKind.Utc);
            }
            
            //vendors
            model.IsLoggedInAsVendor = _workContext.CurrentVendor != null;
            model.AvailableVendors.Add(new SelectListItem
            {
                Text = "None",
                Value = "0"
            });
            var vendors = _vendorService.GetAllVendors(showHidden: true);
            foreach (var vendor in vendors)
            {
                model.AvailableVendors.Add(new SelectListItem
                {
                    Text = vendor.Name,
                    Value = vendor.Id.ToString()
                });
            }
            
            //default values
            if (setPredefinedValues)
            {
                model.Published = true;
            }
        }
        public ActionResult Create(AdvertisementModel model, HttpPostedFileBase Banner)
        {
            if (model == null)
                throw new ArgumentNullException("advertisement");

            string requestUrl = string.Format(Request.Url.Scheme + "://" + Request.Url.Authority + "/Admin/Picture/AsyncUpload");
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUrl);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.Accept = "application/json; charset=UTF-8";

            var email = "*****@*****.**";
            var password = "******";
            string credentials = String.Format("{0}:{1}", email, password);
            byte[] bytes = Encoding.ASCII.GetBytes(credentials);
            string base64 = Convert.ToBase64String(bytes);
            string authorization = String.Concat("Basic ", base64);
            request.Headers.Add("Authorization", authorization);

            //byte[] fileBytes = System.IO.File.ReadAllBytes(Banner.FileName);
            //StringBuilder serializedBytes = new StringBuilder();

            ////Let's serialize the bytes of your file
            //fileBytes.ToList<byte>().ForEach(x => serializedBytes.AppendFormat("{0}.", Convert.ToUInt32(x)));
            //string postParameters = String.Format("{0}", serializedBytes.ToString());
            //byte[] postData = Encoding.UTF8.GetBytes(postParameters);
            //////////////

            byte[] data;
            using (Stream inputStream = Banner.InputStream)
            {
                MemoryStream memoryStream = inputStream as MemoryStream;
                //whether the stream returned is already a MemoryStream
                if (memoryStream == null)
                {
                    memoryStream = new MemoryStream();
                    inputStream.CopyTo(memoryStream);
                }
                data = memoryStream.ToArray();
            }

            byte[] buffer = new byte[0x1000];
            Banner.InputStream.Read(buffer, 0, buffer.Length);
            //////////////////////////////////
            using (var streamWriter = request.GetRequestStream())
            {
                streamWriter.Write(data, 0, data.Length);
                streamWriter.Flush();
                streamWriter.Close();
            }

            var httpResponse = (HttpWebResponse)request.GetResponse();
            //Receipt Receipt = null;
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                var responstText = streamReader.ReadToEnd();
                //Receipt = serializer.Deserialize<Receipt>(responstText);
            }

            try
            {
                if (ModelState.IsValid)
                {
                    //a vendor should have access only to his products
                    if (_workContext.CurrentVendor != null)
                    {
                        model.VendorId = _workContext.CurrentVendor.Id;
                    }

                    var advertise = new ModelsMapper().CreateMap<AdvertisementModel, Advertisement>(model);
                    advertise.CreatedOnUtc = DateTime.UtcNow;
                    advertise.UpdatedOnUtc = DateTime.UtcNow;

                    _advertisementService.InsertAdvertisement(advertise);

                    return RedirectToAction("Edit", new { Id = model.Id });
                }
            }
            catch (Exception exc)
            {
                ModelState.AddModelError("", exc.Message + "<br />" + exc.InnerException.InnerException.Message);
            }

            PrepareAdvertisementModel(model, null, true, true);
            //AddLocales(_languageService, model.Locales);
            //PrepareAclModel(model, null, false);
            PrepareStoresMappingModel(model, null, false);
            return View(model);
        }
        public ActionResult Edit(AdvertisementModel model)
        {
            if (model == null)
                throw new ArgumentNullException("model");
            try
            {
                //Update
                if (ModelState.IsValid)
                {
                    var advertisement = _advertisementService.GetAdvertisementById(model.Id);
                    advertisement = new ModelsMapper().CreateMap<AdvertisementModel, Advertisement>(model, advertisement);
                    advertisement.UpdatedOnUtc = DateTime.UtcNow;
                    _advertisementService.UpdateAdvertisement(advertisement);
                    return RedirectToAction("Index");
                }
            }
            catch (Exception exc)
            {
                ModelState.AddModelError("", exc.Message + "<br />" + exc.InnerException.InnerException.Message);
            }

            PrepareAdvertisementModel(model, null, false, true);
            //AddLocales(_languageService, model.Locales);
            //PrepareAclModel(model, null, false);
            PrepareStoresMappingModel(model, null, false);
            return View();
        }
        public ActionResult Create()
        {
            //if (!_permissionService.Authorize(StandardPermissionProvider.ManageProducts))
            //    return AccessDeniedView();

            var model = new AdvertisementModel();
            PrepareAdvertisementModel(model, null, true, true);
            //AddLocales(_languageService, model.Locales);
            //PrepareAclModel(model, null, false);
            PrepareStoresMappingModel(model, null, false);
            return View(model);
        }