public ActionResult Add(PageModel model)
        {
            if (!_permissionService.Authorize(PermissionProvider.ManagePages))
                return AccessDeniedView();

            if (ModelState.IsValid)
            {
                try
                {
                    Page page = model.ToEntity();
                    page.CreatedBy = _workContext.CurrentUser.Id;
                    page.LastModifiedBy = _workContext.CurrentUser.Id;
                    _pageService.InsertPage(page);

                    SuccessNotification("The page details have been added successfully.");
                    return RedirectToAction("index");
                }
                catch (Exception)
                {
                    ErrorNotification("An error occurred saving the page details, please try again.");
                }
            }

            PrepareBreadcrumbs();
            AddBreadcrumb("Add New Page", null);

            return View(model);
        }
        public ActionResult Hide(PageModel model)
        {
            if (!_permissionService.Authorize(PermissionProvider.ManagePages))
                return AccessDeniedView();

            // get the page
            var page = _pageService.GetPageById(model.Id);

            // check we have a page and they are not deleted
            if (page == null || page.Deleted || page.IsSystemPage)
                return RedirectToAction("Index");
            page.LastModifiedBy = _workContext.CurrentUser.Id;

            try
            {
                page.Active = !page.Active;
                _pageService.UpdatePage(page);

                SuccessNotification(page.Active ? "The page has been shown successfully." : "The page has been hidden successfully.");

                return RedirectToAction("Edit", page.Id);
            }
            catch (Exception)
            {
                ErrorNotification("An error occurred hiding the page, please try again.");
            }

            PrepareBreadcrumbs();
            AddBreadcrumb("Edit Page", null);

            return View(model);
        }
        public ActionResult Edit(PageModel model)
        {
            if (!_permissionService.Authorize(PermissionProvider.ManagePages))
                return AccessDeniedView();

            // get the page
            var page = _pageService.GetPageById(model.Id);

            // check we have a page and they are not deleted
            if (page == null || page.Deleted)
                return RedirectToAction("index");

            // Update fields
            page.Title = model.Title;
            page.Content = model.Content;
            page.FileTitle = model.FileTitle;
            page.MetaTitle = model.MetaTitle;
            page.MetaDescription = model.MetaDescription;
            page.MetaKeywords = model.MetaKeywords;
            page.MetaTitle = model.MetaTitle;

            if (ModelState.IsValid)
            {
                try
                {
                    _pageService.UpdatePage(page);
                    SuccessNotification("The page details have been updated successfully.");
                    return RedirectToAction("edit", page.Id);
                }
                catch (Exception)
                {
                    ErrorNotification("An error occurred saving the page details, please try again.");
                }
            }
            else
            {
                ErrorNotification("We were unable to make the change, please review the form and correct the errors.");
            }

            PrepareBreadcrumbs();
            AddBreadcrumb("Edit Page", null);

            model = page.ToModel();
            model.Media = model.Media.Select(PrepareListMediaModel).OrderBy(m => m.FileName).ToList();

            return View(model);
        }
        public ActionResult Edit(PageModel model, HttpPostedFileBase uploadedFile)
        {
            if (!_permissionService.Authorize(PermissionProvider.ManagePages))
                return AccessDeniedView();

            bool validUpload = true;

            if (string.IsNullOrEmpty(model.MediaItem.Name))
            {
                if (ModelState.ContainsKey("MediaItem.Name"))
                    ModelState["MediaItem.Name"].Errors.Add(new ModelError("Name is required."));
                validUpload = false;
            }

            if (uploadedFile == null || uploadedFile.ContentLength <= 0)
            {
                ErrorNotification("Please select a file to upload.");
                validUpload = false;
            }

            if (validUpload)
            {
                string path = HttpContext.Server.MapPath("/Uploads/Media/");
                string filename = _webHelper.GetUniqueFileName(path, uploadedFile.FileName);

                // Create the media object
                var media = new Media
                {
                    EntityId = model.Id,
                    EntityTypeId = (int) EntityType.Page,
                    FileName = filename,
                    Link = model.MediaItem.Link,
                    Name = model.MediaItem.Name
                };

                // Save the file locally
                uploadedFile.SaveAs(path + filename);

                // Insert the media record
                _mediaService.InsertMedia(media);

                // Clear the fields
                model.MediaItem.Link = "";
                model.MediaItem.Name = "";
            }

            model.Media = _mediaService.GetAllMediaByEntityId(EntityType.Page, model.Id).Select(x => x.ToModel()).Select(PrepareListMediaModel).OrderBy(m => m.FileName).ToList();

            var page = _pageService.GetPageById(model.Id);
            model.IsSystemPage = page.IsSystemPage;
            model.CreatedDate = page.CreatedDate;

            PrepareBreadcrumbs();
            AddBreadcrumb("Edit Page", null);

            return View(model);
        }
        public static PageModel ToModel(this Page entity)
        {
            if (entity == null)
                return null;

            var mediaService = EngineContext.Current.Resolve<IMediaService>();

            var model = new PageModel
            {
                Active = entity.Active,
                Content = entity.Content.EncodeEmails(),
                CreatedDate = entity.CreatedDate,
                Deleted = entity.Deleted,
                FileTitle = entity.FileTitle,
                Id = entity.Id,
                IsSystemPage = entity.IsSystemPage,
                LastModifiedBy = entity.LastModifiedBy,
                LastModifiedDate = entity.LastModifiedDate,
                Media = mediaService.GetAllMediaByEntityId(EntityType.Page, entity.Id).Select(x => x.ToModel()).ToList(),
                MetaDescription = entity.MetaDescription,
                MetaKeywords = entity.MetaKeywords,
                MetaTitle = entity.MetaTitle,
                Priority = entity.Priority,
                Title = entity.Title
            };

            return model;
        }